-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from j10ccc/feat/new-request-function
feat(service): new request implementation
- Loading branch information
Showing
15 changed files
with
308 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import useTimeInstance from "./useTimeString"; | ||
import useRequest from "./useRequest"; | ||
import useRequest, { useRequestNext } from "./useRequest"; | ||
import useDarkMode from "./useDarkMode"; | ||
|
||
export { | ||
useTimeInstance, | ||
useRequest, | ||
useRequestNext, | ||
useDarkMode | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
/** | ||
* @deprecated | ||
*/ | ||
export const ServerCode = { | ||
OK: 1, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Taro from "@tarojs/taro"; | ||
import { persistedStateStorage } from "../storage"; | ||
import RequestError, { MPErrorCode, ServiceErrorCode } from "./requestError"; | ||
import { api } from "@/services"; | ||
|
||
export default class CookieUtils { | ||
private static keyInStorage = "__cookie__"; | ||
private static cookie: string = ""; | ||
|
||
/** | ||
* 从内存、持久化储存中获取 Cookie | ||
* | ||
* 若两处都没有 Cookie,则触发登录流程获取新的 Cookie | ||
*/ | ||
public static async get(): Promise<string> { | ||
if (!this.cookie) { | ||
const cookieInStore = persistedStateStorage.getItem(this.keyInStorage); | ||
this.cookie = cookieInStore || await this.refresh(); | ||
} | ||
|
||
return this.cookie; | ||
} | ||
|
||
/** | ||
* 登录以获取服务端 Cookie | ||
* | ||
* @throws {RequestError} | ||
* @returns 新 Cookie | ||
*/ | ||
public static async refresh(): Promise<string> { | ||
try { | ||
const { code, errMsg } = await Taro.login({ timeout: 3000 }); | ||
if (!code) { | ||
console.error(new Error(errMsg)); | ||
return Promise.reject( | ||
new RequestError({ message: errMsg, code: MPErrorCode.MP_LOGIN_ERROR_MISSING_WX_CODE }) | ||
); | ||
} | ||
|
||
const taroWrapped = await Taro.request<{ data: { user: any }, code: number }>({ | ||
url: process.env.HOST + api.user.login.wechat, | ||
data: { code }, | ||
method: "POST" | ||
}); | ||
const { data: realResponse, cookies } = taroWrapped; | ||
if (realResponse && realResponse.code === ServiceErrorCode.OK) { | ||
if (cookies && cookies.length > 0) { | ||
const cookie = cookies[0]; // 现业务全局仅有一个 Cookie,所以取第一个 | ||
persistedStateStorage.setItem(this.keyInStorage, cookie); | ||
return cookie; | ||
} | ||
return Promise.reject( | ||
new RequestError({ message: "小程序登录失败", code: MPErrorCode.MP_LOGIN_ERROR_MISSING_COOKIE }) | ||
); | ||
} | ||
throw new Error(JSON.stringify(taroWrapped)); | ||
} catch (e) { | ||
console.error(e); | ||
throw new RequestError({ message: "小程序登录失败", code: MPErrorCode.MP_LOGIN_ERROR_UNKNOWN }); | ||
} | ||
} | ||
|
||
public static clear() { | ||
persistedStateStorage.removeItem(this.keyInStorage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const MPErrorCode = { | ||
MP_NETWORK_ERROR: Symbol("MP_NETWORK_ERROR"), | ||
MP_LOGIN_ERROR_MISSING_WX_CODE: Symbol("MP_LOGIN_ERROR_MISSING_WX_CODE"), | ||
MP_LOGIN_ERROR_MISSING_COOKIE: Symbol("MP_LOGIN_ERROR_MISSING_COOKIE"), | ||
MP_LOGIN_ERROR_UNKNOWN: Symbol("MP_LOGIN_ERROR_UNKNOWN") | ||
}; | ||
|
||
export enum ServiceErrorCode { | ||
OK = 1, | ||
NOT_ADMIN = 100403, | ||
|
||
USER_ALREADY_EXISTED = 200508, | ||
USER_USERNAME_PASSWORD_UNMATCHED = 200504, | ||
USER_NOT_LOGIN = 200503, | ||
|
||
SYSTEM_ERROR_1 = 200500, | ||
SYSTEM_ERROR_2 = 200506, | ||
|
||
ACTIVATION_SCHOOL_ID_OR_ID_NOT_EXIST_NOT_MATCH = 200510, | ||
ACTIVATION_PASSWORD_LENGTH_ERROR = 200511, | ||
ACTIVATION_PASSPORT_EXISTED = 200512, | ||
ACTIVATION_SCHOOL_ID_ERROR = 200513 | ||
} | ||
|
||
export default class RequestError extends Error { | ||
public message: string; | ||
public code: ServiceErrorCode | number | symbol; | ||
|
||
constructor(props: { | ||
message: string; | ||
code: ServiceErrorCode | number | symbol; | ||
}) { | ||
super(); | ||
this.message = props.message; | ||
this.code = props.code; | ||
} | ||
} |
Oops, something went wrong.