Commit 3d38437b authored by 任国军's avatar 任国军

add jwt

parent 4e1c48c8
Pipeline #21639 passed with stage
in 12 seconds
......@@ -8,7 +8,7 @@ class InstitutionController extends Controller {
const { ctx } = this;
const queryParams = ctx.request.query;
const ret = await ctx.service.course.v4.institution.getClassList(queryParams);
const ret = await ctx.service.course.v5.institution.getClassList(queryParams);
ctx.success(ret);
}
......@@ -21,7 +21,7 @@ class InstitutionController extends Controller {
if (!class_id) {
ctx.failed('error class_id');
}
const ret = await ctx.service.course.v4.institution.getClassInfo(class_id);
const ret = await ctx.service.course.v5.institution.getClassInfo(class_id);
ctx.success(ret);
}
......
......@@ -18,11 +18,7 @@ const OPENID = Symbol('Context#openId');
module.exports = {
failed(message) {
const method = this.request.method.toLowerCase();
if (method === 'post') {
this.throw(422, message);
} else {
this.throw(400, message);
}
},
success(data = false) {
const method = this.request.method.toLowerCase();
......@@ -30,13 +26,13 @@ module.exports = {
this.status = 200;
this.body = data || {};
} else if (method === 'post') {
this.status = 201;
this.status = 200;
this.body = data || {};
} else if (method === 'put' || method === 'delete') {
this.status = data ? 200 : 204;
this.status = 200;
this.body = data ? data : '';
} else {
this.status = 204;
this.status = 200;
this.body = '';
}
},
......
......@@ -18,15 +18,12 @@ module.exports = {
async verify_token(ctx) {
const token = this.get_jwt(ctx);
const decode_res = await ctx.service.jwt.decode_token(token);
ctx.logger.info('decode_res', decode_res);
const token_black = await this.app.memcache.get('auth_token_' + decode_res.data.user_id);
ctx.logger.info('token_black', token_black, token);
const token_black = await this.app.memcache.get('auth_token_' + decode_res.data.user_uuid);
if (token_black == token) {
ctx.failed('token 已失效');
this.throw(422, 'jwt校验失败');
}
// if (ctx.request.body.user_id != decode_res.data.user_id) {
// ctx.failed('用户 ID 与 Token 不一致');
// }
ctx.setUserUuid(decode_res.data.user_uuid);
ctx.setOpenId(decode_res.data.openid);
return decode_res;
},
......
......@@ -2,18 +2,19 @@
module.exports = app => {
const router = app.router.namespace(app.config.projectRootPath + '/course/v5');
const miniAuth = app.middleware.miniAuthV5();// 因为不跟现有的用户中心系统,所以使用单独的登录中间件
// const miniAuth = app.middleware.miniAuthV5();// 因为不跟现有的用户中心系统,所以使用单独的登录中间件
const auth = app.middleware.verifyAuth();
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', '/user/register_user', miniAuth, 'course.v5.user.registerUserInfo');// 授权后注册用户
router.get('third', '/user/info', miniAuth, 'course.v5.user.getUserInfo');// 获取用户信息
router.post('third', '/user/register_user', auth, 'course.v5.user.registerUserInfo');// 授权后注册用户
router.get('third', '/user/info', auth, 'course.v5.user.getUserInfo');// 获取用户信息
router.get('third', '/category/all', 'course.v5.option.getCategoryList');// 获取分类列表
router.get('third', '/banner/all', 'course.v5.option.getBannerList');// 获取banner列表
router.get('third', '/class/all', 'course.v5.institution.getClassList');// 获取课程列表
router.get('third', '/class/:class_id', 'course.v5.institution.getClassInfo');// 获取课程详情
router.get('third', '/class/all', auth, 'course.v5.institution.getClassList');// 获取课程列表
router.get('third', '/class/:class_id', auth, 'course.v5.institution.getClassInfo');// 获取课程详情
};
......@@ -20,14 +20,16 @@ class InstitutionSubService extends Service {
let flag = false;
// 年龄筛选
if (!ctx.isEmpty(input.age)) {
const filterByAge = await ctx.classModel.CourseV5ClassToAge.findAll({ where: { age_id: input.age, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
const filterByAge = await ctx.classModel.V5.CourseV5ClassToAge.findAll({ where: { age_id: input.age, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
filterIds = R.pluck('class_id', filterByAge);
console.log(filterIds);
flag = true;
}
// 科目类型
if (!ctx.isEmpty(input.cat_id)) {
const filterByCategory = await ctx.classModel.CourseV5ClassToCat.findAll({ where: { cat_id: input.cate_id, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
const filterByCategory = await ctx.classModel.V5.CourseV5ClassToCat.findAll({ where: { cat_id: input.cat_id, status: 1, is_deleted: 0 }, attributes: [ 'class_id' ] });
filterIds = flag ? _.intersection(filterIds, R.pluck('class_id', filterByCategory)) : R.pluck('class_id', filterByCategory);
console.log(filterIds);
flag = true;
}
// 课程状态
......@@ -42,7 +44,7 @@ class InstitutionSubService extends Service {
filter.where.id = { $in: filterIds };
}
const classList = await ctx.classModel.CourseV5Class.findAndCountAll(filter);
const classList = await ctx.classModel.V5.CourseV5Class.findAndCountAll(filter);
// 用户收藏的课程
const userCollection = await ctx.classModel.V5.CourseUserCollection.findAll({ where: { user_uuid: ctx.userUuid, is_deleted: 0, type: 3 }, raw: true });
......@@ -76,21 +78,34 @@ class InstitutionSubService extends Service {
// 额外字段
const classColumns = await ctx.classModel.V5.CourseV5ClassToColumn.findAll({ where: { class_id: id, status: 1, is_deleted: 0 } });
let columnList = await ctx.classModel.V5.CourseV5Column.findAll({ where: { id: { $in: R.pluck('column_id', classColumns) }, status: 1, is_deleted: 0 }, attributes: [ 'name' ] });
let columnList = await ctx.classModel.V5.CourseV5Column.findAll({ where: { id: { $in: R.pluck('column_id', classColumns) }, status: 1, is_deleted: 0 }, attributes: [ 'id', 'name' ], raw: true });
columnList = _.groupBy(columnList, 'id');
for (const i in columnList) {
columnList[i][0].value = [];
}
// 机构数据
const institution = await ctx.classModel.V5.CourseV5Institution.findOne({ where: { id: classInfo.institution_id } });
const columns = [];
for (const v of classColumns) {
if (!ctx.isEmpty(columnList[v.column_id])) {
columns.push({
id: v.column_id,
key: columnList[v.column_id][0].name,
columnList[v.column_id][0].value.push({
id: v.id,
value: v.value,
});
}
}
for (const i in columnList) {
if (columnList[i][0].value.length > 0) {
columns.push(columnList[i][0]);
}
}
classInfo.columns = columns;
classInfo.institution_name = ctx.isEmpty(institution) ? '' : institution.name;
classInfo.institution_description = ctx.isEmpty(institution) ? '' : institution.description;
return classInfo;
}
......
......@@ -44,7 +44,7 @@ class UserService extends Service {
phone,
};
await ctx.app.memcache.set(key, value);
const authToken = ctx.helper.md5(userInfo.uuid + userInfo.openid + userInfo.phone + 'jbwl');
const authToken = await this.service.jwt.apply({ user_uuid: userInfo.uuid, openid: userInfo.openid });
const ret = {
user_uuid: userInfo.uuid,
......@@ -86,7 +86,7 @@ class UserService extends Service {
phone: userInfo.phone,
};
await app.memcache.set(key, value);
const authToken = ctx.helper.md5(userInfo.uuid + openid + userInfo.phone + 'jbwl');
const authToken = await this.service.jwt.apply({ user_uuid: userInfo.uuid, openid: userInfo.openid });
const ret = {
user_uuid: userInfo.uuid,
......@@ -175,7 +175,7 @@ class UserService extends Service {
phone: data.phone,
};
await app.memcache.set(key, value);
const authToken = ctx.helper.md5(data.uuid + user.openid + data.phone + 'jbwl');
const authToken = await this.service.jwt.apply({ user_uuid: data.uuid, openid: user.openid });
const ret = {
user_uuid: data.uuid,
......
'use strict';
const Service = require('egg').Service;
class JWTService extends Service {
async apply(data) {
const { ctx } = this;
const exp = ctx.app.config.jwt.exp;
const secret = ctx.app.config.jwt.secret;
const auth_token = ctx.app.jwt.sign(
{
data,
exp,
},
secret
);
return auth_token;
}
async decode_token(token) {
const { ctx } = this;
return ctx.app.jwt.decode(token, ctx.app.config.jwt.secret);
}
async del_token(token) {
const user_id = this.decode_token(token).data.user_id;
await this.app.memcache.set('auth_token_' + user_id, token, this.ctx.app.config.jwt.exp);
return true;
}
}
module.exports = JWTService;
......@@ -17,6 +17,13 @@ module.exports = appInfo => {
domainWhiteList: [],
};
config.jwt = {
secret: '51business',
exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30,
enable: false, // default is false
ignore: [],
};
config.middleware = [ 'errorHandler', 'deviceLogin', 'deviceInit', 'responseSet' ];
// config.middleware = [];
......
......@@ -24,6 +24,13 @@ module.exports = appInfo => {
domainWhiteList: [],
};
config.jwt = {
secret: '51business',
exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 30,
enable: false, // default is false
ignore: [],
};
config.sequelize = {
datasources: [
......
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