zoukankan      html  css  js  c++  java
  • 分享使用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);
        });
     
      }
    })
     
    

      

  • 相关阅读:
    二项式反演
    快速沃尔什变换
    springMVC的form标签
    springMVC的拦截器配置
    RESTful使用方法
    springMVC数据绑定
    使用spring框架自带的字符拦截器
    将idea中的项目上传至github
    springMVC的使用方式
    springMVC的概述
  • 原文地址:https://www.cnblogs.com/seeto/p/10518363.html
Copyright © 2011-2022 走看看