baja-lite 1.3.25 → 1.3.27
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/.eslintignore +7 -0
- package/.eslintrc.cjs +89 -0
- package/.prettierrc +4 -0
- package/.vscode/settings.json +8 -0
- package/ci.js +29 -0
- package/package-cjs.json +17 -0
- package/package.json +2 -2
- package/{boot-remote.js → src/boot-remote.ts} +7 -6
- package/{boot.js → src/boot.ts} +38 -31
- package/{code.js → src/code.ts} +66 -71
- package/{convert-xml.js → src/convert-xml.ts} +155 -105
- package/src/enum.ts +71 -0
- package/src/error.ts +11 -0
- package/src/fn.ts +295 -0
- package/{index.d.ts → src/index.ts} +12 -11
- package/{list.js → src/list.ts} +9 -8
- package/src/math.ts +405 -0
- package/src/object.ts +247 -0
- package/{snowflake.js → src/snowflake.ts} +52 -33
- package/src/sql.ts +5023 -0
- package/{sqlite.js → src/sqlite.ts} +52 -53
- package/src/string.ts +111 -0
- package/src/test-mysql.ts +144 -0
- package/{test-postgresql.js → src/test-postgresql.ts} +80 -91
- package/{test-sqlite.js → src/test-sqlite.ts} +80 -90
- package/{test-xml.js → src/test-xml.ts} +1 -1
- package/{test.js → src/test.ts} +3 -2
- package/src/wx/base.ts +76 -0
- package/src/wx/mini.ts +147 -0
- package/src/wx/organ.ts +290 -0
- package/{wx/types.d.ts → src/wx/types.ts} +10 -21
- package/tsconfig.cjs.json +42 -0
- package/tsconfig.json +44 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/xml/event-report.xml +13 -0
- package/yarn.lock +1757 -0
- package/boot-remote.d.ts +0 -2
- package/boot.d.ts +0 -2
- package/code.d.ts +0 -2
- package/convert-xml.d.ts +0 -10
- package/enum.d.ts +0 -18
- package/enum.js +0 -59
- package/error.d.ts +0 -5
- package/error.js +0 -13
- package/fn.d.ts +0 -128
- package/fn.js +0 -172
- package/index.js +0 -11
- package/list.d.ts +0 -10
- package/math.d.ts +0 -83
- package/math.js +0 -451
- package/object.d.ts +0 -83
- package/object.js +0 -221
- package/snowflake.d.ts +0 -12
- package/sql.d.ts +0 -1788
- package/sql.js +0 -4765
- package/sqlite.d.ts +0 -32
- package/string.d.ts +0 -17
- package/string.js +0 -105
- package/test-mysql.d.ts +0 -2
- package/test-mysql.js +0 -109
- package/test-postgresql.d.ts +0 -2
- package/test-sqlite.d.ts +0 -1
- package/test-xml.d.ts +0 -1
- package/test.d.ts +0 -1
- package/wx/base.d.ts +0 -11
- package/wx/base.js +0 -78
- package/wx/mini.d.ts +0 -52
- package/wx/mini.js +0 -112
- package/wx/organ.d.ts +0 -65
- package/wx/organ.js +0 -171
- package/wx/types.js +0 -1
- package/wx.js +0 -3
- /package/{README.md → Readme.md} +0 -0
- /package/{wx.d.ts → src/wx.ts} +0 -0
|
@@ -1,90 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
SellerSKU: '
|
|
46
|
-
site:
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
],
|
|
82
|
-
mode: DeleteMode.TempTable
|
|
83
|
-
});
|
|
84
|
-
console.log(rt3);
|
|
85
|
-
return 1;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
return ret;
|
|
89
|
-
}
|
|
90
|
-
go();
|
|
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();
|
package/{test.js → src/test.ts}
RENAMED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { snowflake } from "snowflake";
|
|
2
|
-
|
|
1
|
+
import { snowflake } from "snowflake";
|
|
2
|
+
|
|
3
|
+
console.log(snowflake.generate());
|
package/src/wx/base.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
2
|
+
import * as rp from 'request-promise';
|
|
3
|
+
import { Throw } from '../error.js';
|
|
4
|
+
import pino from 'pino';
|
|
5
|
+
const logger =
|
|
6
|
+
process.env['NODE_ENV'] !== 'production' ? pino({
|
|
7
|
+
name: 'wx',
|
|
8
|
+
transport: {
|
|
9
|
+
target: 'pino-pretty'
|
|
10
|
+
}
|
|
11
|
+
}) : pino({ name: 'wx' });
|
|
12
|
+
export abstract class BaseWx {
|
|
13
|
+
protected authErrorCodes = [40001, 40014, 41001, 42001];
|
|
14
|
+
protected name: string = '';
|
|
15
|
+
protected tokenUrl: string = '';
|
|
16
|
+
private tokenData = '';
|
|
17
|
+
protected mock = false;
|
|
18
|
+
protected async getToken(force?: boolean): Promise<string> {
|
|
19
|
+
let token = '';
|
|
20
|
+
const now = +new Date();
|
|
21
|
+
let need = force === true;
|
|
22
|
+
if (this.tokenData) {
|
|
23
|
+
const datas = this.tokenData.split('^_^');
|
|
24
|
+
token = datas[0]!;
|
|
25
|
+
const lastTime = datas[1]!;
|
|
26
|
+
const spliceTime = datas[2]!;
|
|
27
|
+
if (now - parseInt(lastTime, 10) - parseInt(spliceTime, 10) > 0) {
|
|
28
|
+
need = true;
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
need = true;
|
|
32
|
+
}
|
|
33
|
+
if (need === true) {
|
|
34
|
+
const data = await this.fetch(() => this.tokenUrl, 'get', {}, false);
|
|
35
|
+
token = data.access_token;
|
|
36
|
+
this.tokenData = `${token}^_^${now}^_^${data.expires_in * 1000}`;
|
|
37
|
+
}
|
|
38
|
+
return token;
|
|
39
|
+
}
|
|
40
|
+
protected async fetch(uri: (token: string) => string, method: 'get' | 'post', data: { [key: string]: any }, needToken = true, buffer = false) {
|
|
41
|
+
if (this.mock === true) {
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
let token = needToken ? await this.getToken() : '';
|
|
45
|
+
if (!needToken || token) {
|
|
46
|
+
const start = +new Date();
|
|
47
|
+
let url = uri(token);
|
|
48
|
+
const param = method === 'get' ? {
|
|
49
|
+
method,
|
|
50
|
+
json: buffer ? false : true,
|
|
51
|
+
qs: data,
|
|
52
|
+
encoding: buffer ? null : undefined
|
|
53
|
+
} : {
|
|
54
|
+
json: data,
|
|
55
|
+
method,
|
|
56
|
+
encoding: buffer ? null : undefined
|
|
57
|
+
};
|
|
58
|
+
let response = await rp.default({
|
|
59
|
+
uri: url,
|
|
60
|
+
...param
|
|
61
|
+
});
|
|
62
|
+
if (this.authErrorCodes.includes(response.errcode)) {
|
|
63
|
+
token = await this.getToken(true);
|
|
64
|
+
url = uri(token);
|
|
65
|
+
response = await rp.default({
|
|
66
|
+
uri: url,
|
|
67
|
+
...param
|
|
68
|
+
});
|
|
69
|
+
Throw.if(response.errcode && response.errcode - 0 !== 0, `${url}-${response.errcode}-${response.errmsg}`);
|
|
70
|
+
}
|
|
71
|
+
Throw.if(response.errcode && response.errcode - 0 !== 0, `${url}-${response.errcode}-${response.errmsg}`);
|
|
72
|
+
logger.info(`fetch data ${+new Date() - start} ms`);
|
|
73
|
+
return response;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/wx/mini.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { Throw } from '../error.js';
|
|
3
|
+
import { BaseWx } from './base.js';
|
|
4
|
+
import { WxLiveInfo, WxLiveReplay, WxMiniConfig } from './types.js';
|
|
5
|
+
export class WxMini extends BaseWx {
|
|
6
|
+
protected name = 'wxMini';
|
|
7
|
+
private config: WxMiniConfig;
|
|
8
|
+
private templNameCache: { [name: string]: { page: string; tmplId: string }[] }; // 订阅消息缓存
|
|
9
|
+
private templKeyCache: { [key: string]: string[] }; // 订阅消息场景分组id
|
|
10
|
+
constructor(config: WxMiniConfig) {
|
|
11
|
+
super();
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.tokenUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.config.appId}&secret=${this.config.appSecret}`;
|
|
14
|
+
this.templNameCache = {};
|
|
15
|
+
this.templKeyCache = {};
|
|
16
|
+
if (this.config.messages) {
|
|
17
|
+
for (const item of this.config.messages) {
|
|
18
|
+
if (!this.templNameCache[item.name]) {
|
|
19
|
+
this.templNameCache[item.name] = [];
|
|
20
|
+
}
|
|
21
|
+
this.templNameCache[item.name]!.push({
|
|
22
|
+
tmplId: item.tmplId,
|
|
23
|
+
page: `pages/${item.model}/${item.page}/${item.page}`
|
|
24
|
+
});
|
|
25
|
+
for (const key of item.keys) {
|
|
26
|
+
if (!this.templKeyCache[key]) {
|
|
27
|
+
this.templKeyCache[key] = [];
|
|
28
|
+
}
|
|
29
|
+
this.templKeyCache[key]!.push(item.tmplId);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async getUnlimited({ scene, model, page, fullpath, png, width, lineColor }: { scene: string; model?: string; page?: string; fullpath?: string; png?: '0' | '1' | 0 | 1 | true | false | 'true' | 'false'; width?: number; lineColor?: { r: number; g: number; b: number } }) {
|
|
35
|
+
Throw.if(!fullpath && (!model || !page), '路径不完整,fullpath或者model+page必须传一个');
|
|
36
|
+
const pageto = fullpath ?? `pages/${model}/${page}/${page}`;
|
|
37
|
+
return await this.fetch(
|
|
38
|
+
(token: string) => `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${token}`,
|
|
39
|
+
'post',
|
|
40
|
+
{
|
|
41
|
+
scene,
|
|
42
|
+
page: pageto,
|
|
43
|
+
line_color: lineColor ?? this.config.qrcode?.lineColor,
|
|
44
|
+
is_hyaline: png === 'true' || png === '1' || png === true || png === 1,
|
|
45
|
+
width: width ?? this.config.qrcode?.width ?? 500
|
|
46
|
+
},
|
|
47
|
+
true,
|
|
48
|
+
true
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
async sendMs(
|
|
52
|
+
{ openids, name, data, scene }: {
|
|
53
|
+
openids: string[];
|
|
54
|
+
name: string;
|
|
55
|
+
data: { [key: string]: string | number };
|
|
56
|
+
scene: string;
|
|
57
|
+
}) {
|
|
58
|
+
const temps = this.templNameCache[name]!;
|
|
59
|
+
const dataSend: { [key: string]: { value: string | number } } = {};
|
|
60
|
+
for (const [key, value] of Object.entries(data)) {
|
|
61
|
+
dataSend[key] = { value: value ?? '' };
|
|
62
|
+
}
|
|
63
|
+
if (scene) {
|
|
64
|
+
scene = `?${scene}`;
|
|
65
|
+
} else {
|
|
66
|
+
scene = '';
|
|
67
|
+
}
|
|
68
|
+
for (const temp of temps) {
|
|
69
|
+
for (const touser of openids) {
|
|
70
|
+
await this.fetch(
|
|
71
|
+
(token: string) => `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${token}`,
|
|
72
|
+
'post',
|
|
73
|
+
{
|
|
74
|
+
touser,
|
|
75
|
+
data: dataSend,
|
|
76
|
+
template_id: temp.tmplId,
|
|
77
|
+
page: `${temp.page}${scene}`
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async code2session(code: string): Promise<{ openid: string; session_key: string; unionid?: string }> {
|
|
84
|
+
const data = await this.fetch(
|
|
85
|
+
() => `https://api.weixin.qq.com/sns/jscode2session?appid=${this.config.appId}&secret=${this.config.appSecret}&js_code=${code}&grant_type=authorization_code`,
|
|
86
|
+
'get',
|
|
87
|
+
{},
|
|
88
|
+
false
|
|
89
|
+
);
|
|
90
|
+
return {
|
|
91
|
+
openid: data.openid,
|
|
92
|
+
session_key: data.session_key,
|
|
93
|
+
unionid: data.unionid
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
getTemplIds() {
|
|
97
|
+
return this.templKeyCache;
|
|
98
|
+
}
|
|
99
|
+
decrypt<T>({ sessionKey, encryptedData, iv }: { iv: string; sessionKey: string; encryptedData: string }): T | undefined {
|
|
100
|
+
Throw.if(!sessionKey, '会话过期,请重新登陆!');
|
|
101
|
+
const sessionKeyBuf = Buffer.from(sessionKey, 'base64');
|
|
102
|
+
const encryptedDataBuf = Buffer.from(encryptedData, 'base64');
|
|
103
|
+
const ivBuf = Buffer.from(iv, 'base64');
|
|
104
|
+
try {
|
|
105
|
+
// 解密
|
|
106
|
+
const decipher = crypto.createDecipheriv('aes-128-cbc', sessionKeyBuf, ivBuf);
|
|
107
|
+
// 设置自动 padding 为 true,删除填充补位
|
|
108
|
+
decipher.setAutoPadding(true);
|
|
109
|
+
const decoded = decipher.update(encryptedDataBuf, undefined, 'utf8') + decipher.final('utf8');
|
|
110
|
+
const decodedData = JSON.parse(decoded);
|
|
111
|
+
Throw.if(decodedData.watermark.appid !== this.config.appId, '加密验证失败');
|
|
112
|
+
return decodedData as T;
|
|
113
|
+
} catch (err) {
|
|
114
|
+
throw new Error('加密验证失败');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async getLiveInfo(start: number, limit: number): Promise<WxLiveInfo[]> {
|
|
118
|
+
const data = await this.fetch(
|
|
119
|
+
(token: string) => `https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=${token}`,
|
|
120
|
+
'post',
|
|
121
|
+
{ start, limit }
|
|
122
|
+
);
|
|
123
|
+
return data.room_info;
|
|
124
|
+
}
|
|
125
|
+
async getLiveReplay(room_id: number, start: number, limit: number): Promise<WxLiveReplay[]> {
|
|
126
|
+
const data = await this.fetch(
|
|
127
|
+
(token: string) => `https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=${token}`,
|
|
128
|
+
'post',
|
|
129
|
+
{ room_id, start, limit, action: 'get_replay' }
|
|
130
|
+
);
|
|
131
|
+
return data.live_replay;
|
|
132
|
+
}
|
|
133
|
+
async getPhone(code: string, openid: string) {
|
|
134
|
+
const data = await this.fetch(
|
|
135
|
+
(token: string) => `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${token}`,
|
|
136
|
+
'post',
|
|
137
|
+
{ code, openid }
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
phoneNumber: data.phone_info.phoneNumber,
|
|
141
|
+
purePhoneNumber: data.phone_info.purePhoneNumber,
|
|
142
|
+
countryCode: data.phone_info.countryCode,
|
|
143
|
+
timestamp: data.phone_info.watermark.timestamp,
|
|
144
|
+
appid: data.phone_info.watermark.appid
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|