xctc-utils 1.6.8 → 1.6.10

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)
@@ -11,6 +11,7 @@ interface configOption {
11
11
  appIdKey?: string;
12
12
  jsCodeKey?: string;
13
13
  debuggerStatus?: boolean;
14
+ cb?: any;
14
15
  }
15
16
  export declare function getUrlCode(config: configOption): void;
16
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)
@@ -52,33 +56,35 @@ function getUrlCode(config) {
52
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;
60
62
  var msgStr = "";
63
+ var isAuth = false; // 是否完成 code 授权
61
64
  if (search) {
62
- searchIndex = search.indexOf("code=");
65
+ codeIndex = search.indexOf("code=");
63
66
  stateIndex = search.indexOf("state=");
67
+ isAuth = search.includes("state=state");
64
68
  }
69
+ // 获取地址栏返回 code 参数值
70
+ var localCode = (0, index_1.getSessionStorage)("urlParamsData") || "";
65
71
  // 本地未存储微信授权 code 值, 并且地址栏携带有 code 值 ,则先存储该值
66
- if (!localCode && searchIndex != -1) {
72
+ if (!localCode && codeIndex != -1 && isAuth) {
67
73
  params.get({ keys: ['code', 'state'], cache: "session", key: cryptokey, iv: cryptoiv });
68
74
  }
69
75
  // 如果本地已经存储 微信授权返回的 code 值,证明用户在执行刷新操作,不再执行微信授权逻辑
70
- if (localCode && stateIndex == -1 && searchIndex == -1) {
76
+ if (localCode && stateIndex == -1 && codeIndex == -1 && !isAuth) {
71
77
  if (debuggerStatus) {
72
78
  alert("本地已经存储 code ,不再执行微信授权逻辑");
73
79
  }
74
80
  return;
75
81
  }
76
82
  // 如果链接地址中带有code,证明执行微信授权登录,并返回授权所需的 code 参数
77
- if (searchIndex >= 0) {
83
+ if (codeIndex >= 0 && isAuth) {
78
84
  if (localCode) {
79
85
  // localCode 存在,用户已执行微信跳转链接,用户可能执行当前网页的刷新操作
80
86
  if (debuggerStatus) {
81
- alert("searchIndex 》 0 ,本地已经存储 code ,不再执行微信授权逻辑");
87
+ alert("codeIndex 》 0 ,本地已经存储 code ,不再执行微信授权逻辑");
82
88
  }
83
89
  return;
84
90
  }
@@ -86,25 +92,20 @@ function getUrlCode(config) {
86
92
  // 当前系统本地没有存储 code,流程为用户只需微信授权登录后返回系统,此时通过 CODE 执行微信登录
87
93
  var urlParamsData = (0, index_1.getSessionStorage)("urlParamsData") || "";
88
94
  var codeData = ((_a = urlParamsData === null || urlParamsData === void 0 ? void 0 : urlParamsData.code) === null || _a === void 0 ? void 0 : _a.str) || ""; // 第一次进入系统为空
89
- if (codeData) {
90
- if (debuggerStatus) {
91
- alert("地址栏携带code已完成本地存储 ,执行微信授权逻辑");
92
- }
93
- login(codeData);
94
- }
95
+ login(codeData);
95
96
  }
96
97
  return;
97
98
  }
98
99
  // 用户通过分享链接进入系统,此时含有 state 参数 , 无code参数
99
- if (stateIndex >= 0) {
100
+ if (stateIndex >= 0 && codeIndex == -1) {
100
101
  // 将 state进行本地存储,存储完成后跳转到微信授权页面
101
102
  if (debuggerStatus) {
102
103
  alert("用户通过分享链接进入系统,无code参数 ,跳转到code授权页面");
103
104
  }
104
105
  return getCode(); // 微信授权页面获取CODE
105
106
  }
106
- // && stateIndex == -1 && searchIndex == -1
107
- if (!localCode && searchIndex == -1) {
107
+ // && stateIndex == -1 && codeIndex == -1
108
+ if (!localCode && codeIndex == -1) {
108
109
  if (debuggerStatus) {
109
110
  alert("无本地code参数 ,跳转到code授权页面");
110
111
  }
@@ -125,7 +126,10 @@ function cryptoConfig(key, iv) {
125
126
  // 执行微信登录
126
127
  function login(code) {
127
128
  var _a;
128
- 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
+ }
129
133
  if (!appId) {
130
134
  console.error("未正常传入微信公众号appId");
131
135
  return;
@@ -134,21 +138,28 @@ function login(code) {
134
138
  console.error("未获取到微信授权的code参数");
135
139
  return;
136
140
  }
141
+ if (!(typeof http === "function")) {
142
+ console.error("未正常传入微信授权登录http函数");
143
+ return;
144
+ }
137
145
  var obj = (_a = {},
138
146
  _a[appIdKey] = appId,
139
147
  _a[jsCodeKey] = code,
140
148
  _a);
141
- if (!http) {
142
- console.error("未正常传入微信授权登录http函数");
143
- return;
144
- }
145
149
  (0, index_1.useSessionStorage)("loginParams", JSON.stringify(obj));
146
150
  // 调试字段 isPassLogin ,为true 时不继续执行微信登录,前端可拿到 code 给后端测试
147
151
  // if(weixinConfig['isPassLogin']) return
148
152
  weixinConfig === null || weixinConfig === void 0 ? void 0 : weixinConfig.http(obj).then(function (res) {
149
153
  var _a;
150
154
  (0, index_1.useSessionStorage)("loginRes", res);
151
- if (res.code == 40163) {
155
+ var code = -1;
156
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("code")) {
157
+ code = res.code;
158
+ }
159
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("Code")) {
160
+ code = res.Code;
161
+ }
162
+ if (code == 40163) {
152
163
  // 未登录则先执行登录
153
164
  return getCode();
154
165
  }
@@ -194,17 +205,16 @@ function login(code) {
194
205
  url = "".concat(url, "?").concat(str);
195
206
  }
196
207
  }
197
- if (weixinConfig['isPassLogin']) {
198
- console.log("不执行跳转操作");
199
- }
200
- else {
201
- loc.replace(url);
202
- }
203
208
  }
204
209
  else {
205
210
  url = "".concat(url, "/").concat(hashVal);
206
- if (weixinConfig['isPassLogin']) {
207
- console.log("不执行跳转操作");
211
+ }
212
+ if (weixinConfig['isPassLogin']) {
213
+ console.log("不执行跳转操作");
214
+ }
215
+ else {
216
+ if (typeof cb === "function") {
217
+ cb(res);
208
218
  }
209
219
  else {
210
220
  loc.replace(url);
@@ -316,18 +326,29 @@ function configReady(config) {
316
326
  openTagListData = openTagList;
317
327
  }
318
328
  config.http(param).then(function (res) {
319
- if (res.code == 0) {
320
- var data = res['data'] || {};
321
- var wx = initSdk();
322
- wx.config({
323
- debug: false,
324
- appId: data.appId,
325
- timestamp: data.timestamp,
326
- nonceStr: data.noncestr,
327
- signature: data.signature,
329
+ if (res) {
330
+ var data = {};
331
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("data")) {
332
+ data = res.data;
333
+ }
334
+ if (res === null || res === void 0 ? void 0 : res.hasOwnProperty("Data")) {
335
+ data = res.Data;
336
+ }
337
+ for (var key in data) {
338
+ var newKey = key.toLowerCase();
339
+ data[newKey] = data[key];
340
+ }
341
+ var obj = {
328
342
  jsApiList: jsApiListData,
329
343
  openTagList: openTagListData,
330
- });
344
+ debug: false,
345
+ appId: data === null || data === void 0 ? void 0 : data.appId,
346
+ timestamp: data === null || data === void 0 ? void 0 : data.timestamp,
347
+ nonceStr: data === null || data === void 0 ? void 0 : data.noncestr,
348
+ signature: data === null || data === void 0 ? void 0 : data.signature, //必填,生成的签名
349
+ };
350
+ var wx = initSdk();
351
+ wx.config(obj);
331
352
  wx.ready(function (res) {
332
353
  console.log("加载微信分享配置完成:", res);
333
354
  (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.8",
3
+ "version": "1.6.10",
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",