123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 'use strict';
- const Controller = require('egg').Controller;
- const uuid = require('uuid');
- const jwt = require('jsonwebtoken');
- class LoginController extends Controller {
- constructor(ctx) {
- super(ctx);
- this.service = this.ctx.service.login;
- this.model = this.ctx.model.Admin;
- }
- // 获取openid
- async auth() {
- try {
- const { redirect_uri, code } = this.ctx.query;
- if (redirect_uri) {
- await this.app.redis.set('redirect_uri', redirect_uri, 'EX', 600);
- }
- if (code) {
- return await this.authBack({ code });
- }
- // TODO: 生成回调地址
- const { wxapi, authUrl = this.ctx.path } = this.app.config;
- 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);
- }
- }
- // GET 获取openid认证回调
- async authBack({ code }) {
- const { weixin } = this.ctx.service;
- 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, token });
- }
- // POST 绑定用户微信号
- async bind() {
- try {
- const { code } = this.ctx.query;
- if (code) {
- return await this.Back({ code });
- }
- // TODO: 生成回调地址
- const { wxapi, authUrl = this.ctx.path } = this.app.config;
- 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`;
- this.ctx.redirect(to_uri);
- } catch (error) {
- console.log(error);
- }
- }
- // GET 绑定认证回调
- async Back({ code }) {
- const { weixin } = this.ctx.service;
- const val = await this.app.redis.get('key');
- const res = await weixin.fetch(code);
- const openid = res.openid;
- // TODO: 重定性到跳转页面
- await this.ctx.render('weixinbind.njk', { openid, id: val });
- }
- // 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, msg, { durable: true });
- } else {
- this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
- }
- this.ctx.ok();
- }
- // POST 检查二维码
- async check() {
- const uuid = this.ctx.query;
- const res = await this.service.checkQrcode(uuid);
- this.ctx.ok(res);
- }
- // 获取uuid创建二维码
- async getuuid() {
- const out_trade_no = uuid.v1();
- await this.app.redis.set(out_trade_no, 'ready', 'EX', 600);
- this.ctx.ok({ uuid: out_trade_no });
- }
- // 二维码登录
- async qrcodelogin() {
- const { ctx } = this;
- const { uuid } = ctx.params;
- await this.service.qrcodelogin({ uuid });
- // this.ctx.ok(res);
- }
- // get uuid换取token
- async qrcodeToken() {
- const { ctx } = this;
- const { uuid } = ctx.query;
- const res = await this.service.qrcodeToken(uuid);
- this.ctx.ok({ data: JSON.parse(res) });
- }
- // 获取微信token
- async gettoken() {
- const { weixin } = this.ctx.service;
- const res = await weixin.gettoken();
- this.ctx.ok({ data: res });
- }
- // 预支付订单
- async orderPay() {
- const { openid, money, out_trade_no, hospitalName, subjectName, specialistName, _id } = this.ctx.request.body;
- await this.ctx.service.weixin.orderPay({ openid, money, out_trade_no, description: `${hospitalName}-${subjectName}-${specialistName}`, _id });
- this.ctx.ok();
- }
- // 支付接口
- async pay() {
- await this.ctx.service.weixin.pay();
- }
- // 关闭订单
- async orderClose() {
- 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.request.body;
- await this.ctx.service.weixin.pushMould({ out_trade_no, openid });
- this.ctx.ok('ok');
- }
- }
- module.exports = LoginController;
|