@fairys/taro-tools-react 0.0.2 → 0.0.4

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.
@@ -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 const;
181
+ return [store, pageDataInstance, store.__defaultValue] as [T, PageInfoDataInstance<T>, string | undefined];
182
182
  };
@@ -1,17 +1,28 @@
1
1
  import Taro from '@tarojs/taro';
2
+ import { globalSettingDataInstance } from 'context/global.setting.data.instance';
3
+ import { authDataInstance } from 'context/auth.data.instance';
2
4
 
3
5
  class NavigateInstance {
4
- /**判断是否已登录(方法需要在项目入口文件中进行挂载)*/
6
+ /**判断是否已登录(方法需要在项目入口文件中进行挂载,如果不挂载,默认使用 authDataInstance.hasMenuPermission 判断是否有菜单权限)*/
5
7
  public isAuth: (url: string) => Promise<boolean> | boolean;
6
-
7
8
  private _isAuth = async (url?: string) => {
8
9
  let isAuthTo = true;
9
- if (url && typeof this.isAuth === 'function') {
10
- isAuthTo = await this.isAuth(url);
10
+ // 判断是否跳转忽略权限校验的路由
11
+ const isIgnoreAuthRoutes = globalSettingDataInstance.isIgnoreAuthRoutes(url);
12
+ // 判断是否使用 authDataInstance中的hasMenuPermission 判断是否有菜单权限
13
+ const useAuthHasMenuPermission = globalSettingDataInstance.store.useAuthHasMenuPermission;
14
+ // 判断是否开启权限校验
15
+ const isEnableAuth = globalSettingDataInstance.store.isEnableAuth;
16
+ let isAuthFunction = this.isAuth;
17
+ if (useAuthHasMenuPermission && typeof isAuthFunction !== 'function' && isEnableAuth) {
18
+ isAuthFunction = authDataInstance.hasMenuPermission;
19
+ }
20
+ if (url && typeof isAuthFunction === 'function' && !isIgnoreAuthRoutes && isEnableAuth) {
21
+ isAuthTo = await isAuthFunction(url);
11
22
  }
12
23
  if (isAuthTo === false) {
13
24
  // 无权访问页面
14
- Taro.showToast({ title: '无权访问', icon: 'none' });
25
+ Taro.showToast({ title: `${url} 无权访问`, icon: 'none' });
15
26
  return false;
16
27
  }
17
28
  return true;
@@ -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,22 +57,11 @@ 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 {}
75
63
  */
76
64
  commonOptions?: Omit<Taro.request.Option<any, any>, 'url'>;
77
-
78
65
  /**
79
66
  * 请求IP地址
80
67
  * @default ''
@@ -85,6 +72,10 @@ export interface RequestInstanceCreateOptions {
85
72
  * @default {}
86
73
  */
87
74
  proxy?: {
75
+ default?: {
76
+ dev?: string;
77
+ pro?: string;
78
+ };
88
79
  dev: Record<string, string | { target: string; pathRewrite: Record<string, string> }>;
89
80
  pro: Record<string, string | { target: string; pathRewrite: Record<string, string> }>;
90
81
  };
@@ -95,16 +86,7 @@ export class RequestInstance {
95
86
  public IP?: string | ((url: string, module?: string, env?: string) => string);
96
87
  /**简单的代理配置*/
97
88
  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';
89
+
108
90
  /**公共请求配置*/
109
91
  public commonOptions: Omit<Taro.request.Option<any, any>, 'url'> = {};
110
92
 
@@ -122,8 +104,6 @@ export class RequestInstance {
122
104
  extends = (options: RequestInstanceCreateOptions = {}) => {
123
105
  this.IP = options.IP || this.IP;
124
106
  this.proxy = options.proxy || this.proxy;
125
- this.tokenFieldName = options.tokenFieldName || this.tokenFieldName;
126
- this.headerTokenName = options.headerTokenName || this.headerTokenName;
127
107
  this.commonOptions = { ...this.commonOptions, ...options.commonOptions };
128
108
  return this;
129
109
  };
@@ -169,6 +149,9 @@ export class RequestInstance {
169
149
  }
170
150
  }
171
151
  }
152
+ if (!host) {
153
+ host = this.proxy?.default?.[process.env.NODE_ENV === 'production' ? 'pro' : 'dev'] || '';
154
+ }
172
155
  if (!host) {
173
156
  host = this.getHttpPath(url, module);
174
157
  }
@@ -195,10 +178,10 @@ export class RequestInstance {
195
178
  /**发送请求,返回 Taro.RequestTask */
196
179
  requestBase = (options: RequestInstanceOptions) => {
197
180
  const { data, header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
198
- const token = Taro.getStorageSync(this.tokenFieldName || 'token');
181
+ const token = Taro.getStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
199
182
  const newHeader = { ...header };
200
183
  if (token) {
201
- newHeader[this.headerTokenName || 'token'] = token;
184
+ newHeader[globalSettingDataInstance.store.headerTokenName || 'token'] = token;
202
185
  } else {
203
186
  if (isIgnoreToken !== true) {
204
187
  // 跳转登录页