榛子应用市场
上篇文章我们分享了如何使用纯的云函数开发的榛子短信短信(http://smsow.zhenzikj.com)SDK,由于微信对于未付费云函数个数的限制,这种方法存在缺陷,经过改进,使用tcb-router作为路由,这样只需要整合到一个云函数中就行
下载sdk和demo: http://smsow.zhenzikj.com/sdkdownload/weixinmp_yun2.html
目前SDK中包含三个功能: send(发送短信)、balance(查询余额)、findSmsByMessageId(查询单条短信)
SDK源码
// 云函数入口文件
const cloud = require('wx-server-sdk')
const TcbRouter = require('tcb-router')
const rq = require('request')
const baseUrl = 'https://smsdeveloper.zhenzikj.com'
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
const app = new TcbRouter({ event });
app.router('send', async (ctx) => {
ctx.body = new Promise(resolve => {
rq({
url: baseUrl + '/sms/send.html',
method: "POST",
json: true,
form: {
apiUrl: event.apiUrl,
appId: event.appId,
appSecret: event.appSecret,
message: event.message,
number: event.number,
messageId: event.messageId,
}
}, function (error, response, body) {
resolve({ body: body, error: error })
});
// setTimeout(() => {
// resolve('male');
// }, 500);
});
});
app.router('balance', async (ctx) => {
ctx.body = new Promise(resolve => {
rq({
url: baseUrl + '/sms/balance.html',
method: "POST",
json: true,
form: {
apiUrl: event.apiUrl,
appId: event.appId,
appSecret: event.appSecret
}
}, function (error, response, body) {
resolve({ body: body, error: error })
});
});
});
app.router('findSmsByMessageId', async (ctx) => {
ctx.body = new Promise(resolve => {
rq({
url: baseUrl + '/sms/findSmsByMessageId.html',
method: "POST",
json: true,
form: {
apiUrl: event.apiUrl,
appId: event.appId,
appSecret: event.appSecret,
messageId: event.messageId
}
}, function (error, response, body) {
resolve({ body: body, error: error })
});
});
});
return app.serve();
}
如何使用SDK
//index.js
const app = getApp()
Page({
data: {
},
onLoad: function() {
},
// 发送短信
send: function () {
wx.cloud.callFunction({
name: 'zhenzisms',
data: {
$url: 'send',
apiUrl: 'https://sms_developer.zhenzikj.com',
appId: '你的appId',
appSecret: '你的appSecret',
message: '你的验证码为:3333',
number: '15811111111',
messageId: ''
}
}).then((res) => {
console.log(res.result.body);
}).catch((e) => {
//console.log(e);
});
},
// 查询余额
balance: function () {
wx.cloud.callFunction({
name: 'zhenzisms',
data: {
$url: 'balance',
apiUrl: 'https://sms_developer.zhenzikj.com',
appId: '你的appId',
appSecret: '你的appSecret'
}
}).then((res) => {
console.log(res.result.body);
}).catch((e) => {
//console.log(e);
});
},
// 查询单条信息
findSmsByMessageId: function () {
wx.cloud.callFunction({
name: 'zhenzisms',
data: {
$url: 'findSmsByMessageId',
apiUrl: 'https://sms_developer.zhenzikj.com',
appId: '你的appId',
appSecret: '你的appSecret',
messageId: 'aaaabbbbba'
}
}).then((res) => {
console.log(res.result.body);
}).catch((e) => {
//console.log(e);
});
}
})
辛苦