123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- 'use strict';
- const xml2js = require('../util/xml2js');
- const crypto = require('crypto');
- const Service = require('egg').Service;
- class WxService extends Service {
- async handleEvent(message) {
- const { FromUserName, Event, EventKey,
- Ticket, Latitude, Longitude, Precision } = message;
- let reply;
- switch (Event) {
- case 'subscribe':
- reply = '欢迎关注吉顺致养';
- break;
- case 'unsubscribe':
- reply = '';
- break;
- case 'SCAN':
- reply = 'EventKey:' + EventKey + ', Ticket:' + Ticket;
- break;
- case 'LOCATION':
- reply = 'Latitude:' + Latitude + ', Longitude:' + Longitude
- + ', Precision:' + Precision;
- break;
- case 'CLICK':
- reply = 'EventKey:' + EventKey;
- break;
- case 'VIEW':
- reply = 'EventKey:' + EventKey;
- break;
- default:
- reply = '';
- break;
- }
- return reply;
- }
- async handleMsg(message) {
- const { MsgType, Content, PicUrl, MediaId,
- Recognition, Label, Url } = message;
- let reply;
- switch (MsgType) {
- case 'text':
- reply = Content;
- break;
- case 'image':
- reply = PicUrl;
- break;
- case 'voice':
- reply = MediaId;
- break;
- case 'video':
- reply = MediaId;
- break;
- case 'shortvideo':
- reply = MediaId;
- break;
- case 'location':
- reply = Label;
- break;
- case 'link':
- reply = Url;
- break;
- default:
- reply = '';
- break;
- }
- return reply;
- }
- async replyMsg(message, Content) {
- const obj = {
- ToUserName: message.FromUserName,
- FromUserName: message.ToUserName,
- CreateTime: new Date().getTime(),
- MsgType: 'text',
- Content,
- };
- return xml2js.createXml(obj);
- }
- async createMenu(appid) {
- const menu = {
- button: [
- {
- type: 'view',
- name: '开始采集',
- url: 'https://info.windd.cn/infoVue/index.html#/',
- },
- {
- name: '资料下载',
- sub_button: [
- {
- type: 'view',
- name: '采集员操作视频',
- url: 'https://mp.weixin.qq.com/s/rjmq3b4ANHSiId6kqGdxxw',
- },
- {
- type: 'view',
- name: '地区管理操作视频',
- url: 'https://mp.weixin.qq.com/s/__QrrvGsjXNTyUU6Ll493A',
- },
- {
- type: 'view',
- name: '积分统计查看视频',
- 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',
- }],
- },
- {
- type: 'view',
- name: '探访查询',
- url: 'https://info.windd.cn/infoVue/#/dependant/login',
- },
- ],
- };
- const config = this.ctx.app.config.wechat_config;
- const accessToken = await this.app.redis.get('access_token');
- const url = config.createMenu.replace('ACCESS_TOKEN', accessToken);
- const res = await this.ctx.curl(url, {
- method: 'POST',
- contentType: 'json',
- data: menu,
- dataType: 'json',
- });
- return res.data.errcode;
- }
- async getUserInfo(openid) {
- // const config = this.ctx.app.config.wechat_config;
- // const getAccessTokenUrl = config.getAccessToken.replace('APPID', 'wx91e8f7300e266ad1')
- // .replace('APPSECRET', '18ed843ea5147e338dfd987d9ddf1085');
- // const getAccessTokenRes = await this.ctx.curl(getAccessTokenUrl, {
- // dataType: 'json',
- // });
- // const access_token = getAccessTokenRes.data.access_token;
- // const access_token = await this.app.redis.get('access_token');
- // this.ctx.logger.info('AccessToken=================' + access_token);
- // const url = config.getUserInfo.replace('ACCESS_TOKEN', access_token)
- // .replace('OPENID', openid);
- // const res = await this.ctx.curl(url, {
- // dataType: 'json',
- // });
- // this.ctx.logger.info('UserInfo=================' + res);
- // return res.data;
- // --------------------
- const config = this.ctx.app.config.wechat_config;
- const access_token = await this.app.redis.get('access_token');
- const url = config.getUserInfo.replace('ACCESS_TOKEN', access_token)
- .replace('OPENID', openid);
- const res = await this.ctx.curl(url, {
- dataType: 'json',
- });
- return res.data;
- }
- async getSign(data) {
- const appId = data.appId;
- const ticket = this.app.redis.get('ticket');
- const nonceStr = Math.random().toString(36).substr(2, 15);
- const timestamp = parseInt(new Date().getTime() / 1000);
- const url = data.url;
- const string = 'jsapi_ticket=' + ticket + '&noncestr='
- + nonceStr + '×tamp=' + timestamp + '&url=' + url;
- const hash = crypto.createHash('sha1');
- hash.update(string);
- const signature = hash.digest('hex');
- return {
- appId,
- timestamp,
- nonceStr,
- signature,
- };
- }
- async oauth(data) {
- const code = data.code;
- const cid = data.cid;
- const result = await this.ctx.model.WxConfigModel.find({ cid });
- if (result.length === 1) {
- const config = this.ctx.app.config.wechat_config;
- const url = config.oauthAccessToken.replace('APPID', result[0].appid)
- .replace('SECRET', result[0].appsecret)
- .replace('CODE', code);
- const res = await this.ctx.curl(url, {
- dataType: 'json',
- });
- return res;
- }
- return {};
- // const access_token = res.data.access_token;
- // const openid = res.data.openid;
- // const userInfoUrl = await config.oauthUserinfo.replace('ACCESS_TOKEN', access_token).replace('OPENID', openid);
- // const userInfoRes = await this.ctx.curl(userInfoUrl, {
- // dataType: 'json',
- // });
- // return userInfoRes.data;
- }
- async getAppletOpenId(data) {
- const code = data.code;
- const cid = data.cid;
- const result = await this.ctx.model.WxConfigModel.find({ cid });
- if (result.length === 1) {
- const config = this.ctx.app.config.wechat_config;
- const url = config.getAppletAccessToken.replace('APPID', result[0].appid)
- .replace('SECRET', result[0].appsecret)
- .replace('CODE', code);
- const res = await this.ctx.curl(url, {
- dataType: 'json',
- });
- return res;
- }
- return {};
- }
- }
- module.exports = WxService;
|