wechat.js 1.3 KB

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