fastman3-dfyjapp-request 1.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.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # fastman3-dfyjapp-request
2
+
3
+ ## CHANGELOG
4
+
5
+ ### v 1.0.0 - 2021.3.28
6
+
7
+ 1. 初始化构建
8
+
9
+ ### v 1.0.1 - 2021.5.20
10
+
11
+ 1. 新增 vtdeviceid 头属性
12
+
13
+ ### v 1.0.2 - 2021.5.31
14
+
15
+ 1. 兼容 APP_ENV 环境变量
16
+
17
+ ### v 1.0.3 - 2021.7.15
18
+
19
+ 1. 新增restful类型请求getRest、postRest
20
+
21
+ ### v 1.0.4 - 2021.9.1
22
+
23
+ 1. 新增requestNative原始 taro request 方法导出
@@ -0,0 +1,2 @@
1
+ export declare const DEVICE_ID_KEY = "__d2__";
2
+ export declare const TOKEN_KEY = "__t__";
@@ -0,0 +1,10 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-04-03 11:39:04
4
+ * @Company: orientsec.com.cn
5
+ * @Description: 常量
6
+ */
7
+ // 设备编号 localstorage key
8
+ export var DEVICE_ID_KEY = "__d2__"; // token localstorage key
9
+
10
+ export var TOKEN_KEY = "__t__";
@@ -0,0 +1,2 @@
1
+ declare const endProductInterceptor: (chain: any) => any;
2
+ export default endProductInterceptor;
@@ -0,0 +1,81 @@
1
+ var __assign = this && this.__assign || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+
6
+ for (var p in s) {
7
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
8
+ }
9
+ }
10
+
11
+ return t;
12
+ };
13
+
14
+ return __assign.apply(this, arguments);
15
+ };
16
+ /*
17
+ * @Author: shenzhiwei
18
+ * @Date: 2021-03-28 14:19:34
19
+ * @Company: orientsec.com.cn
20
+ * @Description: UI交互上的最终处理 chain
21
+ */
22
+
23
+
24
+ import { removeStorageSync } from "fastman3-dfyjapp-syncstorage";
25
+ import { TOKEN_KEY } from "../constants";
26
+ import { storeToken } from "../utils";
27
+
28
+ var endProductInterceptor = function endProductInterceptor(chain) {
29
+ var requestParams = chain.requestParams;
30
+ if (requestParams === null || requestParams === void 0 ? void 0 : requestParams.isNative) return chain.proceed(requestParams);
31
+ var RESTFUL_PATH_BY_FASTMAN3 = requestParams.RESTFUL_PATH_BY_FASTMAN3;
32
+ return chain.proceed(requestParams).then(function (res) {
33
+ // header, paylaod 设置默认值 {}
34
+ var _a = res.header,
35
+ header = _a === void 0 ? {} : _a,
36
+ _b = res.payload,
37
+ payload = _b === void 0 ? {} : _b;
38
+ var code = payload.code,
39
+ info = payload.info,
40
+ data = payload.data;
41
+ var tempToken = header.token; // token 不暴露给上层
42
+
43
+ header.token = "***"; // restful 接口==>自定义字段RESTFUL_PATH_BY_FASTMAN3存在时重新赋值赋值
44
+ // 解决返回数据结构不一致
45
+
46
+ if (RESTFUL_PATH_BY_FASTMAN3) {
47
+ data = res;
48
+ code = res.data.code;
49
+ info = res.data.restfulInfo;
50
+ } // 是否服务端会话过期
51
+
52
+
53
+ var isServerSessionExpired = code >= 3000 && code <= 3019 || code === 3030 || code === 10026 || code === 5200;
54
+
55
+ if (code === 0) {
56
+ // 本地存储或覆盖token
57
+ storeToken(tempToken);
58
+ return __assign({
59
+ header: header
60
+ }, data);
61
+ } else if (code >= 1000 && code <= 1999) {
62
+ return Promise.reject({
63
+ code: 1000,
64
+ info: "\u670D\u52A1\u5668\u53D1\u751F\u9519\u8BEF(" + code + ")"
65
+ });
66
+ } else if (isServerSessionExpired) {
67
+ removeStorageSync(TOKEN_KEY);
68
+ return Promise.reject({
69
+ code: 3000,
70
+ info: "\u672A\u767B\u5F55\u6216\u767B\u5F55\u6001\u5DF2\u5931\u6548(" + code + ")"
71
+ });
72
+ } else {
73
+ return Promise.reject({
74
+ code: code,
75
+ info: info
76
+ });
77
+ }
78
+ });
79
+ };
80
+
81
+ export default endProductInterceptor;
@@ -0,0 +1,2 @@
1
+ declare const httpStatusInterceptor: (chain: any) => any;
2
+ export default httpStatusInterceptor;
@@ -0,0 +1,83 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-03-28 13:58:45
4
+ * @Company: orientsec.com.cn
5
+ * @Description: HttpStatus 处理 chain
6
+ */
7
+
8
+ /**
9
+ * Http 状态码一览表
10
+ */
11
+ var HTTP_STATUS = {
12
+ SUCCESS: 200,
13
+ CREATED: 201,
14
+ ACCEPTED: 202,
15
+ CLIENT_ERROR: 400,
16
+ AUTHENTICATE: 401,
17
+ FORBIDDEN: 403,
18
+ NOT_FOUND: 404,
19
+ SERVER_ERROR: 500,
20
+ BAD_GATEWAY: 502,
21
+ SERVICE_UNAVAILABLE: 503,
22
+ GATEWAY_TIMEOUT: 504
23
+ };
24
+
25
+ var httpStatusInterceptor = function httpStatusInterceptor(chain) {
26
+ var requestParams = chain.requestParams;
27
+ if (requestParams === null || requestParams === void 0 ? void 0 : requestParams.isNative) return chain.proceed(requestParams);
28
+ var RESTFUL_PATH_BY_FASTMAN3 = requestParams.RESTFUL_PATH_BY_FASTMAN3;
29
+ return chain.proceed(requestParams).then(function (res) {
30
+ if (res.statusCode === HTTP_STATUS.SUCCESS) {
31
+ // restful接口=>自定义字段存在时 直接返回 res
32
+ return RESTFUL_PATH_BY_FASTMAN3 ? res : res.data;
33
+ } else if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
34
+ return Promise.reject({
35
+ code: res.statusCode,
36
+ info: "请求资源不存在(404)"
37
+ });
38
+ } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
39
+ return Promise.reject({
40
+ code: res.statusCode,
41
+ info: "服务端出现了问题(502)"
42
+ });
43
+ } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
44
+ return Promise.reject({
45
+ code: res.statusCode,
46
+ info: "没有权限访问(403)"
47
+ });
48
+ } else if (res.statusCode === HTTP_STATUS.AUTHENTICATE) {
49
+ return Promise.reject({
50
+ code: res.statusCode,
51
+ info: "需要鉴权(401)"
52
+ });
53
+ } else if (res.statusCode === HTTP_STATUS.SERVER_ERROR) {
54
+ return Promise.reject({
55
+ code: res.statusCode,
56
+ info: "服务端发生错误(500)"
57
+ });
58
+ } else if (res.statusCode === HTTP_STATUS.SERVICE_UNAVAILABLE) {
59
+ return Promise.reject({
60
+ code: res.statusCode,
61
+ info: "服务不可用(503)"
62
+ });
63
+ } else if (res.statusCode === HTTP_STATUS.GATEWAY_TIMEOUT) {
64
+ return Promise.reject({
65
+ code: res.statusCode,
66
+ info: "网关超时(504)"
67
+ });
68
+ } else if (res.statusCode === HTTP_STATUS.CLIENT_ERROR) {
69
+ return Promise.reject({
70
+ code: res.statusCode,
71
+ info: "请求无效(400)"
72
+ });
73
+ } else {
74
+ // 其它未能捕获的http异常
75
+ return Promise.reject({
76
+ code: res.statusCode,
77
+ info: "Http\u53D1\u751F\u5176\u5B83\u5F02\u5E38(" + res.statusCode + ")"
78
+ });
79
+ }
80
+ });
81
+ };
82
+
83
+ export default httpStatusInterceptor;
@@ -0,0 +1,12 @@
1
+ import transformInterceptor from "./transformInterceptor";
2
+ import securityInterceptor from "./securityInterceptor";
3
+ import httpStatusInterceptor from "./httpStatusInterceptor";
4
+ import endProductInterceptor from "./endProductInterceptor";
5
+ import logInterceptor from "./logInterceptor";
6
+ import { timeoutInterceptor } from "./timeoutInterceptor";
7
+ /**
8
+ * 遵循洋葱模型规则:先进后出,请开发者注意数组中拦截器的注册顺序
9
+ */
10
+ export { endProductInterceptor, transformInterceptor, // 先 request 后 response
11
+ logInterceptor, securityInterceptor, // 后 reuqest 先 response
12
+ httpStatusInterceptor, timeoutInterceptor, };
@@ -0,0 +1,19 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-03-28 10:40:58
4
+ * @Company: orientsec.com.cn
5
+ * @Description: 拦截器导出
6
+ */
7
+ import transformInterceptor from "./transformInterceptor";
8
+ import securityInterceptor from "./securityInterceptor";
9
+ import httpStatusInterceptor from "./httpStatusInterceptor";
10
+ import endProductInterceptor from "./endProductInterceptor";
11
+ import logInterceptor from "./logInterceptor";
12
+ import { timeoutInterceptor } from "./timeoutInterceptor";
13
+ /**
14
+ * 遵循洋葱模型规则:先进后出,请开发者注意数组中拦截器的注册顺序
15
+ */
16
+
17
+ export { endProductInterceptor, transformInterceptor // 先 request 后 response
18
+ , logInterceptor, securityInterceptor // 后 reuqest 先 response
19
+ , httpStatusInterceptor, timeoutInterceptor };
@@ -0,0 +1 @@
1
+ export default function logInterceptor(chain: any): any;
@@ -0,0 +1,24 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-04-02 09:06:04
4
+ * @Company: orientsec.com.cn
5
+ * @Description: 日志拦截器
6
+ */
7
+ export default function logInterceptor(chain) {
8
+ var requestParams = chain.requestParams;
9
+ var method = requestParams.method,
10
+ data = requestParams.data,
11
+ url = requestParams.url,
12
+ FUNCNO_POWER_BY_FASTMAN3 = requestParams.FUNCNO_POWER_BY_FASTMAN3,
13
+ RESTFUL_PATH_BY_FASTMAN3 = requestParams.RESTFUL_PATH_BY_FASTMAN3; // eslint-disable-next-line no-console
14
+
15
+ console.log("http " + (method || "GET") + " --> " + url + " data[" + (FUNCNO_POWER_BY_FASTMAN3 || RESTFUL_PATH_BY_FASTMAN3) + "]: ", data);
16
+ var p = chain.proceed(requestParams);
17
+ var res = p.then(function (res) {
18
+ // eslint-disable-next-line no-console
19
+ console.log("http <-- " + url + " result[" + FUNCNO_POWER_BY_FASTMAN3 + "]:", JSON.stringify(res));
20
+ return res;
21
+ });
22
+ if (typeof p.abort === "function") res.abort = p.abort;
23
+ return res;
24
+ }
@@ -0,0 +1,2 @@
1
+ declare const securityInterceptor: (chain: any) => any;
2
+ export default securityInterceptor;
@@ -0,0 +1,78 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
+
3
+ /*
4
+ * @Author: shenzhiwei
5
+ * @Date: 2021-03-28 10:35:23
6
+ * @Company: orientsec.com.cn
7
+ * @Description: 加解密处理 chain
8
+ */
9
+ import { rsa, hex, aes, ecb, pkcs7, md5, utf8 } from "fastman3-common-crypto";
10
+ import { guid } from "../utils"; // 从配置文件中读取
11
+
12
+ var appSecret = process.env.APP_SECRET;
13
+
14
+ var securityInterceptor = function securityInterceptor(chain) {
15
+ var requestParams = chain.requestParams;
16
+ if (requestParams === null || requestParams === void 0 ? void 0 : requestParams.isNative) return chain.proceed(requestParams);
17
+ var data = requestParams.data,
18
+ RESTFUL_PATH_BY_FASTMAN3 = requestParams.RESTFUL_PATH_BY_FASTMAN3,
19
+ method = requestParams.method;
20
+ console.log('当前数据 RESTFUL_PATH_BY_FASTMAN3 ', RESTFUL_PATH_BY_FASTMAN3); // 1.生成key
21
+ // 保证每次请求随机生成128Bit字节,可使用guid的原理来生成不重复字符串
22
+
23
+ var rc4Key = guid();
24
+ var rc4KeyEncrypt = rsa.encrypt(rc4Key); // 2.生成content
25
+
26
+ var rc4keyHex = hex.parse(rc4Key);
27
+ var aesResult = aes.encrypt(data, rc4keyHex, {
28
+ mode: ecb,
29
+ padding: pkcs7
30
+ });
31
+ var contentEncrypt = aesResult.toString(); // 3.生成sign
32
+
33
+ var signEncrypt = md5(contentEncrypt + appSecret); // 加密后消息体
34
+
35
+ var e = {
36
+ encrypt: 2,
37
+ key: rc4KeyEncrypt,
38
+ content: contentEncrypt,
39
+ sign: signEncrypt.toString()
40
+ }; // restful接口,不需要加解密==>当类型为GET请求时,且body为null
41
+
42
+ if (RESTFUL_PATH_BY_FASTMAN3) {
43
+ if (method == "GET") {
44
+ requestParams.data = null;
45
+ }
46
+ } else {
47
+ requestParams.data = JSON.stringify(e);
48
+ }
49
+
50
+ return chain.proceed(requestParams).then(function (res) {
51
+ var responseEntity = res && _typeof(res) === "object" ? res : JSON.parse(res);
52
+
53
+ if (RESTFUL_PATH_BY_FASTMAN3) {
54
+ // restful接口 不需要加解密,直接返回结果
55
+ return responseEntity;
56
+ } else {
57
+ // 进行验签
58
+ var mySign = md5(responseEntity.content + appSecret).toString();
59
+
60
+ if (mySign !== responseEntity.sign) {
61
+ return Promise.reject({
62
+ code: -1,
63
+ info: "签名验签失败222"
64
+ });
65
+ } // 签名通过后对content进行解密
66
+
67
+
68
+ var aesResult_1 = aes.decrypt(responseEntity.content, rc4keyHex, {
69
+ mode: ecb,
70
+ padding: pkcs7
71
+ });
72
+ var plaintext = aesResult_1.toString(utf8);
73
+ return JSON.parse(plaintext);
74
+ }
75
+ });
76
+ };
77
+
78
+ export default securityInterceptor;
@@ -0,0 +1 @@
1
+ export declare function timeoutInterceptor(chain: any): Promise<unknown>;
@@ -0,0 +1,29 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-04-03 13:55:14
4
+ * @Company: orientsec.com.cn
5
+ * @Description: 超时连接器
6
+ */
7
+ export function timeoutInterceptor(chain) {
8
+ var requestParams = chain.requestParams;
9
+ var p;
10
+ var res = new Promise(function (resolve, reject) {
11
+ var timeout = setTimeout(function () {
12
+ // @ts-ignore
13
+ timeout = null;
14
+ reject(new Error("网络链接超时,请稍后再试!"));
15
+ }, requestParams && requestParams.timeout || 60000);
16
+ p = chain.proceed(requestParams);
17
+ p.then(function (res) {
18
+ if (!timeout) return;
19
+ clearTimeout(timeout);
20
+ resolve(res);
21
+ })["catch"](function (err) {
22
+ timeout && clearTimeout(timeout);
23
+ reject(err);
24
+ });
25
+ }); // @ts-ignore
26
+
27
+ if (p !== undefined && typeof p.abort === "function") res.abort = p.abort;
28
+ return res;
29
+ }
@@ -0,0 +1,2 @@
1
+ declare const transformInterceptor: (chain: any) => any;
2
+ export default transformInterceptor;
@@ -0,0 +1,106 @@
1
+ var __assign = this && this.__assign || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+
6
+ for (var p in s) {
7
+ if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
8
+ }
9
+ }
10
+
11
+ return t;
12
+ };
13
+
14
+ return __assign.apply(this, arguments);
15
+ };
16
+ /*
17
+ * @Author: shenzhiwei
18
+ * @Date: 2021-03-28 10:35:05
19
+ * @Company: orientsec.com.cn
20
+ * @Description: 消息结构处理 chain
21
+ */
22
+
23
+
24
+ import { AppAuthorize } from "fastman3-dfyjapp-jsbridge";
25
+ import { getVersion, guid } from "../utils";
26
+ import { isFromApp, isFromWeiXin } from "fastman3-common-helper";
27
+ import { setStorageSync, getStorageSync } from "fastman3-dfyjapp-syncstorage";
28
+ import { DEVICE_ID_KEY, TOKEN_KEY } from "../constants";
29
+
30
+ var transformInterceptor = function transformInterceptor(chain) {
31
+ var requestParams = chain.requestParams;
32
+ if (requestParams === null || requestParams === void 0 ? void 0 : requestParams.isNative) return chain.proceed(requestParams);
33
+ var FUNCNO_POWER_BY_FASTMAN3 = requestParams.FUNCNO_POWER_BY_FASTMAN3;
34
+ var body = {
35
+ header: {
36
+ appId: process.env.APP_ID,
37
+ ver: getVersion() || "1.0",
38
+ channel: process.env.CHANNEL || "1",
39
+ funcNo: FUNCNO_POWER_BY_FASTMAN3,
40
+ timestamp: new Date().getTime(),
41
+ funcVer: process.env.FUNC_VER || "6"
42
+ },
43
+ payload: __assign({}, requestParams.data),
44
+ sign: ""
45
+ }; // 2020-12-28:新增 refreshToken 方案,替换原 authSign
46
+
47
+ if (!!AppAuthorize.newAuthSign) {
48
+ body.header["auth"] = AppAuthorize.newAuthSign;
49
+ } // 微信授权机制 //
50
+
51
+
52
+ var tokenStorage = getStorageSync(TOKEN_KEY);
53
+
54
+ if (!!tokenStorage) {
55
+ body.header["token"] = tokenStorage;
56
+ } // 非APP增加deviceId属性,APP则使用vtDeviceId属性。
57
+
58
+
59
+ if (!isFromApp()) {
60
+ // 增加opstation属性
61
+ body.header["opStation"] = "33333333333"; // 微信接入时opStation设置为微信的OpenID
62
+
63
+ if (isFromWeiXin()) {
64
+ if (AppAuthorize.openId) {
65
+ body.header["opStation"] = AppAuthorize.openId;
66
+ }
67
+ } // 增加deviceid机制
68
+
69
+
70
+ var d = getStorageSync(DEVICE_ID_KEY);
71
+
72
+ if (!!d) {
73
+ body.header["deviceId"] = d + "";
74
+ } else {
75
+ //旧版本指纹,生成的指纹较短
76
+ var deviceId = guid();
77
+
78
+ if (!!(deviceId != null && (!!deviceId.length ? true : deviceId.length > 0))) {
79
+ setStorageSync(DEVICE_ID_KEY, deviceId);
80
+ body.header["deviceId"] = deviceId;
81
+ }
82
+ }
83
+ } else {
84
+ if (AppAuthorize.vtDeviceId) {
85
+ body.header["vtDeviceId"] = AppAuthorize.vtDeviceId;
86
+ }
87
+ }
88
+
89
+ requestParams.data = JSON.stringify(body);
90
+ return chain.proceed(requestParams).then(function (res) {
91
+ var _a; // 解析服务端时间戳
92
+
93
+
94
+ if ((_a = res === null || res === void 0 ? void 0 : res.header) === null || _a === void 0 ? void 0 : _a.timestamp) {
95
+ var sysdate = new Date(parseInt(res.header.timestamp));
96
+ res.header.sysdate = sysdate;
97
+ res.header.sysyear = sysdate.getFullYear();
98
+ res.header.sysmonth = sysdate.getMonth() + 1;
99
+ res.header.sysday = sysdate.getDate();
100
+ }
101
+
102
+ return res;
103
+ });
104
+ };
105
+
106
+ export default transformInterceptor;
@@ -0,0 +1,43 @@
1
+ import Taro from "@tarojs/taro";
2
+ import { storeToken, storeDeviceId } from "./utils";
3
+ declare type PromiseSuccessCR1 = Omit<Taro.request.SuccessCallbackResult, "data">;
4
+ declare type PromiseSuccessCR2 = Omit<PromiseSuccessCR1, "errMsg">;
5
+ declare type PromiseSuccessCR = Omit<PromiseSuccessCR2, "statusCode">;
6
+ declare type Params = {
7
+ method: "GET" | "OPTIONS" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT" | undefined;
8
+ };
9
+ declare class fetch {
10
+ baseOptions<T>(params: any, method?: Params["method"]): Promise<T & PromiseSuccessCR>;
11
+ /**
12
+ * 发起中台POST请求
13
+ * @param funcNo 接口功能号,通常由 IFXXXXXX 格式命名
14
+ * @param data 数据,对象类型
15
+ * @param url
16
+ */
17
+ post<T = any>(funcNo: string, data?: {
18
+ [key: string]: any;
19
+ }): Promise<T & PromiseSuccessCR>;
20
+ /**
21
+ * 发起中台POST restful请求
22
+ * @param restfulPath restful接口路径,通常由/xxx/xxx...
23
+ * @param data 数据,对象类型
24
+ */
25
+ postRest<T = any>(restfulPath: string, data?: {
26
+ [key: string]: any;
27
+ }): Promise<T & PromiseSuccessCR>;
28
+ /**
29
+ * 发起中台GET restful请求
30
+ * @param restfulPath restful接口路径,通常由/xxx/xxx...
31
+ * @param data 数据,对象类型
32
+ */
33
+ getRest<T = any>(restfulPath: string, data?: {
34
+ [key: string]: any;
35
+ }): Promise<T & PromiseSuccessCR>;
36
+ /**
37
+ * 纯净版request
38
+ */
39
+ requestNative(params: any): Taro.RequestTask<any>;
40
+ }
41
+ declare const _default: fetch;
42
+ export default _default;
43
+ export { storeToken, storeDeviceId, };
package/es/request.js ADDED
@@ -0,0 +1,152 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-03-28 10:18:00
4
+ * @Company: orientsec.com.cn
5
+ * @Description: 基于 taro-request 进行简单封装
6
+ */
7
+ import { addInterceptor, request } from "@tarojs/taro"; // import interceptors from "./interceptors";
8
+
9
+ import { endProductInterceptor, transformInterceptor, logInterceptor, securityInterceptor, httpStatusInterceptor, timeoutInterceptor } from "./interceptors";
10
+ import { isFromApp } from "fastman3-common-helper";
11
+ import { storeToken, storeDeviceId, createRestfulGetPath } from "./utils";
12
+ addInterceptor(endProductInterceptor);
13
+ addInterceptor(transformInterceptor);
14
+ addInterceptor(logInterceptor);
15
+
16
+ if (process.env.APP_ENV !== "mock") {
17
+ addInterceptor(securityInterceptor);
18
+ }
19
+
20
+ addInterceptor(httpStatusInterceptor);
21
+ addInterceptor(timeoutInterceptor);
22
+
23
+ var fetch =
24
+ /** @class */
25
+ function () {
26
+ function fetch() {}
27
+
28
+ fetch.prototype.baseOptions = function (params, method) {
29
+ if (method === void 0) {
30
+ method = "GET";
31
+ }
32
+
33
+ var data = params.data,
34
+ FUNCNO_POWER_BY_FASTMAN3 = params.FUNCNO_POWER_BY_FASTMAN3,
35
+ RESTFUL_PATH_BY_FASTMAN3 = params.RESTFUL_PATH_BY_FASTMAN3; // APP网关完整地址
36
+
37
+ var url;
38
+ /**
39
+ * 区分 APP网关地址
40
+ * process.env.APP_ENV - 环境变量;
41
+ * RESTFUL_PATH_BY_FASTMAN3 - restful类型接口路径
42
+ */
43
+
44
+ if (process.env.APP_ENV === "mock" && !RESTFUL_PATH_BY_FASTMAN3) {
45
+ url = process.env.GATEWAY_URL + "/" + FUNCNO_POWER_BY_FASTMAN3;
46
+ } else if (RESTFUL_PATH_BY_FASTMAN3) {
47
+ url = process.env.GATEWAY_RESTFUL_URL + RESTFUL_PATH_BY_FASTMAN3;
48
+ } else {
49
+ url = process.env.GATEWAY_URL;
50
+ }
51
+
52
+ var option = {
53
+ url: url,
54
+ data: data,
55
+ method: method,
56
+ header: {
57
+ "Content-Type": "application/json"
58
+ },
59
+ // 是否携带 cookie
60
+ credentials: !isFromApp() ? "omit" : /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(location.hostname) || location.hostname === "localhost" ? "omit" : "include",
61
+ FUNCNO_POWER_BY_FASTMAN3: FUNCNO_POWER_BY_FASTMAN3,
62
+ RESTFUL_PATH_BY_FASTMAN3: RESTFUL_PATH_BY_FASTMAN3,
63
+ mode: "cors",
64
+ timeout: process.env.TIME_OUT || 15000
65
+ };
66
+ return new Promise(function (resolve, reject) {
67
+ // @ts-ignore
68
+ request(option).then(function (res) {
69
+ resolve(res);
70
+ })["catch"](function (err) {
71
+ // 针对 taro request 内部封装的特殊逻辑处理,一般url不存在或跨域会触发
72
+ if (err.message === "Failed to fetch") {
73
+ reject({
74
+ code: -1,
75
+ info: "请求失败"
76
+ });
77
+ } // 网络请求超时
78
+ else if (err.message === "网络链接超时,请稍后再试!") {
79
+ reject({
80
+ code: -2,
81
+ info: err.message
82
+ });
83
+ } // 特殊网络容错处理
84
+ else if (err.status && err.statusText) {
85
+ reject({
86
+ code: err.status,
87
+ info: err.statusText
88
+ });
89
+ } else {
90
+ reject(err);
91
+ }
92
+ });
93
+ });
94
+ };
95
+ /**
96
+ * 发起中台POST请求
97
+ * @param funcNo 接口功能号,通常由 IFXXXXXX 格式命名
98
+ * @param data 数据,对象类型
99
+ * @param url
100
+ */
101
+
102
+
103
+ fetch.prototype.post = function (funcNo, data) {
104
+ var params = {
105
+ FUNCNO_POWER_BY_FASTMAN3: funcNo,
106
+ data: data
107
+ };
108
+ return this.baseOptions(params, "POST");
109
+ };
110
+ /**
111
+ * 发起中台POST restful请求
112
+ * @param restfulPath restful接口路径,通常由/xxx/xxx...
113
+ * @param data 数据,对象类型
114
+ */
115
+
116
+
117
+ fetch.prototype.postRest = function (restfulPath, data) {
118
+ var params = {
119
+ RESTFUL_PATH_BY_FASTMAN3: restfulPath,
120
+ data: data
121
+ };
122
+ return this.baseOptions(params, "POST");
123
+ };
124
+ /**
125
+ * 发起中台GET restful请求
126
+ * @param restfulPath restful接口路径,通常由/xxx/xxx...
127
+ * @param data 数据,对象类型
128
+ */
129
+
130
+
131
+ fetch.prototype.getRest = function (restfulPath, data) {
132
+ var params = {
133
+ RESTFUL_PATH_BY_FASTMAN3: createRestfulGetPath(restfulPath, data)
134
+ };
135
+ return this.baseOptions(params, "GET");
136
+ };
137
+ /**
138
+ * 纯净版request
139
+ */
140
+
141
+
142
+ fetch.prototype.requestNative = function (params) {
143
+ return request(Object.assign({
144
+ isNative: true
145
+ }, params));
146
+ };
147
+
148
+ return fetch;
149
+ }();
150
+
151
+ export default new fetch();
152
+ export { storeToken, storeDeviceId };
package/es/utils.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * 获取APP版本号
3
+ */
4
+ export declare const getVersion: () => any;
5
+ /**
6
+ * rc4Key
7
+ */
8
+ export declare const guid: () => string;
9
+ /**
10
+ * 在作用域中存储token
11
+ * @param token 授权凭证
12
+ */
13
+ export declare const storeToken: (token: string) => void;
14
+ /**
15
+ * 在作用域中存储deviceId
16
+ * @param token 授权凭证
17
+ */
18
+ export declare const storeDeviceId: (deviceId: string) => void;
19
+ /**
20
+ * 拼接path,组合成一个完整的restful-get请求路径
21
+ * @param path 路径path /xxx/xx
22
+ * @param param 参数 {a:1,...}
23
+ * @returns
24
+ */
25
+ export declare const createRestfulGetPath: (path: string, param: any) => any;
package/es/utils.js ADDED
@@ -0,0 +1,75 @@
1
+ /*
2
+ * @Author: shenzhiwei
3
+ * @Date: 2021-03-28 11:32:10
4
+ * @Company: orientsec.com.cn
5
+ * @Description: 工具包,计划移入 packages 中统一管理,每一个迭代升版一次
6
+ */
7
+ import Taro from "@tarojs/taro";
8
+ import { setStorageSync, getStorageSync } from "fastman3-dfyjapp-syncstorage";
9
+ import { TOKEN_KEY, DEVICE_ID_KEY } from "./constants";
10
+ /**
11
+ * 获取APP版本号
12
+ */
13
+
14
+ export var getVersion = function getVersion() {
15
+ var groups = Taro.getEnv() !== Taro.ENV_TYPE.WEB || navigator.userAgent.toLowerCase().match(/DFYJ\/([\d.]+)/i);
16
+
17
+ if (!groups) {
18
+ return undefined;
19
+ } else {
20
+ return groups[1];
21
+ }
22
+ };
23
+ /**
24
+ * rc4Key
25
+ */
26
+
27
+ export var guid = function guid() {
28
+ return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function (c) {
29
+ var r = Math.random() * 16 | 0,
30
+ v = c == "x" ? r : r & 0x3 | 0x8;
31
+ return v.toString(16);
32
+ });
33
+ };
34
+ /**
35
+ * 在作用域中存储token
36
+ * @param token 授权凭证
37
+ */
38
+
39
+ export var storeToken = function storeToken(token) {
40
+ var tokenPrevious = getStorageSync(TOKEN_KEY);
41
+
42
+ if (tokenPrevious !== token) {
43
+ setStorageSync(TOKEN_KEY, token);
44
+ }
45
+ };
46
+ /**
47
+ * 在作用域中存储deviceId
48
+ * @param token 授权凭证
49
+ */
50
+
51
+ export var storeDeviceId = function storeDeviceId(deviceId) {
52
+ var deviceIdPrevious = getStorageSync(DEVICE_ID_KEY);
53
+
54
+ if (deviceIdPrevious !== deviceId) {
55
+ setStorageSync(DEVICE_ID_KEY, deviceId);
56
+ }
57
+ };
58
+ /**
59
+ * 拼接path,组合成一个完整的restful-get请求路径
60
+ * @param path 路径path /xxx/xx
61
+ * @param param 参数 {a:1,...}
62
+ * @returns
63
+ */
64
+
65
+ export var createRestfulGetPath = function createRestfulGetPath(path, param) {
66
+ var pathLink;
67
+
68
+ for (var k in param) {
69
+ var value = param[k] !== undefined ? param[k] : '';
70
+ pathLink += "&" + k + "=" + encodeURIComponent(value);
71
+ }
72
+
73
+ pathLink = path + "?" + (pathLink === null || pathLink === void 0 ? void 0 : pathLink.substr(1));
74
+ return pathLink.replace(' ', '');
75
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "fastman3-dfyjapp-request",
3
+ "version": "1.0.4",
4
+ "description": "a network request for dfyj app",
5
+ "main": "es/request.js",
6
+ "scripts": {
7
+ "build": "father-build"
8
+ },
9
+ "keywords": [
10
+ "http",
11
+ "fetch"
12
+ ],
13
+ "engines": {
14
+ "node": ">=10"
15
+ },
16
+ "author": "shenzhiwei",
17
+ "license": "ISC",
18
+ "peerDependencies": {
19
+ "@tarojs/taro": "^3.2.0",
20
+ "fastman3-common-crypto": "^1.0.0",
21
+ "fastman3-common-helper": "^1.0.0",
22
+ "fastman3-dfyjapp-jsbridge": "^1.0.0",
23
+ "fastman3-dfyjapp-syncstorage": "^1.0.0"
24
+ },
25
+ "dependencies": {
26
+ },
27
+ "devDependencies": {
28
+ "father-build": "1.19.4"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "es",
33
+ "lib"
34
+ ],
35
+ "sideEffects": false
36
+ }