xctc-utils 1.6.0 → 1.6.2

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 CHANGED
@@ -4,44 +4,47 @@
4
4
  #### 更新到最新版本 npm install xctc-utils@latest
5
5
  #### 项目中引入 import useUtils from "xctc-utils"
6
6
 
7
-
7
+ ### 缓存操作集合
8
8
  #### LocalStorage使用,存取值时数据已经过处理
9
9
  ```
10
10
  存储: useUtils.useLocalStorage(key,value)
11
11
  取值: useUtils.getLocalStorage(key)
12
12
  ```
13
-
14
13
  #### sessionStorage使用,存取值时数据已经过处理
15
14
  ```
16
-
17
15
  存储: useUtils.useSessionStorage(key,value)
18
16
  取值: useUtils.getSessionStorage(key)
19
17
 
18
+ ```
19
+ #### 删除缓存
20
+ ```
21
+ key:string 需要删除的缓存键
22
+ all:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
23
+ 如果 all 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
24
+ 删除临时缓存: useUtils.removeSessionStorage(key,all)
25
+ 删除永久缓存: useUtils.removeLocalStorage(key,all)
26
+
20
27
  ```
21
28
 
29
+ ### 剪贴板操作集合
22
30
  #### 将数据复制到剪贴板函数
23
31
  ```
24
32
  useUtils.handleCopyValue(val)
25
33
  ```
26
- #### 删除缓存
27
- ```
28
- key:string 需要删除的缓存键
29
- isAll:boolean 为true时删除调用方法的所有缓存,否则只删除当前传入的 key 值
30
- 如果 isAll 为 true 时,方法会优先执行缓存的clear函数, key 传任意值,
31
- 删除临时缓存: useUtils.removeSessionStorage(key,isAll)
32
- 删除永久缓存: useUtils.removeLocalStorage(key,isAll)
33
34
 
34
- ```
35
+ ### AES 加密、解密处理函数集合
35
36
  #### AES 加密、解密,同一个数据的加密和解密传入的key 和 iv保持一致。
36
37
  ```
37
38
  work: 需要加密的对象,如传入的是对象,该方法默认进行JSON序列化处理。
38
39
  key:16位或者32位字符串作为密钥
39
40
  iv:16位或者32位字符串作为密钥偏移量
40
41
  data: encrypt方法加密后返回的数据
41
- 加密:useUtils.encrypt( work:any , key:string , iv:string )
42
- 解密:useUtils.decrypt( data:string , key:string , iv:string )
42
+ 加密:useUtils.crypto.encrypt( work:any , key:string , iv:string )
43
+ 解密:useUtils.crypto.decrypt( data:string , key:string , iv:string )
43
44
  ```
44
- #### 类型判断
45
+
46
+ ### 数据类型判断集合
47
+ #### 数据类型判断
45
48
  ```
46
49
  useUtils.type.isFunction(val) // 是否是函数
47
50
  useUtils.type.isObject(val) // 是否是对象 null 类型为对象,该方法已对null进行过滤
@@ -52,34 +55,22 @@ useUtils.type.isBoolean(val) // 是否是boolean类型
52
55
  useUtils.type.isWindow(val) // 是否在浏览器环境下运行
53
56
 
54
57
  ```
55
- #### 获取当前设备环境
56
- 设备环境:
58
+ #### json数据类型判断
59
+ ```
60
+ useUtils.isJson(data) // 判断传入数据是否是 JSON对象字符串,如果是,返回序列化后的JSON对象,否则返回false
57
61
  ```
58
62
 
59
- 当前使用设备类型: useUtils.deviceEnvironment() // android ios
63
+ ### 环境判断
64
+ #### 获取当前设备环境:设备环境
65
+ ```
66
+ 当前使用设备类型: useUtils.deviceEnvironment() // android ios ,未识别到返回 other
60
67
  是否在微信浏览器环境中: useUtils.weixinBrowser() // true false
61
68
  是否是手持设备: useUtils.isMobile() // true 移动设备 false PC设备
62
69
 
63
70
  ```
64
71
 
65
-
66
- #### crypto-js 加密解密方法
67
- ```
68
- word: 需要加密的数据,数据可是字符串,对象等
69
- key: 加密 密钥
70
- iv: 密钥偏移量
71
- data:encrypt方法加密后返回的数据
72
- 对同一个数据进行加密、解密时,encrypt和decrypt的iv和key保持一致
73
- 对word进行CryptoJS.enc.Utf8.parse转义
74
- CryptoJS.AES.encrypt加密,加密模式:CBC ; padding: CryptoJS.pad.Pkcs7
75
- 对数据进行加密
76
- useUtils.encrypt(word:any,key:string,iv:string)
77
- // 对数据进行解密
78
- useUtils.decrypt(data:string,key:string,iv:string)
79
-
80
- ```
81
- #### 微信H5使用方法集合
82
- ##### 微信授权登录
72
+ ### 微信授权等方法集合
73
+ #### 微信授权登录
83
74
  ```
84
75
  interface configOption {
85
76
  appId?:string, // 当前微信服务号 appId
@@ -91,7 +82,7 @@ interface configOption {
91
82
  cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
92
83
  }
93
84
  进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
94
- useUtils.weixinUrlCode(config)
85
+ useUtils.weixin.getUrlCode(config)
95
86
 
96
87
  ```
97
88
  #### 微信config接口权限注入
@@ -103,12 +94,11 @@ interface ShareConfig{
103
94
  jsApiList?:string[],
104
95
  }
105
96
 
106
- useUtils.weixinShareConfig(config:ShareConfig)
97
+ useUtils.weixin.configReady(config:ShareConfig)
107
98
  ```
108
-
109
99
  #### 微信分享接口加载
110
100
  ```
111
- interface Share{
101
+ interface ShareOptions{
112
102
  title?:string, // 分享标题 默认值 微信分享
113
103
  desc?:string, // 分享描述 默认值 微信分享描述
114
104
  link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致,默认取当前访问地址
@@ -117,15 +107,25 @@ interface Share{
117
107
  iv?:string, // 分享链接中对 state 数据加密的iv 必须与 weixinUrlCode 方法中的 cryptoiv 参数一致
118
108
  key?:string, // 分享链接中对 state 数据加密的 key 必须与 weixinUrlCode 方法中的 cryptokey 参数一致
119
109
  }
120
- useUtils.weixinShareInit(config:Share)
110
+ useUtils.weixin.shareReady(config:ShareOptions)
111
+ ```
112
+ #### 唤起微信支付
113
+ ```
114
+ interface ConfigPay{
115
+ timeStamp:string, // 支付签名时间戳
116
+ nonceStr:string, // 支付签名随机串,不长于 32 位
117
+ package:string, // 统一支付接口返回的prepay_id参数值
118
+ signType:string, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
119
+ paySign:string,// 支付签名
120
+ cb?:any,//回调
121
+ }
122
+ useUtils.weixin.onlinePay(config:ConfigPay)
121
123
  ```
122
124
 
123
125
  ### 时间相关方法
124
126
  #### 获取当前时间的时间戳
125
127
  ```
126
-
127
- useUtils.getTime() // 返回秒
128
-
128
+ useUtils.date.getTime() // 返回秒
129
129
  ```
130
130
  #### 时间戳转任意格式时间字符串
131
131
  ```
@@ -135,7 +135,7 @@ useUtils.getTime() // 返回秒
135
135
  * @param format 解析后展示时间字符串格式,默认 "YYYY-MM-DD HH:mm:ss" 格式, 可传格式如: YYYY年MM月DD日 HH时 YYYY/MM/DD HH
136
136
  * @returns
137
137
  */
138
- useUtils.formatTimeStamp(num,format) // 返回 format 格式时间戳
138
+ useUtils.date.formatTimeStamp(num,format) // 返回 format 格式时间戳
139
139
 
140
140
  ```
141
141
  #### 时间字符串转时间戳
@@ -145,6 +145,44 @@ useUtils.formatTimeStamp(num,format) // 返回 format 格式时间戳
145
145
  * @param str 字符串格式必须为: 2017/03/03 12:23:55 格式,避免兼容性,不能传 “-” 分割的时间字符串 2017-03-03
146
146
  * @returns
147
147
  */
148
- useUtils.formatStrTime(str) // 返回时间戳 秒
148
+ useUtils.date.formatStrTime(str) // 返回时间戳 秒
149
+
150
+ ```
151
+
152
+ ### DOM处理集合
153
+ #### 获取ID元素
154
+ ```
155
+ useUtils.$id("domid") // 如果存在当前dom元素,则返回dom对象,否则返回false
156
+ ```
157
+ ### ueditor 富文本操作函数集合
158
+ #### 计算当前富文本内容高度
159
+ ```
160
+ var option = {
161
+ id:"ueditor" , // 富文本DOM容器 id
162
+ content:"" , // ueditor 富文本内容
163
+ }
164
+ // 返回的富文本内容包含input dom元素,input元素的content-data-height属性计算富文本高度
165
+ // <input type="hidden" content-data-height="${height}" id="ueditorHeight" />
166
+ useUtils.checkIframeContentHeight(option)
167
+ ```
168
+ #### 获取指定富文本内容高度
169
+ ```
170
+ // 返回富文本元素所占页面高度,默认返回 0
171
+ useUtils.getIframeContentHeight("ueditor")
172
+ ```
173
+ ### 其它处理函数集合
174
+ #### 通过身份证号码的出生年月日,获取出生日期
175
+ ```
176
+ // 传入身份证号码,返回出生具体时间,默认返回空
177
+ 正确返回: {
178
+ year:year,
179
+ month:month,
180
+ day:day,
181
+ hour:hour,
182
+ minute:minute,
183
+ second:second,
184
+ num:nowNumber // 具体时间戳
185
+ }
186
+ useUtils.findIdcardAge(idcard)
187
+ ```
149
188
 
150
- ```
package/app.ts CHANGED
@@ -6,7 +6,6 @@
6
6
  // ts-node app.ts
7
7
 
8
8
  import {formatTimeStamp,formatStrTime} from "./src/time"
9
- import {weixinUrlCode} from "./src/weixin"
10
9
  import {decrypt,encrypt} from "./src/crypto"
11
10
 
12
11
 
package/dist/index.js CHANGED
@@ -39,9 +39,9 @@ var utils = __importStar(require("./utils"));
39
39
  var crypto = __importStar(require("./crypto"));
40
40
  var weixin = __importStar(require("./weixin"));
41
41
  var time = __importStar(require("./time"));
42
- var mobile = __importStar(require("./mobile"));
43
42
  var format = __importStar(require("./format"));
44
43
  var isType = __importStar(require("./is"));
45
44
  var modal = __importStar(require("./modal"));
46
- var obj = __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto), time), mobile), format), { type: __assign({}, isType) }), modal);
45
+ var params = __importStar(require("./params"));
46
+ var obj = __assign(__assign(__assign({}, storage), utils), { weixin: __assign({}, weixin), crypto: __assign({}, crypto), date: __assign({}, time), format: __assign({}, format), type: __assign({}, isType), modal: __assign({}, modal), params: __assign({}, params) });
47
47
  exports.default = obj;
@@ -0,0 +1,15 @@
1
+ interface SetOptions {
2
+ url: string;
3
+ data: string;
4
+ keyword: string;
5
+ key: string;
6
+ iv: string;
7
+ }
8
+ export declare const set: (options: SetOptions) => string | undefined;
9
+ /**
10
+ *
11
+ * @param keys 需要截取地址栏参数键数组值
12
+ * @param cache 是否需要缓存地址栏数据: 不传或传空,则不做处理,session 临时缓存 local永久缓存
13
+ */
14
+ export declare const get: (keys: string[], cache?: string) => void;
15
+ export {};
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.get = exports.set = void 0;
4
+ var crypto_1 = require("../crypto");
5
+ var storage_1 = require("../storage");
6
+ var defaultKey = "5uMz4R8rop26DkC8";
7
+ var defaultIv = "5uMz4Rsd0926DkC8";
8
+ var is_1 = require("../is");
9
+ var win = window;
10
+ var loc = win.location;
11
+ var set = function (options) {
12
+ var _a = options.url, url = _a === void 0 ? "" : _a, _b = options.data, data = _b === void 0 ? "" : _b, _c = options.keyword, keyword = _c === void 0 ? "state" : _c, _d = options.key, key = _d === void 0 ? defaultKey : _d, _e = options.iv, iv = _e === void 0 ? defaultIv : _e;
13
+ if (!data || !url)
14
+ return;
15
+ if (url.length < 10 || !url.includes("http") || !url.includes(".")) {
16
+ console.error("params.set函数必须传入合法的url地址,如:http://dousougou.com");
17
+ return;
18
+ }
19
+ if (!(0, is_1.isString)(data) || !data.includes("=")) {
20
+ console.error("params.set函数data参数必须传入合法的字符串,如id=1&age=22");
21
+ return;
22
+ }
23
+ var val = (0, crypto_1.encrypt)(data, key, iv);
24
+ var urlData = "".concat(url, "?").concat(keyword, "=").concat(val);
25
+ return urlData;
26
+ };
27
+ exports.set = set;
28
+ /**
29
+ *
30
+ * @param keys 需要截取地址栏参数键数组值
31
+ * @param cache 是否需要缓存地址栏数据: 不传或传空,则不做处理,session 临时缓存 local永久缓存
32
+ */
33
+ var get = function (keys, cache) {
34
+ var win = window;
35
+ var loc = win.location;
36
+ var search = loc.search;
37
+ if (search.startsWith("?")) {
38
+ search = search.substring(1);
39
+ }
40
+ var urlArr = search.split("&");
41
+ var paramsData = {};
42
+ for (var k = 0; k < keys.length; k++) {
43
+ var keyItem = keys[k];
44
+ if (keyItem.includes("=")) {
45
+ console.error("传入的参数不能含有=字符,合法格式:['code','state']");
46
+ break;
47
+ }
48
+ var urlItem = "";
49
+ for (var i = 0; k < urlArr.length; i++) {
50
+ if (urlItem === null || urlItem === void 0 ? void 0 : urlItem.includes(keyItem)) {
51
+ urlItem = "";
52
+ break;
53
+ }
54
+ urlItem = urlArr[i];
55
+ if (urlItem === null || urlItem === void 0 ? void 0 : urlItem.includes(keyItem)) {
56
+ var dataStr = urlItem.substring(urlItem.indexOf(keyItem) + keyItem.length + 1);
57
+ var decryptStr = (0, crypto_1.decrypt)(dataStr);
58
+ paramsData[keyItem] = {
59
+ data: getParamsObj(decryptStr),
60
+ str: dataStr
61
+ };
62
+ }
63
+ }
64
+ }
65
+ if (cache == "session") {
66
+ (0, storage_1.useSessionStorage)("urlParamsData", paramsData);
67
+ }
68
+ if (cache == "local") {
69
+ (0, storage_1.useLocalStorage)("urlParamsData", paramsData);
70
+ }
71
+ };
72
+ exports.get = get;
73
+ var getParamsObj = function (objStr) {
74
+ var objData = {};
75
+ if (objStr === null || objStr === void 0 ? void 0 : objStr.includes("=")) {
76
+ var objArr = objStr.split("&");
77
+ for (var k = 0; k < objArr.length; k++) {
78
+ var item = objArr[k];
79
+ if (item.includes("=")) {
80
+ var indexVal = item.indexOf("=");
81
+ objData[item.substring(0, indexVal)] = item.substring(indexVal + 1);
82
+ }
83
+ }
84
+ }
85
+ return objData;
86
+ };
@@ -17,14 +17,14 @@ export declare function getSessionStorage(key: string): any;
17
17
  /**
18
18
  *
19
19
  * @param key 需要删除的key值
20
- * @param isAll 是否删除所有缓存
20
+ * @param all 是否删除所有缓存
21
21
  * @returns
22
22
  */
23
- export declare function removeSessionStorage(key?: string, isAll?: boolean): void;
23
+ export declare function removeSessionStorage(key?: string, all?: boolean): void;
24
24
  /**
25
25
  *
26
26
  * @param key 需要删除的key值
27
- * @param isAll 是否删除所有缓存
27
+ * @param all 是否删除所有缓存
28
28
  * @returns
29
29
  */
30
- export declare function removeLocalStorage(key?: string, isAll?: boolean): void;
30
+ export declare function removeLocalStorage(key?: string, all?: boolean): void;
@@ -78,11 +78,11 @@ function getStorage(key, obj) {
78
78
  /**
79
79
  *
80
80
  * @param key 需要删除的key值
81
- * @param isAll 是否删除所有缓存
81
+ * @param all 是否删除所有缓存
82
82
  * @returns
83
83
  */
84
- function removeSessionStorage(key, isAll) {
85
- if ((0, is_1.isBoolean)(isAll) && isAll) {
84
+ function removeSessionStorage(key, all) {
85
+ if ((0, is_1.isBoolean)(all) && all) {
86
86
  wss.clear();
87
87
  return;
88
88
  }
@@ -94,11 +94,11 @@ exports.removeSessionStorage = removeSessionStorage;
94
94
  /**
95
95
  *
96
96
  * @param key 需要删除的key值
97
- * @param isAll 是否删除所有缓存
97
+ * @param all 是否删除所有缓存
98
98
  * @returns
99
99
  */
100
- function removeLocalStorage(key, isAll) {
101
- if ((0, is_1.isBoolean)(isAll) && isAll) {
100
+ function removeLocalStorage(key, all) {
101
+ if ((0, is_1.isBoolean)(all) && all) {
102
102
  wls.clear();
103
103
  return;
104
104
  }
@@ -35,7 +35,7 @@ export declare function getDateData(value: string | number, format?: string): {
35
35
  * @param idcard 身份证号码
36
36
  * @returns
37
37
  */
38
- export declare function findIdcardAge(idcard: string): void | {
38
+ export declare function findIdcardAge(idcard: string): "" | {
39
39
  year: string | number;
40
40
  month: string | number;
41
41
  day: string | number;
@@ -182,7 +182,8 @@ exports.getDateData = getDateData;
182
182
  */
183
183
  function findIdcardAge(idcard) {
184
184
  if (!idcard || idcard.length < 15) {
185
- return console.error("传入正确的身份证号码");
185
+ console.error("传入正确的身份证号码");
186
+ return "";
186
187
  }
187
188
  // 提取身份证号中的出生日期部分
188
189
  var birthday = idcard.substring(6, 14);
@@ -194,6 +195,6 @@ function findIdcardAge(idcard) {
194
195
  var str = "".concat(year, "-").concat(month, "-").concat(day);
195
196
  return getDateData(str, "YYYY-MM-DD HH:mm:ss");
196
197
  }
197
- return;
198
+ return "";
198
199
  }
199
200
  exports.findIdcardAge = findIdcardAge;
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { UeditorHeightOption } from "./types";
2
+ export declare function isMobile(): boolean;
2
3
  /**
3
4
  * 判断当前传入的字符串是否为 json格式 字符串
4
5
  * @param str
package/dist/utils.js CHANGED
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
3
+ exports.handleCopyValue = exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = exports.isMobile = void 0;
4
+ var win = window;
5
+ // 是否是手机端环境
6
+ function isMobile() {
7
+ var info = win.navigator.userAgent;
8
+ var isPhone = /mobile/i.test(info);
9
+ return isPhone;
10
+ }
11
+ exports.isMobile = isMobile;
4
12
  /**
5
13
  * 判断当前传入的字符串是否为 json格式 字符串
6
14
  * @param str
@@ -9,9 +9,9 @@ interface configOption {
9
9
  isPassLogin?: boolean;
10
10
  redirectUri?: string;
11
11
  }
12
- export declare function weixinUrlCode(config: configOption): void;
13
- export declare function weixinEnvSDK(): any;
14
- export declare function weixinCode(appId?: string, scope?: string): void;
12
+ export declare function getUrlCode(config: configOption): void;
13
+ export declare function envSDK(): any;
14
+ export declare function getCode(appId?: string, scope?: string): void;
15
15
  interface ShareConfig {
16
16
  http?: any;
17
17
  cb?: any;
@@ -20,7 +20,7 @@ interface ShareConfig {
20
20
  openTagList?: string[];
21
21
  url?: string;
22
22
  }
23
- export declare function weixinShareConfig(config: ShareConfig): void;
23
+ export declare function configReady(config: ShareConfig): void;
24
24
  interface ShareOptions {
25
25
  title?: string;
26
26
  desc?: string;
@@ -31,7 +31,7 @@ interface ShareOptions {
31
31
  key?: string;
32
32
  shareUrl?: string;
33
33
  }
34
- export declare function weixinShareInit(options: ShareOptions): void;
34
+ export declare function shareReady(options: ShareOptions): void;
35
35
  /**
36
36
  *
37
37
  * @param options
@@ -45,5 +45,5 @@ interface ConfigPay {
45
45
  paySign: string;
46
46
  cb?: any;
47
47
  }
48
- export declare function weixinPay(config: ConfigPay): void;
48
+ export declare function onlinePay(config: ConfigPay): void;
49
49
  export {};
@@ -1,37 +1,59 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.weixinPay = exports.weixinShareInit = exports.weixinShareConfig = exports.weixinCode = exports.weixinEnvSDK = exports.weixinUrlCode = void 0;
26
+ exports.onlinePay = exports.shareReady = exports.configReady = exports.getCode = exports.envSDK = exports.getUrlCode = void 0;
4
27
  // import * as weixin from 'weixin-js-sdk' // 引入微信SDK
5
28
  var weixin = require("weixin-js-sdk");
6
29
  var utils_1 = require("../utils");
7
30
  var crypto_1 = require("../crypto");
31
+ var params = __importStar(require("../params"));
8
32
  var index_1 = require("../storage/index");
9
33
  var w = window;
10
34
  var loc = w.location;
11
35
  var defaultKey = "5uMz4R8rop26DkC8";
12
36
  var defaultIv = "5uMz4Rsd0926DkC8";
13
37
  var weixinConfig = {};
14
- function weixinUrlCode(config) {
38
+ function getUrlCode(config) {
39
+ var _a;
15
40
  config = config || {};
16
- var appId = config['appId'] || "";
41
+ var appId = (config === null || config === void 0 ? void 0 : config.appId) || "";
17
42
  if (!appId)
18
43
  return;
19
44
  weixinConfig = config;
20
- var scope = config['scope'] || "snsapi_userinfo";
21
- var codeKey = config['codeKey'] || "weixinCode";
22
- var stateKey = config['stateKey'] || "weixinState";
45
+ var scope = (config === null || config === void 0 ? void 0 : config.scope) || "snsapi_userinfo";
46
+ var codeKey = (config === null || config === void 0 ? void 0 : config.codeKey) || "weixinAuthCode";
47
+ var stateKey = (config === null || config === void 0 ? void 0 : config.stateKey) || "weixinAuthState";
23
48
  weixinConfig['scope'] = scope;
24
49
  weixinConfig['codeKey'] = codeKey;
25
50
  weixinConfig['stateKey'] = stateKey;
26
51
  (0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
27
- var _a = weixinConfig.cryptoiv, cryptoiv = _a === void 0 ? "" : _a, _b = weixinConfig.cryptokey, cryptokey = _b === void 0 ? "" : _b;
52
+ var _b = weixinConfig.cryptoiv, cryptoiv = _b === void 0 ? "" : _b, _c = weixinConfig.cryptokey, cryptokey = _c === void 0 ? "" : _c;
28
53
  var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
29
- weixinInit();
30
- // 获取地址栏返回 code 参数
31
- var localCode = "";
32
- if (codeKey) {
33
- localCode = (0, index_1.getSessionStorage)(codeKey);
34
- }
54
+ initSdk(); // 加载微信sdk环境
55
+ // 获取地址栏返回 code 参数值
56
+ var localCode = (0, index_1.getSessionStorage)("urlParamsData") || "";
35
57
  // 默认第一次通过链接进入系统,没有code和state
36
58
  var searchIndex = -1;
37
59
  var stateIndex = -1;
@@ -39,63 +61,36 @@ function weixinUrlCode(config) {
39
61
  searchIndex = search.indexOf("code=");
40
62
  stateIndex = search.indexOf("state=");
41
63
  }
64
+ // 如果本地已经存储 微信授权返回的 code 值,证明用户在执行刷新操作,不再执行微信授权逻辑
42
65
  if (localCode && stateIndex == -1 && searchIndex == -1) {
43
66
  return;
44
67
  }
45
- // 如果链接地址中带有code
68
+ // 如果链接地址中带有code,证明执行微信授权登录,并返回授权所需的 code 参数
46
69
  if (searchIndex >= 0) {
47
70
  if (localCode) {
48
71
  // localCode 存在,用户已执行微信跳转链接,用户可能执行当前网页的刷新操作
49
72
  return;
50
73
  }
51
74
  else {
52
- // 当前系统没有存储 code,流程为用户只需微信授权登录后返回系统,此时通过CODE执行微信登录
53
- var urlArr = formatSearch(search, "code=");
54
- var code = "";
55
- for (var k = 0; k < urlArr.length; k++) {
56
- var item = urlArr[k];
57
- if (item.indexOf("code=") != -1) {
58
- code = item.substr(5);
59
- if (codeKey)
60
- (0, index_1.useSessionStorage)(codeKey, code);
61
- }
62
- }
63
- if (code) {
64
- weixinLogin(code);
75
+ // 当前系统本地没有存储 code,流程为用户只需微信授权登录后返回系统,此时通过 CODE 执行微信登录
76
+ var urlParamsData = (0, index_1.getSessionStorage)("urlParamsData") || "";
77
+ var codeData = ((_a = urlParamsData === null || urlParamsData === void 0 ? void 0 : urlParamsData.code) === null || _a === void 0 ? void 0 : _a.str) || ""; // 第一次进入系统为空
78
+ if (codeData) {
79
+ login(codeData);
65
80
  }
66
81
  }
67
82
  return;
68
83
  }
69
- // 用户通过分享链接进入系统,此时含有State参数,无code参数
84
+ // 用户通过分享链接进入系统,此时含有 state 参数 , 无code参数
70
85
  if (stateIndex >= 0) {
71
- var hrefState = "";
72
- var urlArr = formatSearch(search, "state=");
73
- for (var k = 0; k < urlArr.length; k++) {
74
- var item = urlArr[k];
75
- if (item.indexOf("state=") != -1) {
76
- hrefState = item.substr(6);
77
- var stateKey_1 = weixinConfig['stateKey'];
78
- if (!stateKey_1) {
79
- stateKey_1 = "weixinState";
80
- weixinConfig['stateKey'] = stateKey_1;
81
- }
82
- (0, index_1.useSessionStorage)(stateKey_1, hrefState);
83
- var cryptoObj = cryptoConfig(cryptokey, cryptoiv);
84
- var stateObj = (0, crypto_1.decrypt)(hrefState, cryptoObj.key, cryptoObj.iv);
85
- if (stateObj instanceof Object)
86
- stateObj = JSON.stringify(stateObj);
87
- (0, index_1.useSessionStorage)("".concat(stateKey_1, "Obj"), stateObj);
88
- }
89
- }
90
86
  // 将 state进行本地存储,存储完成后跳转到微信授权页面
91
- weixinCode(); // 微信授权页面获取CODE
92
- return;
87
+ return getCode(); // 微信授权页面获取CODE
93
88
  }
94
89
  if (!localCode && stateIndex == -1 && searchIndex == -1) {
95
- return weixinCode();
90
+ return getCode();
96
91
  }
97
92
  }
98
- exports.weixinUrlCode = weixinUrlCode;
93
+ exports.getUrlCode = getUrlCode;
99
94
  function cryptoConfig(key, iv) {
100
95
  if (!key)
101
96
  key = defaultKey;
@@ -107,26 +102,41 @@ function cryptoConfig(key, iv) {
107
102
  };
108
103
  }
109
104
  // 执行微信登录
110
- function weixinLogin(code) {
105
+ function login(code) {
111
106
  var appId = weixinConfig.appId, http = weixinConfig.http, stateKey = weixinConfig.stateKey, redirectUri = weixinConfig.redirectUri;
112
- if (!appId)
107
+ if (!appId) {
108
+ console.error("未正常传入微信公众号appId");
109
+ return;
110
+ }
111
+ if (!code) {
112
+ console.error("未获取到微信授权的code参数");
113
113
  return;
114
+ }
114
115
  var obj = {
115
116
  "app_id": appId,
116
117
  "js_code": code
117
118
  };
118
- if (!http)
119
+ if (!http) {
120
+ console.error("未正常传入微信授权登录http函数");
119
121
  return;
120
- (0, index_1.useSessionStorage)("weixinLoginParams", JSON.stringify(obj));
122
+ }
123
+ (0, index_1.useSessionStorage)("loginParams", JSON.stringify(obj));
121
124
  // 调试字段 isPassLogin ,为true 时不继续执行微信登录,前端可拿到 code 给后端测试
122
125
  // if(weixinConfig['isPassLogin']) return
123
126
  weixinConfig.http(obj).then(function (res) {
124
- (0, index_1.useSessionStorage)("weixinLoginRes", res);
127
+ var _a;
128
+ (0, index_1.useSessionStorage)("loginRes", res);
125
129
  if (res.code == 40163) {
126
130
  // 未登录则先执行登录
127
- return weixinCode();
131
+ return getCode();
132
+ }
133
+ var data = {};
134
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("data")) {
135
+ data = res.data;
136
+ }
137
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("Data")) {
138
+ data = res.Data;
128
139
  }
129
- var data = res['data'] || {};
130
140
  for (var key in data) {
131
141
  var item = data[key];
132
142
  if (item instanceof Object) {
@@ -140,24 +150,24 @@ function weixinLogin(code) {
140
150
  url = encodeURIComponent(redirectUri);
141
151
  }
142
152
  var hashVal = loc.hash;
143
- var state = (0, index_1.getSessionStorage)(stateKey);
153
+ var urlParamsData = (0, index_1.getSessionStorage)("urlParamsData") || "";
154
+ var stateData = ((_a = urlParamsData === null || urlParamsData === void 0 ? void 0 : urlParamsData.state) === null || _a === void 0 ? void 0 : _a.data) || ""; // 默认有值,链接携带
144
155
  var str = "";
145
- if (state) {
146
- var stateObj = (0, index_1.getSessionStorage)("".concat(stateKey, "Obj"));
147
- for (var key in stateObj) {
156
+ if (stateData) {
157
+ for (var key in stateData) {
148
158
  if (key == "path") {
149
- if (stateObj[key][0] == "/") {
150
- url = "".concat(url, "/#").concat(stateObj.path);
159
+ if (stateData[key][0] == "/") {
160
+ url = "".concat(url, "/#").concat(stateData.path);
151
161
  }
152
162
  else {
153
- url = "".concat(url, "/#/").concat(stateObj.path);
163
+ url = "".concat(url, "/#/").concat(stateData.path);
154
164
  }
155
165
  }
156
166
  else {
157
- str += "".concat(key, "=").concat(stateObj[key], "&");
167
+ str += "".concat(key, "=").concat(stateData[key], "&");
158
168
  }
159
169
  }
160
- if (stateObj['path']) {
170
+ if (stateData === null || stateData === void 0 ? void 0 : stateData.path) {
161
171
  if (str) {
162
172
  url = "".concat(url, "?").concat(str);
163
173
  }
@@ -179,7 +189,7 @@ function weixinLogin(code) {
179
189
  }
180
190
  }
181
191
  }).catch(function (err) {
182
- (0, index_1.useSessionStorage)("weixinLoginError", err);
192
+ (0, index_1.useSessionStorage)("loginError", err);
183
193
  });
184
194
  }
185
195
  /**
@@ -193,11 +203,14 @@ function formatSearch(search, keys) {
193
203
  return false;
194
204
  var index = search.indexOf(keys);
195
205
  search = search.substr(index);
206
+ if (search.startsWith("?")) {
207
+ search = search.substring(1);
208
+ }
196
209
  var urlArr = search.split("&");
197
210
  return urlArr;
198
211
  }
199
212
  // 加载当前微信sdk环境
200
- function weixinInit() {
213
+ function initSdk() {
201
214
  var wx = null;
202
215
  if (w.wx && w.wx.ready) {
203
216
  wx = w.wx;
@@ -209,13 +222,14 @@ function weixinInit() {
209
222
  return wx;
210
223
  }
211
224
  // 返回微信环境 sdk
212
- function weixinEnvSDK() {
213
- var env = weixinInit() || {};
225
+ function envSDK() {
226
+ var env = initSdk() || {};
214
227
  return env;
215
228
  }
216
- exports.weixinEnvSDK = weixinEnvSDK;
229
+ exports.envSDK = envSDK;
217
230
  // 前往微信授权登录页面
218
- function weixinCode(appId, scope) {
231
+ function getCode(appId, scope) {
232
+ params.get(['code', 'state'], "session");
219
233
  if ((0, utils_1.weixinBrowser)()) {
220
234
  var id = "";
221
235
  if (weixinConfig['appId']) {
@@ -225,7 +239,7 @@ function weixinCode(appId, scope) {
225
239
  id = appId;
226
240
  }
227
241
  if (!id) {
228
- console.log("调用跳转微信授权方法时weixinCode,请传入参数:appId");
242
+ console.log("调用跳转微信授权方法时getCode,请传入参数:appId");
229
243
  return;
230
244
  }
231
245
  var scopeVal = "snsapi_userinfo";
@@ -249,7 +263,7 @@ function weixinCode(appId, scope) {
249
263
  console.log("非微信内核浏览器执行其他业务");
250
264
  }
251
265
  }
252
- exports.weixinCode = weixinCode;
266
+ exports.getCode = getCode;
253
267
  var defaultJsApiList = [
254
268
  'updateAppMessageShareData', 'updateTimelineShareData', "onMenuShareWeibo", "onMenuShareQZone", "startRecord", "stopRecord", "closeWindow", "scanQRCode", "chooseWXPay", "chooseCard", "openCard",
255
269
  "onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "openProductSpecificView", "addCard",
@@ -259,7 +273,7 @@ var defaultOpenTagList = [
259
273
  "wx-open-subscribe", "wx-open-launch-app", "wx-open-launch-weapp",
260
274
  "wx-open-audio"
261
275
  ];
262
- function weixinShareConfig(config) {
276
+ function configReady(config) {
263
277
  if (!config['http'])
264
278
  return;
265
279
  var url = loc.origin;
@@ -281,7 +295,7 @@ function weixinShareConfig(config) {
281
295
  config.http(param).then(function (res) {
282
296
  if (res.code == 0) {
283
297
  var data = res['data'] || {};
284
- var wx = weixinInit();
298
+ var wx = initSdk();
285
299
  wx.config({
286
300
  debug: false,
287
301
  appId: data.appId,
@@ -293,18 +307,18 @@ function weixinShareConfig(config) {
293
307
  });
294
308
  wx.ready(function (res) {
295
309
  console.log("加载微信分享配置完成:", res);
296
- (0, index_1.useSessionStorage)("weixinShareConfigSuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
310
+ (0, index_1.useSessionStorage)("configReadySuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
297
311
  if (config['cb'] && typeof config.cb === "function")
298
312
  config.cb();
299
313
  });
300
314
  wx.error(function (err) {
301
- (0, index_1.useSessionStorage)("weixinShareConfigError", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u9519\u8BEF\uFF1A".concat(err));
315
+ (0, index_1.useSessionStorage)("configReadyError", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u9519\u8BEF\uFF1A".concat(err));
302
316
  });
303
317
  }
304
318
  });
305
319
  }
306
- exports.weixinShareConfig = weixinShareConfig;
307
- function weixinShareInit(options) {
320
+ exports.configReady = configReady;
321
+ function shareReady(options) {
308
322
  var title = "微信分享标题";
309
323
  if (options['title'])
310
324
  title = options.title;
@@ -330,10 +344,10 @@ function weixinShareInit(options) {
330
344
  }
331
345
  if (options['shareUrl'])
332
346
  link = options['shareUrl'];
333
- var wx = weixinInit();
347
+ var wx = initSdk();
334
348
  (0, index_1.useSessionStorage)("weixinShareUrl", link);
335
349
  wx.ready(function () {
336
- (0, index_1.useSessionStorage)("weixinShareInit", "success");
350
+ (0, index_1.useSessionStorage)("shareReady", "success");
337
351
  //需在用户可能点击分享按钮前就先调用
338
352
  wx.updateAppMessageShareData({
339
353
  title: title,
@@ -365,9 +379,9 @@ function weixinShareInit(options) {
365
379
  console.log("配置进入错误:", res);
366
380
  });
367
381
  }
368
- exports.weixinShareInit = weixinShareInit;
369
- function weixinPay(config) {
370
- var wx = weixinInit();
382
+ exports.shareReady = shareReady;
383
+ function onlinePay(config) {
384
+ var wx = initSdk();
371
385
  // let { timeStamp,signType,prepay_id,paySign,package,nonceStr,appId } = params
372
386
  var cb = "";
373
387
  if (config['cb'] && typeof config.cb === "function")
@@ -396,4 +410,4 @@ function weixinPay(config) {
396
410
  }
397
411
  });
398
412
  }
399
- exports.weixinPay = weixinPay;
413
+ exports.onlinePay = onlinePay;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```\r 时间戳转时间,字符串转时间戳",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",