|
@@ -12,25 +12,84 @@ class UserService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx, 'user');
|
|
|
this.model = this.ctx.model.User;
|
|
|
+ this.smodel = this.ctx.model.Staff;
|
|
|
+ this.emodel = this.ctx.model.Expert;
|
|
|
}
|
|
|
|
|
|
|
|
|
- async create(data) {
|
|
|
- const { userid, openid, passwd, role_id, name, phone, type } = data;
|
|
|
-
|
|
|
- assert(name && phone && passwd, '缺少部分信息项');
|
|
|
- assert(/^\d{11}$/i.test(phone), '无效的手机号');
|
|
|
- const user = await this.model.findOne({ phone });
|
|
|
+ async register(data) {
|
|
|
+ console.log(data);
|
|
|
+ assert(data.name && data.phone && data.passwd, '缺少部分信息项');
|
|
|
+ assert(/^\d{11}$/i.test(data.phone), '无效的手机号');
|
|
|
+ const user = await this.model.findOne({ phone: data.phone });
|
|
|
if (user) {
|
|
|
throw new BusinessError(ErrorCode.DATA_EXISTED);
|
|
|
}
|
|
|
- const newdata = data;
|
|
|
- const pas = await this.createJwtPwd(passwd);
|
|
|
+ const newdata = _.cloneDeep(data);
|
|
|
+ const pas = await this.createJwtPwd(data.passwd);
|
|
|
newdata.passwd = pas;
|
|
|
+ let result = {};
|
|
|
+ if (data.type === '0' || data.type === '1') {
|
|
|
+ result = await this.smodel.create(data);
|
|
|
+ }
|
|
|
+ if (data.type === '2') {
|
|
|
+ result = await this.emodel.create(data);
|
|
|
+ }
|
|
|
+ newdata.userid = result.id;
|
|
|
const res = await this.model.create(newdata);
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
+ async update({ id }, data) {
|
|
|
+ const user = await this.model.findById(id);
|
|
|
+ const { userid, openid, role_id, name, phone, type } = data;
|
|
|
+ if (userid) {
|
|
|
+ user.userid = userid;
|
|
|
+ }
|
|
|
+ if (openid) {
|
|
|
+ user.openid = openid;
|
|
|
+ }
|
|
|
+ if (role_id) {
|
|
|
+ user.role_id = role_id;
|
|
|
+ }
|
|
|
+ if (name) {
|
|
|
+ if (user.type === '0' || user.type === '1') {
|
|
|
+ const staff = await this.smodel.findById(user.userid);
|
|
|
+ staff.name = name;
|
|
|
+ await staff.save();
|
|
|
+ }
|
|
|
+ if (user.type === '2') {
|
|
|
+ const expert = await this.emodel.findById(user.userid);
|
|
|
+ expert.name = name;
|
|
|
+ await expert.save();
|
|
|
+ }
|
|
|
+ user.name = name;
|
|
|
+ }
|
|
|
+ if (phone) {
|
|
|
+ const _user = await this.model.find({ phone });
|
|
|
+ if (_user.length === 0 || (_user.lenght === 1 && _user[0].id === user.id)) {
|
|
|
+ if (user.type === '0' || user.type === '1') {
|
|
|
+ const staff = await this.smodel.findById(user.userid);
|
|
|
+ staff.phone = phone;
|
|
|
+ await staff.save();
|
|
|
+ }
|
|
|
+ if (user.type === '2') {
|
|
|
+ const expert = await this.emodel.findById(user.userid);
|
|
|
+ expert.phone = phone;
|
|
|
+ await expert.save();
|
|
|
+ }
|
|
|
+ user.phone = phone;
|
|
|
+ } else {
|
|
|
+ throw new BusinessError(ErrorCode.DATA_EXISTED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (type) {
|
|
|
+ user.type = type;
|
|
|
+ }
|
|
|
+ const res = await user.save();
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
async uppasswd(data) {
|
|
|
const { userid, oldpasswd, newpasswd } = data;
|