WeChat applets click the button to generate sample code with the parameter QR code

Source: Internet
Author: User
This article mainly introduces in detail the sample code used by the applet to generate a QR code with parameters by clicking the button, which has some reference value. interested friends can refer to the following objectives:
Request and display the QR code
Steps:
Click the button to generate a QR code
Steps:
1. the applet requests the QR code to the background
2. the background (java/php) requests tonken from the client based on the applet information
3. request the QR code image after obtaining the tonken in the background
4. save the image in the background on the server and return the path to the applet.
5. download the image based on the path obtained by The applet
6. download the image and save it to your local device.
7. Save the saved path and display it in the image tag.
Code:
Wxml

Click Generate QR code

S code

Primary: function (e) {var _ url = 'background address'; wx. request ({url: _ url, // style data: [{id: agentCode}], method: 'post', header: {'content-type ': 'application/json'}, success: function (res) {// if 00 is set, the request is successful. data. code = '00') {console. log ("succeeded") // download the QR code wx. downloadFile ({url: res. data. body [0]. URL, success: function (res) {// if the id in the QR code is a fixed value, you can save the image to your local device; otherwise, you do not need to save wx. saveFile ({tempFilePath: res. tempFilePath, success: function (res) {console. log ("saved successfully") _ that. setData ({filePath: res. savedFilePath}) console. log (res. savedFilePath) try {// id is a set value, and the saved address is saved to the cache. if it is not a set value, you only need to setData for the row wx. setStorageSync ('filepath', res. savedFilePath)} catch (e) {console. log (e) }}, fail: function (res) {console. log ("failed to save") console. log (res) }}, fail: function (res) {util. msg ("error", "communication failed") console. log (res) }}else {console. log ("error") util. msg ("error", res. data. msg) }}, fail: function () {util. msg ("error", "communication failed") console. log (res )}})}

Java code

// Access the background URLString URL = systemConfig. getString ("LoginOrRegisterUrl"); // request type String grant_type = systemConfig. getString ("grant_type"); // unique credential key of a third-party user String secret = systemConfig. getString ("secret"); // The unique credential of a third-party user String appId = systemConfig. getString ("appid"); // The URLString tokenUrl = systemConfig. getString ("tokenUrl"); // Obtain token String sendGet = HttpClientConnectionManager from the background request. 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) {// if no token exists, the error code and error message agentDTO are returned. setCode ("0002"); agentDTO. setDesc ("failed to get tokenId"); return agentDTO;} System. out. println (access_token); // access the json parameter Map in the background
 
  
Map = new HashMap
  
   
(); Map. put ("path", "pages/register"); // address map of the hop in your QR code. put ("width", "430"); // image size: JSONObject json = JSONObject. fromObject (map); HttpClientConnectionManager. httpPostWithJSON (URL + access_token, json. toString (), id + "max"); // return to the front-end background server file read path String downloadUrl = systemConfig. getString ("agentImgDownloadUrl") + id + "max" + "/"; // return the download path String downloadfileUrl = downloadUrl + id + "max" + ". jpg "; agentResView. setURL (downloadfileUrl); agentDTO. setAgentResView (agentResView); agentDTO. setCode ("00"); agentDTO. setDesc ("successful"); return agentDTO;
  
 

Tip
1. this is the code to apply for a QR code. you can use for or other methods to apply for multiple QR codes.
2. when the id is quantitative, click the button each time to determine whether the filePath cache exists. If yes, the value is displayed directly. If no value exists, the two-dimensional code is requested from the background.

The above is the details of the sample code with the parameter QR code generated by the applet user clicking the button. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.