@hairy/wechat-jssdk 1.47.0 → 1.49.0

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.
@@ -0,0 +1,117 @@
1
+ import * as WxType from "jweixin";
2
+
3
+ //#region src/types.d.ts
4
+ declare module 'jweixin' {
5
+ type v4ApiMethod = 'updateTimelineShareData' | 'updateAppMessageShareData';
6
+ function config(config_: {
7
+ debug?: boolean | undefined;
8
+ appId: string;
9
+ timestamp: number;
10
+ nonceStr: string;
11
+ signature: string;
12
+ jsApiList: (WxType.ApiMethod | v4ApiMethod)[];
13
+ }): void;
14
+ }
15
+ declare global {
16
+ const wx: typeof WxType;
17
+ }
18
+ type WechatJssdkConfig = Omit<Parameters<typeof wx.config>[0], 'catch' | 'finally' | 'then'>;
19
+ interface WechatJssdkOptions {
20
+ /**
21
+ * 初始化请求配置
22
+ */
23
+ requestConfig: () => Promise<WechatJssdkConfig> | WechatJssdkConfig;
24
+ /**
25
+ * 是否立即加载 config
26
+ *
27
+ * @default true
28
+ */
29
+ immediate?: boolean;
30
+ /**
31
+ * wechat-jssdk 版本
32
+ *
33
+ * @default 1.3.0
34
+ */
35
+ version?: '1.2.0' | '1.3.0' | '1.4.0' | '1.5.0' | '1.6.0';
36
+ }
37
+ //#endregion
38
+ //#region src/helper.d.ts
39
+ /**
40
+ * jssdk 的前置处理类
41
+ * @constructor 加载配置(apiHost,appId,config,version,jssdk)
42
+ * @function implement 提供 wxApi 执行器(等待 config 与 ready 结束,在调用 api)
43
+ * @function config 加载配置,提供 promise 式返回
44
+ * @function ready wx.ready,提供 promise 式返回
45
+ */
46
+ declare class WechatJssdkHelper {
47
+ private version;
48
+ private isReady;
49
+ private requestConfig;
50
+ private loadPromise?;
51
+ private configPromise?;
52
+ private readyPromise?;
53
+ private loadJssdk;
54
+ constructor(options: WechatJssdkOptions);
55
+ config(): Promise<void>;
56
+ ready(): Promise<void>;
57
+ error(): Promise<any>;
58
+ protected implement<Key extends keyof typeof wx | string>(key: Key, options?: any): Promise<void>;
59
+ }
60
+ //#endregion
61
+ //#region src/index.d.ts
62
+ /**
63
+ * @description WechatJssdk Api 封装,与 [jssdk 文档](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html) 保持一致,主要以下改动
64
+ * @description 内置请求,读取 config 配置,初始化自动调用 wx.config
65
+ * @description 所有 api 的 promise 化处理(仅等待 wx.ready)
66
+ * @description 简易化初始化构建,config 处理
67
+ * @description 可指定加载 jssdk 版本(默认 1.3.0)
68
+ * @template `const wxJssdk = new WechatJssdk({env, config})`
69
+ */
70
+ declare class WechatJssdk extends WechatJssdkHelper {
71
+ constructor(options: WechatJssdkOptions);
72
+ updateAppMessageShareData(options: WxType.IonMenuShareAppMessage): Promise<void>;
73
+ updateTimelineShareData(options: WxType.IonMenuShareAppMessage): Promise<void>;
74
+ addCard(): Promise<void>;
75
+ checkJsApi(options: WxType.IcheckJsApi): Promise<void>;
76
+ chooseCard(options: WxType.IchooseCard): Promise<void>;
77
+ chooseImage(options: WxType.IchooseImage): Promise<void>;
78
+ chooseWXPay(options: WxType.IchooseWXPay): Promise<void>;
79
+ closeWindow(): Promise<void>;
80
+ consumeAndShareCard(options: WxType.IconsumeAndShareCard): Promise<void>;
81
+ downloadImage(options: WxType.IdownloadImage): Promise<void>;
82
+ downloadVoice(options: WxType.IupdownloadVoice): Promise<void>;
83
+ getLocalImgData(options: WxType.IgetLocalImgData): Promise<void>;
84
+ getLocation(options: WxType.IgetLocation): Promise<void>;
85
+ getNetworkType(options: WxType.IgetNetworkType): Promise<void>;
86
+ hideAllNonBaseMenuItem(): Promise<void>;
87
+ hideMenuItems(options: WxType.IhideMenuItems): Promise<void>;
88
+ hideOptionMenu(): Promise<void>;
89
+ onMenuShareAppMessage(options: WxType.IonMenuShareAppMessage): Promise<void>;
90
+ onMenuShareQQ(options: WxType.IonMenuShareQQ): Promise<void>;
91
+ onMenuShareQZone(options: WxType.IonMenuShareQZone): Promise<void>;
92
+ onMenuShareTimeline(options: WxType.IonMenuShareTimeline): Promise<void>;
93
+ onMenuShareWeibo(options: WxType.IonMenuShareWeibo): Promise<void>;
94
+ onSearchBeacons(options: WxType.IonSearchBeacons): Promise<void>;
95
+ onVoicePlayEnd(options: WxType.IonVoicePlayEnd): Promise<void>;
96
+ onVoiceRecordEnd(options: WxType.IonVoiceRecordEnd): Promise<void>;
97
+ openCard(options: WxType.IopenCard): Promise<void>;
98
+ openLocation(options: WxType.IopenLocation): Promise<void>;
99
+ openProductSpecificView(options: WxType.IopenProductSpecificView): Promise<void>;
100
+ pauseVoice(options: WxType.IplaypausestopVoice): Promise<void>;
101
+ playVoice(options: WxType.IplaypausestopVoice): Promise<void>;
102
+ previewImage(options: WxType.IpreviewImage): Promise<void>;
103
+ scanQRCode(options: WxType.IscanQRCode): Promise<void>;
104
+ showAllNonBaseMenuItem(): Promise<void>;
105
+ showMenuItems(options: WxType.IshowMenuItems): Promise<void>;
106
+ showOptionMenu(): Promise<void>;
107
+ startRecord(): Promise<void>;
108
+ startSearchBeacons(options: WxType.IstartSearchBeacons): Promise<void>;
109
+ stopRecord(options: WxType.IstopRecord): Promise<void>;
110
+ stopSearchBeacons(options: WxType.IstopSearchBeacons): Promise<void>;
111
+ stopVoice(options: WxType.IplaypausestopVoice): Promise<void>;
112
+ translateVoice(options: WxType.ItranslateVoice): Promise<void>;
113
+ uploadImage(options: WxType.IuploadImage): Promise<void>;
114
+ uploadVoice(options: WxType.IupdownloadVoice): Promise<void>;
115
+ }
116
+ //#endregion
117
+ export { WechatJssdk };
package/dist/index.mjs ADDED
@@ -0,0 +1,242 @@
1
+ import { noop } from "@hairy/utils";
2
+
3
+ //#region src/internal/load_script.ts
4
+ function loadScript(source, onLoaded = noop, options = {}) {
5
+ const { type = "text/javascript", async = true, crossOrigin, referrerPolicy, noModule, defer, waitForScriptLoad = true } = options;
6
+ return new Promise((resolve, reject) => {
7
+ let shouldAppend = false;
8
+ let element = document.querySelector(`script[src="${source}"]`);
9
+ if (!element) {
10
+ element = document.createElement("script");
11
+ element.type = type;
12
+ element.async = async;
13
+ element.src = source;
14
+ if (defer) element.defer = defer;
15
+ if (crossOrigin) element.crossOrigin = crossOrigin;
16
+ if (noModule) element.noModule = noModule;
17
+ if (referrerPolicy) element.referrerPolicy = referrerPolicy;
18
+ shouldAppend = true;
19
+ } else if (Object.hasOwn(element.dataset, "loaded")) resolve(element);
20
+ element.addEventListener("error", (event) => reject(event));
21
+ element.addEventListener("abort", (event) => reject(event));
22
+ element.addEventListener("load", () => {
23
+ element.dataset.loaded = "true";
24
+ onLoaded(element);
25
+ resolve(element);
26
+ });
27
+ if (shouldAppend) element = document.head.appendChild(element);
28
+ if (!waitForScriptLoad) resolve(element);
29
+ });
30
+ }
31
+
32
+ //#endregion
33
+ //#region src/helper.ts
34
+ /**
35
+ * jssdk 的前置处理类
36
+ * @constructor 加载配置(apiHost,appId,config,version,jssdk)
37
+ * @function implement 提供 wxApi 执行器(等待 config 与 ready 结束,在调用 api)
38
+ * @function config 加载配置,提供 promise 式返回
39
+ * @function ready wx.ready,提供 promise 式返回
40
+ */
41
+ var WechatJssdkHelper = class {
42
+ version;
43
+ isReady = false;
44
+ requestConfig;
45
+ loadPromise;
46
+ configPromise;
47
+ readyPromise;
48
+ loadJssdk() {
49
+ this.loadPromise = loadScript(`https://res.wx.qq.com/open/js/jweixin-${this.version}.js`).then(() => {
50
+ this.loadPromise = void 0;
51
+ });
52
+ }
53
+ constructor(options) {
54
+ const { immediate = true, version, requestConfig } = options;
55
+ this.version = version || "1.3.0";
56
+ this.requestConfig = requestConfig;
57
+ this.loadJssdk();
58
+ if (immediate) this.config();
59
+ }
60
+ async config() {
61
+ if (this.configPromise) return this.configPromise;
62
+ if (this.loadPromise) await this.loadPromise;
63
+ this.configPromise = (async () => {
64
+ wx.config(await this.requestConfig());
65
+ })();
66
+ return this.configPromise;
67
+ }
68
+ async ready() {
69
+ if (this.readyPromise) return this.readyPromise;
70
+ if (this.loadPromise) await this.loadPromise;
71
+ if (this.isReady) return;
72
+ this.readyPromise = new Promise((resolve, reject) => {
73
+ wx.ready(() => {
74
+ this.isReady = true;
75
+ this.readyPromise = void 0;
76
+ resolve();
77
+ });
78
+ wx.error((error) => {
79
+ this.isReady = false;
80
+ this.readyPromise = void 0;
81
+ reject(error);
82
+ });
83
+ });
84
+ return this.readyPromise;
85
+ }
86
+ async error() {
87
+ return this.ready().catch((error) => error);
88
+ }
89
+ async implement(key, options) {
90
+ if (this.configPromise) await this.configPromise;
91
+ await this.ready();
92
+ wx[key](options);
93
+ }
94
+ };
95
+
96
+ //#endregion
97
+ //#region src/index.ts
98
+ /**
99
+ * @description WechatJssdk Api 封装,与 [jssdk 文档](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html) 保持一致,主要以下改动
100
+ * @description 内置请求,读取 config 配置,初始化自动调用 wx.config
101
+ * @description 所有 api 的 promise 化处理(仅等待 wx.ready)
102
+ * @description 简易化初始化构建,config 处理
103
+ * @description 可指定加载 jssdk 版本(默认 1.3.0)
104
+ * @template `const wxJssdk = new WechatJssdk({env, config})`
105
+ */
106
+ var WechatJssdk = class extends WechatJssdkHelper {
107
+ constructor(options) {
108
+ super(options);
109
+ }
110
+ updateAppMessageShareData(options) {
111
+ return this.implement("updateAppMessageShareData", options);
112
+ }
113
+ updateTimelineShareData(options) {
114
+ return this.implement("updateTimelineShareData", options);
115
+ }
116
+ addCard() {
117
+ return this.implement("addCard");
118
+ }
119
+ checkJsApi(options) {
120
+ return this.implement("checkJsApi", options);
121
+ }
122
+ chooseCard(options) {
123
+ return this.implement("chooseCard", options);
124
+ }
125
+ chooseImage(options) {
126
+ return this.implement("chooseImage", options);
127
+ }
128
+ chooseWXPay(options) {
129
+ return this.implement("chooseWXPay", options);
130
+ }
131
+ closeWindow() {
132
+ return this.implement("closeWindow");
133
+ }
134
+ consumeAndShareCard(options) {
135
+ return this.implement("consumeAndShareCard", options);
136
+ }
137
+ downloadImage(options) {
138
+ return this.implement("downloadImage", options);
139
+ }
140
+ downloadVoice(options) {
141
+ return this.implement("downloadVoice", options);
142
+ }
143
+ getLocalImgData(options) {
144
+ return this.implement("getLocalImgData", options);
145
+ }
146
+ getLocation(options) {
147
+ return this.implement("getLocation", options);
148
+ }
149
+ getNetworkType(options) {
150
+ return this.implement("getNetworkType", options);
151
+ }
152
+ hideAllNonBaseMenuItem() {
153
+ return this.implement("hideAllNonBaseMenuItem");
154
+ }
155
+ hideMenuItems(options) {
156
+ return this.implement("hideMenuItems", options);
157
+ }
158
+ hideOptionMenu() {
159
+ return this.implement("hideOptionMenu");
160
+ }
161
+ onMenuShareAppMessage(options) {
162
+ return this.implement("onMenuShareAppMessage", options);
163
+ }
164
+ onMenuShareQQ(options) {
165
+ return this.implement("onMenuShareQQ", options);
166
+ }
167
+ onMenuShareQZone(options) {
168
+ return this.implement("onMenuShareQZone", options);
169
+ }
170
+ onMenuShareTimeline(options) {
171
+ return this.implement("onMenuShareTimeline", options);
172
+ }
173
+ onMenuShareWeibo(options) {
174
+ return this.implement("onMenuShareWeibo", options);
175
+ }
176
+ onSearchBeacons(options) {
177
+ return this.implement("onSearchBeacons", options);
178
+ }
179
+ onVoicePlayEnd(options) {
180
+ return this.implement("onVoicePlayEnd", options);
181
+ }
182
+ onVoiceRecordEnd(options) {
183
+ return this.implement("onVoiceRecordEnd", options);
184
+ }
185
+ openCard(options) {
186
+ return this.implement("openCard", options);
187
+ }
188
+ openLocation(options) {
189
+ return this.implement("openLocation", options);
190
+ }
191
+ openProductSpecificView(options) {
192
+ return this.implement("openProductSpecificView", options);
193
+ }
194
+ pauseVoice(options) {
195
+ return this.implement("pauseVoice", options);
196
+ }
197
+ playVoice(options) {
198
+ return this.implement("playVoice", options);
199
+ }
200
+ previewImage(options) {
201
+ return this.implement("previewImage", options);
202
+ }
203
+ scanQRCode(options) {
204
+ return this.implement("scanQRCode", options);
205
+ }
206
+ showAllNonBaseMenuItem() {
207
+ return this.implement("showAllNonBaseMenuItem");
208
+ }
209
+ showMenuItems(options) {
210
+ return this.implement("showMenuItems", options);
211
+ }
212
+ showOptionMenu() {
213
+ return this.implement("showOptionMenu");
214
+ }
215
+ startRecord() {
216
+ return this.implement("startRecord");
217
+ }
218
+ startSearchBeacons(options) {
219
+ return this.implement("startSearchBeacons", options);
220
+ }
221
+ stopRecord(options) {
222
+ return this.implement("stopRecord", options);
223
+ }
224
+ stopSearchBeacons(options) {
225
+ return this.implement("stopSearchBeacons", options);
226
+ }
227
+ stopVoice(options) {
228
+ return this.implement("stopVoice", options);
229
+ }
230
+ translateVoice(options) {
231
+ return this.implement("translateVoice", options);
232
+ }
233
+ uploadImage(options) {
234
+ return this.implement("uploadImage", options);
235
+ }
236
+ uploadVoice(options) {
237
+ return this.implement("uploadVoice", options);
238
+ }
239
+ };
240
+
241
+ //#endregion
242
+ export { WechatJssdk };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hairy/wechat-jssdk",
3
3
  "type": "module",
4
- "version": "1.47.0",
4
+ "version": "1.49.0",
5
5
  "description": "Library for Wechat Jssdk",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",
@@ -14,33 +14,30 @@
14
14
  "bugs": "https://github.com/hairyf/hairylib/issues",
15
15
  "keywords": [],
16
16
  "sideEffects": false,
17
- "main": "./dist/index.js",
18
- "publishConfig": {
19
- "jsdelivr": "./dist/index.global.js"
17
+ "exports": {
18
+ ".": {
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.cjs"
21
+ },
22
+ "./package.json": "./package.json"
20
23
  },
24
+ "main": "./dist/index.mjs",
25
+ "module": "./dist/index.mjs",
26
+ "types": "./dist/index.d.mts",
21
27
  "files": [
22
28
  "dist"
23
29
  ],
24
- "dependencies": {
25
- "@hairy/utils": "1.47.0"
30
+ "publishConfig": {
31
+ "jsdelivr": "./dist/index.iife.js"
26
32
  },
27
- "devDependencies": {
28
- "@types/jweixin": "^1.0.8"
33
+ "dependencies": {
34
+ "@types/jweixin": "^1.0.8",
35
+ "@hairy/utils": "1.49.0"
29
36
  },
30
37
  "scripts": {
31
- "build": "tsup",
32
- "dev": "tsup --watch",
38
+ "build": "tsdown",
39
+ "dev": "tsdown --watch",
33
40
  "start": "tsx src/index.ts"
34
41
  },
35
- "module": "./dist/index.js",
36
- "types": "./dist/index.d.ts",
37
- "unpkg": "./dist/index.global.js",
38
- "exports": {
39
- ".": {
40
- "import": "./dist/index.js",
41
- "require": "./dist/index.cjs",
42
- "types": "./dist/index.d.ts"
43
- },
44
- "./*": "./*"
45
- }
42
+ "unpkg": "./dist/index.iife.js"
46
43
  }
package/dist/index.d.ts DELETED
@@ -1,114 +0,0 @@
1
- import * as WxType from 'jweixin';
2
-
3
- declare module 'jweixin' {
4
- type v4ApiMethod = 'updateTimelineShareData' | 'updateAppMessageShareData';
5
- function config(config_: {
6
- debug?: boolean | undefined;
7
- appId: string;
8
- timestamp: number;
9
- nonceStr: string;
10
- signature: string;
11
- jsApiList: (WxType.ApiMethod | v4ApiMethod)[];
12
- }): void;
13
- }
14
- declare global {
15
- const wx: typeof WxType;
16
- }
17
- type WechatJssdkConfig = Omit<Parameters<typeof wx.config>[0], 'catch' | 'finally' | 'then'>;
18
- interface WechatJssdkOptions {
19
- /**
20
- * 初始化请求配置
21
- */
22
- requestConfig: () => Promise<WechatJssdkConfig> | WechatJssdkConfig;
23
- /**
24
- * 是否立即加载 config
25
- *
26
- * @default true
27
- */
28
- immediate?: boolean;
29
- /**
30
- * wechat-jssdk 版本
31
- *
32
- * @default 1.3.0
33
- */
34
- version?: '1.2.0' | '1.3.0' | '1.4.0' | '1.5.0' | '1.6.0';
35
- }
36
-
37
- /**
38
- * jssdk 的前置处理类
39
- * @constructor 加载配置(apiHost,appId,config,version,jssdk)
40
- * @function implement 提供 wxApi 执行器(等待 config 与 ready 结束,在调用 api)
41
- * @function config 加载配置,提供 promise 式返回
42
- * @function ready wx.ready,提供 promise 式返回
43
- */
44
- declare class WechatJssdkHelper {
45
- private version;
46
- private isReady;
47
- private requestConfig;
48
- private loadPromise?;
49
- private configPromise?;
50
- private readyPromise?;
51
- private loadJssdk;
52
- constructor(options: WechatJssdkOptions);
53
- config(): Promise<void>;
54
- ready(): Promise<void>;
55
- error(): Promise<any>;
56
- protected implement<Key extends keyof typeof wx | string>(key: Key, options?: any): Promise<void>;
57
- }
58
-
59
- /**
60
- * @description WechatJssdk Api 封装,与 [jssdk 文档](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html) 保持一致,主要以下改动
61
- * @description 内置请求,读取 config 配置,初始化自动调用 wx.config
62
- * @description 所有 api 的 promise 化处理(仅等待 wx.ready)
63
- * @description 简易化初始化构建,config 处理
64
- * @description 可指定加载 jssdk 版本(默认 1.3.0)
65
- * @template `const wxJssdk = new WechatJssdk({env, config})`
66
- */
67
- declare class WechatJssdk extends WechatJssdkHelper {
68
- constructor(options: WechatJssdkOptions);
69
- updateAppMessageShareData(options: WxType.IonMenuShareAppMessage): Promise<void>;
70
- updateTimelineShareData(options: WxType.IonMenuShareAppMessage): Promise<void>;
71
- addCard(): Promise<void>;
72
- checkJsApi(options: WxType.IcheckJsApi): Promise<void>;
73
- chooseCard(options: WxType.IchooseCard): Promise<void>;
74
- chooseImage(options: WxType.IchooseImage): Promise<void>;
75
- chooseWXPay(options: WxType.IchooseWXPay): Promise<void>;
76
- closeWindow(): Promise<void>;
77
- consumeAndShareCard(options: WxType.IconsumeAndShareCard): Promise<void>;
78
- downloadImage(options: WxType.IdownloadImage): Promise<void>;
79
- downloadVoice(options: WxType.IupdownloadVoice): Promise<void>;
80
- getLocalImgData(options: WxType.IgetLocalImgData): Promise<void>;
81
- getLocation(options: WxType.IgetLocation): Promise<void>;
82
- getNetworkType(options: WxType.IgetNetworkType): Promise<void>;
83
- hideAllNonBaseMenuItem(): Promise<void>;
84
- hideMenuItems(options: WxType.IhideMenuItems): Promise<void>;
85
- hideOptionMenu(): Promise<void>;
86
- onMenuShareAppMessage(options: WxType.IonMenuShareAppMessage): Promise<void>;
87
- onMenuShareQQ(options: WxType.IonMenuShareQQ): Promise<void>;
88
- onMenuShareQZone(options: WxType.IonMenuShareQZone): Promise<void>;
89
- onMenuShareTimeline(options: WxType.IonMenuShareTimeline): Promise<void>;
90
- onMenuShareWeibo(options: WxType.IonMenuShareWeibo): Promise<void>;
91
- onSearchBeacons(options: WxType.IonSearchBeacons): Promise<void>;
92
- onVoicePlayEnd(options: WxType.IonVoicePlayEnd): Promise<void>;
93
- onVoiceRecordEnd(options: WxType.IonVoiceRecordEnd): Promise<void>;
94
- openCard(options: WxType.IopenCard): Promise<void>;
95
- openLocation(options: WxType.IopenLocation): Promise<void>;
96
- openProductSpecificView(options: WxType.IopenProductSpecificView): Promise<void>;
97
- pauseVoice(options: WxType.IplaypausestopVoice): Promise<void>;
98
- playVoice(options: WxType.IplaypausestopVoice): Promise<void>;
99
- previewImage(options: WxType.IpreviewImage): Promise<void>;
100
- scanQRCode(options: WxType.IscanQRCode): Promise<void>;
101
- showAllNonBaseMenuItem(): Promise<void>;
102
- showMenuItems(options: WxType.IshowMenuItems): Promise<void>;
103
- showOptionMenu(): Promise<void>;
104
- startRecord(): Promise<void>;
105
- startSearchBeacons(options: WxType.IstartSearchBeacons): Promise<void>;
106
- stopRecord(options: WxType.IstopRecord): Promise<void>;
107
- stopSearchBeacons(options: WxType.IstopSearchBeacons): Promise<void>;
108
- stopVoice(options: WxType.IplaypausestopVoice): Promise<void>;
109
- translateVoice(options: WxType.ItranslateVoice): Promise<void>;
110
- uploadImage(options: WxType.IuploadImage): Promise<void>;
111
- uploadVoice(options: WxType.IupdownloadVoice): Promise<void>;
112
- }
113
-
114
- export { WechatJssdk };