baja-lite 1.6.4 → 1.6.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.
Files changed (73) hide show
  1. package/boot-remote.d.ts +2 -0
  2. package/{src/boot-remote.ts → boot-remote.js} +66 -64
  3. package/boot.d.ts +2 -0
  4. package/{src/boot.ts → boot.js} +166 -170
  5. package/code.d.ts +2 -0
  6. package/{src/code.ts → code.js} +405 -414
  7. package/convert-xml.d.ts +10 -0
  8. package/{src/convert-xml.ts → convert-xml.js} +410 -460
  9. package/error.d.ts +5 -0
  10. package/error.js +13 -0
  11. package/event.d.ts +10 -0
  12. package/event.js +38 -0
  13. package/fn.d.ts +128 -0
  14. package/fn.js +172 -0
  15. package/{src/index.ts → index.d.ts} +10 -11
  16. package/index.js +10 -0
  17. package/math.d.ts +83 -0
  18. package/math.js +451 -0
  19. package/object.d.ts +126 -0
  20. package/object.js +321 -0
  21. package/package.json +13 -13
  22. package/snowflake.d.ts +12 -0
  23. package/{src/snowflake.ts → snowflake.js} +108 -127
  24. package/sql.d.ts +2151 -0
  25. package/sql.js +5371 -0
  26. package/sqlite.d.ts +32 -0
  27. package/{src/sqlite.ts → sqlite.js} +156 -157
  28. package/string.d.ts +17 -0
  29. package/string.js +105 -0
  30. package/test-mysql.d.ts +2 -0
  31. package/test-mysql.js +114 -0
  32. package/test-postgresql.d.ts +2 -0
  33. package/{src/test-postgresql.ts → test-postgresql.js} +91 -80
  34. package/test-sqlite.d.ts +1 -0
  35. package/{src/test-sqlite.ts → test-sqlite.js} +90 -80
  36. package/test-xml.d.ts +1 -0
  37. package/{src/test-xml.ts → test-xml.js} +2 -2
  38. package/test.d.ts +1 -0
  39. package/{src/test.ts → test.js} +2 -3
  40. package/wx/base.d.ts +11 -0
  41. package/wx/base.js +78 -0
  42. package/wx/mini.d.ts +52 -0
  43. package/wx/mini.js +112 -0
  44. package/wx/organ.d.ts +65 -0
  45. package/wx/organ.js +171 -0
  46. package/{src/wx/types.ts → wx/types.d.ts} +560 -549
  47. package/wx/types.js +1 -0
  48. package/{src/wx.ts → wx.d.ts} +3 -3
  49. package/wx.js +3 -0
  50. package/.eslintignore +0 -7
  51. package/.eslintrc.cjs +0 -89
  52. package/.prettierrc +0 -7
  53. package/.vscode/settings.json +0 -9
  54. package/ci.js +0 -33
  55. package/package-cjs.json +0 -17
  56. package/pnpm-lock.yaml +0 -2840
  57. package/pnpm-workspace.yaml +0 -2
  58. package/src/error.ts +0 -11
  59. package/src/event.ts +0 -34
  60. package/src/fn.ts +0 -295
  61. package/src/math.ts +0 -405
  62. package/src/object.ts +0 -342
  63. package/src/sql.ts +0 -5529
  64. package/src/string.ts +0 -111
  65. package/src/test-mysql.ts +0 -148
  66. package/src/wx/base.ts +0 -77
  67. package/src/wx/mini.ts +0 -147
  68. package/src/wx/organ.ts +0 -290
  69. package/tsconfig.cjs.json +0 -42
  70. package/tsconfig.json +0 -44
  71. package/xml/event-report.xml +0 -13
  72. package/yarn.lock +0 -1977
  73. /package/{Readme.md → README.md} +0 -0
@@ -1,80 +1,91 @@
1
- import { DBType, Field, SqlType } from 'baja-lite-field';
2
- import 'reflect-metadata';
3
- import { Boot } from './boot.js';
4
- import { ColumnMode, DB, SelectResult, SqlService, } from './sql.js';
5
- class AmaFuck2 {
6
- @Field({ type: SqlType.int, length: 200, id: true, uuid: true })
7
- userid?: number;
8
- @Field({ type: SqlType.varchar, length: 200, def: '333' })
9
- username?: string;
10
- @Field({ type: SqlType.varchar, length: 200 })
11
- pwd?: string;
12
- }
13
-
14
- @DB({
15
- tableName: 'nfc_user', clz: AmaFuck2, dbType: DBType.Postgresql
16
- })
17
- class AmaService2 extends SqlService<AmaFuck2> {
18
-
19
- }
20
-
21
- // interface Menu {
22
- // resourceid: string;
23
- // resourcepid: string;
24
- // resourcename: string;
25
- // children: Menu[];
26
- // }
27
-
28
- export async function go2() {
29
- await Boot({
30
- Postgresql: {
31
- database: 'nfc',
32
- user: 'postgres',
33
- password: 'abcd1234',
34
- port: 5432,
35
- host: '127.0.0.1'
36
- },
37
- log: 'info',
38
- columnMode: ColumnMode.HUMP,
39
- sqlDir: 'E:/pro/baja-lite/xml',
40
- sqlMap: {
41
- ['test.test']: `SELECT * FROM nfc_user {{#username}} WHERE username = :username {{/username}}`
42
- }
43
- });
44
- const service = new AmaService2();
45
- await service.transaction<number>({
46
- fn: async conn => {
47
- const rt5 = await service.select<{ username: string }>({
48
- sqlId: 'test.test',
49
- params: { username: '1111' },
50
- selectResult: SelectResult.RS_CS,
51
- conn
52
- })
53
- console.log(55, rt5.length);
54
- await service.insert({
55
- data: {
56
- userid: 22,
57
- username: '222',
58
- pwd: '333'
59
- }
60
- })
61
- return 1;
62
- }
63
- });
64
-
65
- // const data = await service.select<Menu>({
66
- // sql: 'SELECT resourceid, resourcepid,resourcename FROM cp_resource ',
67
- // params: {
68
- // site: '1234',
69
- // matchInfo: { reportType: 'yyyy', id: '11' }
70
- // },
71
- // // mapper: [
72
- // // ['site', ['info', 'bSist'], 989],
73
- // // ['site', ['info2', 'bSist'], 33],
74
- // // ['site', ['Bsite'], 0]
75
- // // ]
76
- // });
77
- // console.log(data);
78
-
79
- }
80
- go2();
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DBType, Field, SqlType } from 'baja-lite-field';
11
+ import 'reflect-metadata';
12
+ import { Boot } from './boot.js';
13
+ import { ColumnMode, DB, SelectResult, SqlService, } from './sql.js';
14
+ class AmaFuck2 {
15
+ }
16
+ __decorate([
17
+ Field({ type: SqlType.int, length: 200, id: true, uuid: true }),
18
+ __metadata("design:type", Number)
19
+ ], AmaFuck2.prototype, "userid", void 0);
20
+ __decorate([
21
+ Field({ type: SqlType.varchar, length: 200, def: '333' }),
22
+ __metadata("design:type", String)
23
+ ], AmaFuck2.prototype, "username", void 0);
24
+ __decorate([
25
+ Field({ type: SqlType.varchar, length: 200 }),
26
+ __metadata("design:type", String)
27
+ ], AmaFuck2.prototype, "pwd", void 0);
28
+ let AmaService2 = class AmaService2 extends SqlService {
29
+ };
30
+ AmaService2 = __decorate([
31
+ DB({
32
+ tableName: 'nfc_user', clz: AmaFuck2, dbType: DBType.Postgresql
33
+ })
34
+ ], AmaService2);
35
+ // interface Menu {
36
+ // resourceid: string;
37
+ // resourcepid: string;
38
+ // resourcename: string;
39
+ // children: Menu[];
40
+ // }
41
+ export async function go2() {
42
+ await Boot({
43
+ Postgresql: {
44
+ database: 'nfc',
45
+ user: 'postgres',
46
+ password: 'abcd1234',
47
+ port: 5432,
48
+ host: '127.0.0.1'
49
+ },
50
+ log: 'info',
51
+ columnMode: ColumnMode.HUMP,
52
+ sqlDir: 'E:/pro/baja-lite/xml',
53
+ sqlMap: {
54
+ ['test.test']: `SELECT * FROM nfc_user {{#username}} WHERE username = :username {{/username}}`
55
+ }
56
+ });
57
+ const service = new AmaService2();
58
+ await service.transaction({
59
+ fn: async (conn) => {
60
+ const rt5 = await service.select({
61
+ sqlId: 'test.test',
62
+ params: { username: '1111' },
63
+ selectResult: SelectResult.RS_CS,
64
+ conn
65
+ });
66
+ console.log(55, rt5.length);
67
+ await service.insert({
68
+ data: {
69
+ userid: 22,
70
+ username: '222',
71
+ pwd: '333'
72
+ }
73
+ });
74
+ return 1;
75
+ }
76
+ });
77
+ // const data = await service.select<Menu>({
78
+ // sql: 'SELECT resourceid, resourcepid,resourcename FROM cp_resource ',
79
+ // params: {
80
+ // site: '1234',
81
+ // matchInfo: { reportType: 'yyyy', id: '11' }
82
+ // },
83
+ // // mapper: [
84
+ // // ['site', ['info', 'bSist'], 989],
85
+ // // ['site', ['info2', 'bSist'], 33],
86
+ // // ['site', ['Bsite'], 0]
87
+ // // ]
88
+ // });
89
+ // console.log(data);
90
+ }
91
+ go2();
@@ -0,0 +1 @@
1
+ import 'reflect-metadata';
@@ -1,80 +1,90 @@
1
- import { DBType, Field, SqlType } from 'baja-lite-field';
2
- import 'reflect-metadata';
3
- import { Boot } from './boot';
4
- import { DB, DeleteMode, InsertMode, SqlService, SyncMode } from './sql';
5
- class AmaFuck {
6
- @Field({ type: SqlType.varchar })
7
- site?: string;
8
- @Field({ type: SqlType.varchar })
9
- SellerSKU2?: string;
10
- @Field({ type: SqlType.varchar, id: true })
11
- SellerSKU?: string;
12
- }
13
-
14
- @DB({
15
- tableName: 'ama_fuck2', clz: AmaFuck, dbType: DBType.Sqlite, sqliteVersion: '0.0.3'
16
- })
17
- class AmaService extends SqlService<AmaFuck> {
18
-
19
- }
20
- async function go() {
21
- await Boot({
22
- Sqlite: 'd:1.db',
23
- log: 'info'
24
- });
25
- const service = new AmaService();
26
- const ret = service.transaction<number>({
27
- sync: SyncMode.Sync,
28
- fn: conn => {
29
- const list = new Array<AmaFuck>({
30
- SellerSKU: '1',
31
- site: '111'
32
- }, {
33
- SellerSKU: '2',
34
- SellerSKU2: '22',
35
- }, {
36
- SellerSKU2: '33',
37
- SellerSKU: '3',
38
- site: '333'
39
- }, {
40
- SellerSKU2: '44',
41
- SellerSKU: '4',
42
- site: '444'
43
- }, {
44
- SellerSKU2: '',
45
- SellerSKU: '66',
46
- site: undefined
47
- });
48
- const rt = service.insert({
49
- sync: SyncMode.Sync,
50
- data: list,
51
- conn, skipEmptyString: false, mode: InsertMode.InsertWithTempTable
52
- });
53
- console.log(rt);
54
- const rt2 = service.update({
55
- sync: SyncMode.Sync,
56
- data: list,
57
- conn, skipEmptyString: false
58
- });
59
- console.log(rt2);
60
- const rt3 = service.delete({
61
- sync: SyncMode.Sync,
62
- where: [
63
- { SellerSKU2: '11', SellerSKU: '1' },
64
- { SellerSKU2: '22', SellerSKU: '2' },
65
- { SellerSKU2: '33', SellerSKU: '3' }
66
- ],
67
- mode: DeleteMode.TempTable
68
- });
69
- console.log(rt3);
70
- return 1;
71
- }
72
- });
73
- return ret;
74
- }
75
-
76
-
77
-
78
-
79
-
80
- go();
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { DBType, Field, SqlType } from 'baja-lite-field';
11
+ import 'reflect-metadata';
12
+ import { Boot } from './boot';
13
+ import { DB, DeleteMode, InsertMode, SqlService, SyncMode } from './sql';
14
+ class AmaFuck {
15
+ }
16
+ __decorate([
17
+ Field({ type: SqlType.varchar }),
18
+ __metadata("design:type", String)
19
+ ], AmaFuck.prototype, "site", void 0);
20
+ __decorate([
21
+ Field({ type: SqlType.varchar }),
22
+ __metadata("design:type", String)
23
+ ], AmaFuck.prototype, "SellerSKU2", void 0);
24
+ __decorate([
25
+ Field({ type: SqlType.varchar, id: true }),
26
+ __metadata("design:type", String)
27
+ ], AmaFuck.prototype, "SellerSKU", void 0);
28
+ let AmaService = class AmaService extends SqlService {
29
+ };
30
+ AmaService = __decorate([
31
+ DB({
32
+ tableName: 'ama_fuck2', clz: AmaFuck, dbType: DBType.Sqlite, sqliteVersion: '0.0.3'
33
+ })
34
+ ], AmaService);
35
+ async function go() {
36
+ await Boot({
37
+ Sqlite: 'd:1.db',
38
+ log: 'info'
39
+ });
40
+ const service = new AmaService();
41
+ const ret = service.transaction({
42
+ sync: SyncMode.Sync,
43
+ fn: conn => {
44
+ const list = new Array({
45
+ SellerSKU: '1',
46
+ site: '111'
47
+ }, {
48
+ SellerSKU: '2',
49
+ SellerSKU2: '22',
50
+ }, {
51
+ SellerSKU2: '33',
52
+ SellerSKU: '3',
53
+ site: '333'
54
+ }, {
55
+ SellerSKU2: '44',
56
+ SellerSKU: '4',
57
+ site: '444'
58
+ }, {
59
+ SellerSKU2: '',
60
+ SellerSKU: '66',
61
+ site: undefined
62
+ });
63
+ const rt = service.insert({
64
+ sync: SyncMode.Sync,
65
+ data: list,
66
+ conn, skipEmptyString: false, mode: InsertMode.InsertWithTempTable
67
+ });
68
+ console.log(rt);
69
+ const rt2 = service.update({
70
+ sync: SyncMode.Sync,
71
+ data: list,
72
+ conn, skipEmptyString: false
73
+ });
74
+ console.log(rt2);
75
+ const rt3 = service.delete({
76
+ sync: SyncMode.Sync,
77
+ where: [
78
+ { SellerSKU2: '11', SellerSKU: '1' },
79
+ { SellerSKU2: '22', SellerSKU: '2' },
80
+ { SellerSKU2: '33', SellerSKU: '3' }
81
+ ],
82
+ mode: DeleteMode.TempTable
83
+ });
84
+ console.log(rt3);
85
+ return 1;
86
+ }
87
+ });
88
+ return ret;
89
+ }
90
+ go();
package/test-xml.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import HTML from 'html-parse-stringify';
1
+ import HTML from 'html-parse-stringify';
2
2
  console.log((HTML.parse(`
3
3
  <sql id="reportField">
4
4
  a.left_e_foul leftEFoul,
@@ -67,4 +67,4 @@ console.log((HTML.parse(`
67
67
  </if>
68
68
  </where>
69
69
  </select>
70
- >`)));
70
+ >`)));
package/test.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,2 @@
1
- import { snowflake } from "snowflake";
2
-
3
- console.log(snowflake.generate());
1
+ import { snowflake } from "snowflake";
2
+ console.log(snowflake.generate());
package/wx/base.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export declare abstract class BaseWx {
2
+ protected authErrorCodes: number[];
3
+ protected name: string;
4
+ protected tokenUrl: string;
5
+ private tokenData;
6
+ protected mock: boolean;
7
+ protected getToken(force?: boolean): Promise<string>;
8
+ protected fetch(uri: (token: string) => string, method: 'get' | 'post', data: {
9
+ [key: string]: any;
10
+ }, needToken?: boolean, buffer?: boolean): Promise<any>;
11
+ }
package/wx/base.js ADDED
@@ -0,0 +1,78 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-argument */
2
+ import pino from 'pino';
3
+ import * as rp from 'request-promise';
4
+ import { Throw } from '../error.js';
5
+ const logger = process.env['NODE_ENV'] !== 'production' ? pino({
6
+ name: 'wx',
7
+ transport: {
8
+ target: 'pino-pretty'
9
+ }
10
+ }) : pino({ name: 'wx' });
11
+ export class BaseWx {
12
+ constructor() {
13
+ this.authErrorCodes = [40001, 40014, 41001, 42001];
14
+ this.name = '';
15
+ this.tokenUrl = '';
16
+ this.tokenData = '';
17
+ this.mock = false;
18
+ }
19
+ async getToken(force) {
20
+ let token = '';
21
+ const now = +new Date();
22
+ let need = force === true;
23
+ if (this.tokenData) {
24
+ const datas = this.tokenData.split('^_^');
25
+ token = datas[0];
26
+ const lastTime = datas[1];
27
+ const spliceTime = datas[2];
28
+ if (now - parseInt(lastTime, 10) - parseInt(spliceTime, 10) > 0) {
29
+ need = true;
30
+ }
31
+ }
32
+ else {
33
+ need = true;
34
+ }
35
+ if (need === true) {
36
+ const data = await this.fetch(() => this.tokenUrl, 'get', {}, false);
37
+ token = data.access_token;
38
+ this.tokenData = `${token}^_^${now}^_^${data.expires_in * 1000}`;
39
+ }
40
+ return token;
41
+ }
42
+ async fetch(uri, method, data, needToken = true, buffer = false) {
43
+ if (this.mock === true) {
44
+ return {};
45
+ }
46
+ let token = needToken ? await this.getToken() : '';
47
+ if (!needToken || token) {
48
+ const start = +new Date();
49
+ let url = uri(token);
50
+ const param = method === 'get' ? {
51
+ method,
52
+ json: buffer ? false : true,
53
+ qs: data,
54
+ encoding: buffer ? null : undefined
55
+ } : {
56
+ json: data,
57
+ method,
58
+ encoding: buffer ? null : undefined
59
+ };
60
+ let response = await rp.default({
61
+ uri: url,
62
+ ...param
63
+ });
64
+ if (this.authErrorCodes.includes(response.errcode)) {
65
+ token = await this.getToken(true);
66
+ url = uri(token);
67
+ response = await rp.default({
68
+ uri: url,
69
+ ...param
70
+ });
71
+ Throw.if(response.errcode && response.errcode - 0 !== 0, `${url}-${response.errcode}-${response.errmsg}`);
72
+ }
73
+ Throw.if(response.errcode && response.errcode - 0 !== 0, `${url}-${response.errcode}-${response.errmsg}`);
74
+ logger.info(`fetch data ${+new Date() - start} ms`);
75
+ return response;
76
+ }
77
+ }
78
+ }
package/wx/mini.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ import { BaseWx } from './base.js';
2
+ import { WxLiveInfo, WxLiveReplay, WxMiniConfig } from './types.js';
3
+ export declare class WxMini extends BaseWx {
4
+ protected name: string;
5
+ private config;
6
+ private templNameCache;
7
+ private templKeyCache;
8
+ constructor(config: WxMiniConfig);
9
+ getUnlimited({ scene, model, page, fullpath, png, width, lineColor }: {
10
+ scene: string;
11
+ model?: string;
12
+ page?: string;
13
+ fullpath?: string;
14
+ png?: '0' | '1' | 0 | 1 | true | false | 'true' | 'false';
15
+ width?: number;
16
+ lineColor?: {
17
+ r: number;
18
+ g: number;
19
+ b: number;
20
+ };
21
+ }): Promise<any>;
22
+ sendMs({ openids, name, data, scene }: {
23
+ openids: string[];
24
+ name: string;
25
+ data: {
26
+ [key: string]: string | number;
27
+ };
28
+ scene: string;
29
+ }): Promise<void>;
30
+ code2session(code: string): Promise<{
31
+ openid: string;
32
+ session_key: string;
33
+ unionid?: string;
34
+ }>;
35
+ getTemplIds(): {
36
+ [key: string]: string[];
37
+ };
38
+ decrypt<T>({ sessionKey, encryptedData, iv }: {
39
+ iv: string;
40
+ sessionKey: string;
41
+ encryptedData: string;
42
+ }): T | undefined;
43
+ getLiveInfo(start: number, limit: number): Promise<WxLiveInfo[]>;
44
+ getLiveReplay(room_id: number, start: number, limit: number): Promise<WxLiveReplay[]>;
45
+ getPhone(code: string, openid: string): Promise<{
46
+ phoneNumber: any;
47
+ purePhoneNumber: any;
48
+ countryCode: any;
49
+ timestamp: any;
50
+ appid: any;
51
+ }>;
52
+ }
package/wx/mini.js ADDED
@@ -0,0 +1,112 @@
1
+ import crypto from 'crypto';
2
+ import { Throw } from '../error.js';
3
+ import { BaseWx } from './base.js';
4
+ export class WxMini extends BaseWx {
5
+ constructor(config) {
6
+ super();
7
+ this.name = 'wxMini';
8
+ this.config = config;
9
+ this.tokenUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.config.appId}&secret=${this.config.appSecret}`;
10
+ this.templNameCache = {};
11
+ this.templKeyCache = {};
12
+ if (this.config.messages) {
13
+ for (const item of this.config.messages) {
14
+ if (!this.templNameCache[item.name]) {
15
+ this.templNameCache[item.name] = [];
16
+ }
17
+ this.templNameCache[item.name].push({
18
+ tmplId: item.tmplId,
19
+ page: `pages/${item.model}/${item.page}/${item.page}`
20
+ });
21
+ for (const key of item.keys) {
22
+ if (!this.templKeyCache[key]) {
23
+ this.templKeyCache[key] = [];
24
+ }
25
+ this.templKeyCache[key].push(item.tmplId);
26
+ }
27
+ }
28
+ }
29
+ }
30
+ async getUnlimited({ scene, model, page, fullpath, png, width, lineColor }) {
31
+ Throw.if(!fullpath && (!model || !page), '路径不完整,fullpath或者model+page必须传一个');
32
+ const pageto = fullpath ?? `pages/${model}/${page}/${page}`;
33
+ return await this.fetch((token) => `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`, 'post', {
34
+ scene,
35
+ page: pageto,
36
+ line_color: lineColor ?? this.config.qrcode?.lineColor,
37
+ is_hyaline: png === 'true' || png === '1' || png === true || png === 1,
38
+ width: width ?? this.config.qrcode?.width ?? 500
39
+ }, true, true);
40
+ }
41
+ async sendMs({ openids, name, data, scene }) {
42
+ const temps = this.templNameCache[name];
43
+ const dataSend = {};
44
+ for (const [key, value] of Object.entries(data)) {
45
+ dataSend[key] = { value: value ?? '' };
46
+ }
47
+ if (scene) {
48
+ scene = `?${scene}`;
49
+ }
50
+ else {
51
+ scene = '';
52
+ }
53
+ for (const temp of temps) {
54
+ for (const touser of openids) {
55
+ await this.fetch((token) => `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${token}`, 'post', {
56
+ touser,
57
+ data: dataSend,
58
+ template_id: temp.tmplId,
59
+ page: `${temp.page}${scene}`
60
+ });
61
+ }
62
+ }
63
+ }
64
+ async code2session(code) {
65
+ const data = await this.fetch(() => `https://api.weixin.qq.com/sns/jscode2session?appid=${this.config.appId}&secret=${this.config.appSecret}&js_code=${code}&grant_type=authorization_code`, 'get', {}, false);
66
+ return {
67
+ openid: data.openid,
68
+ session_key: data.session_key,
69
+ unionid: data.unionid
70
+ };
71
+ }
72
+ getTemplIds() {
73
+ return this.templKeyCache;
74
+ }
75
+ decrypt({ sessionKey, encryptedData, iv }) {
76
+ Throw.if(!sessionKey, '会话过期,请重新登陆!');
77
+ const sessionKeyBuf = Buffer.from(sessionKey, 'base64');
78
+ const encryptedDataBuf = Buffer.from(encryptedData, 'base64');
79
+ const ivBuf = Buffer.from(iv, 'base64');
80
+ try {
81
+ // 解密
82
+ const decipher = crypto.createDecipheriv('aes-128-cbc', sessionKeyBuf, ivBuf);
83
+ // 设置自动 padding 为 true,删除填充补位
84
+ decipher.setAutoPadding(true);
85
+ const decoded = decipher.update(encryptedDataBuf, undefined, 'utf8') + decipher.final('utf8');
86
+ const decodedData = JSON.parse(decoded);
87
+ Throw.if(decodedData.watermark.appid !== this.config.appId, '加密验证失败');
88
+ return decodedData;
89
+ }
90
+ catch (err) {
91
+ throw new Error('加密验证失败');
92
+ }
93
+ }
94
+ async getLiveInfo(start, limit) {
95
+ const data = await this.fetch((token) => `https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=${token}`, 'post', { start, limit });
96
+ return data.room_info;
97
+ }
98
+ async getLiveReplay(room_id, start, limit) {
99
+ const data = await this.fetch((token) => `https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=${token}`, 'post', { room_id, start, limit, action: 'get_replay' });
100
+ return data.live_replay;
101
+ }
102
+ async getPhone(code, openid) {
103
+ const data = await this.fetch((token) => `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${token}`, 'post', { code, openid });
104
+ return {
105
+ phoneNumber: data.phone_info.phoneNumber,
106
+ purePhoneNumber: data.phone_info.purePhoneNumber,
107
+ countryCode: data.phone_info.countryCode,
108
+ timestamp: data.phone_info.watermark.timestamp,
109
+ appid: data.phone_info.watermark.appid
110
+ };
111
+ }
112
+ }