85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../main');
|
||
|
||
class userService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = 'login,loginByCode,register';
|
||
const _noNeedRight = '*';
|
||
const _noNeedLog = 'info';
|
||
const _noNeedAuth = '*';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
async login(data) {
|
||
const { service } = this;
|
||
const { username, password } = data;
|
||
const result = await service.systems.auth.user.login(username, password);
|
||
console.log('result', result);
|
||
if (result.code) {
|
||
return this.success('登录成功', result);
|
||
}
|
||
return this.fail('登录失败:' + result.message, result);
|
||
|
||
}
|
||
async loginByCode(data) {
|
||
const { service } = this;
|
||
const code = typeof data?.code === 'string' ? data.code.trim() : '';
|
||
if (!code) {
|
||
return this.fail('登录失败:缺少登录码 code', {
|
||
success: false,
|
||
data: null,
|
||
code: 0,
|
||
message: '缺少登录码 code',
|
||
});
|
||
}
|
||
//code先进行base64解码,看是否为json以及json是否有uuid字段,如果有则继续执行否则返回报错
|
||
let decoded;
|
||
try {
|
||
const buffer = Buffer.from(code, 'base64');
|
||
const decodedStr = buffer.toString('utf-8');
|
||
decoded = JSON.parse(decodedStr);
|
||
} catch (err) {
|
||
return this.fail('登录失败:无效的登录码格式', {
|
||
success: false,
|
||
data: null,
|
||
code: 0,
|
||
message: '无效的登录码格式',
|
||
});
|
||
}
|
||
if (!decoded || typeof decoded !== 'object' || !decoded.uuid) {
|
||
return this.fail('登录失败:登录码缺少必要的uuid字段', {
|
||
success: false,
|
||
data: null,
|
||
code: 0,
|
||
message: '登录码缺少必要的uuid字段',
|
||
});
|
||
}
|
||
const result = await service.systems.auth.user.loginByCode(code);
|
||
if (result.code) {
|
||
return this.success('登录成功', result);
|
||
}
|
||
return this.fail('登录失败:' + result.message, result);
|
||
}
|
||
async logout() {
|
||
return this.success('登出成功');
|
||
}
|
||
async info(data) {
|
||
const { service } = this;
|
||
const result = await service.systems.auth.user.info(data);
|
||
return this.success('获取成功' + JSON.stringify(data), result);
|
||
}
|
||
async changeRole(data) {
|
||
const { service } = this;
|
||
const result = await service.systems.auth.user.changeRole(data);
|
||
return this.success('获取成功', result, 200);
|
||
}
|
||
//注册
|
||
async register(data) {
|
||
const { service } = this;
|
||
const result = await service.systems.auth.user.register(data);
|
||
return this.success(result);
|
||
}
|
||
}
|
||
module.exports = userService;
|