wechat.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. require('../util/constants');
  3. const Service = require('egg').Service;
  4. class WechatService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.appid = 'wxdf3ed83c095be97a';
  8. }
  9. // 发送微信模板消息
  10. async sendTemplateMsg(templateid, openid, first, keyword1, keyword2, keyword3, remark) {
  11. const url = this.ctx.app.config.sendDirMq + this.ctx.app.config.appid;
  12. const requestData = { // 发送模板消息的数据
  13. touser: openid,
  14. template_id: templateid,
  15. url: '',
  16. data: {
  17. first: {
  18. value: first,
  19. color: '#173177',
  20. },
  21. keyword1: {
  22. value: keyword1,
  23. color: '#1d1d1d',
  24. },
  25. keyword2: {
  26. value: keyword2,
  27. color: '#1d1d1d',
  28. },
  29. keyword3: {
  30. value: keyword3,
  31. color: '#1d1d1d',
  32. },
  33. remark: {
  34. value: remark,
  35. color: '#173177',
  36. },
  37. },
  38. };
  39. console.log('templateid---' + templateid);
  40. console.log('openid---' + openid);
  41. console.log('requestData---' + JSON.stringify(requestData));
  42. await this.ctx.curl(url, {
  43. method: 'post',
  44. headers: {
  45. 'content-type': 'application/json',
  46. },
  47. dataType: 'json',
  48. data: JSON.stringify(requestData),
  49. });
  50. }
  51. }
  52. module.exports = WechatService;