目标: 请求二维码并展示 步骤: 用户点击按钮生成二维码 步骤: 1、微信小程序往后台请求二维码 2、后台(java/php) 根据微信小程序信息往微信端请求tonken 3、后台得到tonken后往微信端请求二维码图片 4、后台得到图片后保存在服务器上,将路径返回给微信小程序 5、微信小程序得到路径后,根据路径下载图片 6、下载图片成功后再保存至本地 7、保存成功后将路径给予image标签里面展示 代码: wxml
<image class="scanimg" src="{{filePath}}" bindtap="getAgain"></image>
<button type="primary" bindtap="primary">点击生成二维码</button>
js代码
primary:function (e) {
var _url = '后台地址';
wx.request({
url: _url,
//请求报文体
data: [{
id: agentCode
}],
method: 'POST',
header: {
'content-type': 'application/json'
},
success: function (res) {
//为00时表示成功,得到二维码的地址
if (res.data.code == '00') {
console.log("成功")
//下载二维码
wx.downloadFile({
url: res.data.body[0].URL,
success: function (res) {
//如果二维码中的id为固定值可以将图片保存到本地,否则不用保存
wx.saveFile({
tempFilePath: res.tempFilePath,
success: function (res) {
console.log("保存成功")
_that.setData
|