// 云函数入口文件
const cloud = require('wx-server-sdk')
const request = require('request')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
let { userInfo, secret } = event
let { openId, appId } = userInfo // 这里获取到的 openId 和 appId 是可信的
let appid = userInfo.appId
let openid = userInfo.appId
let synchronous_get = function (url, params) {
let options = { url: url };
return new Promise(function (resolve, reject) {
request.get(options, function (error, response, body) {
if (error) {
reject(error);
}
else {
//resolve(body);
resolve(response);
}
});
});
}
//同步请求 getAccessToken
let demo = async function () {
let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
+ appid + '&secret=' + secret;
let body = await synchronous_get(url);
console.log(body); //这里得到结果后,传给客户端,但是云函数日志为null,客户端日志也为null
//把getAccessToken返回结果返回给客户端
return {
body
}
}
//客户端调用代码
var getAccessToken = (secrets) => {
wx.cloud.callFunction({
// 需调用的云函数名
name: 'getAccessToken',
//name: 'getAceessToken2',
// 传给云函数的参数
data: {
secret: secrets
},
success: function (res) {
console.log(res.result)
},
complete: res => {
console.log(res)
},
})
}