wxService.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. 'use strict';
  2. const xml2js = require('../util/xml2js');
  3. const crypto = require('crypto');
  4. const Service = require('egg').Service;
  5. class WxService extends Service {
  6. async handleEvent(message) {
  7. const { FromUserName, Event, EventKey,
  8. Ticket, Latitude, Longitude, Precision } = message;
  9. let reply;
  10. switch (Event) {
  11. case 'subscribe':
  12. reply = '欢迎关注吉顺致养';
  13. break;
  14. case 'unsubscribe':
  15. reply = '';
  16. break;
  17. case 'SCAN':
  18. reply = 'EventKey:' + EventKey + ', Ticket:' + Ticket;
  19. break;
  20. case 'LOCATION':
  21. reply = 'Latitude:' + Latitude + ', Longitude:' + Longitude
  22. + ', Precision:' + Precision;
  23. break;
  24. case 'CLICK':
  25. reply = 'EventKey:' + EventKey;
  26. break;
  27. case 'VIEW':
  28. reply = 'EventKey:' + EventKey;
  29. break;
  30. default:
  31. reply = '';
  32. break;
  33. }
  34. return reply;
  35. }
  36. async handleMsg(message) {
  37. const { MsgType, Content, PicUrl, MediaId,
  38. Recognition, Label, Url } = message;
  39. let reply;
  40. switch (MsgType) {
  41. case 'text':
  42. reply = Content;
  43. break;
  44. case 'image':
  45. reply = PicUrl;
  46. break;
  47. case 'voice':
  48. reply = MediaId;
  49. break;
  50. case 'video':
  51. reply = MediaId;
  52. break;
  53. case 'shortvideo':
  54. reply = MediaId;
  55. break;
  56. case 'location':
  57. reply = Label;
  58. break;
  59. case 'link':
  60. reply = Url;
  61. break;
  62. default:
  63. reply = '';
  64. break;
  65. }
  66. return reply;
  67. }
  68. async replyMsg(message, Content) {
  69. const obj = {
  70. ToUserName: message.FromUserName,
  71. FromUserName: message.ToUserName,
  72. CreateTime: new Date().getTime(),
  73. MsgType: 'text',
  74. Content,
  75. };
  76. return xml2js.createXml(obj);
  77. }
  78. async createMenu(appid) {
  79. const menu = {
  80. button: [
  81. {
  82. type: 'view',
  83. name: '开始采集',
  84. url: 'https://info.windd.cn/infoVue/index.html#/',
  85. },
  86. {
  87. name: '资料下载',
  88. sub_button: [
  89. {
  90. type: 'view',
  91. name: '采集员操作视频',
  92. url: 'https://mp.weixin.qq.com/s/rjmq3b4ANHSiId6kqGdxxw',
  93. },
  94. {
  95. type: 'view',
  96. name: '地区管理操作视频',
  97. url: 'https://mp.weixin.qq.com/s/__QrrvGsjXNTyUU6Ll493A',
  98. },
  99. {
  100. type: 'view',
  101. name: '积分统计查看视频',
  102. url: 'https://mp.weixin.qq.com/mp/video?__biz=MzA3OTU5NDUzOQ==&mid=501812762&sn=2b4189cf712597f5d48b2de11f86ffab&vid=wxv_1052717373041541120&idx=1&vidsn=193aa5b5e695deb584ac4e5f418859da&fromid=1&scene=18&xtrack=1#wechat_redirect',
  103. }],
  104. },
  105. {
  106. type: 'view',
  107. name: '探访查询',
  108. url: 'https://info.windd.cn/infoVue/#/dependant/login',
  109. },
  110. ],
  111. };
  112. const config = this.ctx.app.config.wechat_config;
  113. const accessToken = await this.app.redis.get('access_token');
  114. const url = config.createMenu.replace('ACCESS_TOKEN', accessToken);
  115. const res = await this.ctx.curl(url, {
  116. method: 'POST',
  117. contentType: 'json',
  118. data: menu,
  119. dataType: 'json',
  120. });
  121. return res.data.errcode;
  122. }
  123. async getUserInfo(openid) {
  124. // const config = this.ctx.app.config.wechat_config;
  125. // const getAccessTokenUrl = config.getAccessToken.replace('APPID', 'wx91e8f7300e266ad1')
  126. // .replace('APPSECRET', '18ed843ea5147e338dfd987d9ddf1085');
  127. // const getAccessTokenRes = await this.ctx.curl(getAccessTokenUrl, {
  128. // dataType: 'json',
  129. // });
  130. // const access_token = getAccessTokenRes.data.access_token;
  131. // const access_token = await this.app.redis.get('access_token');
  132. // this.ctx.logger.info('AccessToken=================' + access_token);
  133. // const url = config.getUserInfo.replace('ACCESS_TOKEN', access_token)
  134. // .replace('OPENID', openid);
  135. // const res = await this.ctx.curl(url, {
  136. // dataType: 'json',
  137. // });
  138. // this.ctx.logger.info('UserInfo=================' + res);
  139. // return res.data;
  140. // --------------------
  141. const config = this.ctx.app.config.wechat_config;
  142. const access_token = await this.app.redis.get('access_token');
  143. const url = config.getUserInfo.replace('ACCESS_TOKEN', access_token)
  144. .replace('OPENID', openid);
  145. const res = await this.ctx.curl(url, {
  146. dataType: 'json',
  147. });
  148. return res.data;
  149. }
  150. async getSign(data) {
  151. const appId = data.appId;
  152. const ticket = this.app.redis.get('ticket');
  153. const nonceStr = Math.random().toString(36).substr(2, 15);
  154. const timestamp = parseInt(new Date().getTime() / 1000);
  155. const url = data.url;
  156. const string = 'jsapi_ticket=' + ticket + '&noncestr='
  157. + nonceStr + '&timestamp=' + timestamp + '&url=' + url;
  158. const hash = crypto.createHash('sha1');
  159. hash.update(string);
  160. const signature = hash.digest('hex');
  161. return {
  162. appId,
  163. timestamp,
  164. nonceStr,
  165. signature,
  166. };
  167. }
  168. async oauth(data) {
  169. const code = data.code;
  170. const cid = data.cid;
  171. const result = await this.ctx.model.WxConfigModel.find({ cid });
  172. if (result.length === 1) {
  173. const config = this.ctx.app.config.wechat_config;
  174. const url = config.oauthAccessToken.replace('APPID', result[0].appid)
  175. .replace('SECRET', result[0].appsecret)
  176. .replace('CODE', code);
  177. const res = await this.ctx.curl(url, {
  178. dataType: 'json',
  179. });
  180. return res;
  181. }
  182. return {};
  183. // const access_token = res.data.access_token;
  184. // const openid = res.data.openid;
  185. // const userInfoUrl = await config.oauthUserinfo.replace('ACCESS_TOKEN', access_token).replace('OPENID', openid);
  186. // const userInfoRes = await this.ctx.curl(userInfoUrl, {
  187. // dataType: 'json',
  188. // });
  189. // return userInfoRes.data;
  190. }
  191. async getAppletOpenId(data) {
  192. const code = data.code;
  193. const cid = data.cid;
  194. const result = await this.ctx.model.WxConfigModel.find({ cid });
  195. if (result.length === 1) {
  196. const config = this.ctx.app.config.wechat_config;
  197. const url = config.getAppletAccessToken.replace('APPID', result[0].appid)
  198. .replace('SECRET', result[0].appsecret)
  199. .replace('CODE', code);
  200. const res = await this.ctx.curl(url, {
  201. dataType: 'json',
  202. });
  203. return res;
  204. }
  205. return {};
  206. }
  207. }
  208. module.exports = WxService;