Commit 7781913e authored by 李尚科's avatar 李尚科

add kefu oauth

parent 9bfb86e5
Pipeline #18860 passed with stage
in 10 seconds
...@@ -201,5 +201,67 @@ class WechatController extends Controller { ...@@ -201,5 +201,67 @@ class WechatController extends Controller {
return user_info; return user_info;
} }
// 微信公众号客服页面授权跳转地址
async kefuOauthLogin() {
const { ctx } = this;
const code = ctx.query.code;
const channel_id = ctx.query.channel_id;
const ident = ctx.query.ident;
if (!ident) ctx.failed('ident配置错误1!');
const wechatApp = await ctx.huodongModel.WechatApp.findOne({ where: { ident } });
if (!wechatApp || !wechatApp.appid || !wechatApp.appsecret) ctx.failed('ident配置错误2!');
const appid = wechatApp.appid;
const appsecret = wechatApp.appsecret;
if (!code || code.length === 0) { // this.app.config.OUT_P_NODE_URL
const target_url = WECHAT_CODE_URL + `?appid=${appid}&redirect_uri=${encodeURIComponent(`${this.app.config.PULIC_BASE_URL}/51business/api/gjj/wechat/oauth_athena?&channel_id=${channel_id}&ident=${ident}`)}&response_type=code&scope=snsapi_base&state=${ident}#wechat_redirect`;
ctx.redirect(target_url); return;
}
const url = `${WECHAT_AUTH}?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`;
const result = await ctx.helper.send_request(url, {}, { method: 'GET' });
ctx.logger.info(JSON.stringify({ wx_kefu_query: result }));
if (result.status !== 200) {
ctx.failed('获取openid失败1');
}
const wx_ret = result.data;
if (!wx_ret.openid) {
ctx.failed('获取openid失败2');
}
const openid = wx_ret.openid;
// 在微信端需要关闭微信重新打开才会销毁cookie值,所以在这里重置cookie值
ctx.cookies.set('openid', openid, { httpOnly: false, signed: false, path: '/', overwrite: true });
ctx.cookies.set('token', null, { httpOnly: false, signed: false, path: '/', overwrite: true });
ctx.cookies.set('user_id', null, { httpOnly: false, signed: false, path: '/', overwrite: true });
ctx.cookies.set('app_user_id', null, { httpOnly: false, signed: false, path: '/', overwrite: true });
ctx.cookies.set('jianbing_customer_id', null, { httpOnly: false, signed: false, path: '/', overwrite: true });
ctx.cookies.set('auth_token', null, { httpOnly: false, signed: false, path: '/', overwrite: true });
const redirect_page = wechatApp.login_url ? wechatApp.login_url : (wechatApp.business_url ? wechatApp.business_url : REDIRECT_PAGE);
const redirect_url = redirect_page.indexOf('http') > -1 ? redirect_page : this.app.config.PULIC_BASE_URL + redirect_page;
// 判断是否已绑定手机号
const user_exist_url = `${this.app.config.NODE_BASE_URL}/user_api/v1/user/is_exist/${openid}`;
const bind_phone_result = await ctx.helper.send_request(user_exist_url, { type: 5 }, { method: 'GET' });
ctx.logger.info(JSON.stringify({ kefu_bind_phone_result: bind_phone_result }));
if (bind_phone_result.status !== 200) {
ctx.redirect(redirect_url + `?channel_id=${channel_id}&type=noLogin`); return;
}
const user_exist_ret = bind_phone_result.data;
if (user_exist_ret.result !== 'true') { // 未绑定的手机号时
ctx.redirect(redirect_url + `?channel_id=${channel_id}&type=noLogin`); return;
}
const login_result = await this.user_login({ code, openid, channel_id });// 已绑定的用户 直接登录
if (login_result.anew && !login_result.token) {
ctx.redirect(redirect_url + `?channel_id=${channel_id}&type=noLogin`); return;
}
ctx.redirect(redirect_url + `?channel_id=${channel_id}&type=hasLogin`); return;// 已导入公积金
}
} }
module.exports = WechatController; module.exports = WechatController;
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const WechatApp = app.huodongModel.define('wechat_app', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: STRING,
ident: STRING,
appid: STRING,
appsecret: STRING,
business_url: STRING,
login_url: STRING,
status: INTEGER,
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return (date && date.toString() !== 'Invalid Date') ? moment(date).format('YYYY-MM-DD HH:mm:ss') : '0000-00-00 00:00:00';
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return (date && date.toString() !== 'Invalid Date') ? moment(date).format('YYYY-MM-DD HH:mm:ss') : '0000-00-00 00:00:00';
},
},
}, {
timestamps: false,
tableName: 'wechat_app',
});
return WechatApp;
};
...@@ -8,5 +8,6 @@ module.exports = app => { ...@@ -8,5 +8,6 @@ module.exports = app => {
router.get('/wechat/check_login', 'gjj.wechat.checkLogin');// 公积金公众号检查用户是否登录 router.get('/wechat/check_login', 'gjj.wechat.checkLogin');// 公积金公众号检查用户是否登录
router.get('third', '/wechat/oauth_login', 'gjj.wechat.oauthLogin');// 公积金公众号授权登录 router.get('third', '/wechat/oauth_login', 'gjj.wechat.oauthLogin');// 公积金公众号授权登录
router.get('third', '/wechat/oauth_athena', 'gjj.wechat.kefuOauthLogin');// 客服页面授权登录
// router.get('/wechat/check_gjj', 'gjj.wechat.checkGjj');// 公积金公众号授权登录 // router.get('/wechat/check_gjj', 'gjj.wechat.checkGjj');// 公积金公众号授权登录
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment