|
@@ -1,7 +1,9 @@
|
|
'use strict';
|
|
'use strict';
|
|
-const crypto = require('crypto');
|
|
|
|
|
|
+// const crypto = require('crypto');
|
|
const assert = require('assert');
|
|
const assert = require('assert');
|
|
const Service = require('egg').Service;
|
|
const Service = require('egg').Service;
|
|
|
|
+const sm3 = require('sm3');
|
|
|
|
+const uuid = require('uuid');
|
|
class AdminUserService extends Service {
|
|
class AdminUserService extends Service {
|
|
constructor(ctx) {
|
|
constructor(ctx) {
|
|
super(ctx);
|
|
super(ctx);
|
|
@@ -11,11 +13,16 @@ class AdminUserService extends Service {
|
|
async create({ userName, password, openid, name, phone }) {
|
|
async create({ userName, password, openid, name, phone }) {
|
|
assert(userName, '缺少用户名');
|
|
assert(userName, '缺少用户名');
|
|
assert(password, '缺少密码');
|
|
assert(password, '缺少密码');
|
|
- password = crypto.createHash('md5').update(password).digest('hex');
|
|
|
|
|
|
+ // 生成uuid盐值
|
|
|
|
+ const salt = uuid.v1();
|
|
|
|
+ // sm3 加盐加密
|
|
|
|
+ password = sm3(`${password}:${salt}`);
|
|
|
|
+ console.log(password, 'password');
|
|
|
|
+ console.log(salt, 'salt');
|
|
const obj = await this.model.findOne({ userName });
|
|
const obj = await this.model.findOne({ userName });
|
|
if (obj) return { errcode: -1001, errmsg: '用户名已存在', data: '' };
|
|
if (obj) return { errcode: -1001, errmsg: '用户名已存在', data: '' };
|
|
try {
|
|
try {
|
|
- const res = await this.model.create({ userName, password, openid, name, phone });
|
|
|
|
|
|
+ const res = await this.model.create({ userName, password, openid, name, phone, salt });
|
|
return { errcode: 0, errmsg: 'ok', data: res };
|
|
return { errcode: 0, errmsg: 'ok', data: res };
|
|
} catch (error) {
|
|
} catch (error) {
|
|
throw error;
|
|
throw error;
|
|
@@ -23,10 +30,10 @@ class AdminUserService extends Service {
|
|
}
|
|
}
|
|
async update({ id, userName, password, openid, name, phone }) {
|
|
async update({ id, userName, password, openid, name, phone }) {
|
|
assert(id, '缺少用户ID');
|
|
assert(id, '缺少用户ID');
|
|
- if (password) password = crypto.createHash('md5').update(password).digest('hex');
|
|
|
|
|
|
+ const user = await this.model.findOne({ _id: id });
|
|
|
|
+ if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
|
|
|
|
+ if (password) password = sm3(`${password}:${user.salt}`);
|
|
try {
|
|
try {
|
|
- const user = await this.model.findOne({ _id: id });
|
|
|
|
- if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
|
|
|
|
await this.model.updateOne({ _id: id }, { userName, password, openid, name, phone });
|
|
await this.model.updateOne({ _id: id }, { userName, password, openid, name, phone });
|
|
return { errcode: 0, errmsg: 'ok', data: 'update' };
|
|
return { errcode: 0, errmsg: 'ok', data: 'update' };
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -68,11 +75,11 @@ class AdminUserService extends Service {
|
|
assert(id, '缺少用ID');
|
|
assert(id, '缺少用ID');
|
|
assert(password, '缺少新密码');
|
|
assert(password, '缺少新密码');
|
|
assert(oldpassword, '缺少原密码');
|
|
assert(oldpassword, '缺少原密码');
|
|
- oldpassword = crypto.createHash('md5').update(oldpassword).digest('hex');
|
|
|
|
const user = await this.model.findOne({ _id: id });
|
|
const user = await this.model.findOne({ _id: id });
|
|
if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
|
|
if (!user) return { errcode: -1001, errmsg: '用户不存在', data: '' };
|
|
|
|
+ oldpassword = sm3(`${password}:${user.salt}`);
|
|
if (user.password !== oldpassword) return { errcode: -1001, errmsg: '原密码不正确', data: '' };
|
|
if (user.password !== oldpassword) return { errcode: -1001, errmsg: '原密码不正确', data: '' };
|
|
- password = crypto.createHash('md5').update(password).digest('hex');
|
|
|
|
|
|
+ password = sm3(`${password}:${user.salt}`);
|
|
try {
|
|
try {
|
|
const res = await this.model.updateOne({ _id: id }, { password });
|
|
const res = await this.model.updateOne({ _id: id }, { password });
|
|
return { errcode: 0, errmsg: 'ok', data: { ...res, password: '' } };
|
|
return { errcode: 0, errmsg: 'ok', data: { ...res, password: '' } };
|