import { GMUserModel } from '@db/GMUser'; import { GMGroupModel } from '@db/GMGroup'; import { Service } from 'egg'; import { STATUS } from '@consts'; import { gameData } from '@pubUtils/data'; /** * Test Service */ export default class GMUsers extends Service { /** * 后台账号登录 */ public async login(username: string, password: string) { console.log('******', username, password) try { const {ctx} = this; let token = ctx.service.utils.genCode(256); let user = await GMUserModel.login(username, password, token); console.log('******', user) if(!user) { return ctx.service.utils.resResult(STATUS.GM_ERR_PASSWORD); } if(!user.isEnable) { return ctx.service.utils.resResult(STATUS.GM_NO_AUTHORITY_GET); } let group = new Array(); if(user.groupId != undefined) { let g = await GMGroupModel.getGroupById(user.groupId); if(g) group.push(g.group); } console.log('******', group, token) return ctx.service.utils.resResult(STATUS.SUCCESS, { ...user, group, token }); }catch(e) { console.log(e) } } public async currentUser() { const {ctx} = this; let user = ctx.user; let group = new Array(); if(user.groupId != undefined) { let g = await GMGroupModel.getGroupById(user.groupId); if(g) group.push(g.group); } return ctx.service.utils.resResult(STATUS.SUCCESS, { user: ctx.user, group }); } /** * 修改我的密码 */ public async changeMyPass(uid: number, password: string) { const {ctx} = this; let user = ctx.user; if(uid != user.uid) return ctx.service.utils.resResult(STATUS.GM_NO_AUTHORITY_POST); let result = await GMUserModel.changePass(uid, password); if(result) { return ctx.service.utils.resResult(STATUS.SUCCESS); } else { return ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } /** * 后台账号列表 */ public async getGmList() { let list = await GMUserModel.getGmAccountList(); if(list) { let result = new Array(); for(let user of list) { let groupId = 0, groupStr = '暂无', envs = []; if(user.groupId != undefined) { let group = await GMGroupModel.getGroupById(user.groupId); if(group) { groupId = user.groupId; envs = user.envs groupStr = group.name; } } result.push({...user, group: groupId, groupStr, env: envs}); } return this.ctx.service.utils.resResult(STATUS.SUCCESS, { gmlist: result }); } else { return this.ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } /** * 创建后台账号 */ public async createGmAccount(name: string, username: string, password: string) { const {ctx} = this; let token = ctx.service.utils.genCode(256); let user = await GMUserModel.getGmAccountByUsername(username); if(user) { return ctx.service.utils.resResult(STATUS.GM_DUPLICATE_GM_ACCOUNT) } else { user = await GMUserModel.createGmAccount(username, password, name, token); return ctx.service.utils.resResult(STATUS.SUCCESS); } } /** * 后台Api列表 */ public async getApiList() { const {ctx} = this; let list = []; for(let [_, obj] of gameData.apiByUrl) { list.push(obj); } if(list) { return ctx.service.utils.resResult(STATUS.SUCCESS, { list }); } else { return ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } /** * 后台用户组列表 */ public async getGroupList() { const {ctx} = this; let list = await GMGroupModel.getAllGroup(); if(list) { return ctx.service.utils.resResult(STATUS.SUCCESS, { list }); } else { return ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } /** * 创建用户组 */ public async createGmGroup(group:string, name:string, desc: string) { const {ctx} = this; let result = await GMGroupModel.getGroup(group); if(result) { return ctx.service.utils.resResult(STATUS.GM_DUPLICATE_GROUP); } else { let result = await GMGroupModel.createGroup(group, name, desc); if(result) { return ctx.service.utils.resResult(STATUS.SUCCESS); } else { return ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } } /** * 修改用户组 */ public async saveGmGroup(groupId: number, group:string, name:string, desc: string) { const {ctx} = this; let result = await GMGroupModel.editGroup(groupId, group, name, desc); if(result) { return ctx.service.utils.resResult(STATUS.SUCCESS); } else { return ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } /** * 用户组添加接口 */ public async saveGmApiToGroup(apiId: number, apis: Array) { const {ctx} = this; let result = await GMGroupModel.saveApiToGroup(apiId, apis); if(result) { return ctx.service.utils.resResult(STATUS.SUCCESS); } else { return ctx.service.utils.resResult(STATUS.INTERNAL_ERR); } } /** * 用户组添加接口 */ public async saveGmGroupToUser(uid: number, groupId: number, envs: string[]) { await GMUserModel.updateInfo(uid, { groupId, envs }); return this.ctx.service.utils.resResult(STATUS.SUCCESS); } }