|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
const Controller = require('egg').Controller;
|
|
|
const uuid = require('uuid');
|
|
|
+const jwt = require('jsonwebtoken');
|
|
|
class LoginController extends Controller {
|
|
|
constructor(ctx) {
|
|
|
super(ctx);
|
|
@@ -9,12 +10,6 @@ class LoginController extends Controller {
|
|
|
this.model = this.ctx.model.Admin;
|
|
|
}
|
|
|
|
|
|
- // GET 通过微信号获得用户信息
|
|
|
- async fetch() {
|
|
|
- const { openid } = this.ctx.requestparam;
|
|
|
- const res = await this.service.fetchByWeixin(openid);
|
|
|
- this.ctx.ok({ userinfo: res });
|
|
|
- }
|
|
|
// 获取openid
|
|
|
async auth() {
|
|
|
try {
|
|
@@ -27,13 +22,13 @@ class LoginController extends Controller {
|
|
|
}
|
|
|
// TODO: 生成回调地址
|
|
|
const { wxapi, authUrl = this.ctx.path } = this.app.config;
|
|
|
- // windos环境
|
|
|
- // const host = this.ctx.header.referer.split('/')[2];
|
|
|
- // linux环境
|
|
|
- const host = this.ctx.header.host;
|
|
|
- const backUrl = encodeURI(`${this.ctx.protocol}://${host}${authUrl}`);
|
|
|
- const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
|
|
|
- this.ctx.redirect(to_uri);
|
|
|
+ const referer = this.ctx.header.referer;
|
|
|
+ if (referer) {
|
|
|
+ const host = referer.split('/')[2];
|
|
|
+ const backUrl = encodeURI(`${this.ctx.protocol}://${host}${authUrl}`);
|
|
|
+ const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
|
|
|
+ this.ctx.redirect(to_uri);
|
|
|
+ }
|
|
|
} catch (error) {
|
|
|
console.log(error);
|
|
|
}
|
|
@@ -44,28 +39,24 @@ class LoginController extends Controller {
|
|
|
const val = await this.app.redis.get('redirect_uri');
|
|
|
const res = await weixin.fetch(code);
|
|
|
const openid = res.openid;
|
|
|
+ const { secret, expiresIn = '1h' } = this.config.jwt;
|
|
|
+ const subject = openid;
|
|
|
+ const token = await jwt.sign({ openid }, secret, { expiresIn, issuer: 'user', subject });
|
|
|
// TODO: 重定性到跳转页面
|
|
|
- await this.ctx.render('redirect.njk', { openid, redirect_uri: val });
|
|
|
+ await this.ctx.render('redirect.njk', { openid, redirect_uri: val, token });
|
|
|
}
|
|
|
|
|
|
// POST 绑定用户微信号
|
|
|
async bind() {
|
|
|
try {
|
|
|
- const { userName, code } = this.ctx.query;
|
|
|
- if (userName) {
|
|
|
- const userinfo = await this.model.findOne({ userName });
|
|
|
- await this.app.redis.set('key', userinfo._id, 'EX', 600);
|
|
|
- }
|
|
|
+ const { code } = this.ctx.query;
|
|
|
if (code) {
|
|
|
return await this.Back({ code });
|
|
|
}
|
|
|
|
|
|
// TODO: 生成回调地址
|
|
|
const { wxapi, authUrl = this.ctx.path } = this.app.config;
|
|
|
- // windos环境
|
|
|
- // const host = this.ctx.header.referer.split('/')[2];
|
|
|
- // linux环境
|
|
|
- const host = this.ctx.header.host;
|
|
|
+ const host = this.ctx.header['x-forwarded-host'];
|
|
|
const backUrl = encodeURI(`${this.ctx.protocol}://${host}${authUrl}`);
|
|
|
const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
|
|
|
|
|
@@ -86,10 +77,13 @@ class LoginController extends Controller {
|
|
|
// mp
|
|
|
async mqtt() {
|
|
|
const key = this.ctx.params.key;
|
|
|
+ let msg = 'success';
|
|
|
+ if (key === 'bind') msg = JSON.stringify({ openid: this.ctx.query.openid });
|
|
|
+ if (key === 'pay') msg = JSON.stringify(this.ctx.query);
|
|
|
// TODO: 发布扫码成功消息
|
|
|
const { mq } = this.ctx;
|
|
|
if (mq) {
|
|
|
- await mq.topic('qrcode.topic', key, 'success', { durable: true });
|
|
|
+ await mq.topic('qrcode.topic', key, msg, { durable: true });
|
|
|
} else {
|
|
|
this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
|
|
|
}
|
|
@@ -142,14 +136,15 @@ class LoginController extends Controller {
|
|
|
}
|
|
|
// 关闭订单
|
|
|
async orderClose() {
|
|
|
- const { out_trade_no } = this.ctx.query;
|
|
|
+ const { out_trade_no } = this.ctx.request.body;
|
|
|
const res = await this.ctx.service.weixin.orderClose({ out_trade_no });
|
|
|
this.ctx.ok(res);
|
|
|
}
|
|
|
// 消息模板下发
|
|
|
async pushMould() {
|
|
|
- const { out_trade_no, openid } = this.ctx.query;
|
|
|
+ const { out_trade_no, openid } = this.ctx.request.body;
|
|
|
await this.ctx.service.weixin.pushMould({ out_trade_no, openid });
|
|
|
+ this.ctx.ok('ok');
|
|
|
}
|
|
|
}
|
|
|
|