12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 'use strict';
- const Service = require('egg').Service;
- const assert = require('assert');
- const moment = require('moment');
- const crypto = require('crypto');
- class adminUserService extends Service {
- async create({ acct, password, userName, phone, state, roleList, columnList }) {
- assert(acct, '帐号不存在');
- assert(password, '密码不存在');
- assert(userName, '用户名不存在');
- assert(phone, '手机号不存在');
- assert(state, '状态不存在');
- const { AdminUser: model } = this.ctx.model;
- const createAt = moment().format('x');
- const hash = crypto.createHmac('sha256', this.app.config.userSecret);
- const pwa = hash.update(password).digest('hex');
- try {
- const res = await model.create({ acct, password: pwa, userName, phone, createAt, state, roleList, columnList });
- return { errmsg: '', errcode: 0, res };
- } catch (error) {
- throw new Error({ errcode: -2001, errmsg: '添加失败' });
- }
- }
- async update({ userName, phone, id, roleList, state, columnList }) {
- assert(id, 'id不存在');
- const { AdminUser: model } = this.ctx.model;
- try {
- await model.findByIdAndUpdate(id, { userName, phone, roleList, state, columnList });
- return { errmsg: '', errcode: 0 };
- } catch (error) {
- throw new Error({ errcode: -2001, errmsg: '修改失败' });
- }
- }
- async pwdUpdate({ password, id, confirmPwd }) {
- assert(id, 'id不存在');
- const { AdminUser: model } = this.ctx.model;
- const hash = crypto.createHmac('sha256', this.app.config.userSecret);
- const cpwd = hash.update(confirmPwd).digest('hex');
- try {
- const res = await model.findById(id);
- if (res.password !== cpwd) {
- return { errmsg: '原密码错误', errcode: -2003 };
- }
- const hash = crypto.createHmac('sha256', this.app.config.userSecret);
- const pwd = hash.update(password).digest('hex');
- await model.findByIdAndUpdate(id, { password: pwd });
- return { errmsg: '', errcode: 0 };
- } catch (error) {
- throw new Error({ errcode: -2001, errmsg: '修改失败' });
- }
- }
- async del({ id }) {
- assert(id, 'id不存在');
- const { AdminUser: model } = this.ctx.model;
- try {
- await model.findOneAndDelete(id);
- return { errmsg: '', errcode: 0 };
- } catch (error) {
- throw new Error({ errcode: -2001, errmsg: '删除失败' });
- }
- }
- async query({ skip, limit, filter }) {
- const { AdminUser: model } = this.ctx.model;
- try {
- let res;
- if (skip && limit) {
- res = await model.find({ ...filter, password: false }).skip(skip * limit).limit(limit);
- } else {
- res = await model.find(filter);
- }
- return { errmsg: '', errcode: 0, data: res };
- } catch (error) {
- throw new Error({ errcode: -2001, errmsg: '查询失败' });
- }
- }
- }
- module.exports = adminUserService;
|