Compare commits
	
		
			10 Commits
		
	
	
		
			fdf6d3ac0a
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| bae8275b11 | |||
| 0a0ffbdb23 | |||
| 557cd99b20 | |||
| 8b4312782d | |||
| f8af24506b | |||
| f1024941ed | |||
| 98c8a2ad86 | |||
| 8ac11bbd28 | |||
| 05f0373834 | |||
| e0bf83f062 | 
							
								
								
									
										65
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										65
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,5 +1,66 @@ | ||||
| dist | ||||
| node_modules | ||||
|  | ||||
| # mac | ||||
| .DS_Store | ||||
| .env | ||||
|  | ||||
| .env* | ||||
| !.env*example | ||||
|  | ||||
| dist | ||||
| build | ||||
| logs | ||||
|  | ||||
| .turbo | ||||
|  | ||||
| pack-dist | ||||
|  | ||||
| # astro | ||||
| .astro | ||||
|  | ||||
| # next | ||||
| .next | ||||
|  | ||||
| # nuxt | ||||
| .nuxt | ||||
|  | ||||
| # vercel | ||||
| .vercel | ||||
|  | ||||
| # vuepress | ||||
| .vuepress/dist | ||||
|  | ||||
| # coverage | ||||
| coverage/ | ||||
|  | ||||
| # typescript | ||||
| *.tsbuildinfo | ||||
|  | ||||
| # debug logs | ||||
| *.log | ||||
| *.tmp | ||||
|  | ||||
| # vscode | ||||
| .vscode/* | ||||
| !.vscode/settings.json | ||||
| !.vscode/tasks.json | ||||
| !.vscode/launch.json | ||||
| !.vscode/extensions.json | ||||
|  | ||||
| # idea | ||||
| .idea | ||||
|  | ||||
| # system | ||||
| Thumbs.db | ||||
| ehthumbs.db | ||||
| Desktop.ini | ||||
|  | ||||
| # temp files | ||||
| *.tmp | ||||
| *.temp | ||||
|  | ||||
| # local development | ||||
| *.local | ||||
|  | ||||
| public/r | ||||
|  | ||||
| .pnpm-store | ||||
							
								
								
									
										24
									
								
								.turbo/turbo-build.log
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								.turbo/turbo-build.log
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | ||||
|  | ||||
| [33m[43m[30m WARN [39m[49m Issue while reading "/home/ubuntu/kevisual/center/submodules/query-login/.npmrc". Failed to replace env in config: ${ME_NPM_TOKEN} | ||||
| [43m[30m WARN [39m[49m Issue while reading "/home/ubuntu/kevisual/center/.npmrc". Failed to replace env in config: ${ME_NPM_TOKEN}[39m | ||||
|  | ||||
| > @kevisual/query-login@0.0.2 build /home/ubuntu/kevisual/center/submodules/query-login | ||||
| > tsup | ||||
|  | ||||
| [34mCLI[39m Building entry: src/query-login-browser.ts, src/query-login-node.ts, src/query-login.ts | ||||
| [34mCLI[39m Using tsconfig: tsconfig.json | ||||
| [34mCLI[39m tsup v8.4.0 | ||||
| [34mCLI[39m Using tsup config: /home/ubuntu/kevisual/center/submodules/query-login/tsup.config.ts | ||||
| [34mCLI[39m Target: esnext | ||||
| [34mCLI[39m Cleaning output folder | ||||
| [34mESM[39m Build start | ||||
| [33mESM[39m [33mYou have emitDecoratorMetadata enabled but @swc/core was not installed, skipping swc plugin[39m | ||||
| [32mESM[39m [1mdist/query-login-node.js    [22m[32m15.31 KB[39m | ||||
| [32mESM[39m [1mdist/query-login-browser.js [22m[32m11.80 KB[39m | ||||
| [32mESM[39m [1mdist/query-login.js         [22m[32m11.58 KB[39m | ||||
| [32mESM[39m ⚡️ Build success in 10ms | ||||
| DTS Build start | ||||
| DTS ⚡️ Build success in 873ms | ||||
| DTS dist/query-login-browser.d.ts 332.00 B | ||||
| DTS dist/query-login-node.d.ts    701.00 B | ||||
| DTS dist/query-login.d.ts         4.80 KB | ||||
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								package.json
									
									
									
									
									
								
							| @@ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "@kevisual/query-login", | ||||
|   "version": "0.0.2", | ||||
|   "version": "0.0.6", | ||||
|   "description": "", | ||||
|   "main": "dist/query-login.js", | ||||
|   "types": "dist/query-login.d.ts", | ||||
| @@ -18,10 +18,10 @@ | ||||
|     "access": "public" | ||||
|   }, | ||||
|   "peerDependencies": { | ||||
|     "@kevisual/query": "^0.0.15" | ||||
|     "@kevisual/query": "^0.0.17" | ||||
|   }, | ||||
|   "devDependencies": { | ||||
|     "@types/node": "^22.13.11", | ||||
|     "@types/node": "^22.14.1", | ||||
|     "tsup": "^8.4.0" | ||||
|   }, | ||||
|   "exports": { | ||||
| @@ -31,7 +31,7 @@ | ||||
|     "./browser": "./dist/query-login-browser.js" | ||||
|   }, | ||||
|   "dependencies": { | ||||
|     "@kevisual/cache": "^0.0.1", | ||||
|     "dotenv": "^16.4.7" | ||||
|     "@kevisual/cache": "^0.0.2", | ||||
|     "dotenv": "^16.5.0" | ||||
|   } | ||||
| } | ||||
| @@ -16,9 +16,18 @@ export interface Cache { | ||||
|    */ | ||||
|   init?: () => Promise<any>; | ||||
| } | ||||
| type User = { | ||||
|   avatar?: string; | ||||
|   description?: string; | ||||
|   id?: string; | ||||
|   needChangePassword?: boolean; | ||||
|   orgs?: string[]; | ||||
|   type?: string; | ||||
|   username?: string; | ||||
| }; | ||||
|  | ||||
| export type CacheLoginUser = { | ||||
|   user?: any; | ||||
|   user?: User; | ||||
|   id?: string; | ||||
|   accessToken?: string; | ||||
|   refreshToken?: string; | ||||
| @@ -29,9 +38,13 @@ type CacheLogin = { | ||||
|  | ||||
| export type CacheStore<T = Cache> = { | ||||
|   name: string; | ||||
|   /** | ||||
|    * 缓存数据 | ||||
|    * @important 需要先调用init | ||||
|    */ | ||||
|   cacheData: CacheLogin; | ||||
|   /** | ||||
|    * 实际操作的cache | ||||
|    * 实际操作的cache, 需要先调用init | ||||
|    */ | ||||
|   cache: T; | ||||
|  | ||||
| @@ -42,7 +55,7 @@ export type CacheStore<T = Cache> = { | ||||
|   /** | ||||
|    * 获取当前用户 | ||||
|    */ | ||||
|   getCurrentUser(): Promise<CacheLoginUser>; | ||||
|   getCurrentUser(): Promise<User>; | ||||
|   /** | ||||
|    * 获取当前用户列表 | ||||
|    */ | ||||
|   | ||||
| @@ -1,15 +1,21 @@ | ||||
| import { Cache, LoginCacheStore, LoginCacheStoreOpts } from './login-cache.ts'; | ||||
| import { Cache } from './login-cache.ts'; | ||||
| import { homedir } from 'node:os'; | ||||
| import { join, dirname } from 'node:path'; | ||||
| import fs from 'node:fs'; | ||||
| import { readFileSync, writeFileSync, accessSync } from 'node:fs'; | ||||
| import { readFile, writeFile, unlink, mkdir } from 'node:fs/promises'; | ||||
| export const fileExists = async (filePath: string, createIfNotExists = false) => { | ||||
| export const fileExists = async ( | ||||
|   filePath: string, | ||||
|   { createIfNotExists = true, isFile = true, isDir = false }: { createIfNotExists?: boolean; isFile?: boolean; isDir?: boolean } = {}, | ||||
| ) => { | ||||
|   try { | ||||
|     accessSync(filePath, fs.constants.F_OK); | ||||
|     return true; | ||||
|   } catch (error) { | ||||
|     if (createIfNotExists) { | ||||
|     if (createIfNotExists && isDir) { | ||||
|       await mkdir(filePath, { recursive: true }); | ||||
|       return true; | ||||
|     } else if (createIfNotExists && isFile) { | ||||
|       await mkdir(dirname(filePath), { recursive: true }); | ||||
|       return false; | ||||
|     } | ||||
| @@ -25,6 +31,9 @@ export const readConfigFile = (filePath: string) => { | ||||
|     return {}; | ||||
|   } | ||||
| }; | ||||
| export const writeConfigFile = (filePath: string, data: any) => { | ||||
|   writeFileSync(filePath, JSON.stringify(data, null, 2)); | ||||
| }; | ||||
| export const getHostName = () => { | ||||
|   const configDir = join(homedir(), '.config', 'envision'); | ||||
|   const configFile = join(configDir, 'config.json'); | ||||
| @@ -41,14 +50,13 @@ export class StorageNode implements Storage { | ||||
|     const configDir = join(homedir(), '.config', 'envision'); | ||||
|     const hostname = getHostName(); | ||||
|     this.filePath = join(configDir, 'config', `${hostname}-storage.json`); | ||||
|     fileExists(this.filePath, true); | ||||
|     fileExists(this.filePath, { isFile: true }); | ||||
|   } | ||||
|   async loadCache() { | ||||
|     const filePath = this.filePath; | ||||
|     try { | ||||
|       const data = await readFile(filePath, 'utf-8'); | ||||
|       const jsonData = JSON.parse(data); | ||||
|       this.cacheData = jsonData; | ||||
|       const data = await readConfigFile(filePath); | ||||
|       this.cacheData = data; | ||||
|     } catch (error) { | ||||
|       this.cacheData = {}; | ||||
|       await writeFile(filePath, JSON.stringify(this.cacheData, null, 2)); | ||||
| @@ -81,24 +89,23 @@ export class LoginNodeCache implements Cache { | ||||
|  | ||||
|   constructor(filepath?: string) { | ||||
|     this.filepath = filepath || join(homedir(), '.config', 'envision', 'config', `${getHostName()}-login.json`); | ||||
|     fileExists(this.filepath, true); | ||||
|     fileExists(this.filepath, { isFile: true }); | ||||
|   } | ||||
|   async get(_key: string) { | ||||
|     const data = readFileSync(this.filepath, 'utf-8'); | ||||
|     try { | ||||
|       const jsonData = JSON.parse(data); | ||||
|       return jsonData; | ||||
|       const filePath = this.filepath; | ||||
|       const data = readConfigFile(filePath); | ||||
|       return data; | ||||
|     } catch (error) { | ||||
|       console.log('get error', error); | ||||
|       return null; | ||||
|       return {}; | ||||
|     } | ||||
|   } | ||||
|   async set(_key: string, value: any) { | ||||
|     const data = readFileSync(this.filepath, 'utf-8'); | ||||
|     try { | ||||
|       const jsonData = JSON.parse(data); | ||||
|       const newData = { ...jsonData, ...value }; | ||||
|       writeFileSync(this.filepath, JSON.stringify(newData, null, 2)); | ||||
|       const data = readConfigFile(this.filepath); | ||||
|       const newData = { ...data, ...value }; | ||||
|       writeConfigFile(this.filepath, newData); | ||||
|     } catch (error) { | ||||
|       console.log('set error', error); | ||||
|     } | ||||
| @@ -112,9 +119,10 @@ export class LoginNodeCache implements Cache { | ||||
|       const jsonData = JSON.parse(data); | ||||
|       return jsonData; | ||||
|     } catch (error) { | ||||
|       console.log('loadCache error', error); | ||||
|       // console.log('loadCache error', error); | ||||
|       console.log('create new cache file:', filePath); | ||||
|       const defaultData = { loginUsers: [] }; | ||||
|       await writeFile(filePath, JSON.stringify(defaultData, null, 2)); | ||||
|       writeConfigFile(filePath, defaultData); | ||||
|       return defaultData; | ||||
|     } | ||||
|   } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| import { Query } from '@kevisual/query'; | ||||
| import { Query, BaseQuery } from '@kevisual/query'; | ||||
| import type { Result, DataOpts } from '@kevisual/query/query'; | ||||
| import { setBaseResponse } from '@kevisual/query/query'; | ||||
| import { LoginCacheStore, CacheStore } from './login-cache.ts'; | ||||
| @@ -21,8 +21,7 @@ export type QueryLoginResult = { | ||||
|   refreshToken: string; | ||||
| }; | ||||
|  | ||||
| export class QueryLogin { | ||||
|   query: Query; | ||||
| export class QueryLogin extends BaseQuery { | ||||
|   /** | ||||
|    * query login cache, 非实际操作, 一个cache的包裹模块 | ||||
|    */ | ||||
| @@ -33,7 +32,9 @@ export class QueryLogin { | ||||
|   onLoad?: () => void; | ||||
|  | ||||
|   constructor(opts?: QueryLoginOpts) { | ||||
|     this.query = opts?.query || new Query(); | ||||
|     super({ | ||||
|       query: opts?.query || new Query(), | ||||
|     }); | ||||
|     this.cacheStore = new LoginCacheStore({ name: 'login', cache: opts.cache }); | ||||
|     this.isBrowser = opts?.isBrowser ?? true; | ||||
|     this.init(); | ||||
| @@ -86,6 +87,41 @@ export class QueryLogin { | ||||
|     } | ||||
|     return res; | ||||
|   } | ||||
|   /** | ||||
|    * 设置token | ||||
|    * @param token | ||||
|    */ | ||||
|   async setLoginToken(token: { accessToken: string; refreshToken: string }) { | ||||
|     const { accessToken, refreshToken } = token; | ||||
|     this.storage.setItem('token', accessToken || ''); | ||||
|     await this.beforeSetLoginUser({ accessToken, refreshToken }); | ||||
|   } | ||||
|   async loginByWechat(data: { code: string }) { | ||||
|     const res = await this.post<QueryLoginResult>({ path: 'wx', key: 'open-login', code: data.code }); | ||||
|     if (res.code === 200) { | ||||
|       const { accessToken, refreshToken } = res?.data || {}; | ||||
|       this.storage.setItem('token', accessToken || ''); | ||||
|       await this.beforeSetLoginUser({ accessToken, refreshToken }); | ||||
|     } | ||||
|     return res; | ||||
|   } | ||||
|   /** | ||||
|    * 检测微信登录,登陆成功后,调用onSuccess,否则调用onError | ||||
|    * @param param0 | ||||
|    */ | ||||
|   async checkWechat({ onSuccess, onError }: { onSuccess?: (res: QueryLoginResult) => void; onError?: (res: any) => void }) { | ||||
|     const url = new URL(window.location.href); | ||||
|     const code = url.searchParams.get('code'); | ||||
|     const state = url.searchParams.get('state'); | ||||
|     if (code && state) { | ||||
|       const res = await this.loginByWechat({ code }); | ||||
|       if (res.code === 200) { | ||||
|         onSuccess?.(res.data); | ||||
|       } else { | ||||
|         onError?.(res); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|   /** | ||||
|    * 登陆成功,需要获取用户信息进行缓存 | ||||
|    * @param param0 | ||||
| @@ -177,7 +213,7 @@ export class QueryLogin { | ||||
|   /** | ||||
|    * 一个简单的401处理, 如果401,则刷新token, 如果refreshToken不存在,则返回401 | ||||
|    * refetch 是否重新请求, 会有bug,无限循环,按需要使用 | ||||
|    * TODO:  | ||||
|    * TODO: | ||||
|    * @param response | ||||
|    * @param ctx | ||||
|    * @param opts | ||||
| @@ -240,7 +276,7 @@ export class QueryLogin { | ||||
|           return config; | ||||
|         }, | ||||
|         afterResponse: async (response, ctx) => { | ||||
|           if (response?.code === 401 && check401) { | ||||
|           if (response?.code === 401 && check401 && !token) { | ||||
|             return await that.afterCheck401ToRefreshToken(response, ctx); | ||||
|           } | ||||
|           return response as any; | ||||
| @@ -248,6 +284,25 @@ export class QueryLogin { | ||||
|       }, | ||||
|     ); | ||||
|   } | ||||
|   /** | ||||
|    * 检查本地用户,如果本地用户存在,则返回本地用户,否则返回null | ||||
|    * @returns | ||||
|    */ | ||||
|   async checkLocalUser() { | ||||
|     const user = await this.cacheStore.getCurrentUser(); | ||||
|     if (user) { | ||||
|       return user; | ||||
|     } | ||||
|     return null; | ||||
|   } | ||||
|   /** | ||||
|    * 检查本地token是否存在,简单的判断是否已经属于登陆状态 | ||||
|    * @returns | ||||
|    */ | ||||
|   async checkLocalToken() { | ||||
|     const token = this.storage.getItem('token'); | ||||
|     return !!token; | ||||
|   } | ||||
|   /** | ||||
|    * 请求更新,切换用户, 使用switchUser | ||||
|    * @param username | ||||
|   | ||||
		Reference in New Issue
	
	Block a user