Compare commits
	
		
			5 Commits
		
	
	
		
			f1024941ed
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| bae8275b11 | |||
| 0a0ffbdb23 | |||
| 557cd99b20 | |||
| 8b4312782d | |||
| f8af24506b | 
							
								
								
									
										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 | ||||
							
								
								
									
										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" | ||||
|   } | ||||
| } | ||||
| @@ -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,15 @@ 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) { | ||||
| @@ -266,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; | ||||
| @@ -274,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