WeChat applet user clicks the button to generate a sample code with parameters for the QR code

高洛峰
Release: 2017-03-26 15:28:19
Original
4078 people have browsed it

Goal:
Request the QR code and display it
Steps:
The user clicks the button to generate the QR code
Steps:
1. The WeChat applet requests the QR code from the background
2. Backend (java/php) Request tonken from WeChat client based on WeChat applet information
3. After getting tonken from backend, request QR code image from Wechat client
4. After getting the image from backend, save it on the server. Return the path to the WeChat applet
5. After the WeChat applet obtains the path, download the image according to the path
6. After successfully downloading the image, save it locally
7. After successfully saving, put the path in the image tag Display
code:
wxml

<image class="scanimg" src="{{filePath}}" bindtap="getAgain"></image>
<button type="primary" bindtap="primary">点击生成二维码</button>
Copy after login

s code

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({
filePath: res.savedFilePath
})
console.log(res.savedFilePath)
try {
//id为定值,则将保存的地址存入缓存,非定值则只需要setData就行
wx.setStorageSync('filePath', res.savedFilePath)
} catch (e) {
console.log(e)
}
              },
              fail: function (res) {
                console.log("保存失败")
                console.log(res)
              }
            })
          }, fail: function (res) {
            util.msg("错误", "通讯失败")
            console.log(res)
          }
        })
      } else {
        console.log("错误")
        util.msg("错误", res.data.msg)
      }
    },
    fail: function () {
      util.msg("错误", "通讯失败")
      console.log(res)
    }
  })
}
Copy after login

java side code

// 访问微信后台的URL
String URL = systemConfig.getString("LoginOrRegisterUrl");
// 请求类型
String grant_type = systemConfig.getString("grant_type");
// 第三方用户唯一凭证密钥
String secret = systemConfig.getString("secret");
// 第三方用户唯一凭证
String appId = systemConfig.getString("appid");
// 请求token时用到的URL
String tokenUrl = systemConfig.getString("tokenUrl");
// 向微信后台请求获取token
                        String sendGet = HttpClientConnectionManager.sendGet(
                                tokenUrl, "grant_type=" + grant_type
                                        + "&secret=" + secret + "&appid="
                                        + appId + "");
                        System.out.println(sendGet);
                        JSONObject json = JSONObject.fromObject(sendGet);
                        access_token = json.get("access_token").toString();
                        if (access_token == null) {
//没有token 则返回错误码和错误信息
                            agentDTO.setCode("0002");
                            agentDTO.setDesc("获取tokenId失败");
                            return agentDTO;
                        }
System.out.println(access_token);
// 访问微信后台带的json参数
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("path", "pages/register");//你二维码中跳向的地址
                    map.put("width", "430");//图片大小
                    JSONObject json = JSONObject.fromObject(map);
                    HttpClientConnectionManager.httpPostWithJSON(URL
                            + access_token, json.toString(), id + "max");
                    // 返回给前端的后台服务器文件读取路径
                    String downloadUrl = systemConfig
                            .getString("agentImgDownloadUrl")
                            + id
                            + "max"
                            + "/";
                    // 返回给前端的后台服务器文件下载路径
                    String downloadfileUrl = downloadUrl + id + "max" + ".jpg";
                    agentResView.setURL(downloadfileUrl);
                    agentDTO.setAgentResView(agentResView);
agentDTO.setCode("00");
agentDTO.setDesc("成功");
                        return agentDTO;
Copy after login

tip
1. This is the code to apply for a QR code , you can use for or other methods to apply for multiple images
2. When the id is quantitative, click the button every time to determine whether the filePath cache exists. If it exists, the value will be displayed directly. If it does not exist, the QR code will be requested from the background

The above is the detailed content of WeChat applet user clicks the button to generate a sample code with parameters for the QR code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!