Commit ec7f4d10 authored by 任国军's avatar 任国军

add appLogin

parent 0e662e6e
Pipeline #25682 passed with stage
in 4 seconds
......@@ -132,6 +132,15 @@ class UserController extends Controller {
const ret = await service.course.v5.user.redeemClass(params);
ctx.success(ret);
}
// app登录
async loginByApp() {
const { ctx, service } = this;
const ret = await service.course.v5.user.loginByApp();
ctx.success(ret);
}
}
module.exports = UserController;
......@@ -14,6 +14,7 @@ const USERID = Symbol('Context#userId');
const OLDCOOKIE = Symbol('Context#oldCookie');
const USERUUID = Symbol('Context#userUuid');
const OPENID = Symbol('Context#openId');
const TYPE = Symbol('Context#type');
module.exports = {
failed(message) {
......@@ -178,6 +179,10 @@ module.exports = {
return this[OPENID];
},
get type() {
return this[TYPE];
},
setAppUserId(app_user_id) {
this[APPUSERID] = app_user_id;
},
......@@ -217,4 +222,8 @@ module.exports = {
setOpenId(openId) {
this[OPENID] = openId;
},
setType(type) {
this[TYPE] = type;
},
};
......@@ -7,6 +7,42 @@ module.exports = options => {
} else {
await ctx.helper.verify_token_sample(ctx);
}
// 判断header中的type
const type = ctx.request.header.type;
ctx.logger.info('course_headers: ' + JSON.stringify(ctx.request.header));
if (!ctx.isEmpty(type) && type !== 'h5' && type !== 'miniProgram' && !ctx.isEmpty(ctx.userUuid)) {
const appUserId = ctx.cookies.get('app_user_id', { signed: false });
const token = ctx.cookies.get('token', { signed: false });
const userId = ctx.cookies.get('user_id', { signed: false });
const deviceId = ctx.cookies.get('device_id', { signed: false });
const deviceLoginId = ctx.cookies.get('device_login_id', { signed: false });
const params = {
app_user_id: appUserId,
token,
uid: userId,
device_id: deviceId,
device_login_id: deviceLoginId,
};
const result = await ctx.helper.send_request(ctx.app.config.NODE_URL + '/user/auth', params, {
method: 'POST',
});
ctx.logger.info(JSON.stringify({ url: ctx.app.config.NODE_URL + '/user/auth', request_header: ctx.request.header, user_auth_params: params, user_auth_result: result }));
if (result.status !== 201 || ctx.isEmpty(result.data)) {
this.throw(401, 'token已失效');
return;
}
const phone = ctx.isEmpty(result.data.data.passport) ? '' : result.data.data.passport;
const user = await ctx.classModel.V5.CourseUser.findOne({ where: { uuid: ctx.userUuid, is_deleted: 0 } });
if (ctx.isEmpty(user) || user.phone !== phone) {
this.throw(401, 'token校验不一致');
}
}
await next();
};
};
......@@ -9,6 +9,7 @@ module.exports = app => {
router.post('third', '/verification_code', 'course.v5.user.sendVerificationCode');// 发送验证码
router.post('third', '/login/phone', 'course.v5.user.loginByPhone');// 手机号登录
router.post('third', '/login/wechat', 'course.v5.user.loginByWX');// 微信登录
router.post('third', '/login/app', 'course.v5.user.loginByApp');// app授权登录
router.post('third', '/user/register_user', auth({ is_force: 0 }), 'course.v5.user.registerUserInfo');// 授权后注册用户
router.get('third', '/user/info', auth({ is_force: 1 }), 'course.v5.user.getUserInfo');// 获取用户信息
router.post('third', '/user/baby', auth({ is_force: 1 }), 'course.v5.user.addUserBaby');// 上传用户宝宝信息
......
......@@ -511,6 +511,76 @@ class UserService extends Service {
return { class_id: codeInfo.class_id, result: true, url: classInfo.button_url };
}
// app登录
async loginByApp() {
const { ctx } = this;
const appUserId = ctx.cookies.get('app_user_id', { signed: false });
const token = ctx.cookies.get('token', { signed: false });
const userId = ctx.cookies.get('user_id', { signed: false });
const deviceId = ctx.cookies.get('device_id', { signed: false });
const deviceLoginId = ctx.cookies.get('device_login_id', { signed: false });
const params = {
app_user_id: appUserId,
token,
uid: userId,
device_id: deviceId,
device_login_id: deviceLoginId,
};
const result = await ctx.helper.send_request(ctx.app.config.NODE_URL + '/user/auth', params, {
method: 'POST',
});
ctx.logger.info(JSON.stringify({ url: ctx.app.config.NODE_URL + '/user/auth', request_header: ctx.request.header, user_auth_params: params, user_auth_result: result }));
if (result.status !== 201 || ctx.isEmpty(result.data)) {
ctx.throw(401, 'token已失效');
return;
}
const phone = ctx.isEmpty(result.data.data.passport) ? '' : result.data.data.passport;
if (ctx.isEmpty(phone)) {
ctx.failed('phone异常');
}
// 判断用户是否存在
let userInfo = await ctx.classModel.V5.CourseUser.findOne({ where: { phone, is_deleted: 0 } });
if (ctx.isEmpty(userInfo)) {
const uuid = uuidV4();
await ctx.classModel.V5.CourseUser.findOrCreate({ where: { phone, is_deleted: 0 }, defaults: { uuid, phone } });
userInfo = await ctx.classModel.V5.CourseUser.findOne({ where: { phone, is_deleted: 0 } });
}
const data = {
app_id: result.data.data.app_id,
app_type_id: result.data.data.app_type_id,
user_id: result.data.data.user_id,
app_user_id: result.data.data.app_user_id,
};
await ctx.classModel.V5.CourseUser.update(data, { where: { id: userInfo.id } });
const key = 'course_v5_user_session_' + userInfo.uuid;
const value = {
user_uuid: userInfo.uuid,
openid: userInfo.openid,
session_key: '',
phone,
};
await ctx.app.memcache.set(key, value, 7 * 24 * 3600);
let authToken = await ctx.app.memcache.get('course_auth_token_' + userInfo.uuid);
if (ctx.isEmpty(authToken)) {
authToken = await this.service.jwt.apply({ user_uuid: userInfo.uuid, openid: userInfo.openid });
await ctx.app.memcache.set('course_auth_token_' + userInfo.uuid, authToken, 30 * 24 * 3600);
}
const ret = {
user_uuid: userInfo.uuid,
auth_token: authToken,
};
return ret;
}
}
module.exports = UserService;
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