评论

分享使用tcb-router路由开发的云函数短信平台SDK

分享使用tcb-router路由开发的云函数短信平台SDK

榛子应用市场
上篇文章我们分享了如何使用纯的云函数开发的榛子短信短信(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);
    });

  }
})

最后一次编辑于  2019-04-01  
点赞 3
收藏
评论

1 个评论

  • 马旭东
    马旭东
    2019-03-21

    辛苦

    2019-03-21
    赞同
    回复 4
    • 邓伟
      邓伟
      2020-02-25
      你是rtx 的那个马旭东吗?
      2020-02-25
      回复
    • 马旭东
      马旭东
      2020-02-25回复邓伟
      rtx 是啥  估计不是 哈哈
      2020-02-25
      回复
    • 邓伟
      邓伟
      2020-02-25
      哈哈,那真不是
      2020-02-25
      回复
    • Luke
      Luke
      2020-03-10
      请问如果使用Java传参。那个data里面应该怎么写啊?
      2020-03-10
      回复
登录 后发表内容