|
@@ -0,0 +1,169 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const _ = require('lodash');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+const jwt = require('jsonwebtoken');
|
|
|
+class UserService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'marketuser');
|
|
|
+ this.model = this.ctx.model.Marketuser;
|
|
|
+ this.modeluser = this.ctx.model.User;
|
|
|
+ }
|
|
|
+
|
|
|
+ async create(data) {
|
|
|
+ const { name, password, role, phone } = data;
|
|
|
+ assert(name, '用户名不能为空');
|
|
|
+ assert(password, '密码不能为空');
|
|
|
+ const has_phone = await this.model.findOne({ phone, role });
|
|
|
+ if (has_phone) {
|
|
|
+ throw new BusinessError('此身份手机号已被注册,请更换手机号');
|
|
|
+ }
|
|
|
+ data.password = { secret: password };
|
|
|
+ const res = await this.model.create(data);
|
|
|
+ if (res) {
|
|
|
+ const newdata = {
|
|
|
+ name,
|
|
|
+ phone: data.phone,
|
|
|
+ passwd: data.password,
|
|
|
+ uid: res.id,
|
|
|
+ role: data.role,
|
|
|
+ pid: data.pid,
|
|
|
+ deptname: data.deptname,
|
|
|
+ code: data.code,
|
|
|
+ };
|
|
|
+ if (role === '5') {
|
|
|
+ newdata.phone = data.institution_code;
|
|
|
+ }
|
|
|
+ const auth_user = await this.modeluser.findOne({ phone, role });
|
|
|
+ if (auth_user) {
|
|
|
+ throw new BusinessError('此身份手机号已被注册,请更换手机号');
|
|
|
+ }
|
|
|
+ const authres = await this.modeluser.create(newdata);
|
|
|
+ return authres;
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户修改密码
|
|
|
+ async uppasswd(data) {
|
|
|
+ const { uid, newpasswd } = data;
|
|
|
+ assert(uid && newpasswd, '缺少部分信息项');
|
|
|
+ // 根据用户id查询其他用户表中是否存在相应数据
|
|
|
+ const user = await this.model.findById(uid, '+password');
|
|
|
+ // 如果用户不存在抛出异常
|
|
|
+ if (!user) {
|
|
|
+ throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
|
+ }
|
|
|
+ user.password = { secret: data.newpasswd };
|
|
|
+ await user.save();
|
|
|
+ }
|
|
|
+
|
|
|
+ async update({ id }, data) {
|
|
|
+ const user = await this.modeluser.findById(id);
|
|
|
+ if (user) {
|
|
|
+ const res = await this.ctx.service.user.update({ id }, data);
|
|
|
+ if (res) {
|
|
|
+ const marketuser = await this.model.findById(user.uid);
|
|
|
+ if (marketuser) {
|
|
|
+ marketuser.name = data.name;
|
|
|
+ marketuser.phone = data.phone;
|
|
|
+ marketuser.addr = data.addr;
|
|
|
+ marketuser.img_path = data.img_path;
|
|
|
+ marketuser.institution_code = data.phone;
|
|
|
+ marketuser.office_phone = data.office_phone;
|
|
|
+ marketuser.profession = data.profession;
|
|
|
+ marketuser.status = data.status;
|
|
|
+ marketuser.role = data.role;
|
|
|
+ marketuser.pid = data.pid;
|
|
|
+ marketuser.deptname = data.deptname;
|
|
|
+ marketuser.code = data.code;
|
|
|
+ marketuser.companytype = data.companytype;
|
|
|
+ marketuser.companydate = data.companydate;
|
|
|
+ marketuser.companycapital = data.companycapital;
|
|
|
+ marketuser.companyperson = data.companyperson;
|
|
|
+ marketuser.sndqyzsr = data.sndqyzsr;
|
|
|
+ marketuser.sndyffy = data.sndyffy;
|
|
|
+ marketuser.companytotal = data.companytotal;
|
|
|
+ marketuser.zjzyfrs = data.zjzyfrs;
|
|
|
+ marketuser.companybrief = data.companybrief;
|
|
|
+ marketuser.mainproduct = data.mainproduct;
|
|
|
+ marketuser.qualifications = data.qualifications;
|
|
|
+ }
|
|
|
+ const marketres = await marketuser.save();
|
|
|
+ return marketres;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async login({ phone, password, role }) {
|
|
|
+ assert(phone, '手机号不能为空');
|
|
|
+ assert(password, '密码不能为空');
|
|
|
+ assert(role, '需要选择用户类型');
|
|
|
+ const user = await this.model.findOne({ phone, role }, '+password');
|
|
|
+ if (!user) {
|
|
|
+ throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
|
+ }
|
|
|
+ if (user.password.secret !== password) {
|
|
|
+ throw new BusinessError(ErrorCode.BAD_PASSWORD);
|
|
|
+ }
|
|
|
+ if (user.status === '0') {
|
|
|
+ throw new BusinessError('用户未审核,请等待审核后再登陆');
|
|
|
+ } else if (user.status === '2') {
|
|
|
+ throw new BusinessError('用户申请被拒绝,请联系平台管理员');
|
|
|
+ }
|
|
|
+ return await this.createJwt(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建登录Token
|
|
|
+ async createJwt({
|
|
|
+ id,
|
|
|
+ name,
|
|
|
+ phone,
|
|
|
+ is_qy,
|
|
|
+ img_path,
|
|
|
+ email,
|
|
|
+ resume,
|
|
|
+ major,
|
|
|
+ office_phone,
|
|
|
+ profession,
|
|
|
+ role,
|
|
|
+ status,
|
|
|
+ columnid,
|
|
|
+ }) {
|
|
|
+ const { secret, expiresIn = '1d', issuer } = this.config.jwt;
|
|
|
+ const subject = phone;
|
|
|
+ // const _userid = id;
|
|
|
+ const res = {
|
|
|
+ name,
|
|
|
+ phone,
|
|
|
+ is_qy,
|
|
|
+ id,
|
|
|
+ img_path,
|
|
|
+ email,
|
|
|
+ resume,
|
|
|
+ major,
|
|
|
+ office_phone,
|
|
|
+ profession,
|
|
|
+ role,
|
|
|
+ status,
|
|
|
+ columnid,
|
|
|
+ };
|
|
|
+ const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ // 重写删除方法
|
|
|
+ /**
|
|
|
+ * 根据id删除=>修改isdel为1
|
|
|
+ * @param {Object} {id} 只接收id,不过需要解构,因为是object形式过来的
|
|
|
+ */
|
|
|
+ async delete({ id }) {
|
|
|
+ const res = await this.model.update({ _id: ObjectId(id) }, { isdel: '1' });
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = UserService;
|