@fairys/taro-tools-react 1.0.17 → 1.0.19
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/utils/request.d.ts +5 -3
- package/esm/utils/request.js +5 -1
- package/esm/utils/valtio/context.d.ts +7 -0
- package/esm/utils/valtio/context.js +15 -0
- package/esm/utils/valtio/index.d.ts +4 -3
- package/esm/utils/valtio/index.js +13 -6
- package/esm/utils/valtio/instance.d.ts +4 -0
- package/esm/utils/valtio/instance.js +11 -6
- package/lib/index.js +2 -2
- package/lib/utils/request.d.ts +5 -3
- package/lib/utils/request.js +5 -1
- package/lib/utils/valtio/context.d.ts +7 -0
- package/lib/utils/valtio/context.js +55 -0
- package/lib/utils/valtio/index.d.ts +4 -3
- package/lib/utils/valtio/index.js +25 -5
- package/lib/utils/valtio/instance.d.ts +4 -0
- package/lib/utils/valtio/instance.js +10 -2
- package/package.json +1 -1
- package/src/utils/request.ts +9 -3
- package/src/utils/valtio/context.tsx +15 -0
- package/src/utils/valtio/index.ts +26 -8
- package/src/utils/valtio/instance.ts +12 -1
package/esm/utils/request.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface TaroRequestOption {
|
|
|
6
6
|
isIgnoreToken?: boolean;
|
|
7
7
|
/**是否提示错误信息*/
|
|
8
8
|
isShowErrorMessage?: boolean;
|
|
9
|
+
/**成功状态码*/
|
|
10
|
+
requestSuccessCode?: number;
|
|
9
11
|
}
|
|
10
12
|
export interface RequestInstanceOptions extends Taro.request.Option<any, any>, TaroRequestOption {
|
|
11
13
|
}
|
|
@@ -107,7 +109,7 @@ export declare class RequestInstance {
|
|
|
107
109
|
storeSign?: string;
|
|
108
110
|
storeCheck?(): boolean;
|
|
109
111
|
header: {
|
|
110
|
-
|
|
112
|
+
'accept-charset': string;
|
|
111
113
|
};
|
|
112
114
|
} | {
|
|
113
115
|
url: string;
|
|
@@ -120,7 +122,7 @@ export declare class RequestInstance {
|
|
|
120
122
|
fail?: (res: TaroGeneral.CallbackResult) => void;
|
|
121
123
|
success?: (result: Taro.downloadFile.FileSuccessCallbackResult) => void;
|
|
122
124
|
header: {
|
|
123
|
-
|
|
125
|
+
'accept-charset': string;
|
|
124
126
|
};
|
|
125
127
|
} | {
|
|
126
128
|
url: string;
|
|
@@ -138,7 +140,7 @@ export declare class RequestInstance {
|
|
|
138
140
|
fail?: (res: TaroGeneral.CallbackResult) => void;
|
|
139
141
|
success?: (result: Taro.uploadFile.SuccessCallbackResult) => void;
|
|
140
142
|
header: {
|
|
141
|
-
|
|
143
|
+
'accept-charset': string;
|
|
142
144
|
};
|
|
143
145
|
};
|
|
144
146
|
/**发送请求,返回 Taro.RequestTask */
|
package/esm/utils/request.js
CHANGED
|
@@ -16,6 +16,7 @@ const codeMessage = {
|
|
|
16
16
|
};
|
|
17
17
|
const requestResponseHandle = (result, options)=>{
|
|
18
18
|
let msg = '';
|
|
19
|
+
const requestSuccessCode = options?.requestSuccessCode;
|
|
19
20
|
try {
|
|
20
21
|
const statusCode = result.statusCode;
|
|
21
22
|
const code = result?.data?.code;
|
|
@@ -24,6 +25,8 @@ const requestResponseHandle = (result, options)=>{
|
|
|
24
25
|
globalMessageDataInstance.toLoginPage();
|
|
25
26
|
} else msg = [
|
|
26
27
|
globalSettingDataInstance.store.requestSuccessCode,
|
|
28
|
+
requestSuccessCode,
|
|
29
|
+
1,
|
|
27
30
|
200
|
|
28
31
|
].includes(code) ? codeMessage[result?.statusCode] : result?.data?.message || codeMessage[code || result?.statusCode] || "\u63A5\u53E3\u5F02\u5E38";
|
|
29
32
|
} catch (error) {
|
|
@@ -111,9 +114,10 @@ class RequestInstance {
|
|
|
111
114
|
return `${host}/${newUrl}`;
|
|
112
115
|
};
|
|
113
116
|
formatRequestOptions = (options)=>{
|
|
114
|
-
const { header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
|
|
117
|
+
const { header = {}, module, isIgnoreToken, isShowErrorMessage, requestSuccessCode, ...restOptions } = options;
|
|
115
118
|
const token = taro.getStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
116
119
|
const newHeader = {
|
|
120
|
+
'accept-charset': 'utf-8',
|
|
117
121
|
...header
|
|
118
122
|
};
|
|
119
123
|
if (token) newHeader[globalSettingDataInstance.store.headerTokenName || 'token'] = token;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
export declare const ValtioContext: import("react").Context<any>;
|
|
3
|
+
export declare function ValtioContextProvider<T>({ value, children }: {
|
|
4
|
+
value: T;
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export declare function useValtioContext<T>(): T;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
const ValtioContext = /*#__PURE__*/ createContext(void 0);
|
|
4
|
+
function ValtioContextProvider({ value, children }) {
|
|
5
|
+
return /*#__PURE__*/ jsx(ValtioContext.Provider, {
|
|
6
|
+
value: value,
|
|
7
|
+
children: children
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
function useValtioContext() {
|
|
11
|
+
const context = useContext(ValtioContext);
|
|
12
|
+
if (!context) throw new Error('useValtioContext must be used within a ValtioContextProvider');
|
|
13
|
+
return context;
|
|
14
|
+
}
|
|
15
|
+
export { ValtioContext, ValtioContextProvider, useValtioContext };
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { ProxyInstanceObjectBase } from './instance';
|
|
2
2
|
export * from './instance';
|
|
3
|
+
export * from './context';
|
|
3
4
|
/**创建简单的状态管理*/
|
|
4
|
-
export declare const useValtioState: <T extends object>(inital?: T) =>
|
|
5
|
+
export declare const useValtioState: <T extends object>(inital?: T) => [T, ProxyInstanceObjectBase<T>, any];
|
|
5
6
|
export declare const useValtioInstaceState: <T extends object = any, K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>, M extends {
|
|
6
7
|
new (...args: any[]): K;
|
|
7
8
|
} = {
|
|
8
9
|
new (...args: any[]): K;
|
|
9
|
-
}>(Instance: M) =>
|
|
10
|
+
}>(Instance: M) => [T, K, any];
|
|
10
11
|
/**
|
|
11
12
|
* 创建valtio proxy 状态管理
|
|
12
13
|
*/
|
|
13
|
-
export declare const useValtioProxyState: <T extends object>(inital?: T) =>
|
|
14
|
+
export declare const useValtioProxyState: <T extends object>(inital?: T) => [T, T, any];
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { useRef } from "react";
|
|
2
2
|
import { proxy, useSnapshot } from "valtio";
|
|
3
|
-
import {
|
|
3
|
+
import { useProxyInstanceObjectBase } from "./instance.js";
|
|
4
4
|
export * from "./instance.js";
|
|
5
|
+
export * from "./context.js";
|
|
5
6
|
const useValtioState = (inital)=>{
|
|
6
|
-
const instance =
|
|
7
|
+
const instance = useProxyInstanceObjectBase(inital);
|
|
7
8
|
const state = useSnapshot(instance.store);
|
|
8
9
|
return [
|
|
9
10
|
state,
|
|
@@ -11,8 +12,13 @@ const useValtioState = (inital)=>{
|
|
|
11
12
|
state.__defaultValue
|
|
12
13
|
];
|
|
13
14
|
};
|
|
15
|
+
const usNewInstance = (Instance)=>{
|
|
16
|
+
const ref = useRef();
|
|
17
|
+
if (!ref.current) ref.current = new Instance();
|
|
18
|
+
return ref.current;
|
|
19
|
+
};
|
|
14
20
|
const useValtioInstaceState = (Instance)=>{
|
|
15
|
-
const instance =
|
|
21
|
+
const instance = usNewInstance(Instance);
|
|
16
22
|
const state = useSnapshot(instance.store);
|
|
17
23
|
return [
|
|
18
24
|
state,
|
|
@@ -21,11 +27,12 @@ const useValtioInstaceState = (Instance)=>{
|
|
|
21
27
|
];
|
|
22
28
|
};
|
|
23
29
|
const useValtioProxyState = (inital)=>{
|
|
24
|
-
const instance = useRef(
|
|
25
|
-
|
|
30
|
+
const instance = useRef();
|
|
31
|
+
if (!instance.current) instance.current = proxy(inital || {});
|
|
32
|
+
const state = useSnapshot(instance.current);
|
|
26
33
|
return [
|
|
27
34
|
state,
|
|
28
|
-
instance,
|
|
35
|
+
instance.current,
|
|
29
36
|
state.__defaultValue
|
|
30
37
|
];
|
|
31
38
|
};
|
|
@@ -15,3 +15,7 @@ export declare class ProxyInstanceObjectBase<T extends Object = any> {
|
|
|
15
15
|
/**创建 ref 对象 (ref对象不做监听更新)*/
|
|
16
16
|
_createRef: <K extends Object = any>(inital?: K) => K;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* 创建单个proxy对象数据基础实例封装
|
|
20
|
+
*/
|
|
21
|
+
export declare const useProxyInstanceObjectBase: <T extends object>(inital?: T) => ProxyInstanceObjectBase<T>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { proxy, ref } from "valtio";
|
|
2
|
-
import react from "react";
|
|
1
|
+
import { proxy, ref as external_valtio_ref } from "valtio";
|
|
2
|
+
import react, { useRef } from "react";
|
|
3
3
|
class ProxyInstanceObjectBase {
|
|
4
4
|
notRefFields = [];
|
|
5
5
|
store = proxy({});
|
|
@@ -13,8 +13,8 @@ class ProxyInstanceObjectBase {
|
|
|
13
13
|
const value = values[key];
|
|
14
14
|
if (Array.isArray(fields) && fields.includes(key)) this.store[key] = values[key];
|
|
15
15
|
else if (Array.isArray(this.notRefFields) && this.notRefFields.includes(key)) this.store[key] = value;
|
|
16
|
-
else if (react.isValidElement(value) || 'function' == typeof value) this.store[key] =
|
|
17
|
-
else if ('object' == typeof value && null !== value) this.store[key] =
|
|
16
|
+
else if (react.isValidElement(value) || 'function' == typeof value) this.store[key] = external_valtio_ref(values[key]);
|
|
17
|
+
else if ('object' == typeof value && null !== value) this.store[key] = external_valtio_ref(values[key]);
|
|
18
18
|
else this.store[key] = values[key];
|
|
19
19
|
});
|
|
20
20
|
return this;
|
|
@@ -31,6 +31,11 @@ class ProxyInstanceObjectBase {
|
|
|
31
31
|
} else delete this.store[names];
|
|
32
32
|
return this;
|
|
33
33
|
};
|
|
34
|
-
_createRef = (inital)=>
|
|
34
|
+
_createRef = (inital)=>external_valtio_ref(inital || {});
|
|
35
35
|
}
|
|
36
|
-
|
|
36
|
+
const useProxyInstanceObjectBase = (inital)=>{
|
|
37
|
+
const ref = useRef();
|
|
38
|
+
if (!ref.current) ref.current = new ProxyInstanceObjectBase()._ctor(inital);
|
|
39
|
+
return ref.current;
|
|
40
|
+
};
|
|
41
|
+
export { ProxyInstanceObjectBase, useProxyInstanceObjectBase };
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __webpack_modules__ = {
|
|
|
3
3
|
"./components": function(module) {
|
|
4
4
|
module.exports = require("./components/index.js");
|
|
5
5
|
},
|
|
6
|
-
"./context": function(module) {
|
|
6
|
+
"./context?4a23": function(module) {
|
|
7
7
|
module.exports = require("./context/index.js");
|
|
8
8
|
},
|
|
9
9
|
"./utils": function(module) {
|
|
@@ -59,7 +59,7 @@ var __webpack_exports__ = {};
|
|
|
59
59
|
return _components__WEBPACK_IMPORTED_MODULE_0__[key];
|
|
60
60
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
61
61
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
62
|
-
var _context__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./context");
|
|
62
|
+
var _context__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./context?4a23");
|
|
63
63
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
64
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
65
|
return _context__WEBPACK_IMPORTED_MODULE_1__[key];
|
package/lib/utils/request.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface TaroRequestOption {
|
|
|
6
6
|
isIgnoreToken?: boolean;
|
|
7
7
|
/**是否提示错误信息*/
|
|
8
8
|
isShowErrorMessage?: boolean;
|
|
9
|
+
/**成功状态码*/
|
|
10
|
+
requestSuccessCode?: number;
|
|
9
11
|
}
|
|
10
12
|
export interface RequestInstanceOptions extends Taro.request.Option<any, any>, TaroRequestOption {
|
|
11
13
|
}
|
|
@@ -107,7 +109,7 @@ export declare class RequestInstance {
|
|
|
107
109
|
storeSign?: string;
|
|
108
110
|
storeCheck?(): boolean;
|
|
109
111
|
header: {
|
|
110
|
-
|
|
112
|
+
'accept-charset': string;
|
|
111
113
|
};
|
|
112
114
|
} | {
|
|
113
115
|
url: string;
|
|
@@ -120,7 +122,7 @@ export declare class RequestInstance {
|
|
|
120
122
|
fail?: (res: TaroGeneral.CallbackResult) => void;
|
|
121
123
|
success?: (result: Taro.downloadFile.FileSuccessCallbackResult) => void;
|
|
122
124
|
header: {
|
|
123
|
-
|
|
125
|
+
'accept-charset': string;
|
|
124
126
|
};
|
|
125
127
|
} | {
|
|
126
128
|
url: string;
|
|
@@ -138,7 +140,7 @@ export declare class RequestInstance {
|
|
|
138
140
|
fail?: (res: TaroGeneral.CallbackResult) => void;
|
|
139
141
|
success?: (result: Taro.uploadFile.SuccessCallbackResult) => void;
|
|
140
142
|
header: {
|
|
141
|
-
|
|
143
|
+
'accept-charset': string;
|
|
142
144
|
};
|
|
143
145
|
};
|
|
144
146
|
/**发送请求,返回 Taro.RequestTask */
|
package/lib/utils/request.js
CHANGED
|
@@ -56,6 +56,7 @@ const codeMessage = {
|
|
|
56
56
|
};
|
|
57
57
|
const requestResponseHandle = (result, options)=>{
|
|
58
58
|
let msg = '';
|
|
59
|
+
const requestSuccessCode = options?.requestSuccessCode;
|
|
59
60
|
try {
|
|
60
61
|
const statusCode = result.statusCode;
|
|
61
62
|
const code = result?.data?.code;
|
|
@@ -64,6 +65,8 @@ const requestResponseHandle = (result, options)=>{
|
|
|
64
65
|
global_message_data_instance_js_namespaceObject.globalMessageDataInstance.toLoginPage();
|
|
65
66
|
} else msg = [
|
|
66
67
|
global_setting_data_instance_js_namespaceObject.globalSettingDataInstance.store.requestSuccessCode,
|
|
68
|
+
requestSuccessCode,
|
|
69
|
+
1,
|
|
67
70
|
200
|
|
68
71
|
].includes(code) ? codeMessage[result?.statusCode] : result?.data?.message || codeMessage[code || result?.statusCode] || "\u63A5\u53E3\u5F02\u5E38";
|
|
69
72
|
} catch (error) {
|
|
@@ -151,9 +154,10 @@ class RequestInstance {
|
|
|
151
154
|
return `${host}/${newUrl}`;
|
|
152
155
|
};
|
|
153
156
|
formatRequestOptions = (options)=>{
|
|
154
|
-
const { header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
|
|
157
|
+
const { header = {}, module, isIgnoreToken, isShowErrorMessage, requestSuccessCode, ...restOptions } = options;
|
|
155
158
|
const token = taro_default().getStorageSync(global_setting_data_instance_js_namespaceObject.globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
156
159
|
const newHeader = {
|
|
160
|
+
'accept-charset': 'utf-8',
|
|
157
161
|
...header
|
|
158
162
|
};
|
|
159
163
|
if (token) newHeader[global_setting_data_instance_js_namespaceObject.globalSettingDataInstance.store.headerTokenName || 'token'] = token;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
export declare const ValtioContext: import("react").Context<any>;
|
|
3
|
+
export declare function ValtioContextProvider<T>({ value, children }: {
|
|
4
|
+
value: T;
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
7
|
+
export declare function useValtioContext<T>(): T;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
ValtioContext: ()=>ValtioContext,
|
|
28
|
+
ValtioContextProvider: ()=>ValtioContextProvider,
|
|
29
|
+
useValtioContext: ()=>useValtioContext
|
|
30
|
+
});
|
|
31
|
+
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
|
|
32
|
+
const external_react_namespaceObject = require("react");
|
|
33
|
+
const ValtioContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(void 0);
|
|
34
|
+
function ValtioContextProvider({ value, children }) {
|
|
35
|
+
return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ValtioContext.Provider, {
|
|
36
|
+
value: value,
|
|
37
|
+
children: children
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function useValtioContext() {
|
|
41
|
+
const context = (0, external_react_namespaceObject.useContext)(ValtioContext);
|
|
42
|
+
if (!context) throw new Error('useValtioContext must be used within a ValtioContextProvider');
|
|
43
|
+
return context;
|
|
44
|
+
}
|
|
45
|
+
exports.ValtioContext = __webpack_exports__.ValtioContext;
|
|
46
|
+
exports.ValtioContextProvider = __webpack_exports__.ValtioContextProvider;
|
|
47
|
+
exports.useValtioContext = __webpack_exports__.useValtioContext;
|
|
48
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
49
|
+
"ValtioContext",
|
|
50
|
+
"ValtioContextProvider",
|
|
51
|
+
"useValtioContext"
|
|
52
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
53
|
+
Object.defineProperty(exports, '__esModule', {
|
|
54
|
+
value: true
|
|
55
|
+
});
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { ProxyInstanceObjectBase } from './instance';
|
|
2
2
|
export * from './instance';
|
|
3
|
+
export * from './context';
|
|
3
4
|
/**创建简单的状态管理*/
|
|
4
|
-
export declare const useValtioState: <T extends object>(inital?: T) =>
|
|
5
|
+
export declare const useValtioState: <T extends object>(inital?: T) => [T, ProxyInstanceObjectBase<T>, any];
|
|
5
6
|
export declare const useValtioInstaceState: <T extends object = any, K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>, M extends {
|
|
6
7
|
new (...args: any[]): K;
|
|
7
8
|
} = {
|
|
8
9
|
new (...args: any[]): K;
|
|
9
|
-
}>(Instance: M) =>
|
|
10
|
+
}>(Instance: M) => [T, K, any];
|
|
10
11
|
/**
|
|
11
12
|
* 创建valtio proxy 状态管理
|
|
12
13
|
*/
|
|
13
|
-
export declare const useValtioProxyState: <T extends object>(inital?: T) =>
|
|
14
|
+
export declare const useValtioProxyState: <T extends object>(inital?: T) => [T, T, any];
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __webpack_modules__ = {
|
|
3
|
+
"./context?fe51": function(module) {
|
|
4
|
+
module.exports = require("./context.js");
|
|
5
|
+
},
|
|
3
6
|
"./instance": function(module) {
|
|
4
7
|
module.exports = require("./instance.js");
|
|
5
8
|
},
|
|
@@ -71,8 +74,19 @@ var __webpack_exports__ = {};
|
|
|
71
74
|
return _instance__WEBPACK_IMPORTED_MODULE_2__[key];
|
|
72
75
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
73
76
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
77
|
+
var _context__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("./context?fe51");
|
|
78
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
|
79
|
+
for(var __WEBPACK_IMPORT_KEY__ in _context__WEBPACK_IMPORTED_MODULE_3__)if ([
|
|
80
|
+
"useValtioProxyState",
|
|
81
|
+
"default",
|
|
82
|
+
"useValtioInstaceState",
|
|
83
|
+
"useValtioState"
|
|
84
|
+
].indexOf(__WEBPACK_IMPORT_KEY__) < 0) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
|
85
|
+
return _context__WEBPACK_IMPORTED_MODULE_3__[key];
|
|
86
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
|
87
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
|
74
88
|
const useValtioState = (inital)=>{
|
|
75
|
-
const instance = (0,
|
|
89
|
+
const instance = (0, _instance__WEBPACK_IMPORTED_MODULE_2__.useProxyInstanceObjectBase)(inital);
|
|
76
90
|
const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance.store);
|
|
77
91
|
return [
|
|
78
92
|
state,
|
|
@@ -80,8 +94,13 @@ var __webpack_exports__ = {};
|
|
|
80
94
|
state.__defaultValue
|
|
81
95
|
];
|
|
82
96
|
};
|
|
97
|
+
const usNewInstance = (Instance)=>{
|
|
98
|
+
const ref = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
|
|
99
|
+
if (!ref.current) ref.current = new Instance();
|
|
100
|
+
return ref.current;
|
|
101
|
+
};
|
|
83
102
|
const useValtioInstaceState = (Instance)=>{
|
|
84
|
-
const instance = (
|
|
103
|
+
const instance = usNewInstance(Instance);
|
|
85
104
|
const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance.store);
|
|
86
105
|
return [
|
|
87
106
|
state,
|
|
@@ -90,11 +109,12 @@ var __webpack_exports__ = {};
|
|
|
90
109
|
];
|
|
91
110
|
};
|
|
92
111
|
const useValtioProxyState = (inital)=>{
|
|
93
|
-
const instance = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)(
|
|
94
|
-
|
|
112
|
+
const instance = (0, react__WEBPACK_IMPORTED_MODULE_0__.useRef)();
|
|
113
|
+
if (!instance.current) instance.current = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.proxy)(inital || {});
|
|
114
|
+
const state = (0, valtio__WEBPACK_IMPORTED_MODULE_1__.useSnapshot)(instance.current);
|
|
95
115
|
return [
|
|
96
116
|
state,
|
|
97
|
-
instance,
|
|
117
|
+
instance.current,
|
|
98
118
|
state.__defaultValue
|
|
99
119
|
];
|
|
100
120
|
};
|
|
@@ -15,3 +15,7 @@ export declare class ProxyInstanceObjectBase<T extends Object = any> {
|
|
|
15
15
|
/**创建 ref 对象 (ref对象不做监听更新)*/
|
|
16
16
|
_createRef: <K extends Object = any>(inital?: K) => K;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* 创建单个proxy对象数据基础实例封装
|
|
20
|
+
*/
|
|
21
|
+
export declare const useProxyInstanceObjectBase: <T extends object>(inital?: T) => ProxyInstanceObjectBase<T>;
|
|
@@ -33,7 +33,8 @@ var __webpack_require__ = {};
|
|
|
33
33
|
var __webpack_exports__ = {};
|
|
34
34
|
__webpack_require__.r(__webpack_exports__);
|
|
35
35
|
__webpack_require__.d(__webpack_exports__, {
|
|
36
|
-
ProxyInstanceObjectBase: ()=>ProxyInstanceObjectBase
|
|
36
|
+
ProxyInstanceObjectBase: ()=>ProxyInstanceObjectBase,
|
|
37
|
+
useProxyInstanceObjectBase: ()=>useProxyInstanceObjectBase
|
|
37
38
|
});
|
|
38
39
|
const external_valtio_namespaceObject = require("valtio");
|
|
39
40
|
const external_react_namespaceObject = require("react");
|
|
@@ -71,9 +72,16 @@ class ProxyInstanceObjectBase {
|
|
|
71
72
|
};
|
|
72
73
|
_createRef = (inital)=>(0, external_valtio_namespaceObject.ref)(inital || {});
|
|
73
74
|
}
|
|
75
|
+
const useProxyInstanceObjectBase = (inital)=>{
|
|
76
|
+
const ref = (0, external_react_namespaceObject.useRef)();
|
|
77
|
+
if (!ref.current) ref.current = new ProxyInstanceObjectBase()._ctor(inital);
|
|
78
|
+
return ref.current;
|
|
79
|
+
};
|
|
74
80
|
exports.ProxyInstanceObjectBase = __webpack_exports__.ProxyInstanceObjectBase;
|
|
81
|
+
exports.useProxyInstanceObjectBase = __webpack_exports__.useProxyInstanceObjectBase;
|
|
75
82
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
76
|
-
"ProxyInstanceObjectBase"
|
|
83
|
+
"ProxyInstanceObjectBase",
|
|
84
|
+
"useProxyInstanceObjectBase"
|
|
77
85
|
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
78
86
|
Object.defineProperty(exports, '__esModule', {
|
|
79
87
|
value: true
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"author": "SunLxy <1011771396@qq.com>",
|
|
4
4
|
"description": "框架组件库",
|
|
5
5
|
"homepage": "https://github.com/autumn-fairy-tales/fairys-taro-react",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.19",
|
|
7
7
|
"main": "esm/index.js",
|
|
8
8
|
"types": "esm/index.d.ts",
|
|
9
9
|
"module": "esm/index.js",
|
package/src/utils/request.ts
CHANGED
|
@@ -23,6 +23,8 @@ export interface TaroRequestOption {
|
|
|
23
23
|
isIgnoreToken?: boolean;
|
|
24
24
|
/**是否提示错误信息*/
|
|
25
25
|
isShowErrorMessage?: boolean;
|
|
26
|
+
/**成功状态码*/
|
|
27
|
+
requestSuccessCode?: number;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export interface RequestInstanceOptions extends Taro.request.Option<any, any>, TaroRequestOption {}
|
|
@@ -45,6 +47,7 @@ const requestResponseHandle = (
|
|
|
45
47
|
options?: RequestInstanceOptions,
|
|
46
48
|
) => {
|
|
47
49
|
let msg = '';
|
|
50
|
+
const requestSuccessCode = options?.requestSuccessCode;
|
|
48
51
|
try {
|
|
49
52
|
const statusCode = result.statusCode;
|
|
50
53
|
// @ts-ignore
|
|
@@ -54,7 +57,7 @@ const requestResponseHandle = (
|
|
|
54
57
|
msg = '请重新登录';
|
|
55
58
|
/**重新跳转登录页面*/
|
|
56
59
|
globalMessageDataInstance.toLoginPage();
|
|
57
|
-
} else if (![globalSettingDataInstance.store.requestSuccessCode, 200].includes(code)) {
|
|
60
|
+
} else if (![globalSettingDataInstance.store.requestSuccessCode, requestSuccessCode, 1, 200].includes(code)) {
|
|
58
61
|
// 提示内容
|
|
59
62
|
// @ts-ignore
|
|
60
63
|
msg = result?.data?.message || codeMessage[code || result?.statusCode] || '接口异常';
|
|
@@ -215,9 +218,12 @@ export class RequestInstance {
|
|
|
215
218
|
};
|
|
216
219
|
|
|
217
220
|
formatRequestOptions = (options: RequestInstanceOptions | DownloadFileOptions | UploadFileOptions) => {
|
|
218
|
-
const { header = {}, module, isIgnoreToken, isShowErrorMessage, ...restOptions } = options;
|
|
221
|
+
const { header = {}, module, isIgnoreToken, isShowErrorMessage, requestSuccessCode, ...restOptions } = options;
|
|
219
222
|
const token = Taro.getStorageSync(globalSettingDataInstance.store.tokenFieldName || 'token');
|
|
220
|
-
const newHeader = {
|
|
223
|
+
const newHeader = {
|
|
224
|
+
'accept-charset': 'utf-8',
|
|
225
|
+
...header,
|
|
226
|
+
};
|
|
221
227
|
if (token) {
|
|
222
228
|
newHeader[globalSettingDataInstance.store.headerTokenName || 'token'] = token;
|
|
223
229
|
} else {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext, ReactNode, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
export const ValtioContext = createContext(undefined);
|
|
4
|
+
|
|
5
|
+
export function ValtioContextProvider<T>({ value, children }: { value: T; children?: ReactNode }) {
|
|
6
|
+
return <ValtioContext.Provider value={value}>{children}</ValtioContext.Provider>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function useValtioContext<T>() {
|
|
10
|
+
const context = useContext<T>(ValtioContext);
|
|
11
|
+
if (!context) {
|
|
12
|
+
throw new Error('useValtioContext must be used within a ValtioContextProvider');
|
|
13
|
+
}
|
|
14
|
+
return context;
|
|
15
|
+
}
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import { useRef } from 'react';
|
|
2
2
|
import { useSnapshot, proxy } from 'valtio';
|
|
3
|
-
import { ProxyInstanceObjectBase } from './instance';
|
|
3
|
+
import { ProxyInstanceObjectBase, useProxyInstanceObjectBase } from './instance';
|
|
4
4
|
export * from './instance';
|
|
5
|
+
export * from './context';
|
|
5
6
|
|
|
6
7
|
/**创建简单的状态管理*/
|
|
7
8
|
export const useValtioState = <T extends object>(inital?: T) => {
|
|
8
|
-
const instance =
|
|
9
|
+
const instance = useProxyInstanceObjectBase(inital);
|
|
9
10
|
const state = useSnapshot(instance.store);
|
|
10
|
-
return [state, instance, (state as any).__defaultValue] as
|
|
11
|
+
return [state, instance, (state as any).__defaultValue] as [T, ProxyInstanceObjectBase<T>, any];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const usNewInstance = <
|
|
15
|
+
T extends object = any,
|
|
16
|
+
K extends ProxyInstanceObjectBase<T> = ProxyInstanceObjectBase<T>,
|
|
17
|
+
M extends { new (...args: any[]): K } = { new (...args: any[]): K },
|
|
18
|
+
>(
|
|
19
|
+
Instance: M,
|
|
20
|
+
) => {
|
|
21
|
+
const ref = useRef<K>();
|
|
22
|
+
if (!ref.current) {
|
|
23
|
+
ref.current = new Instance();
|
|
24
|
+
}
|
|
25
|
+
return ref.current;
|
|
11
26
|
};
|
|
12
27
|
|
|
13
28
|
export const useValtioInstaceState = <
|
|
@@ -17,16 +32,19 @@ export const useValtioInstaceState = <
|
|
|
17
32
|
>(
|
|
18
33
|
Instance: M,
|
|
19
34
|
) => {
|
|
20
|
-
const instance =
|
|
35
|
+
const instance = usNewInstance<T, K, M>(Instance);
|
|
21
36
|
const state = useSnapshot(instance.store);
|
|
22
|
-
return [state, instance, (state as any).__defaultValue] as
|
|
37
|
+
return [state, instance, (state as any).__defaultValue] as [T, K, any];
|
|
23
38
|
};
|
|
24
39
|
|
|
25
40
|
/**
|
|
26
41
|
* 创建valtio proxy 状态管理
|
|
27
42
|
*/
|
|
28
43
|
export const useValtioProxyState = <T extends object>(inital?: T) => {
|
|
29
|
-
const instance = useRef(
|
|
30
|
-
|
|
31
|
-
|
|
44
|
+
const instance = useRef<T>();
|
|
45
|
+
if (!instance.current) {
|
|
46
|
+
instance.current = proxy(inital || ({} as T));
|
|
47
|
+
}
|
|
48
|
+
const state = useSnapshot(instance.current);
|
|
49
|
+
return [state, instance.current, (state as any).__defaultValue] as [T, T, any];
|
|
32
50
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { proxy, ref } from 'valtio';
|
|
2
|
-
import React from 'react';
|
|
2
|
+
import React, { useRef } from 'react';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 单个proxy对象数据基础实例封装
|
|
@@ -59,3 +59,14 @@ export class ProxyInstanceObjectBase<T extends Object = any> {
|
|
|
59
59
|
return ref<K>(inital || ({} as K)) as K;
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 创建单个proxy对象数据基础实例封装
|
|
65
|
+
*/
|
|
66
|
+
export const useProxyInstanceObjectBase = <T extends object>(inital?: T) => {
|
|
67
|
+
const ref = useRef<ProxyInstanceObjectBase<T>>();
|
|
68
|
+
if (!ref.current) {
|
|
69
|
+
ref.current = new ProxyInstanceObjectBase<T>()._ctor(inital);
|
|
70
|
+
}
|
|
71
|
+
return ref.current;
|
|
72
|
+
};
|