@fairys/taro-tools-react 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/components/Mesage/index.js +4 -4
- package/esm/context/auth.data.instance.d.ts +55 -0
- package/esm/context/auth.data.instance.js +67 -0
- package/esm/context/global.data.instance.d.ts +3 -5364
- package/esm/context/global.data.instance.js +4 -3
- package/esm/context/global.setting.data.instance.d.ts +21 -4
- package/esm/context/global.setting.data.instance.js +5 -1
- package/esm/context/page.data.instance.d.ts +1 -1
- package/esm/context/page.data.instance.js +1 -1
- package/esm/context/page.info.data.instance.d.ts +1 -1
- package/esm/context/page.info.data.instance.js +1 -1
- package/esm/styles/index.css +4 -0
- package/esm/utils/request.d.ts +4 -20
- package/esm/utils/request.js +4 -7
- package/package.json +1 -1
- package/src/components/Mesage/index.tsx +7 -4
- package/src/context/auth.data.instance.ts +133 -0
- package/src/context/global.data.instance.ts +11 -11
- package/src/context/global.setting.data.instance.ts +29 -1
- package/src/context/page.data.instance.ts +2 -2
- package/src/context/page.info.data.instance.ts +2 -2
- package/src/utils/request.ts +13 -27
|
@@ -2,8 +2,8 @@ import { proxy, ref, useSnapshot } from "valtio";
|
|
|
2
2
|
import utils_navigate from "../utils/navigate.js";
|
|
3
3
|
import { createUseId } from "../utils/useId.js";
|
|
4
4
|
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
5
|
+
import { globalSettingDataInstance } from "./global.setting.data.instance.js";
|
|
5
6
|
class GlobalDataInstance extends ProxyInstanceObjectBase {
|
|
6
|
-
loginPageRoute = 'pages/login/index';
|
|
7
7
|
store = proxy({
|
|
8
8
|
messageData: ref([]),
|
|
9
9
|
toastData: void 0
|
|
@@ -46,8 +46,9 @@ class GlobalDataInstance extends ProxyInstanceObjectBase {
|
|
|
46
46
|
});
|
|
47
47
|
};
|
|
48
48
|
toLoginPage = ()=>{
|
|
49
|
-
const
|
|
50
|
-
const
|
|
49
|
+
const loginPageRoute = globalSettingDataInstance.store.loginPageRoute || '';
|
|
50
|
+
const isLoginPage = utils_navigate.isCurrentPage(loginPageRoute || '');
|
|
51
|
+
const _loginPageRoute = `${loginPageRoute || ''}`.replace(/^\//, '');
|
|
51
52
|
if (isLoginPage) return;
|
|
52
53
|
utils_navigate.navigateTo({
|
|
53
54
|
url: `/${_loginPageRoute}`
|
|
@@ -5,6 +5,26 @@ export interface GlobalSettingDataInstanceState {
|
|
|
5
5
|
* @default 200
|
|
6
6
|
*/
|
|
7
7
|
requestSuccessCode?: number;
|
|
8
|
+
/**
|
|
9
|
+
* token过期返回code,跳转登录页面
|
|
10
|
+
* @default 401
|
|
11
|
+
*/
|
|
12
|
+
tokenExpiredCode?: number;
|
|
13
|
+
/**
|
|
14
|
+
* 本地存储token字段名
|
|
15
|
+
* @default token
|
|
16
|
+
*/
|
|
17
|
+
tokenFieldName?: string;
|
|
18
|
+
/**
|
|
19
|
+
* 请求头token字段名
|
|
20
|
+
* @default token
|
|
21
|
+
*/
|
|
22
|
+
headerTokenName?: string;
|
|
23
|
+
/**
|
|
24
|
+
* 设置登录页面路由(需要在入口文件中进行设置)
|
|
25
|
+
* @default pages/login/index
|
|
26
|
+
*/
|
|
27
|
+
loginPageRoute?: string;
|
|
8
28
|
/**数据默认值不使用*/
|
|
9
29
|
__defaultValue?: string;
|
|
10
30
|
}
|
|
@@ -22,7 +42,4 @@ export declare const globalSettingDataInstance: GlobalSettingDataInstance;
|
|
|
22
42
|
/**
|
|
23
43
|
* 全局设置数据状态管理
|
|
24
44
|
*/
|
|
25
|
-
export declare const useGlobalSettingData: () =>
|
|
26
|
-
readonly requestSuccessCode?: number;
|
|
27
|
-
readonly __defaultValue?: string;
|
|
28
|
-
}, GlobalSettingDataInstance, string];
|
|
45
|
+
export declare const useGlobalSettingData: () => [GlobalSettingDataInstanceState, GlobalSettingDataInstance, string | undefined];
|
|
@@ -2,7 +2,11 @@ import { proxy, useSnapshot } from "valtio";
|
|
|
2
2
|
import { ProxyInstanceObjectBase } from "../utils/valtio/instance.js";
|
|
3
3
|
class GlobalSettingDataInstance extends ProxyInstanceObjectBase {
|
|
4
4
|
store = proxy({
|
|
5
|
-
requestSuccessCode: 200
|
|
5
|
+
requestSuccessCode: 200,
|
|
6
|
+
tokenFieldName: 'token',
|
|
7
|
+
headerTokenName: 'token',
|
|
8
|
+
tokenExpiredCode: 401,
|
|
9
|
+
loginPageRoute: 'pages/login/index'
|
|
6
10
|
});
|
|
7
11
|
extendStore = (state)=>{
|
|
8
12
|
this._setValues(state);
|
|
@@ -68,4 +68,4 @@ export interface PageDataOptions<T extends PageDataInstanceState = PageDataInsta
|
|
|
68
68
|
/**
|
|
69
69
|
* 页面级数据状态管理
|
|
70
70
|
*/
|
|
71
|
-
export declare const usePageData: <T extends PageDataInstanceState = PageDataInstanceState>(options?: PageDataOptions<T>) =>
|
|
71
|
+
export declare const usePageData: <T extends PageDataInstanceState = PageDataInstanceState>(options?: PageDataOptions<T>) => [T, PageDataInstance<T>, string | undefined];
|
|
@@ -63,7 +63,7 @@ class PageDataInstance extends ProxyInstanceObjectBase {
|
|
|
63
63
|
let saveData = {};
|
|
64
64
|
if (this.requestConfig?.onAfter) saveData = this.requestConfig.onAfter(result);
|
|
65
65
|
else {
|
|
66
|
-
const dataList = result?.data?.list || result?.data?.records || [];
|
|
66
|
+
const dataList = result?.data?.list || result?.data?.rows || result?.data?.records || [];
|
|
67
67
|
let newDataList = [];
|
|
68
68
|
if (1 === this.store.page) newDataList = dataList;
|
|
69
69
|
else if (this.is_scroll_page) newDataList = [
|
|
@@ -69,4 +69,4 @@ export interface PageInfoDataOptions<T extends PageInfoDataInstanceState = PageI
|
|
|
69
69
|
/**
|
|
70
70
|
* 页面级数据状态管理
|
|
71
71
|
*/
|
|
72
|
-
export declare const usePageInfoData: <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>(options?: PageInfoDataOptions<T>) =>
|
|
72
|
+
export declare const usePageInfoData: <T extends PageInfoDataInstanceState = PageInfoDataInstanceState>(options?: PageInfoDataOptions<T>) => [T, PageInfoDataInstance<T>, string | undefined];
|
|
@@ -69,7 +69,7 @@ class PageInfoDataInstance extends ProxyInstanceObjectBase {
|
|
|
69
69
|
try {
|
|
70
70
|
if (this.requestSaveInfoConfig?.onSaveInfo) return void console.error("\u672A\u914D\u7F6E requestSaveInfoConfig.onSaveInfo \u8BF7\u6C42\u65B9\u6CD5");
|
|
71
71
|
this.updatedLoading(true);
|
|
72
|
-
const newParams = await this.requestSaveInfoConfig?.onSaveBefore?.(this.store);
|
|
72
|
+
const newParams = await this.requestSaveInfoConfig?.onSaveBefore?.(this.store) || this.store.editFormData || {};
|
|
73
73
|
const result = await this.requestSaveInfoConfig.onSaveInfo?.(newParams);
|
|
74
74
|
this.updatedLoading(false);
|
|
75
75
|
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
package/esm/styles/index.css
CHANGED
package/esm/utils/request.d.ts
CHANGED
|
@@ -8,16 +8,6 @@ export interface RequestInstanceOptions extends Taro.request.Option<any, any> {
|
|
|
8
8
|
isShowErrorMessage?: boolean;
|
|
9
9
|
}
|
|
10
10
|
export interface RequestInstanceCreateOptions {
|
|
11
|
-
/**
|
|
12
|
-
* 本地存储token字段名
|
|
13
|
-
* @default token
|
|
14
|
-
*/
|
|
15
|
-
tokenFieldName?: string;
|
|
16
|
-
/**
|
|
17
|
-
* 请求头token字段名
|
|
18
|
-
* @default token
|
|
19
|
-
*/
|
|
20
|
-
headerTokenName?: string;
|
|
21
11
|
/**
|
|
22
12
|
* 公共请求配置
|
|
23
13
|
* @default {}
|
|
@@ -33,6 +23,10 @@ export interface RequestInstanceCreateOptions {
|
|
|
33
23
|
* @default {}
|
|
34
24
|
*/
|
|
35
25
|
proxy?: {
|
|
26
|
+
default?: {
|
|
27
|
+
dev?: string;
|
|
28
|
+
pro?: string;
|
|
29
|
+
};
|
|
36
30
|
dev: Record<string, string | {
|
|
37
31
|
target: string;
|
|
38
32
|
pathRewrite: Record<string, string>;
|
|
@@ -48,16 +42,6 @@ export declare class RequestInstance {
|
|
|
48
42
|
IP?: string | ((url: string, module?: string, env?: string) => string);
|
|
49
43
|
/**简单的代理配置*/
|
|
50
44
|
proxy?: RequestInstanceCreateOptions['proxy'];
|
|
51
|
-
/**
|
|
52
|
-
* 本地存储token字段名
|
|
53
|
-
* @default token
|
|
54
|
-
*/
|
|
55
|
-
tokenFieldName: string;
|
|
56
|
-
/**
|
|
57
|
-
* 请求头token字段名
|
|
58
|
-
* @default token
|
|
59
|
-
*/
|
|
60
|
-
headerTokenName: string;
|
|
61
45
|
/**公共请求配置*/
|
|
62
46
|
commonOptions: Omit<Taro.request.Option<any, any>, 'url'>;
|
|
63
47
|
constructor(options?: RequestInstanceCreateOptions);
|
package/esm/utils/request.js
CHANGED
|
@@ -20,7 +20,7 @@ const requestResponseHandle = (result, options)=>{
|
|
|
20
20
|
const statusCode = result.statusCode;
|
|
21
21
|
const code = result?.data?.code;
|
|
22
22
|
if (result?.data) {
|
|
23
|
-
if (401 === statusCode || 401 === code) {
|
|
23
|
+
if (401 === statusCode || 401 === code || code === globalSettingDataInstance.store.tokenExpiredCode) {
|
|
24
24
|
msg = "\u8BF7\u91CD\u65B0\u767B\u5F55";
|
|
25
25
|
globalDataInstance.toLoginPage();
|
|
26
26
|
} else if (![
|
|
@@ -40,8 +40,6 @@ const requestResponseHandle = (result, options)=>{
|
|
|
40
40
|
class RequestInstance {
|
|
41
41
|
IP;
|
|
42
42
|
proxy;
|
|
43
|
-
tokenFieldName = 'token';
|
|
44
|
-
headerTokenName = 'token';
|
|
45
43
|
commonOptions = {};
|
|
46
44
|
constructor(options = {}){
|
|
47
45
|
this.extends(options);
|
|
@@ -53,8 +51,6 @@ class RequestInstance {
|
|
|
53
51
|
extends = (options = {})=>{
|
|
54
52
|
this.IP = options.IP || this.IP;
|
|
55
53
|
this.proxy = options.proxy || this.proxy;
|
|
56
|
-
this.tokenFieldName = options.tokenFieldName || this.tokenFieldName;
|
|
57
|
-
this.headerTokenName = options.headerTokenName || this.headerTokenName;
|
|
58
54
|
this.commonOptions = {
|
|
59
55
|
...this.commonOptions,
|
|
60
56
|
...options.commonOptions
|
|
@@ -90,6 +86,7 @@ class RequestInstance {
|
|
|
90
86
|
}
|
|
91
87
|
}
|
|
92
88
|
}
|
|
89
|
+
if (!host) host = this.proxy?.default?.['production' === process.env.NODE_ENV ? 'pro' : 'dev'] || '';
|
|
93
90
|
if (!host) host = this.getHttpPath(url, module);
|
|
94
91
|
return {
|
|
95
92
|
host,
|
|
@@ -108,11 +105,11 @@ class RequestInstance {
|
|
|
108
105
|
};
|
|
109
106
|
requestBase = (options)=>{
|
|
110
107
|
const { data, header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
|
|
111
|
-
const token = taro.getStorageSync(
|
|
108
|
+
const token = taro.getStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
112
109
|
const newHeader = {
|
|
113
110
|
...header
|
|
114
111
|
};
|
|
115
|
-
if (token) newHeader[
|
|
112
|
+
if (token) newHeader[globalSettingDataInstance.store.headerTokenName || 'token'] = token;
|
|
116
113
|
else if (true !== isIgnoreToken) {
|
|
117
114
|
if (false !== isShowErrorMessage) globalDataInstance.showMessage({
|
|
118
115
|
content: "\u672A\u767B\u5F55",
|
package/package.json
CHANGED
|
@@ -88,7 +88,7 @@ export const FairysTaroMessageItem = (props: FairysTaroMessageItemProps) => {
|
|
|
88
88
|
|
|
89
89
|
const classIconName = useMemo(
|
|
90
90
|
() =>
|
|
91
|
-
clsx('fairys_taro-ui-message-icon fairystaro__box-border ', iconClassName, {
|
|
91
|
+
clsx('fairys_taro-ui-message-icon fairystaro__box-border fairystaro__text-[0.6rem]', iconClassName, {
|
|
92
92
|
'ant-design--close-circle-outlined': type === 'error' && !isIcon,
|
|
93
93
|
'ant-design--exclamation-circle-outlined': type === 'warning' && !isIcon,
|
|
94
94
|
'ant-design--check-circle-outlined': type === 'success' && !isIcon,
|
|
@@ -107,7 +107,7 @@ export const FairysTaroMessageItem = (props: FairysTaroMessageItemProps) => {
|
|
|
107
107
|
);
|
|
108
108
|
|
|
109
109
|
const titleClassNames = useMemo(
|
|
110
|
-
() => clsx('fairys_taro-ui-message-item-title fairystaro__font-bold', titleClassName),
|
|
110
|
+
() => clsx('fairys_taro-ui-message-item-title fairystaro__font-bold fairystaro__text-[0.6rem]', titleClassName),
|
|
111
111
|
[titleClassName],
|
|
112
112
|
);
|
|
113
113
|
|
|
@@ -157,7 +157,10 @@ export const FairysTaroMessageItem = (props: FairysTaroMessageItemProps) => {
|
|
|
157
157
|
<Fragment />
|
|
158
158
|
)}
|
|
159
159
|
{children ? (
|
|
160
|
-
<View
|
|
160
|
+
<View
|
|
161
|
+
className={clsx('fairys_taro-ui-message-item-body fairystaro__text-[0.6rem]', bodyClassName)}
|
|
162
|
+
style={bodyStyle}
|
|
163
|
+
>
|
|
161
164
|
{children}
|
|
162
165
|
</View>
|
|
163
166
|
) : (
|
|
@@ -192,7 +195,7 @@ export const FairysTaroPortalMessage = (props: FairysTaroMessageProps) => {
|
|
|
192
195
|
const classNames = useMemo(
|
|
193
196
|
() =>
|
|
194
197
|
clsx(
|
|
195
|
-
'fairys_taro-ui-portal-message fairystaro__pointer-events-none fairystaro__position-fixed fairystaro__top-0
|
|
198
|
+
'fairys_taro-ui-portal-message fairystaro__pointer-events-none fairystaro__position-fixed fairystaro__top-0 fairystaro__right-0 fairystaro__bottom-0 fairystaro__left-0',
|
|
196
199
|
messageClassName,
|
|
197
200
|
),
|
|
198
201
|
[messageClassName],
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import Taro from '@tarojs/taro';
|
|
2
|
+
import { proxy, useSnapshot } from 'valtio';
|
|
3
|
+
import { globalSettingDataInstance } from 'context/global.setting.data.instance';
|
|
4
|
+
|
|
5
|
+
/**用户信息,权限判断等*/
|
|
6
|
+
export interface AuthDataInstanceState<T = any> {
|
|
7
|
+
/**用户信息*/
|
|
8
|
+
userInfo?: T;
|
|
9
|
+
/**登录凭证(token)*/
|
|
10
|
+
token?: string;
|
|
11
|
+
/**权限列表*/
|
|
12
|
+
permissions?: string[];
|
|
13
|
+
/**数据默认值不使用*/
|
|
14
|
+
__defaultValue?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class AuthDataInstance<T = any> {
|
|
18
|
+
store = proxy<AuthDataInstanceState<T>>({
|
|
19
|
+
userInfo: undefined,
|
|
20
|
+
token: undefined,
|
|
21
|
+
permissions: undefined,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 设置用户信息
|
|
26
|
+
* @param userInfo 用户信息
|
|
27
|
+
*/
|
|
28
|
+
set userInfo(userInfo: T) {
|
|
29
|
+
this.store.userInfo = userInfo;
|
|
30
|
+
if (userInfo) {
|
|
31
|
+
Taro.setStorageSync('userInfo', JSON.stringify(userInfo));
|
|
32
|
+
} else {
|
|
33
|
+
Taro.removeStorageSync('userInfo');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 获取用户信息
|
|
39
|
+
* @returns 用户信息
|
|
40
|
+
*/
|
|
41
|
+
get userInfo(): T {
|
|
42
|
+
if (!this.store.userInfo) {
|
|
43
|
+
const userInfo = Taro.getStorageSync('userInfo');
|
|
44
|
+
if (userInfo) {
|
|
45
|
+
try {
|
|
46
|
+
this.store.userInfo = JSON.parse(userInfo);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error('解析用户信息失败', error);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return (this.store.userInfo || {}) as T;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 设置登录凭证(token)
|
|
57
|
+
* @param token 登录凭证(token)
|
|
58
|
+
*/
|
|
59
|
+
set token(token: string) {
|
|
60
|
+
this.store.token = token;
|
|
61
|
+
if (token) {
|
|
62
|
+
Taro.setStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token', token);
|
|
63
|
+
} else {
|
|
64
|
+
Taro.removeStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* 获取登录凭证(token)
|
|
69
|
+
* @returns 登录凭证(token)
|
|
70
|
+
*/
|
|
71
|
+
get token(): string {
|
|
72
|
+
if (!this.store.token) {
|
|
73
|
+
const token = Taro.getStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
74
|
+
if (token) {
|
|
75
|
+
this.store.token = token;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return this.store.token || '';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 设置权限列表
|
|
83
|
+
* @param permissions 权限列表
|
|
84
|
+
*/
|
|
85
|
+
set permissions(permissions: string[]) {
|
|
86
|
+
this.store.permissions = permissions;
|
|
87
|
+
if (permissions) {
|
|
88
|
+
Taro.setStorageSync('permissions', JSON.stringify(permissions));
|
|
89
|
+
} else {
|
|
90
|
+
Taro.removeStorageSync('permissions');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 获取权限列表
|
|
95
|
+
* @returns 权限列表
|
|
96
|
+
*/
|
|
97
|
+
get permissions(): string[] {
|
|
98
|
+
if (!this.store.permissions) {
|
|
99
|
+
const permissions = Taro.getStorageSync('permissions');
|
|
100
|
+
if (permissions) {
|
|
101
|
+
try {
|
|
102
|
+
this.store.permissions = JSON.parse(permissions);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.error('解析权限列表失败', error);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return this.store.permissions || [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 判断是否有指定权限
|
|
113
|
+
* @param permission 权限
|
|
114
|
+
* @returns 是否有指定权限
|
|
115
|
+
*/
|
|
116
|
+
hasPermission(permission: string): boolean {
|
|
117
|
+
return this.permissions.includes(permission);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const authDataInstance = new AuthDataInstance();
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 全局数据状态管理
|
|
125
|
+
*/
|
|
126
|
+
export function useAuthData<T = any>() {
|
|
127
|
+
const store = useSnapshot(authDataInstance.store);
|
|
128
|
+
return [store, authDataInstance, store.__defaultValue] as [
|
|
129
|
+
AuthDataInstanceState<T>,
|
|
130
|
+
AuthDataInstance<T>,
|
|
131
|
+
string | undefined,
|
|
132
|
+
];
|
|
133
|
+
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { proxy, ref, useSnapshot } from 'valtio';
|
|
2
|
-
import { TaroToastProps } from '@nutui/nutui-react-taro';
|
|
2
|
+
import type { TaroToastProps } from '@nutui/nutui-react-taro';
|
|
3
3
|
import navigate from 'utils/navigate';
|
|
4
4
|
import { createUseId } from 'utils/useId';
|
|
5
5
|
import React from 'react';
|
|
6
|
-
import { FairysTaroMessageItemProps } from 'components/Mesage';
|
|
6
|
+
import type { FairysTaroMessageItemProps } from 'components/Mesage';
|
|
7
7
|
import { ProxyInstanceObjectBase } from 'utils/valtio/instance';
|
|
8
|
+
import { globalSettingDataInstance } from './global.setting.data.instance';
|
|
8
9
|
|
|
9
10
|
export interface MessageDataType extends FairysTaroMessageItemProps {
|
|
10
11
|
/**用于唯一标识提示框(默认自动生成)*/
|
|
@@ -32,12 +33,6 @@ export interface GlobalDataInstanceState {
|
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
export class GlobalDataInstance extends ProxyInstanceObjectBase<GlobalDataInstanceState> {
|
|
35
|
-
/**
|
|
36
|
-
* 设置登录页面路由(需要在入口文件中进行设置)
|
|
37
|
-
* @param loginPageRoute 登录页面路由(默认pages/login/index)
|
|
38
|
-
*/
|
|
39
|
-
public loginPageRoute = 'pages/login/index';
|
|
40
|
-
|
|
41
36
|
store = proxy<GlobalDataInstanceState>({
|
|
42
37
|
messageData: ref([]),
|
|
43
38
|
toastData: undefined,
|
|
@@ -82,8 +77,9 @@ export class GlobalDataInstance extends ProxyInstanceObjectBase<GlobalDataInstan
|
|
|
82
77
|
|
|
83
78
|
/**跳转登录页面*/
|
|
84
79
|
toLoginPage = () => {
|
|
85
|
-
const
|
|
86
|
-
const
|
|
80
|
+
const loginPageRoute = globalSettingDataInstance.store.loginPageRoute || '';
|
|
81
|
+
const isLoginPage = navigate.isCurrentPage(loginPageRoute || '');
|
|
82
|
+
const _loginPageRoute = `${loginPageRoute || ''}`.replace(/^\//, '');
|
|
87
83
|
if (isLoginPage) {
|
|
88
84
|
// 如果是登录页面不进行跳转
|
|
89
85
|
return;
|
|
@@ -102,5 +98,9 @@ export const globalDataInstance = new GlobalDataInstance();
|
|
|
102
98
|
*/
|
|
103
99
|
export const useGlobalData = () => {
|
|
104
100
|
const store = useSnapshot(globalDataInstance.store);
|
|
105
|
-
return [store, globalDataInstance, store.__defaultValue] as
|
|
101
|
+
return [store, globalDataInstance, store.__defaultValue] as [
|
|
102
|
+
GlobalDataInstanceState,
|
|
103
|
+
GlobalDataInstance,
|
|
104
|
+
string | undefined,
|
|
105
|
+
];
|
|
106
106
|
};
|
|
@@ -7,6 +7,26 @@ export interface GlobalSettingDataInstanceState {
|
|
|
7
7
|
* @default 200
|
|
8
8
|
*/
|
|
9
9
|
requestSuccessCode?: number;
|
|
10
|
+
/**
|
|
11
|
+
* token过期返回code,跳转登录页面
|
|
12
|
+
* @default 401
|
|
13
|
+
*/
|
|
14
|
+
tokenExpiredCode?: number;
|
|
15
|
+
/**
|
|
16
|
+
* 本地存储token字段名
|
|
17
|
+
* @default token
|
|
18
|
+
*/
|
|
19
|
+
tokenFieldName?: string;
|
|
20
|
+
/**
|
|
21
|
+
* 请求头token字段名
|
|
22
|
+
* @default token
|
|
23
|
+
*/
|
|
24
|
+
headerTokenName?: string;
|
|
25
|
+
/**
|
|
26
|
+
* 设置登录页面路由(需要在入口文件中进行设置)
|
|
27
|
+
* @default pages/login/index
|
|
28
|
+
*/
|
|
29
|
+
loginPageRoute?: string;
|
|
10
30
|
/**数据默认值不使用*/
|
|
11
31
|
__defaultValue?: string;
|
|
12
32
|
}
|
|
@@ -14,6 +34,10 @@ export interface GlobalSettingDataInstanceState {
|
|
|
14
34
|
export class GlobalSettingDataInstance extends ProxyInstanceObjectBase<GlobalSettingDataInstanceState> {
|
|
15
35
|
store = proxy<GlobalSettingDataInstanceState>({
|
|
16
36
|
requestSuccessCode: 200,
|
|
37
|
+
tokenFieldName: 'token',
|
|
38
|
+
headerTokenName: 'token',
|
|
39
|
+
tokenExpiredCode: 401,
|
|
40
|
+
loginPageRoute: 'pages/login/index',
|
|
17
41
|
});
|
|
18
42
|
/**
|
|
19
43
|
* 扩展全局设置数据状态
|
|
@@ -32,5 +56,9 @@ export const globalSettingDataInstance = new GlobalSettingDataInstance();
|
|
|
32
56
|
*/
|
|
33
57
|
export const useGlobalSettingData = () => {
|
|
34
58
|
const store = useSnapshot(globalSettingDataInstance.store);
|
|
35
|
-
return [store, globalSettingDataInstance, store.__defaultValue] as
|
|
59
|
+
return [store, globalSettingDataInstance, store.__defaultValue] as [
|
|
60
|
+
GlobalSettingDataInstanceState,
|
|
61
|
+
GlobalSettingDataInstance,
|
|
62
|
+
string | undefined,
|
|
63
|
+
];
|
|
36
64
|
};
|
|
@@ -119,7 +119,7 @@ export class PageDataInstance<
|
|
|
119
119
|
if (this.requestConfig?.onAfter) {
|
|
120
120
|
saveData = this.requestConfig.onAfter(result);
|
|
121
121
|
} else {
|
|
122
|
-
const dataList = result?.data?.list || result?.data?.records || [];
|
|
122
|
+
const dataList = result?.data?.list || result?.data?.rows || result?.data?.records || [];
|
|
123
123
|
/**如果是第一页则直接返回数据,否则进行拼接数据*/
|
|
124
124
|
let newDataList = [];
|
|
125
125
|
if (this.store.page === 1) {
|
|
@@ -211,5 +211,5 @@ export const usePageData = <T extends PageDataInstanceState = PageDataInstanceSt
|
|
|
211
211
|
) => {
|
|
212
212
|
const pageDataInstance = useRef(new PageDataInstance<T>(options)).current;
|
|
213
213
|
const store = useSnapshot(pageDataInstance.store) as T;
|
|
214
|
-
return [store, pageDataInstance, store.__defaultValue] as
|
|
214
|
+
return [store, pageDataInstance, store.__defaultValue] as [T, PageDataInstance<T>, string | undefined];
|
|
215
215
|
};
|
|
@@ -129,7 +129,7 @@ export class PageInfoDataInstance<
|
|
|
129
129
|
return;
|
|
130
130
|
}
|
|
131
131
|
this.updatedLoading(true);
|
|
132
|
-
const newParams = await this.requestSaveInfoConfig?.onSaveBefore?.(this.store);
|
|
132
|
+
const newParams = (await this.requestSaveInfoConfig?.onSaveBefore?.(this.store)) || this.store.editFormData || {};
|
|
133
133
|
const result = await this.requestSaveInfoConfig.onSaveInfo?.(newParams);
|
|
134
134
|
this.updatedLoading(false);
|
|
135
135
|
if (result && result.code === globalSettingDataInstance.store.requestSuccessCode) {
|
|
@@ -178,5 +178,5 @@ export const usePageInfoData = <T extends PageInfoDataInstanceState = PageInfoDa
|
|
|
178
178
|
) => {
|
|
179
179
|
const pageDataInstance = useRef(new PageInfoDataInstance<T>(options)).current;
|
|
180
180
|
const store = useSnapshot(pageDataInstance.store) as T;
|
|
181
|
-
return [store, pageDataInstance, store.__defaultValue] as
|
|
181
|
+
return [store, pageDataInstance, store.__defaultValue] as [T, PageInfoDataInstance<T>, string | undefined];
|
|
182
182
|
};
|
package/src/utils/request.ts
CHANGED
|
@@ -3,8 +3,6 @@ import { globalSettingDataInstance } from 'context/global.setting.data.instance'
|
|
|
3
3
|
import { globalDataInstance } from 'context/global.data.instance';
|
|
4
4
|
|
|
5
5
|
const codeMessage = {
|
|
6
|
-
// 200: '服务器成功返回请求的数据',
|
|
7
|
-
// 201: '新建或修改数据成功',
|
|
8
6
|
400: '发出的请求错误',
|
|
9
7
|
401: '用户没有权限',
|
|
10
8
|
403: '用户访问被禁止',
|
|
@@ -34,7 +32,7 @@ const requestResponseHandle = (result: Taro.request.SuccessCallbackResult<any>,
|
|
|
34
32
|
const statusCode = result.statusCode;
|
|
35
33
|
const code = result?.data?.code;
|
|
36
34
|
if (result?.data) {
|
|
37
|
-
if (statusCode === 401 || code === 401) {
|
|
35
|
+
if (statusCode === 401 || code === 401 || code === globalSettingDataInstance.store.tokenExpiredCode) {
|
|
38
36
|
// 权限问题 ,重新登录
|
|
39
37
|
msg = '请重新登录';
|
|
40
38
|
/**重新跳转登录页面*/
|
|
@@ -59,16 +57,6 @@ const requestResponseHandle = (result: Taro.request.SuccessCallbackResult<any>,
|
|
|
59
57
|
};
|
|
60
58
|
|
|
61
59
|
export interface RequestInstanceCreateOptions {
|
|
62
|
-
/**
|
|
63
|
-
* 本地存储token字段名
|
|
64
|
-
* @default token
|
|
65
|
-
*/
|
|
66
|
-
tokenFieldName?: string;
|
|
67
|
-
/**
|
|
68
|
-
* 请求头token字段名
|
|
69
|
-
* @default token
|
|
70
|
-
*/
|
|
71
|
-
headerTokenName?: string;
|
|
72
60
|
/**
|
|
73
61
|
* 公共请求配置
|
|
74
62
|
* @default {}
|
|
@@ -85,6 +73,10 @@ export interface RequestInstanceCreateOptions {
|
|
|
85
73
|
* @default {}
|
|
86
74
|
*/
|
|
87
75
|
proxy?: {
|
|
76
|
+
default?: {
|
|
77
|
+
dev?: string;
|
|
78
|
+
pro?: string;
|
|
79
|
+
};
|
|
88
80
|
dev: Record<string, string | { target: string; pathRewrite: Record<string, string> }>;
|
|
89
81
|
pro: Record<string, string | { target: string; pathRewrite: Record<string, string> }>;
|
|
90
82
|
};
|
|
@@ -95,16 +87,7 @@ export class RequestInstance {
|
|
|
95
87
|
public IP?: string | ((url: string, module?: string, env?: string) => string);
|
|
96
88
|
/**简单的代理配置*/
|
|
97
89
|
public proxy?: RequestInstanceCreateOptions['proxy'];
|
|
98
|
-
|
|
99
|
-
* 本地存储token字段名
|
|
100
|
-
* @default token
|
|
101
|
-
*/
|
|
102
|
-
public tokenFieldName = 'token';
|
|
103
|
-
/**
|
|
104
|
-
* 请求头token字段名
|
|
105
|
-
* @default token
|
|
106
|
-
*/
|
|
107
|
-
public headerTokenName = 'token';
|
|
90
|
+
|
|
108
91
|
/**公共请求配置*/
|
|
109
92
|
public commonOptions: Omit<Taro.request.Option<any, any>, 'url'> = {};
|
|
110
93
|
|
|
@@ -122,8 +105,6 @@ export class RequestInstance {
|
|
|
122
105
|
extends = (options: RequestInstanceCreateOptions = {}) => {
|
|
123
106
|
this.IP = options.IP || this.IP;
|
|
124
107
|
this.proxy = options.proxy || this.proxy;
|
|
125
|
-
this.tokenFieldName = options.tokenFieldName || this.tokenFieldName;
|
|
126
|
-
this.headerTokenName = options.headerTokenName || this.headerTokenName;
|
|
127
108
|
this.commonOptions = { ...this.commonOptions, ...options.commonOptions };
|
|
128
109
|
return this;
|
|
129
110
|
};
|
|
@@ -169,9 +150,14 @@ export class RequestInstance {
|
|
|
169
150
|
}
|
|
170
151
|
}
|
|
171
152
|
}
|
|
153
|
+
if (!host) {
|
|
154
|
+
host = this.proxy?.default?.[process.env.NODE_ENV === 'production' ? 'pro' : 'dev'] || '';
|
|
155
|
+
}
|
|
156
|
+
|
|
172
157
|
if (!host) {
|
|
173
158
|
host = this.getHttpPath(url, module);
|
|
174
159
|
}
|
|
160
|
+
|
|
175
161
|
return {
|
|
176
162
|
host,
|
|
177
163
|
url: _url,
|
|
@@ -195,10 +181,10 @@ export class RequestInstance {
|
|
|
195
181
|
/**发送请求,返回 Taro.RequestTask */
|
|
196
182
|
requestBase = (options: RequestInstanceOptions) => {
|
|
197
183
|
const { data, header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
|
|
198
|
-
const token = Taro.getStorageSync(
|
|
184
|
+
const token = Taro.getStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
199
185
|
const newHeader = { ...header };
|
|
200
186
|
if (token) {
|
|
201
|
-
newHeader[
|
|
187
|
+
newHeader[globalSettingDataInstance.store.headerTokenName || 'token'] = token;
|
|
202
188
|
} else {
|
|
203
189
|
if (isIgnoreToken !== true) {
|
|
204
190
|
// 跳转登录页
|