Applet code generation process with reference
1, the applet client upload parameters required for generating two-dimensional code to the cloud function
2, and a cloud appid appsecret request function uses the access_token
. 3, cloud access_token + applet function uses the client upload parameter generating two-dimensional code
4, the generated 2D cloud function code is returned to the end of the applet (or returned to the database fileID, for pick-side applet fileID, subsequently generated to search a database, the database generation operation is not performed again, repeatedly generating prevent small program code file)
Applet client upload applet code parameters required
wx.cloud.callFunction ({
name: 'the getImage', // function name cloud
data: {// applet code parameters required
page: "pages / xxxx / xxxx " ,
ID: ID,
},
Complete: RES => {
the console.log ( 'CallFunction Test Result:', RES)
this.setData ({// Get returns the applet code
xcxCodeImageData: res.result,
})
}
})
cloud and function with appid appsecret access_token request
to create a cloud function getImage, and the function corresponding to the cloud directory import request, request-promise, axios frame (request for data),
NPM the install --save // request request frame
npm install - save request-promise // request frame promise style
data type provided npm install --save axios // request data frame, the flow may be returned to `stream`
# Note: install can be abbreviated as i; save effect is to add to the library which package.json
cloud file import function frame
const the require Cloud = ( 'WX-Server-SDK')
const = Axios the require ( 'Axios')
var = the require RP ( 'request-Promise');
const the require FS = ( 'FS');
var = the require Stream ( 'Stream');
# need not all be introduced, introducing the following as appropriate according to the actual use of the actual
request to obtain the access_token
// request frame style promise
RP ( 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=secret=appSecret'
.then (function (resultValue) {
the console.log ( "request success:" )
the console.log (the JSON.parse (resultValue))
})
.catch (function (ERR) {});
});
// Nodejs原生写法
const http = require("https")
const url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=secret=appSecret"
http.get(url,(res)=>{
var resultValue = ""
res.on("data",(data)=>{
resultValue+=data
})
res.on("end",()=>{
console.log(resultValue)
})
}).on("error",(e)=>{
console.log(`获取数据失败: ${e.message}`)
})
获取小程序码
var options = {
method: 'POST',
url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' + access_token',
body: {
page: "pages/xxx/xxx
scene: "id=xxx"
},
JSON: to true
};
RP (Options)
.then (function (parsedBody) {
the console.log (parsedBody) // applet code image data
})
.catch (function (ERR) {});
the server a complete code
var rp the require = ( 'Request-Promise');
const the require FS = ( 'FS');
var = the require Stream ( 'Stream');
// 请求微信access_token
rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret')
.then(function(resultValue) {
console.log("请求 success:" + resultValue)
console.log(JSON.parse(resultValue).access_token)
// 请求小程序码
var http = require("http"),
data = {
// 小程序码参数
"page": "pages/CardDetail/CardDetail",
"width": 300,
"scene": "id=W6MIjlJhFW5Pec-Y",
};
data = JSON.stringify(data);
var options = {
method: "POST",
host: "api.weixin.qq.com",
path: "/wxa/getwxacodeunlimit?access_token=" + JSON.parse(resultValue).access_token,
headers: {
"Content-Type": "application/json",
"Content-Length": data.length
}
};
var req = http.request(options, function (res) {
res.setEncoding("binary");
var imgData = '';
res.on ( 'Data', function (the chunk) {
imgData the chunk + =;
});
res.on ( "End", function () {
// return the image data is converted to the desired uploadFile method parameter file fileContent stream format, and a local normal output data can be acquired uploadFile try to execute the applet code with this method, a method of using two
var bufferStream new new stream.PassThrough = ();
bufferStream.end (new new Buffer (imgData));
Console. log ( 'the required flow profile uploadFile method fileContent ----')
the console.log (bufferStream)
// Sublime output to the Text can be run locally, and may open the two-dimensional code
// local storage path
var path = 'public . / '+ Date.now () +' PNG ';
fs.writeFile (path, imgData, "binary", function (ERR) {
IF (ERR) {
the console.log ( "down fail");
}
the console.log ( "Down Success");
});
});
});
req.write (Data);
req.end ();
})
.catch (function (ERR) {});
the server two complete code (paste may be used directly)
const = the require Cloud ( 'WX-Server-SDK')
const = Axios the require ( 'Axios')
var the require RP = ( 'Request-Promise');
cloud.init ()
// 云函数入口函数
exports.main = async (event, context) => {
console.log(event)
try {
const resultValue = await rp('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret')
const token = JSON.parse(resultValue).access_token;
console.log('------ TOKEN:', token);
const response = await axios({
method: 'post',
url: 'https://api.weixin.qq.com/wxa/getwxacodeunlimit',
responseType: 'stream',
params: {
access_token: token,
},
data: {
page: event.page,
width: 300,
scene: "id=" + event.id,
},
});
cloud.uploadFile the await return ({
cloudPath: 'xcxcodeimages /' + Date.now () + '.png',
fileContent: response.data,
});
} the catch (ERR) {
the console.log ( '>>>>> > ERROR: ', ERR)
}
}
Request frame-related documents: HTTPS: //www.npmjs.com/package/request
Request frame promise style related documents: HTTPS: //www.npmjs.com/package/request-promise
Axios framework Related documents: https: //www.npmjs.com/package/axios
applet cloud development documents: HTTPS: //developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html
- -------------------
author: technology blog Stevin's
source: CSDN
original: https: //blog.csdn.net/feng2qing/article/details/82914505
copyright: This article is a blogger original article, reproduced, please attach Bowen link!