123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- // 用户管理
- class UserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'user');
- this.model = this.ctx.model.User.User;
- this.studentModel = this.ctx.model.User.Student;
- this.schoolModel = this.ctx.model.User.School;
- this.coachModel = this.ctx.model.User.Coach;
- }
- /**
- * 检查数据重复
- */
- async createCheck() {
- const data = this.ctx.request.body;
- const { card, phone, openid } = data;
- let num = await this.model.count({ card });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该身份证号已被注册');
- num = await this.model.count({ phone });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该手机号码已被注册');
- num = await this.model.count({ openid });
- if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该微信号码已被注册');
- return true;
- }
- /**
- * 微信小程序登录
- * @param {String} openid 微信小程序的openid
- */
- async wxAppLogin({ openid }) {
- const user = await this.model.findOne({ openid });
- // 没注册的需要手动注册
- if (!user) return user;
- const { type, _id } = user;
- // 超级管理员,普通用户,游客不需要其他信息,user里的就是
- if ([ '-1', '0', '10' ].includes(type)) return user;
- let infoModel;
- if (type === '1') infoModel = this.schoolModel;
- else if (type === '2') infoModel = this.coachModel;
- else if (type === '3') infoModel = this.studentModel;
- else throw new BusinessError(ErrorCode.USER_NOT_EXIST, '无法确定该用户的角色');
- const info = await infoModel.findOne({ user_id: _id });
- return { ...JSON.parse(JSON.stringify(user)), info };
- }
- }
- module.exports = UserService;
|