You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.4 KiB
176 lines
4.4 KiB
const bcrypt = require('bcryptjs');
|
|
const jwt = require('jsonwebtoken');
|
|
const {
|
|
promisify
|
|
} = require('util');
|
|
/**
|
|
* 用户业务操作
|
|
*/
|
|
const config = require('./../config.js');
|
|
const validator = require('validator')
|
|
const userModel = require('./../models/user-info')
|
|
const userCode = require('./../codes/user')
|
|
|
|
const user = {
|
|
|
|
/**
|
|
* 创建用户
|
|
* @param {object} user 用户信息
|
|
* @return {object} 创建结果
|
|
*/
|
|
async create(user) {
|
|
let salt = await promisify(bcrypt.genSalt)(10);
|
|
let pwd = await promisify(bcrypt.hash)(user.password, salt);
|
|
user.password = pwd;
|
|
let result = await userModel.create(user)
|
|
return result
|
|
},
|
|
/**
|
|
* 更新用户
|
|
* @param {object} user 用户信息
|
|
* @return {object} 创建结果
|
|
*/
|
|
async update(user, id) {
|
|
let data = {
|
|
...user,
|
|
modifiedTime: new Date().getTime()
|
|
}
|
|
// /不能修改用户名
|
|
delete data.username;
|
|
// 不能修改邮箱
|
|
delete data.email;
|
|
let resultData = await userModel.update(data, id)
|
|
return resultData
|
|
},
|
|
/**
|
|
* 查找存在用户信息
|
|
* @param {object} formData 查找的表单数据
|
|
* @return {object|null} 查找结果
|
|
*/
|
|
async getExistOne(formData) {
|
|
let resultData = await userModel.getExistOne({
|
|
'email': formData.email,
|
|
'username': formData.username
|
|
})
|
|
return resultData
|
|
},
|
|
/**
|
|
* 根据ID查找存在用户信息
|
|
* @param {object} formData 查找的表单数据
|
|
* @return {object|null} 查找结果
|
|
*/
|
|
async getExistOneByIdOrUserName(formData) {
|
|
let resultData = await userModel.getExistOneByIdOrUserName({
|
|
'id': formData.id
|
|
})
|
|
if (resultData) {
|
|
return resultData[0];
|
|
}
|
|
return resultData
|
|
},
|
|
|
|
|
|
/**
|
|
* 登录Token获取
|
|
* @param {object} formData 登录表单信息
|
|
* @return {object} 登录业务操作结果
|
|
*/
|
|
async validatorPwd(formData, rowData) {
|
|
let token = '';
|
|
let isSame = await promisify(bcrypt.compare)(formData.password, rowData.password);
|
|
if (isSame) {
|
|
// 1小时的登录时间
|
|
token = jwt.sign({
|
|
id: rowData.id,
|
|
username: rowData.username,
|
|
}, config.share_key, {
|
|
expiresIn: '1h'
|
|
});
|
|
token = 'Bearer ' + token;
|
|
}
|
|
return token
|
|
},
|
|
|
|
|
|
/**
|
|
* 根据用户名查找用户业务操作
|
|
* @param {string} userName 用户名
|
|
* @return {object|null} 查找结果
|
|
*/
|
|
async getUserInfoByUserName(userName) {
|
|
|
|
let resultData = await userModel.getUserInfoByUserName(userName) || {}
|
|
// let userInfo = {
|
|
// // id: resultData.id,
|
|
// email: resultData.email,
|
|
// username: resultData.username,
|
|
// detailInfo: resultData.detail_info,
|
|
// createTime: resultData.create_time
|
|
// }
|
|
return resultData
|
|
},
|
|
|
|
|
|
/**
|
|
* 检验用户注册数据
|
|
* @param {object} userInfo 用户注册数据
|
|
* @return {object} 校验结果
|
|
*/
|
|
validatorSignUp(userInfo) {
|
|
let result = {
|
|
success: false,
|
|
message: '',
|
|
data: null
|
|
}
|
|
if (/^[a-z0-9\_\-]{6,16}$/.test(userInfo.username || '') === false) {
|
|
result.message = userCode.ERROR_USER_NAME
|
|
return result
|
|
}
|
|
if (userInfo.email && !validator.isEmail(userInfo.email)) {
|
|
;
|
|
result.message = userCode.ERROR_EMAIL
|
|
return result
|
|
}
|
|
if (!/[\w+]{6,16}/.test(userInfo.password || '')) {
|
|
result.message = userCode.ERROR_PASSWORD
|
|
return result
|
|
}
|
|
if (userInfo.password !== userInfo.confirmPassword) {
|
|
result.message = userCode.ERROR_PASSWORD_CONFORM
|
|
return result
|
|
}
|
|
|
|
result.success = true
|
|
return result
|
|
},
|
|
/**
|
|
* 检验用户必须的数据
|
|
* @param {object} userInfo 用户数据
|
|
* @return {object} 校验结果
|
|
*/
|
|
validatorNeed(userInfo, needPwd = true) {
|
|
let result = {
|
|
success: false,
|
|
message: '',
|
|
}
|
|
if (!userInfo.username && !userInfo.email) {
|
|
result.success = false;
|
|
result.message = '用户名或者邮箱不能为空'
|
|
return result
|
|
}
|
|
if (userInfo.email && !validator.isEmail(userInfo.email)) {
|
|
result.success = false;
|
|
result.message = userCode.ERROR_EMAIL
|
|
return result
|
|
}
|
|
if (needPwd && !userInfo.password) {
|
|
result.success = false;
|
|
result.message = '密码不能为空'
|
|
return result
|
|
}
|
|
result.success = true
|
|
return result
|
|
}
|
|
}
|
|
|
|
module.exports = user
|