这是调试的时候,只要是调用本地的云函数就能成功
一旦调用远程的就报错,调用失败
本地node版本10.5.1;安装依赖库wx-server-sdk,crypto,qcloudsms_js
云开发环境
函数上传部署成功了的
云函数:代码如下
// 云函数入口文件
const cloud = require(‘wx-server-sdk’)
const QcloudSms = require(“qcloudsms_js”)
const crypto = require(‘crypto’)
const sdkappid = “我的ID”;
const appkey = “我的key”;
const tpl_id = 442042;//短信模板ID
const smsSign = “签名”;//短信签名
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => new Promise((resolve, reject) => {
var qcloudsms = QcloudSms(sdkappid, appkey);
var ssender = qcloudsms.SmsSingleSender();
var phoneCode = () => {
let myCode = ’ ';
for (let i = 0; i <= 3; i++) {
myCode += Math.floor(Math.random() * 10).toString();
}
return myCode
};//生成随机验证码
var param = new Array(phoneCode());//封装验证码
var hsparam = crypto.createHash(‘sha256’).update(param[0]).digest(‘hex’);
var nationcode = event.nationcode //手机号地区码
var userPhone = event.phone;//手机号码测试
ssender.sendWithParam(nationcode, userPhone, tpl_id, param, smsSign, “”, “”, (err, res, resData) => {
if (err) {
console.log("err: ", err);
reject({ err })
} else {
resolve({ res: res.req, resData, hsparam })
}
});
})