mirror of
https://github.com/Teamlinker/Teamlinker.git
synced 2025-06-03 03:00:17 +00:00
add
This commit is contained in:
parent
cc404a6927
commit
fc48e20231
@ -22,7 +22,7 @@ const api={
|
||||
req:<{
|
||||
name:string
|
||||
}>{},
|
||||
res:{},
|
||||
res:<boolean>{},
|
||||
ignoreValidate:true
|
||||
},
|
||||
logout:{
|
||||
@ -44,7 +44,7 @@ const api={
|
||||
method:ECommon_HttpApi_Method.GET,
|
||||
path:"/refresh",
|
||||
req:{},
|
||||
res:<Omit<ICommon_Model_User,"password"> | Omit<ICommon_Model_Admin,"password">>{},
|
||||
res:<Omit<ICommon_Model_User,"password">>{},
|
||||
},
|
||||
update:{
|
||||
method:ECommon_HttpApi_Method.PUT,
|
||||
|
@ -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 = {
|
||||
|
15
code/server/common/cache/keys/base.ts
vendored
15
code/server/common/cache/keys/base.ts
vendored
@ -4,9 +4,11 @@ class RedisBaseKey<T> {
|
||||
protected redis:InstanceType<typeof Redis>=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<number> {
|
||||
let ret=await this.redis.getTTL(this.name)
|
||||
@ -15,18 +17,21 @@ class RedisBaseKey<T> {
|
||||
async setTTL(seconds:number):Promise<void> {
|
||||
await this.redis.setTTL(this.name,seconds)
|
||||
}
|
||||
async del() {
|
||||
await this.redis.del(this.name);
|
||||
}
|
||||
}
|
||||
|
||||
export class RedisStringKey<T> extends RedisBaseKey<T> {
|
||||
constructor(name:string,type:T){
|
||||
super(name,type)
|
||||
constructor(name:string,type:T,ttl?:number){
|
||||
super(name,type,ttl)
|
||||
}
|
||||
async get():Promise<T>{
|
||||
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<T[]>{
|
||||
let ret=await this.redis.scan<T>(this.name,this.type);
|
||||
|
10
code/server/common/cache/keys/user.ts
vendored
10
code/server/common/cache/keys/user.ts
vendored
@ -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>().String)
|
||||
let obj=new RedisStringKey(StringUtil.format(USER_TOKEN_KEY,userId),cacheRedisType<string>().String,3600)
|
||||
obj.setTTL
|
||||
return obj
|
||||
}
|
||||
export function info(userId:string)
|
||||
{
|
||||
let obj=new RedisStringKey(StringUtil.format(USER_INFO_KEY,userId),cacheRedisType<ICommon_Model_User>().Object,3600)
|
||||
return obj
|
||||
}
|
||||
}
|
@ -12,7 +12,9 @@ export abstract class Entity<T> {
|
||||
}
|
||||
assignItem(item:any) {
|
||||
if(typeof(item)=="object") {
|
||||
this.item=<T>{}
|
||||
if(!this.item) {
|
||||
this.item=<T>{}
|
||||
}
|
||||
for(let key in item) {
|
||||
this.item[key]=item[key]
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -30,5 +30,55 @@ class UserController {
|
||||
throw Err.User.userPasswordWrong
|
||||
}
|
||||
}
|
||||
|
||||
@DHttpApi(userApi.routes.checkUser)
|
||||
async checkUser(@DHttpReqParamRequired("name") name:string):Promise<typeof userApi.routes.checkUser.res> {
|
||||
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<typeof userApi.routes.refresh.res> {
|
||||
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<typeof userApi.routes.update.res> {
|
||||
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
|
@ -38,17 +38,23 @@ export default class User extends Entity<ICommon_Model_User> {
|
||||
}
|
||||
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<InstanceType<typeof User>>{
|
||||
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<InstanceType<typeof User>>{
|
||||
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<InstanceType<typeof User>>{
|
||||
@ -77,10 +83,20 @@ export default class User extends Entity<ICommon_Model_User> {
|
||||
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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user