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.
 
 
 
 
 

156 lines
4.4 KiB

'use strict';
const Service = require('egg').Service;
const _ = require("lodash");
class UserService extends Service {
//用户登录
async login(uesrData) {
let { ctx } = this;
let filterOption = {
"filters": {
"submission.username": uesrData.username,
"submission.password": uesrData.password
},
"sort": {
"create_at": -1
},
"skip": 0,
"limit": 1,
}
let res = await ctx.service.utils.getDataList("User", filterOption);
let returnData =
{
haserror: true,
msg: `账号或密码出错请重新登录`,
username: _.get(uesrData, ["username"], ""),
};
if (res.total) {
//登录成功后新增一条登录信息
await this.addLoginInfo(_.get(uesrData, ["username"], ""), _.get(res, ["results", 0, "role"], "operator"))
returnData =
{
haserror: false,
msg: `${_.get(uesrData, ["username"], "")}用户登录成功`,
username: _.get(uesrData, ["username"], ""),
_id: _.get(res, ["results", 0, "_id"], ""),
role: _.get(res, ["results", 0, "role"], "operator"),
token: this.genToken(_.get(res, ["results", 0, "_id"], "")),
}
}
return returnData
}
//新增用户
async addUser(uesrData) {
let { ctx } = this;
let params = {
submission: {
username: _.get(uesrData, ["username"], ""),
password: _.get(uesrData, ["password"], "")
}
}
let returnData =
{
haserror: true,
msg: `${_.get(uesrData, ["username"], "")}用户重复,请输入不同用户`,
username: _.get(uesrData, ["username"]),
};
try {
let result = await ctx.model.User.create(params)
//注册成功后新增一条登录信息
await this.addLoginInfo(_.get(uesrData, ["username"], ""), result.role)
returnData = {
haserror: false,
msg: `${_.get(result, ["submission", "username"], "")}用户新建成功`,
username: _.get(result, ["submission", "username"]),
_id: result._id,
role: result.role,
token: this.genToken(result._id),
};
} catch (error) {
}
return returnData
}
// //修改用户
// async updateUser(filter) {
// let { ctx } = this;
// let res = await ctx.model.User.findOneAndUpdate({ username: filter.username }, { password: filter.password }, function (err, data) {
// if (err) {
// return "修改失败"
// } else {
// return "修改成功"
// }
// }
// );
// return res
// }
// //删除用户
// async delUser(uesrData) {
// let { ctx } = this;
// let res = await ctx.model.User.findOneAndUpdate({ username: filter.username }, { delete: true }, function (err, data) {
// if (err) {
// return "删除失败"
// } else {
// return "删除成功"
// }
// }
// );
// return res
// }
//获取用户列表
async getUserList(uesrData) {
let { ctx } = this;
let filterOption = {
"project": {
"submission.username": "$submission.username",
"role": "$role",
"create_at": "$create_at"
},
"sort": {
"create_at": -1
},
"skip": 0,
"limit": 10,
...uesrData
}
filterOption = _.merge(filterOption, uesrData)
let res = await ctx.service.utils.getDataList("User", filterOption);
return res
}
//获取用户登录列表
async getUserLoginList(uesrData) {
let { ctx } = this;
let filterOption = {
"project": {
"username": "$username",
"role": "$role",
"create_at": "$create_at"
},
"sort": {
"create_at": -1
},
"skip": 0,
"limit": 10,
...uesrData
}
filterOption = _.merge(filterOption, uesrData)
let res = await ctx.service.utils.getDataList("UserLoginRecord", filterOption);
return res
}
//登陆后或者注册后新增一条登录信息数据
async addLoginInfo(username = "", role = "operator") {
let { ctx, app } = this
let res = await ctx.model.UserLoginRecord.create({ username: username, role: role })
return res
}
//根据用户id生成token
genToken(id) {
// //生成Token
let { app } = this
let token = app.jwt.sign({
userID: id, //需要存储的Token数据
}, app.config.jwt.secret, { expiresIn: 24 * 60 * 60 + 's' });
return token
}
}
module.exports = UserService;