diff --git a/code/common/routes/user.ts b/code/common/routes/user.ts index 624e308..4876ef8 100644 --- a/code/common/routes/user.ts +++ b/code/common/routes/user.ts @@ -22,7 +22,7 @@ const api={ req:<{ name:string }>{}, - res:{}, + res:{}, ignoreValidate:true }, logout:{ @@ -44,7 +44,7 @@ const api={ method:ECommon_HttpApi_Method.GET, path:"/refresh", req:{}, - res: | Omit>{}, + res:>{}, }, update:{ method:ECommon_HttpApi_Method.PUT, diff --git a/code/common/status/error.ts b/code/common/status/error.ts index d7c82b1..1001aa6 100644 --- a/code/common/status/error.ts +++ b/code/common/status/error.ts @@ -1,11 +1,11 @@ export namespace Err { export let Common = { paramError:{ - code:0, + code:1, msg:"param error" }, mysqlError:{ - code:1, + code:2, msg:"mysql error" } } @@ -43,6 +43,10 @@ export namespace Err { overFileSize:{ code:2000, msg:"over file size" + }, + requireParam:{ + code:2001, + msg:"require param" } } export let Project = { diff --git a/code/server/common/cache/keys/base.ts b/code/server/common/cache/keys/base.ts index fd3dee9..f015b54 100644 --- a/code/server/common/cache/keys/base.ts +++ b/code/server/common/cache/keys/base.ts @@ -4,9 +4,11 @@ class RedisBaseKey { protected redis:InstanceType=getRedisInstance() protected type:T protected name:string - constructor(name:string,type:T){ - this.name=name, + protected ttl:number + constructor(name:string,type:T,ttl?:number){ + this.name=name this.type=type + this.ttl=ttl } async getTTL():Promise { let ret=await this.redis.getTTL(this.name) @@ -15,18 +17,21 @@ class RedisBaseKey { async setTTL(seconds:number):Promise { await this.redis.setTTL(this.name,seconds) } + async del() { + await this.redis.del(this.name); + } } export class RedisStringKey extends RedisBaseKey { - constructor(name:string,type:T){ - super(name,type) + constructor(name:string,type:T,ttl?:number){ + super(name,type,ttl) } async get():Promise{ let ret=await this.redis.get(this.name,this.type) return ret } async set(value:T,ttl?:number){ - await this.redis.set(this.name,value,ttl); + await this.redis.set(this.name,value,ttl??this.ttl); } async scan():Promise{ let ret=await this.redis.scan(this.name,this.type); diff --git a/code/server/common/cache/keys/user.ts b/code/server/common/cache/keys/user.ts index c904f76..e6b4e07 100644 --- a/code/server/common/cache/keys/user.ts +++ b/code/server/common/cache/keys/user.ts @@ -1,3 +1,4 @@ +import { ICommon_Model_User } from './../../../../common/model/user'; import { ECommon_Services } from '../../../../common/types'; import { IServer_Common_Nacos_Instance } from './../../types/nacos'; import { RedisStringKey } from './base'; @@ -5,9 +6,16 @@ import {cacheRedisType} from "../../types/cache" import StringUtil from "../../util/string" export namespace REDIS_USER { let USER_TOKEN_KEY=`${ECommon_Services.User}:user:{0}:token` + let USER_INFO_KEY=`${ECommon_Services.User}:user:{0}:info` export function token(userId:string) { - let obj=new RedisStringKey(StringUtil.format(USER_TOKEN_KEY,userId),cacheRedisType().String) + let obj=new RedisStringKey(StringUtil.format(USER_TOKEN_KEY,userId),cacheRedisType().String,3600) + obj.setTTL + return obj + } + export function info(userId:string) + { + let obj=new RedisStringKey(StringUtil.format(USER_INFO_KEY,userId),cacheRedisType().Object,3600) return obj } } \ No newline at end of file diff --git a/code/server/common/entity/entity.ts b/code/server/common/entity/entity.ts index ea1aea3..a438903 100644 --- a/code/server/common/entity/entity.ts +++ b/code/server/common/entity/entity.ts @@ -12,7 +12,9 @@ export abstract class Entity { } assignItem(item:any) { if(typeof(item)=="object") { - this.item={} + if(!this.item) { + this.item={} + } for(let key in item) { this.item[key]=item[key] } diff --git a/code/server/common/http/http.ts b/code/server/common/http/http.ts index e51bb3f..caed412 100644 --- a/code/server/common/http/http.ts +++ b/code/server/common/http/http.ts @@ -2,6 +2,7 @@ import { ICommon_Http_Route } from './../../../common/routes/types'; import { EServer_Common_Http_Structure_HandleParam_Category, IServer_Common_Http_Proxy, IServer_Common_Http_Req_File, IServer_Common_Http_Structure, IServer_Common_Http_Structure_HandleParam } from "../types/http"; import "reflect-metadata" import HttpContext from "./context" +import { Err } from '../../../common/status/error'; var g_objRoute:{ [param:string]:IServer_Common_Http_Structure }={} @@ -153,10 +154,7 @@ export async function handleHttpCall(obj:IServer_Common_Http_Structure,ctx:IServ { if(obj.required) { - throw { - code:1, - msg:"qq" - } + throw Err.Http.requireParam } value=obj.defaultValue arr.push(value) diff --git a/code/server/common/util/http.ts b/code/server/common/util/http.ts index 1b08d68..7758244 100644 --- a/code/server/common/util/http.ts +++ b/code/server/common/util/http.ts @@ -8,10 +8,9 @@ export function generateHttpErrorResponse(obj:{ } } -export function generateHttpOkResponse(msg:string,data) { +export function generateHttpOkResponse(data) { return { code:0, - msg:msg, data:data } } diff --git a/code/server/gateway/app/app.ts b/code/server/gateway/app/app.ts index 5879a44..23ffd57 100644 --- a/code/server/gateway/app/app.ts +++ b/code/server/gateway/app/app.ts @@ -6,7 +6,7 @@ import userRpcApi from '../rpc/user'; import Application from "../../common/app/app" import { getNacosInstance } from "../../common/nacos/nacos"; import { CacheService } from './../cache/service'; -import {generateHttpErrorResponse} from "../../common/util/http" +import {generateHttpErrorResponse, generateHttpOkResponse} from "../../common/util/http" import * as userApi from "../../../common/routes/user" import * as projectApi from "../../../common/routes/project" import { ICommon_HttpApi } from "../../../common/routes/types"; @@ -185,7 +185,12 @@ export default class GateWay extends Application { ctx.set(key, ret.headers[key]); } } - ctx.body = ret.data; + if(typeof(ret.data)=="string" || typeof(ret.data)=="number" || typeof(ret.data)=="boolean" || typeof(ret.data)=="object") + { + ctx.body = generateHttpOkResponse(ret.data); + } else { + ctx.body = ret.data; + } await next() } else { await next() diff --git a/code/server/user/http/user.ts b/code/server/user/http/user.ts index eaf25d3..c7d28d5 100644 --- a/code/server/user/http/user.ts +++ b/code/server/user/http/user.ts @@ -30,5 +30,55 @@ class UserController { throw Err.User.userPasswordWrong } } + + @DHttpApi(userApi.routes.checkUser) + async checkUser(@DHttpReqParamRequired("name") name:string):Promise { + let user=await UserService.getItemByName(name) + if(!user) { + return false + } + return true + } + + @DHttpApi(userApi.routes.logout) + async logout(@DHttpUser userInfo:IUserSession) { + let user=await UserService.getItemById(userInfo.userId) + if(!user) { + throw Err.User.userNotFound + } + await user.stopSession() + } + + @DHttpApi(userApi.routes.refresh) + async refresh(@DHttpUser userInfo:IUserSession):Promise { + let user=await UserService.getItemById(userInfo.userId) + if(!user) { + throw Err.User.userNotFound + } + let item=user.getItem() + delete item.password + return item + } + + @DHttpApi(userApi.routes.update) + async update(@DHttpUser userInfo:IUserSession,@DHttpContent content:typeof userApi.routes.update.req):Promise { + let user=await UserService.getItemById(userInfo.userId) + if(!user) { + throw Err.User.userNotFound + } + user.assignItem(content) + let obj=await user.update(); + delete obj.password + return obj + } + + @DHttpApi(userApi.routes.remove) + async remove(@DHttpUser userInfo:IUserSession) { + let user=await UserService.getItemById(userInfo.userId) + if(!user) { + throw Err.User.userNotFound + } + await user.delete() + } } export default new UserController \ No newline at end of file diff --git a/code/server/user/service/user.ts b/code/server/user/service/user.ts index 4222769..aff9f29 100644 --- a/code/server/user/service/user.ts +++ b/code/server/user/service/user.ts @@ -38,17 +38,23 @@ export default class User extends Entity { } let obj = await userMapper.getUserById(this.item.id); this.item=obj; + let session=REDIS_USER.info(this.item.id); + await session.set(obj) return this.item; } - static async getItemById(id:string):Promise>{ - let obj = await userMapper.getUserById(id); - if(obj) { - let user = new User; - user.setItem(obj); - return user; - } else { - return null; - } + static async getItemById(id:string,reload:boolean=false):Promise>{ + let session=REDIS_USER.info(id); + let obj=await session.get(); + if(!obj || reload) { + obj = await userMapper.getUserById(id); + if(!obj) { + return null + } + await session.set(obj) + } + let user = new User; + user.setItem(obj); + return user; } static async getItemByName(name:string):Promise>{ @@ -77,10 +83,20 @@ export default class User extends Entity { return } else { let session=REDIS_USER.token(this.item.id) - await session.set(token,3600); + await session.set(token); + let sessionInfo=REDIS_USER.info(this.item.id) + await sessionInfo.set(this.item); resolve(token) } }) }) } + + async stopSession() { + if(!this.item || !this.item.id) { + throw Err.User.userNotFound + } + let session=REDIS_USER.token(this.item.id) + await session.del() + } } \ No newline at end of file