ソースを参照

添加微信模板消息功能

liuyu 5 年 前
コミット
5b3d157c18
2 ファイル変更44 行追加3 行削除
  1. 39 0
      app/service/weixin.js
  2. 5 3
      config/config.default.js

+ 39 - 0
app/service/weixin.js

@@ -139,6 +139,45 @@ class WeixinAuthService extends AxiosService {
     const [ status ] = val.split(':', 2);
     return { status };
   }
+
+  // 发送微信模板消息
+  async sendTemplateMsg(templateid, openid, first, keyword1, keyword2, remark) {
+    const url = this.ctx.app.config.sendDirMq + this.ctx.app.config.appid;
+    const requestData = { // 发送模板消息的数据
+      touser: openid,
+      template_id: templateid,
+      url: '',
+      data: {
+        first: {
+          value: first,
+          color: '#173177',
+        },
+        keyword1: {
+          value: keyword1,
+          color: '#1d1d1d',
+        },
+        keyword2: {
+          value: keyword2,
+          color: '#1d1d1d',
+        },
+        remark: {
+          value: remark,
+          color: '#173177',
+        },
+      },
+    };
+    console.log('templateid---' + templateid);
+    console.log('openid---' + openid);
+    console.log('requestData---' + JSON.stringify(requestData));
+    await this.ctx.curl(url, {
+      method: 'post',
+      headers: {
+        'content-type': 'application/json',
+      },
+      dataType: 'json',
+      data: JSON.stringify(requestData),
+    });
+  }
 }
 
 module.exports = WeixinAuthService;

+ 5 - 3
config/config.default.js

@@ -1,7 +1,9 @@
 /* eslint valid-jsdoc: "off" */
 
 'use strict';
+
 const { jwt } = require('./config.secret');
+
 /**
  * @param {Egg.EggAppInfo} appInfo app info
  */
@@ -33,7 +35,7 @@ module.exports = appInfo => {
   // add your config here
   config.cluster = {
     listen: {
-      port: 8001,
+      port: 7001,
     },
   };
 
@@ -51,7 +53,7 @@ module.exports = appInfo => {
   config.sendDirMq = 'http://wx.cc-lotus.info/api.weixin.qq.com/cgi-bin/message/template/send?appid=';
   // appID
   config.appid = 'wxdf3ed83c095be97a';
-  config.REVIEW_TEMPLATE_ID = 'BI4h0AQpdctm74I7-7PyHAspSMX2oJOTJVQsgrppOag';
+  config.REVIEW_TEMPLATE_ID = 'oqmozBcdkCzBOvNrdzMQ1lDJcobD5uBEpNoLWec0bPY';
 
   // config.amqp = {
   //   client: {
@@ -66,7 +68,7 @@ module.exports = appInfo => {
 
   // mongoose config
   config.mongoose = {
-    url: 'mongodb://127.0.0.1:27017/visit',
+    url: 'mongodb://127.0.0.1:27017/train',
     options: {
       user: 'admin',
       pass: 'admin',