12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const _ = require('lodash');
- const Excel = require('exceljs');
- const { ObjectId } = require('mongoose').Types;
- const moment = require('moment');
- // 用户
- class UserService extends CrudService {
- constructor(ctx) {
- super(ctx, 'user');
- this.model = this.ctx.model.User;
- }
- // async create(data) {
- // // TODO:保存数据
- // data = await this.beforeCreate(data);
- // console.log(data);
- // let res = await this.model.create(data);
- // res = await this.afterCreate(data, res);
- // return res;
- // }
- async resetPwd({ id }, { password }) {
- const data = await this.model.findById(id);
- if (!data) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- data.password = { secret: password };
- await data.save();
- }
- /**
- * 登陆
- * @param {Object} body 登陆参数
- * @param body.account
- * @param body.password
- */
- async login({ account, password }) {
- let user = await this.model.findOne({ account }, '+password');
- if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
- const { password: upwd, status, role_id } = user;
- // if (status !== '1') { throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态'); }
- if (password !== upwd.secret) { throw new BusinessError(ErrorCode.BAD_PASSWORD); }
- user = JSON.parse(JSON.stringify(user));
- delete user.meta;
- delete user.__v;
- delete user.password;
- const token = this.ctx.service.util.jwt.encrypt(user);
- return token;
- }
- // 监听用户vip信息
- // async watchvip() {
- // const list = [];
- // const data = await this.model.find();
- // for (const val of data) {
- // if (val.is_vip === '1') {
- // if (moment().isAfter(val.vip_end_time)) list.push(val);
- // }
- // }
- // if (list.length > 0) {
- // for (const val of list) {
- // await this.model.updateOne({ _id: ObjectId(val._id) }, { is_vip: '0' });
- // }
- // }
- // }
- }
- module.exports = UserService;
|