index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const service = require('./service/index.js');
  2. class UniSubscribemsg {
  3. // 构造函数
  4. constructor(data = {}) {
  5. let {
  6. dcloudAppid,
  7. provider, // 平台 weixin-mp 微信小程序 weixin-h5 微信公众号
  8. } = data;
  9. this.config = {
  10. dcloudAppid,
  11. provider
  12. }
  13. }
  14. // API - 发送订阅消息
  15. async sendSubscribeMessage(data = {}) {
  16. let res = await this._call("sendSubscribeMessage", data);
  17. return res;
  18. }
  19. // API - 发送公众号模板消息
  20. async sendTemplateMessage(data = {}) {
  21. let res = await this._call("sendTemplateMessage", data);
  22. return res;
  23. }
  24. // API - 检测用户是否关注了公众号
  25. async getSubscribeUserInfo(data = {}) {
  26. let res = await this._call("getSubscribeUserInfo", data);
  27. return res;
  28. }
  29. // 测试API - 获取当前登录用户的openid(此接口仅测试时使用)
  30. async getOpenid(data = {}) {
  31. let res = await this._call("getOpenid", data);
  32. return res;
  33. }
  34. // 私有函数
  35. async _call(name, data = {}) {
  36. let runService = service[this.config.provider];
  37. if (!runService) {
  38. throw new Error(`不支持平台:${this.config.provider}`);
  39. }
  40. let runFunction = runService[name];
  41. if (!runFunction) {
  42. throw new Error(`平台:${this.config.provider}不支持:${name}`);
  43. }
  44. let res = await runFunction.call(this, data);
  45. return res;
  46. }
  47. }
  48. module.exports = UniSubscribemsg;