xctc-utils 1.6.7 → 1.6.9

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
@@ -97,6 +97,12 @@ interface configOption {
97
97
  stateKey?:string, // 回调地址栏中对 state 进行存储的键 stateKey:state
98
98
  cryptoiv?:string, // 将地址栏携带参数加密iv, 必须与 weixinShareInit 方法中的 iv 参数一致
99
99
  cryptokey?:string, // 将地址栏携带参数加密key, 必须与 weixinShareInit 方法中的 key 参数一致
100
+ isPassLogin?:boolean, 是否忽略微信跳转登录
101
+ redirectUri?:string, // 回调地址
102
+ appIdKey?:string, // 向后端发起http请求时,app_id对应的键,默认键值: app_id
103
+ jsCodeKey?:string, // 向后端发起http请求时,js_code对应的键,默认键值: js_code,完整格式 httpdata = { app_id:"xxxxx",js_code:"xxxxx" }
104
+ debuggerStatus?:boolean,//是否开启 debugger 弹框提示
105
+ cb?:any; // 回调函数
100
106
  }
101
107
  进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
102
108
  useUtils.weixin.getUrlCode(config)
@@ -10,6 +10,8 @@ interface configOption {
10
10
  redirectUri?: string;
11
11
  appIdKey?: string;
12
12
  jsCodeKey?: string;
13
+ debuggerStatus?: boolean;
14
+ cb?: any;
13
15
  }
14
16
  export declare function getUrlCode(config: configOption): void;
15
17
  export declare function envSDK(): any;
@@ -37,6 +37,10 @@ var defaultIv = "5uMz4Rsd0926DkC8";
37
37
  var weixinConfig = {};
38
38
  function getUrlCode(config) {
39
39
  var _a;
40
+ if (!(0, utils_1.weixinBrowser)()) {
41
+ console.error("非微信环境下,该函数将终止执行!");
42
+ return;
43
+ }
40
44
  config = config || {};
41
45
  var appId = (config === null || config === void 0 ? void 0 : config.appId) || "";
42
46
  if (!appId)
@@ -49,44 +53,62 @@ function getUrlCode(config) {
49
53
  weixinConfig['codeKey'] = codeKey;
50
54
  weixinConfig['stateKey'] = stateKey;
51
55
  (0, index_1.useSessionStorage)("weixinConfig", weixinConfig);
52
- var _b = weixinConfig.cryptoiv, cryptoiv = _b === void 0 ? "" : _b, _c = weixinConfig.cryptokey, cryptokey = _c === void 0 ? "" : _c;
56
+ var _b = weixinConfig.cryptoiv, cryptoiv = _b === void 0 ? defaultIv : _b, _c = weixinConfig.cryptokey, cryptokey = _c === void 0 ? defaultKey : _c, _d = weixinConfig.debuggerStatus, debuggerStatus = _d === void 0 ? false : _d;
53
57
  var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
54
58
  initSdk(); // 加载微信sdk环境
55
- // 获取地址栏返回 code 参数值
56
- var localCode = (0, index_1.getSessionStorage)("urlParamsData") || "";
57
59
  // 默认第一次通过链接进入系统,没有code和state
58
- var searchIndex = -1;
60
+ var codeIndex = -1;
59
61
  var stateIndex = -1;
62
+ var msgStr = "";
63
+ var isAuth = false; // 是否完成 code 授权
60
64
  if (search) {
61
- searchIndex = search.indexOf("code=");
65
+ codeIndex = search.indexOf("code=");
62
66
  stateIndex = search.indexOf("state=");
67
+ isAuth = search.includes("state=state");
68
+ }
69
+ // 获取地址栏返回 code 参数值
70
+ var localCode = (0, index_1.getSessionStorage)("urlParamsData") || "";
71
+ // 本地未存储微信授权 code 值, 并且地址栏携带有 code 值 ,则先存储该值
72
+ if (!localCode && codeIndex != -1 && isAuth) {
73
+ params.get({ keys: ['code', 'state'], cache: "session", key: cryptokey, iv: cryptoiv });
63
74
  }
64
75
  // 如果本地已经存储 微信授权返回的 code 值,证明用户在执行刷新操作,不再执行微信授权逻辑
65
- if (localCode && stateIndex == -1 && searchIndex == -1) {
76
+ if (localCode && stateIndex == -1 && codeIndex == -1 && !isAuth) {
77
+ if (debuggerStatus) {
78
+ alert("本地已经存储 code ,不再执行微信授权逻辑");
79
+ }
66
80
  return;
67
81
  }
68
82
  // 如果链接地址中带有code,证明执行微信授权登录,并返回授权所需的 code 参数
69
- if (searchIndex >= 0) {
83
+ if (codeIndex >= 0 && isAuth) {
70
84
  if (localCode) {
71
85
  // localCode 存在,用户已执行微信跳转链接,用户可能执行当前网页的刷新操作
86
+ if (debuggerStatus) {
87
+ alert("codeIndex 》 0 ,本地已经存储 code ,不再执行微信授权逻辑");
88
+ }
72
89
  return;
73
90
  }
74
91
  else {
75
92
  // 当前系统本地没有存储 code,流程为用户只需微信授权登录后返回系统,此时通过 CODE 执行微信登录
76
93
  var urlParamsData = (0, index_1.getSessionStorage)("urlParamsData") || "";
77
94
  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);
80
- }
95
+ login(codeData);
81
96
  }
82
97
  return;
83
98
  }
84
99
  // 用户通过分享链接进入系统,此时含有 state 参数 , 无code参数
85
- if (stateIndex >= 0) {
100
+ if (stateIndex >= 0 && codeIndex == -1) {
86
101
  // 将 state进行本地存储,存储完成后跳转到微信授权页面
102
+ if (debuggerStatus) {
103
+ alert("用户通过分享链接进入系统,无code参数 ,跳转到code授权页面");
104
+ }
87
105
  return getCode(); // 微信授权页面获取CODE
88
106
  }
89
- if (!localCode && stateIndex == -1 && searchIndex == -1) {
107
+ // && stateIndex == -1 && codeIndex == -1
108
+ if (!localCode && codeIndex == -1) {
109
+ if (debuggerStatus) {
110
+ alert("无本地code参数 ,跳转到code授权页面");
111
+ }
90
112
  return getCode();
91
113
  }
92
114
  }
@@ -104,7 +126,10 @@ function cryptoConfig(key, iv) {
104
126
  // 执行微信登录
105
127
  function login(code) {
106
128
  var _a;
107
- var appId = weixinConfig.appId, http = weixinConfig.http, redirectUri = weixinConfig.redirectUri, _b = weixinConfig.appIdKey, appIdKey = _b === void 0 ? "app_id" : _b, _c = weixinConfig.jsCodeKey, jsCodeKey = _c === void 0 ? "js_code" : _c;
129
+ var appId = weixinConfig.appId, http = weixinConfig.http, redirectUri = weixinConfig.redirectUri, _b = weixinConfig.appIdKey, appIdKey = _b === void 0 ? "app_id" : _b, _c = weixinConfig.jsCodeKey, jsCodeKey = _c === void 0 ? "js_code" : _c, _d = weixinConfig.cb, cb = _d === void 0 ? "" : _d, _e = weixinConfig.debuggerStatus, debuggerStatus = _e === void 0 ? false : _e;
130
+ if (debuggerStatus) {
131
+ alert("地址栏携带code已完成本地存储 ,执行微信授权逻辑");
132
+ }
108
133
  if (!appId) {
109
134
  console.error("未正常传入微信公众号appId");
110
135
  return;
@@ -113,14 +138,14 @@ function login(code) {
113
138
  console.error("未获取到微信授权的code参数");
114
139
  return;
115
140
  }
141
+ if (!(typeof http === "function")) {
142
+ console.error("未正常传入微信授权登录http函数");
143
+ return;
144
+ }
116
145
  var obj = (_a = {},
117
146
  _a[appIdKey] = appId,
118
147
  _a[jsCodeKey] = code,
119
148
  _a);
120
- if (!http) {
121
- console.error("未正常传入微信授权登录http函数");
122
- return;
123
- }
124
149
  (0, index_1.useSessionStorage)("loginParams", JSON.stringify(obj));
125
150
  // 调试字段 isPassLogin ,为true 时不继续执行微信登录,前端可拿到 code 给后端测试
126
151
  // if(weixinConfig['isPassLogin']) return
@@ -173,17 +198,16 @@ function login(code) {
173
198
  url = "".concat(url, "?").concat(str);
174
199
  }
175
200
  }
176
- if (weixinConfig['isPassLogin']) {
177
- console.log("不执行跳转操作");
178
- }
179
- else {
180
- loc.replace(url);
181
- }
182
201
  }
183
202
  else {
184
203
  url = "".concat(url, "/").concat(hashVal);
185
- if (weixinConfig['isPassLogin']) {
186
- console.log("不执行跳转操作");
204
+ }
205
+ if (weixinConfig['isPassLogin']) {
206
+ console.log("不执行跳转操作");
207
+ }
208
+ else {
209
+ if (typeof cb === "function") {
210
+ cb(res);
187
211
  }
188
212
  else {
189
213
  loc.replace(url);
@@ -230,8 +254,6 @@ function envSDK() {
230
254
  exports.envSDK = envSDK;
231
255
  // 前往微信授权登录页面
232
256
  function getCode(appId, scope) {
233
- var _a = weixinConfig.cryptoiv, cryptoiv = _a === void 0 ? defaultIv : _a, _b = weixinConfig.cryptokey, cryptokey = _b === void 0 ? defaultKey : _b;
234
- params.get({ keys: ['code', 'state'], cache: "session", key: cryptokey, iv: cryptoiv });
235
257
  if ((0, utils_1.weixinBrowser)()) {
236
258
  var id = "";
237
259
  if (weixinConfig['appId']) {
@@ -298,17 +320,28 @@ function configReady(config) {
298
320
  }
299
321
  config.http(param).then(function (res) {
300
322
  if (res.code == 0) {
301
- var data = res['data'] || {};
302
- var wx = initSdk();
303
- wx.config({
304
- debug: false,
305
- appId: data.appId,
306
- timestamp: data.timestamp,
307
- nonceStr: data.noncestr,
308
- signature: data.signature,
323
+ var data = {};
324
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("data")) {
325
+ data = res.data;
326
+ }
327
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("Data")) {
328
+ data = res.Data;
329
+ }
330
+ for (var key in data) {
331
+ var newKey = key.toLowerCase();
332
+ data[newKey] = data[key];
333
+ }
334
+ var obj = {
309
335
  jsApiList: jsApiListData,
310
336
  openTagList: openTagListData,
311
- });
337
+ debug: false,
338
+ appId: data === null || data === void 0 ? void 0 : data.appId,
339
+ timestamp: data === null || data === void 0 ? void 0 : data.timestamp,
340
+ nonceStr: data === null || data === void 0 ? void 0 : data.noncestr,
341
+ signature: data === null || data === void 0 ? void 0 : data.signature, //必填,生成的签名
342
+ };
343
+ var wx = initSdk();
344
+ wx.config(obj);
312
345
  wx.ready(function (res) {
313
346
  console.log("加载微信分享配置完成:", res);
314
347
  (0, index_1.useSessionStorage)("configReadySuccess", "\u52A0\u8F7D\u5FAE\u4FE1\u5206\u4EAB\u914D\u7F6E\u5B8C\u6210\uFF1A".concat(res));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.6.7",
3
+ "version": "1.6.9",
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",