xctc-utils 1.0.4 → 1.0.6
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 +66 -7
- package/app.ts +10 -0
- package/dist/crypto/index.js +24 -1
- package/dist/index.js +27 -4
- package/dist/weixin/index.d.ts +18 -8
- package/dist/weixin/index.js +104 -5
- package/package.json +30 -29
- package/tsconfig.json +9 -1
package/README.md
CHANGED
|
@@ -1,17 +1,76 @@
|
|
|
1
|
-
|
|
1
|
+
### 项目中常用的方法集合
|
|
2
|
+
#### email: dybself@163.com
|
|
3
|
+
#### 安装 npm i xctc-utils
|
|
4
|
+
#### 项目中引入 import utils from "xctc-utils"
|
|
5
|
+
|
|
6
|
+
#### LocalStorage使用,存取值时数据已经过处理
|
|
2
7
|
```
|
|
3
|
-
useLocalStorage(key,value)
|
|
8
|
+
存储: utils.useLocalStorage(key,value)
|
|
9
|
+
取值: utils.getLocalStorage(key)
|
|
4
10
|
```
|
|
5
|
-
|
|
11
|
+
|
|
12
|
+
#### sessionStorage使用,存取值时数据已经过处理
|
|
6
13
|
```
|
|
7
|
-
|
|
14
|
+
存储: utils.useSessionStorage(key,value)
|
|
15
|
+
取值: utils.getSessionStorage(key)
|
|
8
16
|
```
|
|
17
|
+
|
|
18
|
+
#### 获取当前设备环境
|
|
9
19
|
设备环境:
|
|
10
20
|
```
|
|
11
|
-
deviceEnvironment() // android ios
|
|
21
|
+
当前使用设备类型: utils.deviceEnvironment() // android ios
|
|
22
|
+
是否在微信浏览器环境中: utils.weixinBrowser() // true false
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
#### 加密解密方法
|
|
27
|
+
```
|
|
28
|
+
word: 需要加密的数据,数据可是字符串,对象等
|
|
29
|
+
key: 加密 密钥
|
|
30
|
+
iv: 密钥偏移量
|
|
31
|
+
对数据进行加密
|
|
32
|
+
utils.encrypt(word:any,key:any,iv:any)
|
|
33
|
+
// 对数据进行解密
|
|
34
|
+
utils.decrypt(word:any,key:any,iv:any)
|
|
12
35
|
```
|
|
13
|
-
|
|
36
|
+
#### 微信H5使用方法集合
|
|
37
|
+
##### 微信授权登录
|
|
38
|
+
```
|
|
39
|
+
interface configOption {
|
|
40
|
+
appId?:string, // 当前微信服务号 appId
|
|
41
|
+
scope?:string,// 网页授权类型
|
|
42
|
+
http?:any, // 执行微信登录时,请求后端的接口方法,默认请求数据格式 {"app_id":"","js_code":"" },app_id为当前传入的配置参数,js_code为自动截取。
|
|
43
|
+
codeKey?:string, // 回调地址栏中code存储键
|
|
44
|
+
stateKey?:string, // 回调地址栏中state 存储键
|
|
45
|
+
cryptoiv?:string, // 将地址栏携带参数加密iv
|
|
46
|
+
cryptokey?:string, // 将地址栏携带参数加密key
|
|
47
|
+
}
|
|
48
|
+
进入系统时,默认调用该方法,自动执行微信跳转授权,回调地址中code处理、state处理,
|
|
49
|
+
utils.weixinUrlCode(config)
|
|
50
|
+
```
|
|
51
|
+
#### 微信config接口权限注入
|
|
52
|
+
```
|
|
53
|
+
interface ShareConfig{
|
|
54
|
+
http:any, // 微信接口权限注入接口,后端接口,执行成功后返回数据格式:{appId:"",timestamp:"",noncestr:"",signature:""},成功后自动调取微信 wx.config 接口
|
|
55
|
+
cb?:any, // wx.ready 成功后的回调方法
|
|
56
|
+
appId:string, // 当前微信服务号 appId
|
|
57
|
+
jsApiList?:string[],
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
utils.weixinShareConfig(config:ShareConfig)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### 微信分享接口加载
|
|
14
64
|
```
|
|
15
|
-
|
|
65
|
+
interface Share{
|
|
66
|
+
title?:string, // 分享标题
|
|
67
|
+
desc?:string, // 分享描述
|
|
68
|
+
link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
|
|
69
|
+
imgUrl: '', // 分享图标
|
|
70
|
+
data?:any,//微信分享时需要携带的数据,默认传键值格式
|
|
71
|
+
iv?:string, // 分享链接中对 state 数据加密的iv
|
|
72
|
+
key?:string, // 分享链接中对 state 数据加密的 key
|
|
73
|
+
}
|
|
74
|
+
utils.weixinShareInit(config:Share)
|
|
16
75
|
```
|
|
17
76
|
|
package/app.ts
ADDED
package/dist/crypto/index.js
CHANGED
|
@@ -1,7 +1,30 @@
|
|
|
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
26
|
exports.decrypt = exports.encrypt = void 0;
|
|
4
|
-
var CryptoJS = require("crypto-js");
|
|
27
|
+
var CryptoJS = __importStar(require("crypto-js"));
|
|
5
28
|
var utils_1 = require("../utils");
|
|
6
29
|
function formatVal(val) {
|
|
7
30
|
var key = "";
|
package/dist/index.js
CHANGED
|
@@ -10,10 +10,33 @@ var __assign = (this && this.__assign) || function () {
|
|
|
10
10
|
};
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
+
}) : function(o, v) {
|
|
27
|
+
o["default"] = v;
|
|
28
|
+
});
|
|
29
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
30
|
+
if (mod && mod.__esModule) return mod;
|
|
31
|
+
var result = {};
|
|
32
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
33
|
+
__setModuleDefault(result, mod);
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
13
36
|
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");
|
|
37
|
+
var storage = __importStar(require("./storage"));
|
|
38
|
+
var utils = __importStar(require("./utils"));
|
|
39
|
+
var crypto = __importStar(require("./crypto"));
|
|
40
|
+
var weixin = __importStar(require("./weixin"));
|
|
18
41
|
var obj = __assign(__assign(__assign(__assign({}, storage), utils), weixin), crypto);
|
|
19
42
|
exports.default = obj;
|
package/dist/weixin/index.d.ts
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
interface configOption {
|
|
2
2
|
appId?: string;
|
|
3
3
|
http?: any;
|
|
4
|
+
scope?: string;
|
|
4
5
|
codeKey?: string;
|
|
5
6
|
stateKey?: string;
|
|
6
7
|
cryptoiv?: string;
|
|
7
8
|
cryptokey?: string;
|
|
8
9
|
}
|
|
9
10
|
export declare function weixinUrlCode(config: configOption): void;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export declare function
|
|
17
|
-
|
|
11
|
+
interface ShareConfig {
|
|
12
|
+
http: any;
|
|
13
|
+
cb?: any;
|
|
14
|
+
appId: string;
|
|
15
|
+
jsApiList?: string[];
|
|
16
|
+
}
|
|
17
|
+
export declare function weixinShareConfig(config: ShareConfig): void;
|
|
18
|
+
interface ShareOptions {
|
|
19
|
+
title?: string;
|
|
20
|
+
desc?: string;
|
|
21
|
+
link: '';
|
|
22
|
+
imgUrl: '';
|
|
23
|
+
data?: any;
|
|
24
|
+
iv?: string;
|
|
25
|
+
key?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function weixinShareInit(options: ShareOptions): void;
|
|
18
28
|
export {};
|
package/dist/weixin/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.weixinShareInit = exports.weixinShareConfig = exports.weixinUrlCode = void 0;
|
|
4
4
|
// import * as weixin from 'weixin-js-sdk' // 引入微信SDK
|
|
5
5
|
var weixin = require("weixin-js-sdk");
|
|
6
6
|
var utils_1 = require("../utils");
|
|
@@ -134,7 +134,7 @@ function formatSearch(search, keys) {
|
|
|
134
134
|
var urlArr = search.split("&");
|
|
135
135
|
return urlArr;
|
|
136
136
|
}
|
|
137
|
-
|
|
137
|
+
// 加载当前微信sdk环境
|
|
138
138
|
function weixinInit() {
|
|
139
139
|
var wx = null;
|
|
140
140
|
if (w.wx && w.wx.ready) {
|
|
@@ -145,14 +145,16 @@ function weixinInit() {
|
|
|
145
145
|
}
|
|
146
146
|
return wx;
|
|
147
147
|
}
|
|
148
|
-
exports.weixinInit = weixinInit;
|
|
149
148
|
function weixinCode() {
|
|
150
149
|
if ((0, utils_1.weixinBrowser)()) {
|
|
151
150
|
if (!weixinConfig['appId'])
|
|
152
151
|
return;
|
|
152
|
+
var scope = "snsapi_userinfo";
|
|
153
|
+
if (weixinConfig['scope'])
|
|
154
|
+
scope = weixinConfig['scope'];
|
|
153
155
|
var base = "https://open.weixin.qq.com/connect/oauth2/authorize?";
|
|
154
156
|
var uri = encodeURIComponent(loc.href.split("?")[0]);
|
|
155
|
-
var config = "appid=".concat(weixinConfig.appId, "&redirect_uri=").concat(uri, "&response_type=code&scope=
|
|
157
|
+
var config = "appid=".concat(weixinConfig.appId, "&redirect_uri=").concat(uri, "&response_type=code&scope=").concat(scope, "&state=state#wechat_redirect");
|
|
156
158
|
var wxJumpURL = base + config;
|
|
157
159
|
loc.replace(wxJumpURL);
|
|
158
160
|
}
|
|
@@ -161,4 +163,101 @@ function weixinCode() {
|
|
|
161
163
|
console.log("非微信内核浏览器执行其他业务");
|
|
162
164
|
}
|
|
163
165
|
}
|
|
164
|
-
|
|
166
|
+
function weixinShareConfig(config) {
|
|
167
|
+
var _a;
|
|
168
|
+
if (!config['http'])
|
|
169
|
+
return;
|
|
170
|
+
var url = loc.href.split("#")[0];
|
|
171
|
+
var param = {
|
|
172
|
+
url: url,
|
|
173
|
+
appId: config.appId,
|
|
174
|
+
};
|
|
175
|
+
var jsApiList = defaultJsApiList;
|
|
176
|
+
if ((_a = config.jsApiList) === null || _a === void 0 ? void 0 : _a.length)
|
|
177
|
+
jsApiList = config.jsApiList;
|
|
178
|
+
config.http(param).then(function (res) {
|
|
179
|
+
if (res.code == 0) {
|
|
180
|
+
var data = res.data;
|
|
181
|
+
var wx = weixinInit();
|
|
182
|
+
wx.config({
|
|
183
|
+
debug: false,
|
|
184
|
+
appId: data.appId,
|
|
185
|
+
timestamp: data.timestamp,
|
|
186
|
+
nonceStr: data.noncestr,
|
|
187
|
+
signature: data.signature,
|
|
188
|
+
jsApiList: jsApiList //必填,允许分享好友,分享朋友圈
|
|
189
|
+
});
|
|
190
|
+
wx.ready(function (res) {
|
|
191
|
+
console.log("加载微信分享配置完成:", res);
|
|
192
|
+
if ((config === null || config === void 0 ? void 0 : config.cb) && typeof config.cb === "function")
|
|
193
|
+
config.cb();
|
|
194
|
+
});
|
|
195
|
+
wx.error(function (err) {
|
|
196
|
+
console.log("加载微信分享配置错误:", err);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
exports.weixinShareConfig = weixinShareConfig;
|
|
202
|
+
var defaultJsApiList = [
|
|
203
|
+
'updateAppMessageShareData', 'updateTimelineShareData', "onMenuShareWeibo", "onMenuShareQZone", "startRecord", "stopRecord", "closeWindow", "scanQRCode", "chooseWXPay", "chooseCard", "openCard",
|
|
204
|
+
"onVoiceRecordEnd", "playVoice", "pauseVoice", "stopVoice", "onVoicePlayEnd", "uploadVoice", "downloadVoice", "chooseImage", "previewImage", "uploadImage", "openProductSpecificView", "addCard",
|
|
205
|
+
"downloadImage", "translateVoice", "getNetworkType", "openLocation", "getLocation", "hideOptionMenu", "showOptionMenu", "hideMenuItems", "showMenuItems", "hideAllNonBaseMenuItem", "showAllNonBaseMenuItem",
|
|
206
|
+
];
|
|
207
|
+
function weixinShareInit(options) {
|
|
208
|
+
var title = "微信分享";
|
|
209
|
+
if (options === null || options === void 0 ? void 0 : options.title)
|
|
210
|
+
title = options.title;
|
|
211
|
+
var desc = "微信分享描述";
|
|
212
|
+
if (options === null || options === void 0 ? void 0 : options.desc)
|
|
213
|
+
desc = options.desc;
|
|
214
|
+
var link = loc.href.split("#")[0];
|
|
215
|
+
if (options === null || options === void 0 ? void 0 : options.link)
|
|
216
|
+
link = options.link;
|
|
217
|
+
var imgUrl = "";
|
|
218
|
+
if (options === null || options === void 0 ? void 0 : options.imgUrl)
|
|
219
|
+
imgUrl = options.imgUrl;
|
|
220
|
+
if (options && typeof options == "object") {
|
|
221
|
+
var state = "";
|
|
222
|
+
if (options === null || options === void 0 ? void 0 : options.data) {
|
|
223
|
+
var data = options.data;
|
|
224
|
+
state = (0, crypto_1.encrypt)(data, options['key'], options['iv']);
|
|
225
|
+
link = "".concat(link, "?state=").concat(state);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
var wx = weixinInit();
|
|
229
|
+
// console.log("shareUrl===",link)
|
|
230
|
+
w.sessionStorage.setItem("shareUrl", link);
|
|
231
|
+
wx.ready(function () {
|
|
232
|
+
//需在用户可能点击分享按钮前就先调用
|
|
233
|
+
wx.updateAppMessageShareData({
|
|
234
|
+
title: title,
|
|
235
|
+
desc: desc,
|
|
236
|
+
link: link,
|
|
237
|
+
imgUrl: imgUrl,
|
|
238
|
+
success: function () {
|
|
239
|
+
// 设置成功
|
|
240
|
+
console.log("成功分享到朋友");
|
|
241
|
+
},
|
|
242
|
+
cancel: function () {
|
|
243
|
+
console.log("分享到朋友失败");
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
wx.updateTimelineShareData({
|
|
247
|
+
title: title,
|
|
248
|
+
link: link,
|
|
249
|
+
imgUrl: imgUrl,
|
|
250
|
+
success: function () {
|
|
251
|
+
// 设置成功
|
|
252
|
+
console.log("成功分享到朋友圈");
|
|
253
|
+
},
|
|
254
|
+
cancel: function () {
|
|
255
|
+
console.log("分享到朋友圈失败");
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
wx.error(function (res) {
|
|
260
|
+
console.log("配置进入错误:", res);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
exports.weixinShareInit = weixinShareInit;
|
package/package.json
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "xctc-utils",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "./dist/index.js",
|
|
6
|
-
"types": "./dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
-
"prepublish": "npm run build",
|
|
10
|
-
"build": "tsc"
|
|
11
|
-
},
|
|
12
|
-
"author": "",
|
|
13
|
-
"license": "ISC",
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"@types/node": "^18.11.19",
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"crypto-js": "^4.1.1",
|
|
21
|
-
"
|
|
22
|
-
"weixin-js-sdk
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "xctc-utils",
|
|
3
|
+
"version": "1.0.6",
|
|
4
|
+
"description": "localStorage存储\r ```\r sessionStorage存储\r ```\r crypto-js加密、解密\r ```\r 微信授权登录、微信分享\r ```\r 设备环境获取\r ```\r 是否是微信浏览器\r ```",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
+
"prepublish": "npm run build",
|
|
10
|
+
"build": "tsc"
|
|
11
|
+
},
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "^18.11.19",
|
|
16
|
+
"ts-node": "^10.9.1",
|
|
17
|
+
"typescript": "^4.9.5"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@types/crypto-js": "^4.1.1",
|
|
21
|
+
"crypto-js": "^4.1.1",
|
|
22
|
+
"weixin-js-sdk": "^1.6.0",
|
|
23
|
+
"weixin-js-sdk-ts": "^1.6.1"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git@gitee.com:npm-management/xctc-utils.git"
|
|
28
|
+
},
|
|
29
|
+
"keywords": []
|
|
30
|
+
}
|