'use strict'; const Controller = require('egg').Controller; const { v4: uuidv4 } = require('uuid'); class WxapiController extends Controller { async get_openid() { const { ctx } = this; const { code, redirect_uri, userId, state } = ctx.query.code; const { appid } = this.app.config.wxconfig; const uuid = uuidv4(); if (redirect_uri) { // redirect_uri = 获取openid后跳转地址(可以是用户正常回调地址,也可以是绑定页面地址) await this.app.redis.set(`redirect_uri${uuid}`, redirect_uri, 'EX', 600); } if (userId) { await this.app.redis.set(`userId${uuid}`, userId, 'EX', 600); } if (code) { return await this.get_openid_back({ code, state }); } // const host = this.ctx.header.referer.split('/')[2]; const host = this.ctx.header.host; const backUrl = `${this.ctx.protocol}://${host}${ctx.path}`; const to_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${backUrl}&response_type=code&state=${uuid}&scope=snsapi_base#wechat_redirect`; ctx.redirect(to_url); } // GET 获取openid认证回调 async get_openid_back({ code, state }) { const redirect_uri = await this.app.redis.get(`redirect_uri${state}`); const userId = await this.app.redis.get(`userId${state}`); if (!redirect_uri) return { errcode: -1001, errmsg: '回调地址不存在', data: '' }; const { appid, appsecret } = this.app.config.wxconfig; const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`; const { openid } = await this.ctx.curl(url); // TODO: 重定性到跳转页面 await this.ctx.render('redirect.njk', { openid, redirect_uri, userId }); } async get_userInfo() { const { ctx } = this; const { code, redirect_uri, state, userId } = ctx.query; const { appid } = this.app.config.wxconfig; const uuid = uuidv4(); if (redirect_uri) { await this.app.redis.set(`redirect_uri${uuid}`, redirect_uri, 'EX', 600); } if (userId) { await this.app.redis.set(`userId${uuid}`, userId, 'EX', 600); } if (code) { return await this.get_userInfo_back({ code, state }); } // const host = this.ctx.header.referer.split('/')[2]; const host = this.ctx.header.host; const backUrl = `${this.ctx.protocol}://${host}${ctx.path}`; const to_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${backUrl}&response_type=code&state=${uuid}&scope=snsapi_userinfo#wechat_redirect`; ctx.redirect(to_url); } // GET 获取用户信息认证回调 async get_userInfo_back({ code, state }) { const redirect_uri = await this.app.redis.get(`redirect_uri${state}`); if (!redirect_uri) return { errcode: -1001, errmsg: '回调地址不存在', data: '' }; const userId = await this.app.redis.get(`userId${state}`); const { appid, appsecret } = this.app.config.wxconfig; const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`; const { openid, access_token } = await this.ctx.curl(url); const wxuserurl = `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`; const wxuserinfo = await this.ctx.curl(wxuserurl); // TODO: 重定性到跳转页面 await this.ctx.render('redirect.njk', { openid, redirect_uri, wxuserinfo, userId }); } // 获取access_token async get_accessToken() { const { appid, appsecret } = this.app.config.wxconfig; const to_uri = `https://api.weixin.qq.com/cgi-bin/token?appid=${appid}&secret=${appsecret}&grant_type=client_credential`; const { access_token } = await this.ctx.curl(to_uri); return access_token; } // 下发模板消息 async pushMould() { const { adminTemplateId } = this.app.config.wxconfig; const access_token = await this.gettoken(); const to_uri = `ttps://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`; const { openid, data } = this.ctx.request.body; await this.ctx.curl(to_uri, { method: 'POST', dataType: 'json', data: JSON.stringify({ touser: openid, template_id: adminTemplateId, data, }), }); } } module.exports = WxapiController;