|
@@ -15,10 +15,14 @@ class UserService extends CrudService {
|
|
this.smodel = this.ctx.model.Staff;
|
|
this.smodel = this.ctx.model.Staff;
|
|
this.emodel = this.ctx.model.Expert;
|
|
this.emodel = this.ctx.model.Expert;
|
|
}
|
|
}
|
|
|
|
+ async create(body) {
|
|
|
|
+ const { passwd, ...data } = body;
|
|
|
|
+ data.passwd = { secret: passwd };
|
|
|
|
+ return await this.model.create(data);
|
|
|
|
+ }
|
|
|
|
|
|
// 重写创建方法
|
|
// 重写创建方法
|
|
async register(data) {
|
|
async register(data) {
|
|
- console.log(data);
|
|
|
|
assert(data.name && data.phone && data.passwd, '缺少部分信息项');
|
|
assert(data.name && data.phone && data.passwd, '缺少部分信息项');
|
|
assert(/^\d{11}$/i.test(data.phone), '无效的手机号');
|
|
assert(/^\d{11}$/i.test(data.phone), '无效的手机号');
|
|
const user = await this.model.findOne({ phone: data.phone });
|
|
const user = await this.model.findOne({ phone: data.phone });
|
|
@@ -26,8 +30,8 @@ class UserService extends CrudService {
|
|
throw new BusinessError(ErrorCode.DATA_EXISTED);
|
|
throw new BusinessError(ErrorCode.DATA_EXISTED);
|
|
}
|
|
}
|
|
const newdata = _.cloneDeep(data);
|
|
const newdata = _.cloneDeep(data);
|
|
- const pas = await this.createJwtPwd(data.passwd);
|
|
|
|
- newdata.passwd = pas;
|
|
|
|
|
|
+ const pas = data.passwd;
|
|
|
|
+ newdata.passwd = { secret: pas };
|
|
let result = {};
|
|
let result = {};
|
|
if (data.type === '0' || data.type === '1' || data.type === '4') {
|
|
if (data.type === '0' || data.type === '1' || data.type === '4') {
|
|
result = await this.smodel.create(data);
|
|
result = await this.smodel.create(data);
|
|
@@ -101,29 +105,25 @@ class UserService extends CrudService {
|
|
throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
}
|
|
}
|
|
// 将用户输入的密码进行加密并与查询到的用户数据密码相比对
|
|
// 将用户输入的密码进行加密并与查询到的用户数据密码相比对
|
|
- const _oldpasswd = await this.createJwtPwd(oldpasswd);
|
|
|
|
// 如果两个密码不一致抛出异常
|
|
// 如果两个密码不一致抛出异常
|
|
- if (_oldpasswd !== user.passwd) {
|
|
|
|
|
|
+ if (oldpasswd !== user.passwd) {
|
|
throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
}
|
|
}
|
|
- const _newpasswd = await this.createJwtPwd(newpasswd);
|
|
|
|
- user.passwd = _newpasswd;
|
|
|
|
|
|
+ user.passwd = { secret: newpasswd };
|
|
await user.save();
|
|
await user.save();
|
|
}
|
|
}
|
|
|
|
|
|
async login(data) {
|
|
async login(data) {
|
|
const { phone, passwd } = data;
|
|
const { phone, passwd } = data;
|
|
// 根据用户输入的手机号查询其他用户表中是否存在相应数据
|
|
// 根据用户输入的手机号查询其他用户表中是否存在相应数据
|
|
- const user = await this.model.findOne({ phone });
|
|
|
|
|
|
+ const user = await this.model.findOne({ phone }, '+passwd');
|
|
// 如果用户不存在抛出异常
|
|
// 如果用户不存在抛出异常
|
|
if (!user) {
|
|
if (!user) {
|
|
throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
}
|
|
}
|
|
- const _user = await this.model.findOne({ phone }, '+passwd');
|
|
|
|
// 将用户输入的密码进行加密并与查询到的用户数据密码相比对
|
|
// 将用户输入的密码进行加密并与查询到的用户数据密码相比对
|
|
- const pas = await this.createJwtPwd(passwd);
|
|
|
|
// 如果两个密码不一致抛出异常
|
|
// 如果两个密码不一致抛出异常
|
|
- if (pas !== _user.passwd) {
|
|
|
|
|
|
+ if (passwd !== user.passwd.secret) {
|
|
throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
}
|
|
}
|
|
// 取出用户的类型,根据用户类型返回相应信息
|
|
// 取出用户的类型,根据用户类型返回相应信息
|