'use strict'; /** * * * @author Ellisran * @date 2020/7/2 * @version */ const moment = require('moment'); const Controller = require('egg').Controller; const crypto = require('crypto'); module.exports = app => { class WechatController extends Controller { /** * 接入微信 * * @param {Object} ctx - egg全局页面 * @return {void} */ async index(ctx) { try { const signature = ctx.query.signature; const timestamp = ctx.query.timestamp; const nonce = ctx.query.nonce; const echostr = ctx.query.echostr; const array = [ctx.app.config.wechatAll.token, timestamp, nonce]; array.sort(); const tempStr = array.join(''); const hashCode = crypto.createHash('sha1'); const resultCode = hashCode.update(tempStr, 'utf8').digest('hex'); if (resultCode === signature) { ctx.body = echostr; // res.send(echostr); } else { throw '验证失败'; } } catch (e) { console.log(e); } } /** * 登录验证 * * @param {Object} ctx - egg全局页面 * @return {void} */ async oauth(ctx) { console.log('hello'); const url = await app.wechat.oauth.getAuthorizeURL('http://jluat.smartcost.com.cn/wx/hello', 'hello world', 'snsapi_userinfo'); console.log(url); // const token = await app.wechat.oauth.getAccessToken(ctx.query.code); // const accessToken = token.data.access_token; // const openid = token.data.openid; // console.log(token); } async hello(ctx) { console.log('hello world'); } } return WechatController; };