xctc-utils 1.0.1 → 1.0.3

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,17 @@
1
+ LocalStorage存储
2
+ ```
3
+ useLocalStorage(key,value)
4
+ ```
5
+ 获取LocalStorage值
6
+ ```
7
+ getLocalStorage(key)
8
+ ```
9
+ 设备环境:
10
+ ```
11
+ deviceEnvironment() // android ios
12
+ ```
13
+ 是否微信APP环境
14
+ ```
15
+ weixinBrowser() // true false
16
+ ```
17
+
@@ -0,0 +1,2 @@
1
+ export declare function encrypt(word: any, key: any, iv: any): string;
2
+ export declare function decrypt(word: any, key: any, iv: any): any;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decrypt = exports.encrypt = void 0;
4
+ var CryptoJS = require("crypto-js");
5
+ var utils_1 = require("../utils");
6
+ function formatVal(val) {
7
+ var key = "";
8
+ if (val) {
9
+ key = CryptoJS.enc.Utf8.parse(val);
10
+ }
11
+ else {
12
+ key = CryptoJS.enc.Utf8.parse('5uMz4R8r0926DkC8');
13
+ }
14
+ return key;
15
+ }
16
+ // 加密
17
+ function encrypt(word, key, iv) {
18
+ // key //十六位十六进制数作为密钥
19
+ // iv 十六位十六进制数作为密钥偏移量
20
+ word = JSON.stringify(word);
21
+ var src = CryptoJS.enc.Utf8.parse(word);
22
+ key = formatVal(key);
23
+ iv = formatVal(iv);
24
+ var encrypted = CryptoJS.AES.encrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
25
+ return encrypted.ciphertext.toString().toUpperCase();
26
+ }
27
+ exports.encrypt = encrypt;
28
+ ;
29
+ // 解密
30
+ function decrypt(word, key, iv) {
31
+ key = formatVal(key);
32
+ iv = formatVal(iv);
33
+ var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
34
+ var src = CryptoJS.enc.Base64.stringify(encryptedHexStr);
35
+ var decrypt = CryptoJS.AES.decrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
36
+ var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
37
+ var data = decryptedStr.toString();
38
+ if ((0, utils_1.isJson)(data)) {
39
+ return JSON.parse(data);
40
+ }
41
+ else {
42
+ return data;
43
+ }
44
+ }
45
+ exports.decrypt = decrypt;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,2 @@
1
- export declare function add(a: number, b: number): number;
2
- declare const obj: {
3
- add: typeof add;
4
- };
1
+ declare const obj: any;
5
2
  export default obj;
package/dist/index.js CHANGED
@@ -1,11 +1,19 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.add = void 0;
4
- function add(a, b) {
5
- return a + b;
6
- }
7
- exports.add = add;
8
- var obj = {
9
- add: add,
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
10
12
  };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ var storage = require("./storage");
15
+ var utils = require("./utils");
16
+ var crypto = require("./crypto");
17
+ var weixin = require("./weixin");
18
+ var obj = __assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto);
11
19
  exports.default = obj;
@@ -0,0 +1,4 @@
1
+ export declare function useLocalStorage(key: string, value: any): false | undefined;
2
+ export declare function getLocalStorage(key: string): any;
3
+ export declare function useSessionStorage(key: string, value: any): false | undefined;
4
+ export declare function getSessionStorage(key: string): any;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSessionStorage = exports.useSessionStorage = exports.getLocalStorage = exports.useLocalStorage = void 0;
4
+ var wls = window.localStorage;
5
+ var wss = window.sessionStorage;
6
+ var utils_1 = require("../utils");
7
+ function useLocalStorage(key, value) {
8
+ if (!key || !value) {
9
+ console.log("useLocalStorage存储时缺少key:value");
10
+ return false;
11
+ }
12
+ useStorage(key, value, wls);
13
+ }
14
+ exports.useLocalStorage = useLocalStorage;
15
+ function getLocalStorage(key) {
16
+ if (!key)
17
+ return;
18
+ return getStorage(key, wls);
19
+ }
20
+ exports.getLocalStorage = getLocalStorage;
21
+ function useSessionStorage(key, value) {
22
+ if (!key || !value) {
23
+ console.log("useSessionStorage存储时缺少key:value");
24
+ return false;
25
+ }
26
+ useStorage(key, value, wss);
27
+ }
28
+ exports.useSessionStorage = useSessionStorage;
29
+ function getSessionStorage(key) {
30
+ if (!key)
31
+ return;
32
+ return getStorage(key, wss);
33
+ }
34
+ exports.getSessionStorage = getSessionStorage;
35
+ function useStorage(key, value, obj) {
36
+ if (value instanceof Object) {
37
+ obj.setItem(key, JSON.stringify(value));
38
+ }
39
+ else {
40
+ obj.setItem(key, value);
41
+ }
42
+ }
43
+ function getStorage(key, obj) {
44
+ var val = obj.getItem(key);
45
+ if (val == 'undefined' || val == null) {
46
+ return "";
47
+ }
48
+ var data = (0, utils_1.isJson)(val);
49
+ if (data)
50
+ val = data;
51
+ return val;
52
+ }
@@ -0,0 +1,4 @@
1
+ export interface UeditorHeightOption {
2
+ id?: string;
3
+ content?: string;
4
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { UeditorHeightOption } from "./types";
2
+ export declare function isJson(str: string): any;
3
+ export declare function $id(id: string): false | HTMLElement;
4
+ export declare function checkIframeContentHeight(option: UeditorHeightOption): string;
5
+ export declare function getIframeContentHeight(id: string): any;
6
+ export declare function deviceEnvironment(): "android" | "ios" | undefined;
7
+ export declare function weixinBrowser(): boolean;
package/dist/utils.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.weixinBrowser = exports.deviceEnvironment = exports.getIframeContentHeight = exports.checkIframeContentHeight = exports.$id = exports.isJson = void 0;
4
+ function isJson(str) {
5
+ if (!str)
6
+ return false;
7
+ try {
8
+ var obj = JSON.parse(str);
9
+ if (obj && typeof obj == "object") {
10
+ return obj;
11
+ }
12
+ else {
13
+ return false;
14
+ }
15
+ }
16
+ catch (e) {
17
+ return false;
18
+ }
19
+ }
20
+ exports.isJson = isJson;
21
+ // 通过ID获取DOM元素
22
+ function $id(id) {
23
+ if (!id)
24
+ return false;
25
+ var dom = document.getElementById(id);
26
+ if (id && dom instanceof HTMLElement) {
27
+ return dom;
28
+ }
29
+ return false;
30
+ }
31
+ exports.$id = $id;
32
+ // 设置当前富文本内容高度
33
+ function checkIframeContentHeight(option) {
34
+ var _a = option.id, id = _a === void 0 ? "ueditor_0" : _a, _b = option.content, content = _b === void 0 ? "" : _b;
35
+ var ifm = $id(id); // 当前富文本id获取到iframe
36
+ var subWeb = { body: "" };
37
+ var tags = document.body.getElementsByTagName("iframe");
38
+ if (tags && tags[0]) {
39
+ subWeb = tags[0]['contentDocument'];
40
+ }
41
+ else {
42
+ if (ifm && ifm.contentDocument)
43
+ subWeb = ifm.contentDocument;
44
+ }
45
+ if (subWeb && subWeb.body) {
46
+ var ifmBody = subWeb.body;
47
+ var last = ifmBody.lastElementChild;
48
+ var height = 0;
49
+ if (last) {
50
+ height = last.offsetTop;
51
+ }
52
+ return "<input type=\"hidden\" content-data-height=\"".concat(height, "\" id=\"ueditorHeight\" />").concat(content);
53
+ }
54
+ return content;
55
+ }
56
+ exports.checkIframeContentHeight = checkIframeContentHeight;
57
+ // 获取到当前dom容器内所有内容的高度
58
+ function getIframeContentHeight(id) {
59
+ if (!id) {
60
+ id = "ueditorHeight";
61
+ }
62
+ var dom = $id(id);
63
+ if (dom) {
64
+ var height = dom.getAttribute("content-data-height");
65
+ return height;
66
+ }
67
+ return 0;
68
+ }
69
+ exports.getIframeContentHeight = getIframeContentHeight;
70
+ function deviceEnvironment() {
71
+ var u = navigator.userAgent;
72
+ if (u.indexOf('Android') > -1 || u.indexOf('Adr') > -1)
73
+ return "android";
74
+ if (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/))
75
+ return "ios";
76
+ }
77
+ exports.deviceEnvironment = deviceEnvironment;
78
+ function weixinBrowser() {
79
+ var ua = navigator.userAgent.toLowerCase();
80
+ var isWeixin = ua.indexOf("micromessenger") != -1;
81
+ if (isWeixin) {
82
+ return true;
83
+ }
84
+ else {
85
+ return false;
86
+ }
87
+ }
88
+ exports.weixinBrowser = weixinBrowser;
@@ -0,0 +1,18 @@
1
+ interface configOption {
2
+ appId?: string;
3
+ http?: any;
4
+ codeKey?: string;
5
+ stateKey?: string;
6
+ cryptoiv?: string;
7
+ cryptokey?: string;
8
+ }
9
+ export declare function weixinUrlCode(config: configOption): void;
10
+ /**
11
+ *
12
+ * 获取地址栏中的 search 参数,将参数转换为数组,最终返回地址栏参数数组
13
+ * keys 需要截取字符串开始字符
14
+ */
15
+ export declare function formatSearch(search: string | undefined, keys: string): false | any[];
16
+ export declare function weixinInit(): any;
17
+ export declare function weixinCode(): void;
18
+ export {};
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.weixinCode = exports.weixinInit = exports.formatSearch = exports.weixinUrlCode = void 0;
4
+ var weixinsdk_1 = require("../weixinsdk"); // 引入微信SDK
5
+ var utils_1 = require("../utils");
6
+ var crypto_1 = require("../crypto");
7
+ var w = window;
8
+ var loc = w.location;
9
+ var weixinConfig = {};
10
+ function weixinUrlCode(config) {
11
+ weixinConfig = config;
12
+ // let word:any = {path:"/user/user-agreement",id:222}
13
+ // let state:string = encrypt(word)
14
+ var search = loc.search; // ?code=0916SzFa1KH9TA0Ke7Ha1AQx6446SzFr&state=123
15
+ // 获取地址栏返回 code 参数
16
+ var codeKey = weixinConfig['codeKey'];
17
+ // key:string="weixinCode"
18
+ var localCode = "weixinCode";
19
+ if (codeKey)
20
+ localCode = w.sessionStorage.getItem(codeKey);
21
+ if (!localCode && search && search.indexOf("code=") != -1) {
22
+ // localCode 不存在, 微信链接中存在 code 用户初次登录系统,
23
+ var urlArr = formatSearch(search, "code=");
24
+ var code = "";
25
+ for (var k = 0; k < urlArr.length; k++) {
26
+ var item = urlArr[k];
27
+ if (item.indexOf("code=") != -1) {
28
+ code = item.substr(5);
29
+ if (codeKey)
30
+ w.sessionStorage.setItem(codeKey, code);
31
+ }
32
+ }
33
+ weixinLogin(code);
34
+ }
35
+ else if (localCode) {
36
+ // localCode 存在,用户已执行微信跳转链接,执行刷新操作
37
+ }
38
+ else {
39
+ // 通过分享链接进入系统时,先提取分享链接中的 state
40
+ var hrefState = "";
41
+ if (search && search.indexOf("state=") != -1) {
42
+ var urlArr = formatSearch(search, "state=");
43
+ for (var k = 0; k < urlArr.length; k++) {
44
+ var item = urlArr[k];
45
+ if (item.indexOf("state=") != -1) {
46
+ hrefState = item.substr(6);
47
+ var stateKey = weixinConfig['stateKey'] || "weixinState";
48
+ w.sessionStorage.setItem(stateKey, hrefState);
49
+ w.sessionStorage.setItem("".concat(stateKey, "Obj"), (0, crypto_1.decrypt)(hrefState, cryptoConfig().key, cryptoConfig().iv));
50
+ }
51
+ }
52
+ }
53
+ // 将进入系统所携带的state参数传到微信登录链接,微信跳转登录后原样返回
54
+ weixinCode(); // 微信授权页面获取CODE
55
+ }
56
+ }
57
+ exports.weixinUrlCode = weixinUrlCode;
58
+ function cryptoConfig() {
59
+ var key = "5uMz4R8rop26DkC8", iv = "5uMz4Rsd0926DkC8";
60
+ if (weixinConfig['cryptoiv'])
61
+ iv = weixinConfig.cryptoiv;
62
+ if (weixinConfig['cryptokey'])
63
+ key = weixinConfig.cryptokey;
64
+ return {
65
+ key: key,
66
+ iv: iv,
67
+ };
68
+ }
69
+ // 执行微信登录
70
+ function weixinLogin(code) {
71
+ if (!weixinConfig['appId'])
72
+ return;
73
+ var obj = {
74
+ "app_id": weixinConfig.appId,
75
+ "js_code": code
76
+ };
77
+ if (!weixinConfig.http)
78
+ return;
79
+ weixinConfig.http(obj).then(function (res) {
80
+ if (res.code == 40163) {
81
+ // 未登录则先执行登录
82
+ return weixinCode();
83
+ }
84
+ var data = res.data;
85
+ for (var key in data) {
86
+ var item = data[key];
87
+ if (item instanceof Object) {
88
+ item = JSON.stringify(item);
89
+ }
90
+ w.sessionStorage.setItem(key, item);
91
+ }
92
+ var url = loc.origin;
93
+ var state = w.sessionStorage.getItem(weixinConfig.key);
94
+ var str = "";
95
+ if (state) {
96
+ var stateObj = (0, crypto_1.decrypt)(state, cryptoConfig().key, cryptoConfig().iv);
97
+ for (var key in stateObj) {
98
+ if (key == "path") {
99
+ if (stateObj[key][0] == "/") {
100
+ url = "".concat(url, "/#").concat(stateObj.path);
101
+ }
102
+ else {
103
+ url = "".concat(url, "/#/").concat(stateObj.path);
104
+ }
105
+ }
106
+ else {
107
+ str += "".concat(key, "=").concat(stateObj[key], "&");
108
+ }
109
+ }
110
+ if (stateObj['path']) {
111
+ if (str) {
112
+ url = "".concat(url, "?").concat(str);
113
+ }
114
+ }
115
+ loc.replace(url);
116
+ }
117
+ else {
118
+ loc.replace(url);
119
+ }
120
+ });
121
+ }
122
+ /**
123
+ *
124
+ * 获取地址栏中的 search 参数,将参数转换为数组,最终返回地址栏参数数组
125
+ * keys 需要截取字符串开始字符
126
+ */
127
+ function formatSearch(search, keys) {
128
+ if (search === void 0) { search = ""; }
129
+ if (!search)
130
+ return false;
131
+ var index = search.indexOf(keys);
132
+ search = search.substr(index);
133
+ var urlArr = search.split("&");
134
+ return urlArr;
135
+ }
136
+ exports.formatSearch = formatSearch;
137
+ function weixinInit() {
138
+ var wx = null;
139
+ if (w.wx && w.wx.ready) {
140
+ wx = w.wx;
141
+ }
142
+ else {
143
+ wx = weixinsdk_1.default;
144
+ }
145
+ return wx;
146
+ }
147
+ exports.weixinInit = weixinInit;
148
+ function weixinCode() {
149
+ if ((0, utils_1.weixinBrowser)()) {
150
+ if (!weixinConfig['appId'])
151
+ return;
152
+ var base = "https://open.weixin.qq.com/connect/oauth2/authorize?";
153
+ var uri = encodeURIComponent(loc.href.split("?")[0]);
154
+ var config = "appid=".concat(weixinConfig.appId, "&redirect_uri=").concat(uri, "&response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect");
155
+ var wxJumpURL = base + config;
156
+ loc.replace(wxJumpURL);
157
+ }
158
+ else {
159
+ // 非微信内核浏览器执行其他业务
160
+ console.log("非微信内核浏览器执行其他业务");
161
+ }
162
+ }
163
+ exports.weixinCode = weixinCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xctc-utils",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -13,5 +13,11 @@
13
13
  "license": "ISC",
14
14
  "devDependencies": {
15
15
  "typescript": "^4.9.5"
16
+ },
17
+ "dependencies": {
18
+ "@types/crypto-js": "^4.1.1",
19
+ "crypto-js": "^4.1.1",
20
+ "weixin-js-sdk": "^1.6.0",
21
+ "weixin-js-sdk-ts": "^1.6.1"
16
22
  }
17
23
  }