@fairys/taro-tools-react 0.0.1
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/README.md +1 -0
- package/esm/components/EnterLoading/index.d.ts +14 -0
- package/esm/components/EnterLoading/index.js +65 -0
- package/esm/components/MainPage/index.d.ts +10 -0
- package/esm/components/MainPage/index.js +49 -0
- package/esm/components/Mesage/index.d.ts +48 -0
- package/esm/components/Mesage/index.js +122 -0
- package/esm/components/Portal/index.d.ts +8 -0
- package/esm/components/Portal/index.js +11 -0
- package/esm/components/Toast/index.d.ts +1 -0
- package/esm/components/Toast/index.js +20 -0
- package/esm/components/connectToastMessage/index.d.ts +10 -0
- package/esm/components/connectToastMessage/index.js +28 -0
- package/esm/components/index.d.ts +6 -0
- package/esm/components/index.js +6 -0
- package/esm/context/global.data.instance.d.ts +5411 -0
- package/esm/context/global.data.instance.js +66 -0
- package/esm/context/global.setting.data.instance.d.ts +28 -0
- package/esm/context/global.setting.data.instance.js +20 -0
- package/esm/context/index.d.ts +4 -0
- package/esm/context/index.js +4 -0
- package/esm/context/page.data.instance.d.ts +71 -0
- package/esm/context/page.data.instance.js +139 -0
- package/esm/context/page.info.data.instance.d.ts +72 -0
- package/esm/context/page.info.data.instance.js +107 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +3 -0
- package/esm/styles/index.css +711 -0
- package/esm/utils/index.d.ts +4 -0
- package/esm/utils/index.js +4 -0
- package/esm/utils/navigate.d.ts +109 -0
- package/esm/utils/navigate.js +45 -0
- package/esm/utils/request.d.ts +112 -0
- package/esm/utils/request.js +212 -0
- package/esm/utils/useId.d.ts +2 -0
- package/esm/utils/useId.js +13 -0
- package/esm/utils/valtio/index.d.ts +9 -0
- package/esm/utils/valtio/index.js +23 -0
- package/esm/utils/valtio/instance.d.ts +15 -0
- package/esm/utils/valtio/instance.js +34 -0
- package/lib/index.js +78 -0
- package/package.json +40 -0
- package/src/components/EnterLoading/index.tsx +57 -0
- package/src/components/MainPage/index.tsx +72 -0
- package/src/components/Mesage/index.tsx +217 -0
- package/src/components/Portal/index.tsx +18 -0
- package/src/components/Toast/index.tsx +22 -0
- package/src/components/connectToastMessage/index.tsx +41 -0
- package/src/components/index.ts +6 -0
- package/src/context/global.data.instance.ts +106 -0
- package/src/context/global.setting.data.instance.ts +36 -0
- package/src/context/index.ts +4 -0
- package/src/context/page.data.instance.ts +215 -0
- package/src/context/page.info.data.instance.ts +182 -0
- package/src/index.ts +3 -0
- package/src/styles/index.css +515 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/navigate.ts +159 -0
- package/src/utils/request.ts +319 -0
- package/src/utils/useId.ts +14 -0
- package/src/utils/valtio/index.ts +23 -0
- package/src/utils/valtio/instance.ts +59 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import Taro from '@tarojs/taro';
|
|
2
|
+
declare class NavigateInstance {
|
|
3
|
+
/**判断是否已登录(方法需要在项目入口文件中进行挂载)*/
|
|
4
|
+
isAuth: (url: string) => Promise<boolean> | boolean;
|
|
5
|
+
private _isAuth;
|
|
6
|
+
/**
|
|
7
|
+
* 判断是否当前页面
|
|
8
|
+
*/
|
|
9
|
+
isCurrentPage: (route: string) => boolean;
|
|
10
|
+
/** 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
|
|
11
|
+
* @supported weapp, h5, rn, tt, harmony, harmony_hybrid
|
|
12
|
+
* @example
|
|
13
|
+
* ```json
|
|
14
|
+
* {
|
|
15
|
+
* "tabBar": {
|
|
16
|
+
* "list": [{
|
|
17
|
+
* "pagePath": "index",
|
|
18
|
+
* "text": "首页"
|
|
19
|
+
* },{
|
|
20
|
+
* "pagePath": "other",
|
|
21
|
+
* "text": "其他"
|
|
22
|
+
* }]
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* Taro.switchTab({
|
|
29
|
+
* url: '/index'
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.switchTab.html
|
|
33
|
+
*/
|
|
34
|
+
switchTab: (options: Taro.switchTab.Option) => Promise<TaroGeneral.CallbackResult>;
|
|
35
|
+
/** 关闭所有页面,打开到应用内的某个页面
|
|
36
|
+
* @supported weapp, h5, rn, tt, harmony, harmony_hybrid
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* Taro.reLaunch({
|
|
40
|
+
* url: 'test?id=1'
|
|
41
|
+
* })
|
|
42
|
+
* ```
|
|
43
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.reLaunch.html
|
|
44
|
+
*/
|
|
45
|
+
reLaunch: (options: Taro.reLaunch.Option) => Promise<TaroGeneral.CallbackResult>;
|
|
46
|
+
/** 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
|
|
47
|
+
* @supported weapp, h5, rn, tt, harmony, harmony_hybrid
|
|
48
|
+
* @h5 未针对 tabbar 页面做限制处理
|
|
49
|
+
* @example
|
|
50
|
+
* ```tsx
|
|
51
|
+
* Taro.redirectTo({
|
|
52
|
+
* url: 'test?id=1'
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.redirectTo.html
|
|
56
|
+
*/
|
|
57
|
+
redirectTo: (options: Taro.redirectTo.Option) => Promise<TaroGeneral.CallbackResult>;
|
|
58
|
+
/** 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 Taro.navigateBack 可以返回到原页面。小程序中页面栈最多十层。
|
|
59
|
+
* @supported weapp, h5, rn, tt, harmony, harmony_hybrid
|
|
60
|
+
* @h5 未针对 tabbar 页面做限制处理
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* Taro.navigateTo({
|
|
64
|
+
* url: 'test?id=1',
|
|
65
|
+
* events: {
|
|
66
|
+
* // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
|
|
67
|
+
* acceptDataFromOpenedPage: function(data) {
|
|
68
|
+
* console.log(data)
|
|
69
|
+
* },
|
|
70
|
+
* someEvent: function(data) {
|
|
71
|
+
* console.log(data)
|
|
72
|
+
* }
|
|
73
|
+
* ...
|
|
74
|
+
* },
|
|
75
|
+
* success: function (res) {
|
|
76
|
+
* // 通过eventChannel向被打开页面传送数据
|
|
77
|
+
* res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
|
|
78
|
+
* }
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html
|
|
82
|
+
*/
|
|
83
|
+
navigateTo: (options: Taro.navigateTo.Option) => Promise<TaroGeneral.CallbackResult>;
|
|
84
|
+
/** 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages 获取当前的页面栈,决定需要返回几层。
|
|
85
|
+
* @supported weapp, h5, rn, tt, harmony, harmony_hybrid
|
|
86
|
+
* @h5 若入参 delta 大于现有页面数时,返回应用打开的第一个页面(如果想要返回首页请使用 reLaunch 方法)。
|
|
87
|
+
* @example
|
|
88
|
+
* ```tsx
|
|
89
|
+
* // 注意:调用 navigateTo 跳转时,调用该方法的页面会被加入堆栈,而 redirectTo 方法则不会。见下方示例代码
|
|
90
|
+
* // 此处是A页面
|
|
91
|
+
* Taro.navigateTo({
|
|
92
|
+
* url: 'B?id=1'
|
|
93
|
+
* })
|
|
94
|
+
* // 此处是B页面
|
|
95
|
+
* Taro.navigateTo({
|
|
96
|
+
* url: 'C?id=1'
|
|
97
|
+
* })
|
|
98
|
+
* // 在C页面内 navigateBack,将返回A页面
|
|
99
|
+
* Taro.navigateBack({
|
|
100
|
+
* delta: 2
|
|
101
|
+
* })
|
|
102
|
+
* ```
|
|
103
|
+
* @see https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html
|
|
104
|
+
*/
|
|
105
|
+
navigateBack: (options?: Taro.navigateBack.Option) => Promise<TaroGeneral.CallbackResult>;
|
|
106
|
+
}
|
|
107
|
+
/**路由跳转*/
|
|
108
|
+
export declare const navigate: NavigateInstance;
|
|
109
|
+
export default navigate;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import taro from "@tarojs/taro";
|
|
2
|
+
class NavigateInstance {
|
|
3
|
+
isAuth;
|
|
4
|
+
_isAuth = async (url)=>{
|
|
5
|
+
let isAuthTo = true;
|
|
6
|
+
if (url && 'function' == typeof this.isAuth) isAuthTo = await this.isAuth(url);
|
|
7
|
+
if (false === isAuthTo) {
|
|
8
|
+
taro.showToast({
|
|
9
|
+
title: "\u65E0\u6743\u8BBF\u95EE",
|
|
10
|
+
icon: 'none'
|
|
11
|
+
});
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
16
|
+
isCurrentPage = (route)=>{
|
|
17
|
+
const _route = route.replace(/^\//, '');
|
|
18
|
+
const currentPath = (taro.getCurrentInstance().router?.path || '').replace(/^\//, '');
|
|
19
|
+
return currentPath === _route;
|
|
20
|
+
};
|
|
21
|
+
switchTab = async (options)=>{
|
|
22
|
+
const isAuthTo = await this._isAuth(options.url);
|
|
23
|
+
if (false === isAuthTo) return;
|
|
24
|
+
return taro.switchTab(options);
|
|
25
|
+
};
|
|
26
|
+
reLaunch = async (options)=>{
|
|
27
|
+
const isAuthTo = await this._isAuth(options.url);
|
|
28
|
+
if (false === isAuthTo) return;
|
|
29
|
+
return taro.reLaunch(options);
|
|
30
|
+
};
|
|
31
|
+
redirectTo = async (options)=>{
|
|
32
|
+
const isAuthTo = await this._isAuth(options.url);
|
|
33
|
+
if (false === isAuthTo) return;
|
|
34
|
+
return taro.redirectTo(options);
|
|
35
|
+
};
|
|
36
|
+
navigateTo = async (options)=>{
|
|
37
|
+
const isAuthTo = await this._isAuth(options.url);
|
|
38
|
+
if (false === isAuthTo) return;
|
|
39
|
+
return taro.navigateTo(options);
|
|
40
|
+
};
|
|
41
|
+
navigateBack = (options)=>taro.navigateBack(options);
|
|
42
|
+
}
|
|
43
|
+
const navigate_navigate = new NavigateInstance();
|
|
44
|
+
const utils_navigate = navigate_navigate;
|
|
45
|
+
export { utils_navigate as default, navigate_navigate as navigate };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import Taro from '@tarojs/taro';
|
|
2
|
+
export interface RequestInstanceOptions extends Taro.request.Option<any, any> {
|
|
3
|
+
/**模块名称*/
|
|
4
|
+
module?: string;
|
|
5
|
+
/**是否忽略token*/
|
|
6
|
+
isIgnoreToken?: boolean;
|
|
7
|
+
/**是否提示错误信息*/
|
|
8
|
+
isShowErrorMessage?: boolean;
|
|
9
|
+
}
|
|
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
|
+
/**
|
|
22
|
+
* 公共请求配置
|
|
23
|
+
* @default {}
|
|
24
|
+
*/
|
|
25
|
+
commonOptions?: Omit<Taro.request.Option<any, any>, 'url'>;
|
|
26
|
+
/**
|
|
27
|
+
* 请求IP地址
|
|
28
|
+
* @default ''
|
|
29
|
+
*/
|
|
30
|
+
IP?: string | ((url: string, module?: string, env?: string) => string);
|
|
31
|
+
/**
|
|
32
|
+
* 简单的代理配置
|
|
33
|
+
* @default {}
|
|
34
|
+
*/
|
|
35
|
+
proxy?: {
|
|
36
|
+
dev: Record<string, string | {
|
|
37
|
+
target: string;
|
|
38
|
+
pathRewrite: Record<string, string>;
|
|
39
|
+
}>;
|
|
40
|
+
pro: Record<string, string | {
|
|
41
|
+
target: string;
|
|
42
|
+
pathRewrite: Record<string, string>;
|
|
43
|
+
}>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export declare class RequestInstance {
|
|
47
|
+
/**请求IP地址*/
|
|
48
|
+
IP?: string | ((url: string, module?: string, env?: string) => string);
|
|
49
|
+
/**简单的代理配置*/
|
|
50
|
+
proxy?: RequestInstanceCreateOptions['proxy'];
|
|
51
|
+
/**
|
|
52
|
+
* 本地存储token字段名
|
|
53
|
+
* @default token
|
|
54
|
+
*/
|
|
55
|
+
tokenFieldName: string;
|
|
56
|
+
/**
|
|
57
|
+
* 请求头token字段名
|
|
58
|
+
* @default token
|
|
59
|
+
*/
|
|
60
|
+
headerTokenName: string;
|
|
61
|
+
/**公共请求配置*/
|
|
62
|
+
commonOptions: Omit<Taro.request.Option<any, any>, 'url'>;
|
|
63
|
+
constructor(options?: RequestInstanceCreateOptions);
|
|
64
|
+
/**创建实例*/
|
|
65
|
+
static create(options?: RequestInstanceCreateOptions): RequestInstance;
|
|
66
|
+
/**扩展请求配置*/
|
|
67
|
+
extends: (options?: RequestInstanceCreateOptions) => this;
|
|
68
|
+
/**获取请求地址*/
|
|
69
|
+
getHttpPath: (url: string, module?: string) => string;
|
|
70
|
+
/**获取转换后地址*/
|
|
71
|
+
getProxyHost: (url: string, module?: string) => {
|
|
72
|
+
host: string;
|
|
73
|
+
url: string;
|
|
74
|
+
};
|
|
75
|
+
/**格式化地址*/
|
|
76
|
+
formatUrl: (url: string, module?: string) => string;
|
|
77
|
+
/**发送请求,返回 Taro.RequestTask */
|
|
78
|
+
requestBase: (options: RequestInstanceOptions) => Taro.RequestTask<any>;
|
|
79
|
+
/**发送请求,返回 Promise */
|
|
80
|
+
request: (options: RequestInstanceOptions) => Promise<{
|
|
81
|
+
code?: number;
|
|
82
|
+
data?: any;
|
|
83
|
+
message?: string;
|
|
84
|
+
}>;
|
|
85
|
+
/**GET请求*/
|
|
86
|
+
GET: (options: RequestInstanceOptions) => Promise<{
|
|
87
|
+
code?: number;
|
|
88
|
+
data?: any;
|
|
89
|
+
message?: string;
|
|
90
|
+
}>;
|
|
91
|
+
/**POST请求*/
|
|
92
|
+
POST: (options: RequestInstanceOptions) => Promise<{
|
|
93
|
+
code?: number;
|
|
94
|
+
data?: any;
|
|
95
|
+
message?: string;
|
|
96
|
+
}>;
|
|
97
|
+
/**发送formData格式数据*/
|
|
98
|
+
formData: (options: RequestInstanceOptions) => Promise<{
|
|
99
|
+
code?: number;
|
|
100
|
+
data?: any;
|
|
101
|
+
message?: string;
|
|
102
|
+
}>;
|
|
103
|
+
/**发送x-www-form-urlencoded格式数据*/
|
|
104
|
+
xFormUrlEncoded: (options: RequestInstanceOptions) => Promise<{
|
|
105
|
+
code?: number;
|
|
106
|
+
data?: any;
|
|
107
|
+
message?: string;
|
|
108
|
+
}>;
|
|
109
|
+
}
|
|
110
|
+
/** 请求*/
|
|
111
|
+
export declare const request: RequestInstance;
|
|
112
|
+
export default request;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import taro from "@tarojs/taro";
|
|
2
|
+
import { globalSettingDataInstance } from "../context/global.setting.data.instance.js";
|
|
3
|
+
import { globalDataInstance } from "../context/global.data.instance.js";
|
|
4
|
+
const codeMessage = {
|
|
5
|
+
400: "\u53D1\u51FA\u7684\u8BF7\u6C42\u9519\u8BEF",
|
|
6
|
+
401: "\u7528\u6237\u6CA1\u6709\u6743\u9650",
|
|
7
|
+
403: "\u7528\u6237\u8BBF\u95EE\u88AB\u7981\u6B62",
|
|
8
|
+
404: "\u8BF7\u6C42\u4E0D\u5B58\u5728\uFF0C\u670D\u52A1\u5668\u6CA1\u6709\u8FDB\u884C\u64CD\u4F5C",
|
|
9
|
+
406: "\u8BF7\u6C42\u7684\u683C\u5F0F\u9519\u8BEF",
|
|
10
|
+
410: "\u8D44\u6E90\u88AB\u6C38\u4E45\u5220\u9664",
|
|
11
|
+
422: "\u9A8C\u8BC1\u9519\u8BEF",
|
|
12
|
+
500: "\u670D\u52A1\u5668\u53D1\u751F\u9519\u8BEF\uFF0C\u8BF7\u68C0\u67E5\u670D\u52A1\u5668",
|
|
13
|
+
502: "nginx\u5F02\u5E38",
|
|
14
|
+
503: "\u670D\u52A1\u4E0D\u53EF\u7528\uFF0C\u670D\u52A1\u5668\u6682\u65F6\u8FC7\u8F7D\u6216\u7EF4\u62A4",
|
|
15
|
+
504: "nginx\u8D85\u65F6"
|
|
16
|
+
};
|
|
17
|
+
const requestResponseHandle = (result, options)=>{
|
|
18
|
+
let msg = '';
|
|
19
|
+
try {
|
|
20
|
+
const statusCode = result.statusCode;
|
|
21
|
+
const code = result?.data?.code;
|
|
22
|
+
if (result?.data) {
|
|
23
|
+
if (401 === statusCode || 401 === code) {
|
|
24
|
+
msg = "\u8BF7\u91CD\u65B0\u767B\u5F55";
|
|
25
|
+
globalDataInstance.toLoginPage();
|
|
26
|
+
} else if (![
|
|
27
|
+
globalSettingDataInstance.store.requestSuccessCode,
|
|
28
|
+
200
|
|
29
|
+
].includes(code)) msg = result?.data?.message || "\u63A5\u53E3\u5F02\u5E38";
|
|
30
|
+
} else msg = codeMessage[result?.statusCode];
|
|
31
|
+
} catch (error) {
|
|
32
|
+
msg = codeMessage[result?.statusCode];
|
|
33
|
+
console.log(error);
|
|
34
|
+
}
|
|
35
|
+
if (msg && options?.isShowErrorMessage !== false) globalDataInstance.showMessage({
|
|
36
|
+
content: msg || "\u8BF7\u6C42\u53D1\u751F\u9519\u8BEF",
|
|
37
|
+
type: 'error'
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
class RequestInstance {
|
|
41
|
+
IP;
|
|
42
|
+
proxy;
|
|
43
|
+
tokenFieldName = 'token';
|
|
44
|
+
headerTokenName = 'token';
|
|
45
|
+
commonOptions = {};
|
|
46
|
+
constructor(options = {}){
|
|
47
|
+
this.extends(options);
|
|
48
|
+
}
|
|
49
|
+
static create(options = {}) {
|
|
50
|
+
const request = new RequestInstance(options);
|
|
51
|
+
return request;
|
|
52
|
+
}
|
|
53
|
+
extends = (options = {})=>{
|
|
54
|
+
this.IP = options.IP || this.IP;
|
|
55
|
+
this.proxy = options.proxy || this.proxy;
|
|
56
|
+
this.tokenFieldName = options.tokenFieldName || this.tokenFieldName;
|
|
57
|
+
this.headerTokenName = options.headerTokenName || this.headerTokenName;
|
|
58
|
+
this.commonOptions = {
|
|
59
|
+
...this.commonOptions,
|
|
60
|
+
...options.commonOptions
|
|
61
|
+
};
|
|
62
|
+
return this;
|
|
63
|
+
};
|
|
64
|
+
getHttpPath = (url, module)=>{
|
|
65
|
+
if ('function' == typeof this.IP) return this.IP(url, module, process.env.NODE_ENV) || '';
|
|
66
|
+
return this.IP || '';
|
|
67
|
+
};
|
|
68
|
+
getProxyHost = (url, module)=>{
|
|
69
|
+
let host = '';
|
|
70
|
+
let _url = url;
|
|
71
|
+
if ('h5' === process.env.TARO_ENV) return {
|
|
72
|
+
host: '',
|
|
73
|
+
url: _url
|
|
74
|
+
};
|
|
75
|
+
if (this.proxy) {
|
|
76
|
+
const proxy = this.proxy['production' === process.env.NODE_ENV ? 'pro' : 'dev'];
|
|
77
|
+
if (proxy) for(const key in proxy){
|
|
78
|
+
const rgx = new RegExp(key);
|
|
79
|
+
const item = proxy[key];
|
|
80
|
+
if (rgx.test(url) && item) {
|
|
81
|
+
if ('string' == typeof item) host = item;
|
|
82
|
+
else if (item?.target) {
|
|
83
|
+
host = item.target;
|
|
84
|
+
if (item.pathRewrite) for(const key in item.pathRewrite){
|
|
85
|
+
const rgx = new RegExp(key);
|
|
86
|
+
_url = url.replace(rgx, item.pathRewrite[key]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (!host) host = this.getHttpPath(url, module);
|
|
94
|
+
return {
|
|
95
|
+
host,
|
|
96
|
+
url: _url
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
formatUrl = (url, module)=>{
|
|
100
|
+
let { host, url: _url } = this.getProxyHost(url, module);
|
|
101
|
+
if (host) host = host.replace(/\/$/, '');
|
|
102
|
+
const newUrl = `${_url}`.replace(/^\//, '').replace(/\/$/, '');
|
|
103
|
+
if (module && 'production' === process.env.NODE_ENV) {
|
|
104
|
+
const m = `${module}`.replace(/^\//, '').replace(/\/$/, '');
|
|
105
|
+
return `${host}/${m}/${newUrl}`;
|
|
106
|
+
}
|
|
107
|
+
return `${host}/${newUrl}`;
|
|
108
|
+
};
|
|
109
|
+
requestBase = (options)=>{
|
|
110
|
+
const { data, header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
|
|
111
|
+
const token = taro.getStorageSync(this.tokenFieldName || 'token');
|
|
112
|
+
const newHeader = {
|
|
113
|
+
...header
|
|
114
|
+
};
|
|
115
|
+
if (token) newHeader[this.headerTokenName || 'token'] = token;
|
|
116
|
+
else if (true !== isIgnoreToken) {
|
|
117
|
+
if (false !== isShowErrorMessage) globalDataInstance.showMessage({
|
|
118
|
+
content: "\u672A\u767B\u5F55",
|
|
119
|
+
type: 'error'
|
|
120
|
+
});
|
|
121
|
+
options?.fail?.({
|
|
122
|
+
errMsg: "\u672A\u767B\u5F55"
|
|
123
|
+
});
|
|
124
|
+
globalDataInstance.toLoginPage();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
return taro.request({
|
|
128
|
+
...this.commonOptions,
|
|
129
|
+
...restOptions,
|
|
130
|
+
header: {
|
|
131
|
+
...newHeader,
|
|
132
|
+
...options?.header || {}
|
|
133
|
+
},
|
|
134
|
+
url: this.formatUrl(options.url, module),
|
|
135
|
+
success: (result)=>{
|
|
136
|
+
requestResponseHandle(result, options);
|
|
137
|
+
options?.success?.(result);
|
|
138
|
+
},
|
|
139
|
+
fail: (result)=>{
|
|
140
|
+
if (false !== isShowErrorMessage) globalDataInstance.showMessage({
|
|
141
|
+
content: result.errMsg || "\u8BF7\u6C42\u53D1\u751F\u9519\u8BEF",
|
|
142
|
+
type: 'error'
|
|
143
|
+
});
|
|
144
|
+
options?.fail?.(result);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
request = (options)=>new Promise((resolve, reject)=>{
|
|
149
|
+
this.requestBase({
|
|
150
|
+
...options,
|
|
151
|
+
success: (result)=>{
|
|
152
|
+
options?.success?.(result);
|
|
153
|
+
resolve(result?.data);
|
|
154
|
+
},
|
|
155
|
+
fail: (result)=>{
|
|
156
|
+
options?.fail?.(result);
|
|
157
|
+
reject(result);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
GET = (options)=>{
|
|
162
|
+
try {
|
|
163
|
+
return this.request({
|
|
164
|
+
...options,
|
|
165
|
+
method: 'GET'
|
|
166
|
+
});
|
|
167
|
+
} catch (error) {
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
POST = (options)=>{
|
|
172
|
+
try {
|
|
173
|
+
return this.request({
|
|
174
|
+
...options,
|
|
175
|
+
method: 'POST'
|
|
176
|
+
});
|
|
177
|
+
} catch (error) {
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
formData = (options)=>{
|
|
182
|
+
try {
|
|
183
|
+
return this.request({
|
|
184
|
+
...options,
|
|
185
|
+
method: 'POST',
|
|
186
|
+
header: {
|
|
187
|
+
'Content-Type': 'multipart/form-data',
|
|
188
|
+
...options.header || {}
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
xFormUrlEncoded = (options)=>{
|
|
196
|
+
try {
|
|
197
|
+
return this.request({
|
|
198
|
+
...options,
|
|
199
|
+
method: 'POST',
|
|
200
|
+
header: {
|
|
201
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
202
|
+
...options.header || {}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
const request_request = new RequestInstance();
|
|
211
|
+
const utils_request = request_request;
|
|
212
|
+
export { RequestInstance, utils_request as default, request_request as request };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
|
+
let localId = 0;
|
|
3
|
+
const createUseId = (suffix = 'D')=>{
|
|
4
|
+
const count = localId++;
|
|
5
|
+
return `FAIRYS_TARO_UI_${count.toString(32)}_${suffix}`;
|
|
6
|
+
};
|
|
7
|
+
const useId = (suffix = 'D')=>{
|
|
8
|
+
const count = useRef(localId++);
|
|
9
|
+
return useMemo(()=>`FAIRYS_TARO_UI_${count.current.toString(32)}_${suffix}`, [
|
|
10
|
+
count.current
|
|
11
|
+
]);
|
|
12
|
+
};
|
|
13
|
+
export { createUseId, useId };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ProxyInstanceObjectBase } from './instance';
|
|
2
|
+
export * from './instance';
|
|
3
|
+
/**创建简单的状态管理*/
|
|
4
|
+
export declare const useValtioState: <T extends object>(inital?: T) => readonly [import("valtio").Snapshot<T>, ProxyInstanceObjectBase<T>, any];
|
|
5
|
+
export declare const useValtioInstaceState: <T extends object = any, K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>, M extends {
|
|
6
|
+
new (...args: any[]): K;
|
|
7
|
+
} = {
|
|
8
|
+
new (...args: any[]): K;
|
|
9
|
+
}>(Instance: M) => readonly [import("valtio").Snapshot<T>, K, any];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useRef } from "react";
|
|
2
|
+
import { useSnapshot } from "valtio";
|
|
3
|
+
import { ProxyInstanceObjectBase } from "./instance.js";
|
|
4
|
+
export * from "./instance.js";
|
|
5
|
+
const useValtioState = (inital)=>{
|
|
6
|
+
const instance = useRef(new ProxyInstanceObjectBase()._ctor(inital)).current;
|
|
7
|
+
const state = useSnapshot(instance.store);
|
|
8
|
+
return [
|
|
9
|
+
state,
|
|
10
|
+
instance,
|
|
11
|
+
state.__defaultValue
|
|
12
|
+
];
|
|
13
|
+
};
|
|
14
|
+
const useValtioInstaceState = (Instance)=>{
|
|
15
|
+
const instance = useRef(new Instance()).current;
|
|
16
|
+
const state = useSnapshot(instance.store);
|
|
17
|
+
return [
|
|
18
|
+
state,
|
|
19
|
+
instance,
|
|
20
|
+
state.__defaultValue
|
|
21
|
+
];
|
|
22
|
+
};
|
|
23
|
+
export { useValtioInstaceState, useValtioState };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 单个proxy对象数据基础实例封装
|
|
3
|
+
*/
|
|
4
|
+
export declare class ProxyInstanceObjectBase<T extends Object = any> {
|
|
5
|
+
/**proxy 可状态更新字段 */
|
|
6
|
+
store: T;
|
|
7
|
+
/**初始化存储值*/
|
|
8
|
+
_ctor: (inital?: Partial<T>, fields?: string[]) => this;
|
|
9
|
+
/**更新store数据 循环对象进行存储,当值是对象的时候存储为ref*/
|
|
10
|
+
_setValues: <K = T>(values: Partial<K>, fields?: string[]) => this;
|
|
11
|
+
/**删除字段值*/
|
|
12
|
+
_deleteValue: (names: string | string[]) => this;
|
|
13
|
+
/**创建 ref 对象 (ref对象不做监听更新)*/
|
|
14
|
+
_createRef: <K extends Object = any>(inital?: K) => K;
|
|
15
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { proxy, ref } from "valtio";
|
|
2
|
+
import react from "react";
|
|
3
|
+
class ProxyInstanceObjectBase {
|
|
4
|
+
store = proxy({});
|
|
5
|
+
_ctor = (inital, fields)=>{
|
|
6
|
+
this._setValues(inital || {}, fields);
|
|
7
|
+
return this;
|
|
8
|
+
};
|
|
9
|
+
_setValues = (values, fields)=>{
|
|
10
|
+
if (!this.store) this.store = proxy({});
|
|
11
|
+
Object.keys(values).forEach((key)=>{
|
|
12
|
+
const value = values[key];
|
|
13
|
+
if (Array.isArray(fields) && fields.includes(key)) this.store[key] = values[key];
|
|
14
|
+
else if (react.isValidElement(value) || 'function' == typeof value) this.store[key] = ref(values[key]);
|
|
15
|
+
else if ('object' == typeof value && null !== value) this.store[key] = ref(values[key]);
|
|
16
|
+
else this.store[key] = values[key];
|
|
17
|
+
});
|
|
18
|
+
return this;
|
|
19
|
+
};
|
|
20
|
+
_deleteValue = (names)=>{
|
|
21
|
+
if (Array.isArray(names)) {
|
|
22
|
+
let cacheValue = this.store;
|
|
23
|
+
const newNames = [
|
|
24
|
+
...names
|
|
25
|
+
];
|
|
26
|
+
const lastField = newNames.pop();
|
|
27
|
+
for(let index = 0; index < newNames.length; index++)cacheValue = cacheValue[newNames[index]];
|
|
28
|
+
if (cacheValue && lastField) delete cacheValue[lastField];
|
|
29
|
+
} else delete this.store[names];
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
32
|
+
_createRef = (inital)=>ref(inital || {});
|
|
33
|
+
}
|
|
34
|
+
export { ProxyInstanceObjectBase };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_modules__ = {
|
|
3
|
+
"./components": function(module) {
|
|
4
|
+
module.exports = require("./components/index.js");
|
|
5
|
+
},
|
|
6
|
+
"./context": function(module) {
|
|
7
|
+
module.exports = require("./context/index.js");
|
|
8
|
+
},
|
|
9
|
+
"./utils": function(module) {
|
|
10
|
+
module.exports = require("./utils/index.js");
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var __webpack_module_cache__ = {};
|
|
14
|
+
function __webpack_require__(moduleId) {
|
|
15
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
16
|
+
if (void 0 !== cachedModule) return cachedModule.exports;
|
|
17
|
+
var module = __webpack_module_cache__[moduleId] = {
|
|
18
|
+
exports: {}
|
|
19
|
+
};
|
|
20
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
21
|
+
return module.exports;
|
|
22
|
+
}
|
|
23
|
+
(()=>{
|
|
24
|
+
__webpack_require__.n = (module)=>{
|
|
25
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
26
|
+
__webpack_require__.d(getter, {
|
|
27
|
+
a: getter
|
|
28
|
+
});
|
|
29
|
+
return getter;
|
|
30
|
+
};
|
|
31
|
+
})();
|
|
32
|
+
(()=>{
|
|
33
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
34
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
get: definition[key]
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
(()=>{
|
|
41
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
42
|
+
})();
|
|
43
|
+
(()=>{
|
|
44
|
+
__webpack_require__.r = (exports1)=>{
|
|
45
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
46
|
+
value: 'Module'
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
49
|
+
value: true
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
})();
|
|
53
|
+
var __webpack_exports__ = {};
|
|
54
|
+
(()=>{
|
|
55
|
+
__webpack_require__.r(__webpack_exports__);
|
|
56
|
+
var _components__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./components");
|
|
57
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
58
|
+
for(var __WEBPACK_IMPORT_KEY__ in _components__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
59
|
+
return _components__WEBPACK_IMPORTED_MODULE_0__[key];
|
|
60
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
61
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
62
|
+
var _context__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./context");
|
|
63
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
64
|
+
for(var __WEBPACK_IMPORT_KEY__ in _context__WEBPACK_IMPORTED_MODULE_1__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
65
|
+
return _context__WEBPACK_IMPORTED_MODULE_1__[key];
|
|
66
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
67
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
68
|
+
var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("./utils");
|
|
69
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
70
|
+
for(var __WEBPACK_IMPORT_KEY__ in _utils__WEBPACK_IMPORTED_MODULE_2__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
71
|
+
return _utils__WEBPACK_IMPORTED_MODULE_2__[key];
|
|
72
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
73
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
74
|
+
})();
|
|
75
|
+
for(var __webpack_i__ in __webpack_exports__)exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
76
|
+
Object.defineProperty(exports, '__esModule', {
|
|
77
|
+
value: true
|
|
78
|
+
});
|