cgserver 10.0.0 → 10.0.1

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.
@@ -10,7 +10,9 @@ const ec_key_1 = require("ec-key");
10
10
  const uuid_1 = require("uuid");
11
11
  const global_1 = require("../global");
12
12
  /**
13
- * 常用的工具函数类
13
+ * 一些通用的常用函数,
14
+ * 比如格式化字符串,常用加解密,
15
+ * 时间计算,类型判断等
14
16
  */
15
17
  class core {
16
18
  /**
@@ -10,6 +10,10 @@ class MongoCacheModel extends MongoManager_1.MongoBaseModel {
10
10
  expireAt = Date.now() + 365 * 24 * 60 * 60 * 1000;
11
11
  }
12
12
  exports.MongoCacheModel = MongoCacheModel;
13
+ /**
14
+ * mongo版本的缓存服务
15
+ * 可以用来缓存kv数据
16
+ */
13
17
  class MongoCacheService extends MongoBaseService_1.MongoBaseService {
14
18
  constructor() {
15
19
  super("cache", MongoCacheModel);
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ControllerManager = void 0;
4
+ /**
5
+ * web服务中mvc的控制器管理器
6
+ */
4
7
  class ControllerManager {
5
8
  _static_ctr = {};
6
9
  _ctr_cls = {};
@@ -25,6 +25,10 @@ const QiniuTool_1 = require("./ThirdParty/QiniuTool");
25
25
  const WechatTool_1 = require("./ThirdParty/WechatTool");
26
26
  const ControllerManager_1 = require("./WebServer/Engine/ControllerManager");
27
27
  const cgserver_1 = require("./cgserver");
28
+ const Core_1 = require("./Core/Core");
29
+ /**
30
+ * cg引擎的,全局对象
31
+ */
28
32
  class global {
29
33
  static gMongoServiceMgr = new MongoServiceManager_1.MongoServiceManager();
30
34
  static gCgServer = new cgserver_1.CgServer();
@@ -50,7 +54,20 @@ class global {
50
54
  static gAlipayTool = new AlipayTool_1.AlipayTool();
51
55
  static gAppleTool = new AppleTool_1.AppleTool();
52
56
  static gProtoFactory = new ProtoFactory_1.ProtoFactory();
57
+ /**
58
+ * web服务中mvc的控制器管理器
59
+ */
53
60
  static gCtrMgr = new ControllerManager_1.ControllerManager();
61
+ /**
62
+ * mongo版本的缓存服务
63
+ * 可以用来缓存kv数据
64
+ */
54
65
  static gMongoCacheSer = new MongoCacheService_1.MongoCacheService();
66
+ /**
67
+ * 一些通用的常用函数,
68
+ * 比如格式化字符串,常用加解密,
69
+ * 时间计算,类型判断等
70
+ */
71
+ static core = Core_1.core;
55
72
  }
56
73
  exports.global = global;
@@ -1,7 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  import * as crypto from "crypto";
3
3
  /**
4
- * 常用的工具函数类
4
+ * 一些通用的常用函数,
5
+ * 比如格式化字符串,常用加解密,
6
+ * 时间计算,类型判断等
5
7
  */
6
8
  export declare class core {
7
9
  /**
@@ -5,6 +5,10 @@ export declare class MongoCacheModel extends MongoBaseModel {
5
5
  data: any;
6
6
  expireAt: number;
7
7
  }
8
+ /**
9
+ * mongo版本的缓存服务
10
+ * 可以用来缓存kv数据
11
+ */
8
12
  export declare class MongoCacheService extends MongoBaseService<MongoCacheModel> {
9
13
  constructor();
10
14
  getData(key: string): Promise<any>;
@@ -1,4 +1,7 @@
1
1
  import { BaseController } from "../Controller/BaseController";
2
+ /**
3
+ * web服务中mvc的控制器管理器
4
+ */
2
5
  export declare class ControllerManager {
3
6
  protected _static_ctr: {
4
7
  [module_name: string]: {
@@ -23,6 +23,10 @@ import { QiniuTool } from './ThirdParty/QiniuTool';
23
23
  import { WechatTool } from './ThirdParty/WechatTool';
24
24
  import { ControllerManager } from './WebServer/Engine/ControllerManager';
25
25
  import { CgServer } from './cgserver';
26
+ import { core } from './Core/Core';
27
+ /**
28
+ * cg引擎的,全局对象
29
+ */
26
30
  export declare class global {
27
31
  static gMongoServiceMgr: MongoServiceManager;
28
32
  static gCgServer: CgServer;
@@ -48,6 +52,19 @@ export declare class global {
48
52
  static gAlipayTool: AlipayTool;
49
53
  static gAppleTool: AppleTool;
50
54
  static gProtoFactory: ProtoFactory;
55
+ /**
56
+ * web服务中mvc的控制器管理器
57
+ */
51
58
  static gCtrMgr: ControllerManager;
59
+ /**
60
+ * mongo版本的缓存服务
61
+ * 可以用来缓存kv数据
62
+ */
52
63
  static gMongoCacheSer: MongoCacheService;
64
+ /**
65
+ * 一些通用的常用函数,
66
+ * 比如格式化字符串,常用加解密,
67
+ * 时间计算,类型判断等
68
+ */
69
+ static core: typeof core;
53
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "10.0.0",
3
+ "version": "10.0.1",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",
@@ -1,128 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoBaseService = void 0;
4
- const MongoManager_1 = require("./MongoManager");
5
- class MongoBaseService {
6
- _table = "";
7
- get table() {
8
- return this._table;
9
- }
10
- _inited = false;
11
- get isInited() {
12
- return this._inited;
13
- }
14
- get mongoDb() {
15
- return MongoManager_1.GMongoMgr.getMongo(this._dbname);
16
- }
17
- _dbname = "";
18
- get dbname() {
19
- if (!this._dbname) {
20
- this._dbname = MongoManager_1.GMongoMgr.defdbname;
21
- }
22
- return this._dbname;
23
- }
24
- _t_type = null;
25
- constructor(table, type, dbname = "") {
26
- this._t_type = type;
27
- this._table = table;
28
- this._dbname = dbname;
29
- }
30
- async getNextId(key = "") {
31
- if (!key) {
32
- key = this._table;
33
- }
34
- let id = await this.mongoDb.getAutoIds(this._table);
35
- return id;
36
- }
37
- toObjectId(id) {
38
- return this.mongoDb.toObjectId(id);
39
- }
40
- /**
41
- * 没有id(非_id)的表不能使用该函数
42
- * @param id
43
- */
44
- async getById(id) {
45
- let rs = await this.mongoDb.findOne(this._table, null, { id: id });
46
- return rs.one;
47
- }
48
- async get(property = null, where = null) {
49
- let rs = await this.mongoDb.findOne(this._table, property, where);
50
- return rs.one;
51
- }
52
- async countDocuments(where = null, options) {
53
- let rs = await this.mongoDb.countDocuments(this._table, where);
54
- return rs.count;
55
- }
56
- async gets(property = null, where = null, sort = null, skip = 0, limit = 0) {
57
- let rs = await this.mongoDb.findMany(this._table, property, where, sort, skip, limit);
58
- return rs.list;
59
- }
60
- async getRandoms(num, property, where = null) {
61
- let rs = await this.mongoDb.simpleAggregate(this._table, property, where, null, num);
62
- return rs.list;
63
- }
64
- async updateOne(model, where, upsert = false) {
65
- let rs = await this.mongoDb.updateOne(this._table, model, where, upsert);
66
- return rs;
67
- }
68
- async updateMany(model, where = null) {
69
- let rs = await this.mongoDb.updateMany(this._table, model, where);
70
- return rs;
71
- }
72
- async insert(model) {
73
- let rs = await this.mongoDb.insertOne(this._table, model);
74
- return rs;
75
- }
76
- async deleteOne(where) {
77
- let rs = await this.mongoDb.deleteOne(this._table, where);
78
- return rs;
79
- }
80
- async deleteMany(where) {
81
- let rs = await this.mongoDb.deleteMany(this._table, where);
82
- return rs;
83
- }
84
- async createIndex(index, options) {
85
- let rs = await this.mongoDb.createIndex(this._table, index, options);
86
- return rs;
87
- }
88
- aggregate(pipeline, options) {
89
- let ret = this.mongoDb.aggregate(this._table, pipeline, options);
90
- return ret;
91
- }
92
- /**
93
- * 仅仅支持一级
94
- * @param array 数据名称 比如 items
95
- * @param where 数组内赛选条件 比如 "items.id":1
96
- * @param pre_match 数组上一级赛选条件 比如 "user_id":1
97
- */
98
- async getsInArray(array, where, pre_match) {
99
- let agg = this.aggregate();
100
- if (pre_match) {
101
- agg = agg.match(pre_match);
102
- }
103
- agg = agg.unwind("$" + array);
104
- if (where) {
105
- agg = agg.match(where);
106
- }
107
- let all = await agg.toArray();
108
- let items = [];
109
- for (let i = 0; i < all.length; ++i) {
110
- items.push(all[i][array]);
111
- }
112
- return items;
113
- }
114
- /**
115
- * 仅仅支持一级
116
- * @param array 数据名称 比如 items
117
- * @param where 数组内赛选条件 比如 "items.id":1
118
- * @param pre_match 数组上一级赛选条件 比如 "user_id":1
119
- */
120
- async getInArray(array, where, pre_match) {
121
- let items = await this.getsInArray(array, where, pre_match);
122
- if (items.length <= 0) {
123
- return null;
124
- }
125
- return items[0];
126
- }
127
- }
128
- exports.MongoBaseService = MongoBaseService;
@@ -1,508 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoExt = exports.GMongoMgr = exports.MongoManager = exports.MgReturn = exports.MrResult = exports.MongoBaseModel = exports.MongoConfig = void 0;
4
- const Log_1 = require("../Logic/Log");
5
- const mongo = require("mongodb");
6
- const _error_1 = require("../Config/_error_");
7
- const Core_1 = require("../Core/Core");
8
- class MongoConfig {
9
- open = false;
10
- host = "127.0.0.1";
11
- port = 27017;
12
- options = null;
13
- database = 'mongodb';
14
- }
15
- exports.MongoConfig = MongoConfig;
16
- class MongoBaseModel {
17
- _id;
18
- }
19
- exports.MongoBaseModel = MongoBaseModel;
20
- class MrResult {
21
- /**
22
- * select 的数据量
23
- */
24
- length = 0;
25
- /**
26
- * 插入数据的自增id
27
- */
28
- insertId = null;
29
- insertIds = null;
30
- /**
31
- * update 更新数据的影响条数
32
- */
33
- changedRows = null;
34
- /**
35
- * 插入或删除数据的影响条数
36
- */
37
- affectedRows = null;
38
- }
39
- exports.MrResult = MrResult;
40
- class MgReturn {
41
- error = null;
42
- result = new MrResult();
43
- list = [];
44
- }
45
- exports.MgReturn = MgReturn;
46
- class MongoManager {
47
- _dbs = {};
48
- _defdbname = "";
49
- get defdbname() {
50
- return this._defdbname;
51
- }
52
- //初始化多个数据库,第一个open数据库为默认数据库
53
- async init(cfgs) {
54
- for (let i = 0; i < cfgs.length; ++i) {
55
- let cfg = cfgs[i];
56
- let ret = await this.addMongo(cfg);
57
- if (!ret) {
58
- return false;
59
- }
60
- }
61
- return true;
62
- }
63
- async addMongo(cfg) {
64
- if (this._dbs[cfg.database]) {
65
- Log_1.GLog.error("数据库配置得database不能相同!database=" + cfg.database);
66
- return false;
67
- }
68
- let mongoext = new MongoExt();
69
- let ret = await mongoext.init(cfg);
70
- if (!ret) {
71
- Log_1.GLog.error("数据库初始化失败!cfg=" + JSON.stringify(cfg));
72
- return false;
73
- }
74
- this._dbs[cfg.database] = mongoext;
75
- this._defdbname = cfg.database;
76
- return true;
77
- }
78
- async removeMongo(dbname, force = false) {
79
- let mongo = this.getMongo(dbname);
80
- if (!mongo) {
81
- return false;
82
- }
83
- mongo.close(force);
84
- }
85
- getMongo(dbname = "") {
86
- if (!dbname) {
87
- dbname = this._defdbname;
88
- }
89
- return this._dbs[dbname];
90
- }
91
- }
92
- exports.MongoManager = MongoManager;
93
- exports.GMongoMgr = new MongoManager();
94
- class MongoExt {
95
- _mongocfg = null;
96
- _init_cbs = [];
97
- _mongoDb = null;
98
- get mongoDb() {
99
- return this._mongoDb;
100
- }
101
- _mongoClient = null;
102
- get mongoClient() {
103
- return this._mongoClient;
104
- }
105
- _mongo_init_succ = false;
106
- _inited = false;
107
- get isValid() {
108
- return this._inited;
109
- }
110
- constructor() {
111
- }
112
- async init(cfg) {
113
- if (!cfg || !cfg.open) {
114
- return false;
115
- }
116
- if (this._inited) {
117
- return false;
118
- }
119
- if (this._mongoDb) {
120
- return true;
121
- }
122
- this._mongocfg = cfg;
123
- this._inited = true;
124
- Log_1.GLog.info("mongo config=" + JSON.stringify(this._mongocfg));
125
- this._mongoClient = new mongo.MongoClient("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
126
- await Core_1.core.safeCall(this._mongoClient.connect, this._mongoClient);
127
- this.onConnect();
128
- this._mongoDb = this._mongoClient.db(this._mongocfg.database);
129
- for (let i = 0; i < this._init_cbs.length; ++i) {
130
- this._init_cbs[i]();
131
- }
132
- return true;
133
- }
134
- close(force = false) {
135
- this._mongoClient.close(force);
136
- }
137
- registerInitCb(cb) {
138
- this._init_cbs.push(cb);
139
- }
140
- onConnect() {
141
- this._mongo_init_succ = true;
142
- Log_1.GLog.info("mongo has connected!");
143
- }
144
- /**
145
- * 获取自增长id
146
- * @param key
147
- * @returns 小于等于0为异常
148
- */
149
- async getAutoIds(key) {
150
- if (!this._mongoDb) {
151
- return -1;
152
- }
153
- let collection = "auto_ids";
154
- let col = this._mongoDb.collection(collection);
155
- try {
156
- let where = this._convertWhere({ _id: key });
157
- let rs = await Core_1.core.safeCall(col.findOneAndUpdate, col, where, { $inc: { id: 1 } }, { upsert: true });
158
- if (rs.id) {
159
- return rs.id + 1;
160
- }
161
- return 1;
162
- }
163
- catch (e) {
164
- Log_1.GLog.error(e.stack);
165
- }
166
- return -2;
167
- }
168
- _convertWhere(where) {
169
- if (!where || !where._id) {
170
- return;
171
- }
172
- if (Core_1.core.isString(where._id) && where._id.length == 24) {
173
- let _id = where._id;
174
- try {
175
- where._id = this.toObjectId(_id);
176
- }
177
- catch (e) {
178
- where._id = _id;
179
- }
180
- }
181
- return where;
182
- }
183
- toObjectId(id) {
184
- return new mongo.ObjectId(id);
185
- }
186
- /**
187
- * 获取单条消息
188
- * @param collection
189
- */
190
- async findOne(collection, property = {}, where = {}) {
191
- this._convertWhere(where);
192
- let rs = { errcode: null, one: null };
193
- if (!this._mongoDb) {
194
- rs.errcode = _error_1.EErrorCode.No_Mongo;
195
- return rs;
196
- }
197
- let one = null;
198
- try {
199
- let col = this._mongoDb.collection(collection);
200
- one = await col.findOne(where, { projection: property });
201
- }
202
- catch (e) {
203
- Log_1.GLog.error({ collection, property, where });
204
- Log_1.GLog.error(e.stack);
205
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
206
- }
207
- rs.one = one;
208
- return rs;
209
- }
210
- async findMany(collection, property = {}, where = {}, sort, skip = 0, limit = 0) {
211
- this._convertWhere(where);
212
- let rs = { errcode: null, list: null };
213
- if (!this._mongoDb) {
214
- rs.errcode = _error_1.EErrorCode.No_Mongo;
215
- return rs;
216
- }
217
- let list = [];
218
- try {
219
- let col = this._mongoDb.collection(collection);
220
- let cursor = col.find(where, { projection: property });
221
- if (sort) {
222
- cursor = cursor.sort(sort);
223
- }
224
- if (skip) {
225
- cursor = cursor.skip(skip);
226
- }
227
- if (limit) {
228
- cursor = cursor.limit(limit);
229
- }
230
- list = await cursor.toArray();
231
- }
232
- catch (e) {
233
- Log_1.GLog.error({ collection, property, where, sort, skip, limit });
234
- Log_1.GLog.error(e.stack);
235
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
236
- }
237
- rs.list = list;
238
- return rs;
239
- }
240
- async countDocuments(collection, where, options) {
241
- this._convertWhere(where);
242
- let rs = { errcode: null, count: -1 };
243
- if (!this._mongoDb) {
244
- rs.errcode = _error_1.EErrorCode.No_Mongo;
245
- return rs;
246
- }
247
- let count = -1;
248
- try {
249
- let col = this._mongoDb.collection(collection);
250
- count = await col.countDocuments(where || {}, options);
251
- }
252
- catch (e) {
253
- Log_1.GLog.error({ collection, where });
254
- Log_1.GLog.error(e.stack);
255
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
256
- }
257
- rs.count = count;
258
- return rs;
259
- }
260
- async deleteOne(collection, where) {
261
- this._convertWhere(where);
262
- let rs = { errcode: null, count: -1 };
263
- if (!this._mongoDb) {
264
- rs.errcode = _error_1.EErrorCode.No_Mongo;
265
- return rs;
266
- }
267
- let del_rs = null;
268
- try {
269
- let col = this._mongoDb.collection(collection);
270
- del_rs = await col.deleteOne(where || {});
271
- }
272
- catch (e) {
273
- Log_1.GLog.error({ collection, where });
274
- Log_1.GLog.error(e.stack);
275
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
276
- }
277
- if (del_rs) {
278
- rs.count = del_rs.deletedCount;
279
- }
280
- return rs;
281
- }
282
- async deleteMany(collection, where) {
283
- this._convertWhere(where);
284
- let rs = { errcode: null, count: -1 };
285
- if (!this._mongoDb) {
286
- rs.errcode = _error_1.EErrorCode.No_Mongo;
287
- return rs;
288
- }
289
- let del_rs = null;
290
- try {
291
- let col = this._mongoDb.collection(collection);
292
- del_rs = await col.deleteMany(where || {});
293
- }
294
- catch (e) {
295
- Log_1.GLog.error({ collection, where });
296
- Log_1.GLog.error(e.stack);
297
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
298
- }
299
- if (del_rs) {
300
- rs.count = del_rs.deletedCount;
301
- }
302
- return rs;
303
- }
304
- /**
305
- * 插入数据
306
- * @param collection
307
- * @param data
308
- */
309
- async insertOne(collection, data) {
310
- let rs = { errcode: null, rs: null };
311
- if (!this._mongoDb) {
312
- rs.errcode = _error_1.EErrorCode.No_Mongo;
313
- return rs;
314
- }
315
- let in_rs = null;
316
- try {
317
- let col = this._mongoDb.collection(collection);
318
- in_rs = await col.insertOne(data);
319
- }
320
- catch (e) {
321
- Log_1.GLog.error({ collection, data });
322
- Log_1.GLog.error(e.stack);
323
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
324
- }
325
- rs.rs = in_rs;
326
- return rs;
327
- }
328
- async insertManay(collection, data) {
329
- let rs = { errcode: null, rs: null };
330
- if (!this._mongoDb) {
331
- rs.errcode = _error_1.EErrorCode.No_Mongo;
332
- return rs;
333
- }
334
- let in_rs = null;
335
- try {
336
- let col = this._mongoDb.collection(collection);
337
- in_rs = await col.insertMany(data);
338
- }
339
- catch (e) {
340
- Log_1.GLog.error({ collection, data });
341
- Log_1.GLog.error(e.stack);
342
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
343
- }
344
- rs.rs = in_rs;
345
- return rs;
346
- }
347
- async updateOne(collection, model, where, upsert = false) {
348
- let _id = model["_id"];
349
- delete model["_id"];
350
- if (!where && _id) {
351
- where["_id"] = _id;
352
- }
353
- this._convertWhere(where);
354
- let rs = { errcode: null, rs: null };
355
- if (!this._mongoDb) {
356
- if (_id) {
357
- model["_id"] = _id;
358
- }
359
- rs.errcode = _error_1.EErrorCode.No_Mongo;
360
- return rs;
361
- }
362
- let up_rs = null;
363
- try {
364
- let updatemodel = null;
365
- let money = false;
366
- for (let key in model) {
367
- if (key.startsWith("$")) {
368
- money = true;
369
- }
370
- }
371
- if (!money) {
372
- updatemodel = { "$set": model };
373
- }
374
- else {
375
- updatemodel = model;
376
- }
377
- let col = this._mongoDb.collection(collection);
378
- up_rs = await col.updateOne(where, updatemodel, { upsert: upsert });
379
- }
380
- catch (e) {
381
- Log_1.GLog.error({ collection, model, where, upsert });
382
- Log_1.GLog.error(e.stack);
383
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
384
- }
385
- rs.rs = up_rs;
386
- if (up_rs?.upsertedId || _id) {
387
- model["_id"] = up_rs?.upsertedId || _id;
388
- }
389
- return rs;
390
- }
391
- async updateMany(collection, model, where, upsert = false) {
392
- this._convertWhere(where);
393
- let rs = { errcode: null, rs: null };
394
- if (!this._mongoDb) {
395
- rs.errcode = _error_1.EErrorCode.No_Mongo;
396
- return rs;
397
- }
398
- let up_rs = null;
399
- try {
400
- let updateModel = null;
401
- let firstKey = Object.keys(model)[0];
402
- if (!firstKey.startsWith("$")) {
403
- updateModel = { $set: model };
404
- }
405
- else {
406
- updateModel = model;
407
- }
408
- let col = this._mongoDb.collection(collection);
409
- up_rs = await col.updateMany(where, updateModel, { upsert: upsert });
410
- }
411
- catch (e) {
412
- Log_1.GLog.error({ collection, model, where, upsert });
413
- Log_1.GLog.error(e.stack);
414
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
415
- }
416
- rs.rs = up_rs;
417
- return rs;
418
- }
419
- async createIndex(collection, index, options) {
420
- let rs = { errcode: null, rs: null };
421
- if (!this._mongoDb) {
422
- rs.errcode = _error_1.EErrorCode.No_Mongo;
423
- return rs;
424
- }
425
- let i_rs = null;
426
- try {
427
- let col = this._mongoDb.collection(collection);
428
- i_rs = await col.createIndex(index, options);
429
- }
430
- catch (e) {
431
- Log_1.GLog.error({ collection, index });
432
- Log_1.GLog.error(e.stack);
433
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
434
- }
435
- rs.rs = i_rs;
436
- return rs;
437
- }
438
- async simpleAggregate(collection, property, where, size, random_size) {
439
- this._convertWhere(where);
440
- let rs = { errcode: null, list: null };
441
- if (!this._mongoDb) {
442
- rs.errcode = _error_1.EErrorCode.No_Mongo;
443
- return rs;
444
- }
445
- let list = [];
446
- try {
447
- let col = this._mongoDb.collection(collection);
448
- let params = [];
449
- if (where) {
450
- params.push({ '$match': where });
451
- }
452
- if (property) {
453
- params.push({ '$project': property });
454
- }
455
- if (random_size) {
456
- params.push({ '$sample': { 'size': random_size } });
457
- }
458
- let agg = col.aggregate(params);
459
- if (size) {
460
- list = await agg.limit(size).toArray();
461
- }
462
- else {
463
- list = await agg.toArray();
464
- }
465
- }
466
- catch (e) {
467
- Log_1.GLog.error(e.stack);
468
- rs.errcode = _error_1.EErrorCode.Mongo_Error;
469
- }
470
- rs.list = list;
471
- return rs;
472
- }
473
- aggregate(collection, pipeline, options) {
474
- if (!this._mongoDb) {
475
- return;
476
- }
477
- let col = this._mongoDb.collection(collection);
478
- let agg = col.aggregate(pipeline, options);
479
- return agg;
480
- }
481
- /**
482
- * 快速事务
483
- * @param collection
484
- * @param cb
485
- */
486
- async quickTransaction(cb, options) {
487
- if (!this._mongoDb) {
488
- return false;
489
- }
490
- let session = this._mongoClient.startSession();
491
- session.startTransaction(options);
492
- try {
493
- let rs = await cb(session);
494
- await session.commitTransaction();
495
- session.endSession();
496
- return rs;
497
- }
498
- catch (e) {
499
- await session.abortTransaction();
500
- Log_1.GLog.error(e.stack);
501
- }
502
- finally {
503
- await session.endSession();
504
- }
505
- return false;
506
- }
507
- }
508
- exports.MongoExt = MongoExt;
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GMongoSerMgr = void 0;
4
- const Log_1 = require("../Logic/Log");
5
- const MongoManager_1 = require("./MongoManager");
6
- class MongoServiceManager {
7
- _dbservicess = {};
8
- add(ser) {
9
- let dbname = ser.dbname;
10
- if (!this._dbservicess[dbname]) {
11
- this._dbservicess[dbname] = {};
12
- }
13
- let classname = ser.constructor.name;
14
- if (this._dbservicess[dbname][classname]) {
15
- Log_1.GLog.error("same db created duplicated service:" + classname);
16
- return;
17
- }
18
- this._dbservicess[dbname][classname] = ser;
19
- }
20
- getService(type, dbname = "") {
21
- if (!dbname) {
22
- dbname = MongoManager_1.GMongoMgr.defdbname;
23
- }
24
- //后有defdbname,所以检测一下,把之前的空转换为defname
25
- if (dbname && this._dbservicess[""]) {
26
- if (!this._dbservicess[dbname]) {
27
- this._dbservicess[dbname] = {};
28
- }
29
- for (let key in this._dbservicess[""]) {
30
- let ser = this._dbservicess[""][key];
31
- this._dbservicess[dbname][key] = ser;
32
- }
33
- this._dbservicess[""] = undefined;
34
- delete this._dbservicess[""];
35
- }
36
- //正式逻辑
37
- let dbservices = this._dbservicess[dbname];
38
- let classname = type.name;
39
- if (!dbservices) {
40
- this._dbservicess[dbname] = {};
41
- }
42
- if (!dbservices[classname]) {
43
- //mongobaseservice 会掉add
44
- new type();
45
- }
46
- return dbservices[classname];
47
- }
48
- }
49
- exports.GMongoSerMgr = new MongoServiceManager();
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GOpenSocial = void 0;
4
- const IServerConfig_1 = require("../Config/IServerConfig");
5
- const HttpTool_1 = require("../Logic/HttpTool");
6
- exports.GOpenSocial = null;
7
- class OpenSocial {
8
- _getNewMsg() {
9
- return {
10
- app_id: IServerConfig_1.GServerCfg.third_cfg.open_social.app_id,
11
- app_secret: IServerConfig_1.GServerCfg.third_cfg.open_social.app_secret
12
- };
13
- }
14
- async getUser(unionid, openid) {
15
- let msg = {
16
- unionid: unionid,
17
- openid: openid
18
- };
19
- let rs = await HttpTool_1.GHttpTool.post({ url: IServerConfig_1.GServerCfg.third_cfg.open_social.user_url, json: msg });
20
- return rs.body;
21
- }
22
- async updatePwd(unionid, openid, new_pwd) {
23
- let msg = {
24
- unionid: unionid,
25
- openid: openid,
26
- password: new_pwd
27
- };
28
- let jsonData = await HttpTool_1.GHttpTool.post({ url: IServerConfig_1.GServerCfg.third_cfg.open_social.update_pwd_url, json: msg });
29
- return jsonData.body || jsonData.error;
30
- }
31
- }
32
- exports.GOpenSocial = new OpenSocial();
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WechatOATool = exports.GWechatOATool = exports.WechatOAMsg = void 0;
4
- //OA=offical account公众号
5
- class WechatOAMsg {
6
- // <xml>
7
- // <ToUserName><![CDATA[toUser]]></ToUserName>
8
- // <FromUserName><![CDATA[fromUser]]></FromUserName>
9
- // <CreateTime>1348831860</CreateTime>
10
- // <MsgType><![CDATA[text]]></MsgType>
11
- // <Content><![CDATA[this is a test]]></Content>
12
- // <MsgId>1234567890123456</MsgId>
13
- // </xml>
14
- toUserName = "";
15
- fromUserName = ""; //其实是一个userid
16
- createTime = -1;
17
- msgType = ""; //text文本
18
- content = "";
19
- msgId = -1; //只有收到消息才会有
20
- }
21
- exports.WechatOAMsg = WechatOAMsg;
22
- exports.GWechatOATool = null;
23
- class WechatOATool {
24
- convertMsg(xmlStr) {
25
- if (!xmlStr) {
26
- return null;
27
- }
28
- let msg = new WechatOAMsg();
29
- let pre = "<ToUserName><![CDATA[";
30
- xmlStr = xmlStr.substr(xmlStr.indexOf(pre) + pre.length);
31
- msg.toUserName = xmlStr.substr(0, xmlStr.indexOf("]"));
32
- pre = "<FromUserName><![CDATA[";
33
- xmlStr = xmlStr.substr(xmlStr.indexOf(pre) + pre.length);
34
- msg.fromUserName = xmlStr.substr(0, xmlStr.indexOf("]"));
35
- pre = "<CreateTime>";
36
- xmlStr = xmlStr.substr(xmlStr.indexOf(pre) + pre.length);
37
- msg.createTime = parseInt(xmlStr.substr(0, xmlStr.indexOf("<")));
38
- pre = "<MsgType><![CDATA[";
39
- xmlStr = xmlStr.substr(xmlStr.indexOf(pre) + pre.length);
40
- msg.msgType = xmlStr.substr(0, xmlStr.indexOf("]"));
41
- pre = "<Content><![CDATA[";
42
- xmlStr = xmlStr.substr(xmlStr.indexOf(pre) + pre.length);
43
- msg.content = xmlStr.substr(0, xmlStr.indexOf("]"));
44
- pre = "<MsgId>";
45
- xmlStr = xmlStr.substr(xmlStr.indexOf(pre) + pre.length);
46
- msg.msgId = parseInt(xmlStr.substr(0, xmlStr.indexOf("<")));
47
- return msg;
48
- }
49
- toReplyXmlStr(msg) {
50
- let xmlStr = "<xml>";
51
- xmlStr += "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>";
52
- xmlStr += "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>";
53
- xmlStr += "<CreateTime>" + msg.createTime + "</CreateTime>";
54
- xmlStr += "<MsgType><![CDATA[" + msg.msgType + "]]></MsgType>";
55
- xmlStr += "<Content><![CDATA[" + msg.content + "]]></Content>";
56
- xmlStr += "</xml>";
57
- return xmlStr;
58
- }
59
- }
60
- exports.WechatOATool = WechatOATool;
61
- exports.GWechatOATool = new WechatOATool();
@@ -1,85 +0,0 @@
1
- import { MongoBaseModel } from "./MongoManager";
2
- import * as mongo from 'mongodb';
3
- export declare class MongoBaseService<T extends MongoBaseModel> {
4
- protected _table: string;
5
- get table(): string;
6
- protected _inited: boolean;
7
- get isInited(): boolean;
8
- get mongoDb(): import("./MongoManager").MongoExt;
9
- protected _dbname: string;
10
- get dbname(): string;
11
- protected _t_type: {
12
- new (): T;
13
- };
14
- constructor(table: string, type: {
15
- new (): T;
16
- }, dbname?: string);
17
- getNextId(key?: string): Promise<number>;
18
- toObjectId(id: string): mongo.BSON.ObjectId;
19
- /**
20
- * 没有id(非_id)的表不能使用该函数
21
- * @param id
22
- */
23
- getById(id: any): Promise<T>;
24
- get(property?: any, where?: any): Promise<T>;
25
- countDocuments(where?: any, options?: mongo.CountDocumentsOptions): Promise<number>;
26
- gets(property?: any, where?: any, sort?: any, skip?: number, limit?: number): Promise<T[]>;
27
- getRandoms(num: number, property: any, where?: any): Promise<T[]>;
28
- updateOne(model: any, where?: any, upsert?: boolean): Promise<{
29
- errcode: {
30
- id: number;
31
- des: string;
32
- };
33
- rs: mongo.UpdateResult<mongo.BSON.Document>;
34
- }>;
35
- updateMany(model: any, where?: any): Promise<{
36
- errcode: {
37
- id: number;
38
- des: string;
39
- };
40
- rs: mongo.BSON.Document | mongo.UpdateResult<mongo.BSON.Document>;
41
- }>;
42
- insert(model: T): Promise<{
43
- errcode: {
44
- id: number;
45
- des: string;
46
- };
47
- rs: mongo.InsertOneResult<any>;
48
- }>;
49
- deleteOne(where: any): Promise<{
50
- errcode: {
51
- id: number;
52
- des: string;
53
- };
54
- count: number;
55
- }>;
56
- deleteMany(where: any): Promise<{
57
- errcode: {
58
- id: number;
59
- des: string;
60
- };
61
- count: number;
62
- }>;
63
- createIndex(index: any, options?: mongo.CreateIndexesOptions): Promise<{
64
- errcode: {
65
- id: number;
66
- des: string;
67
- };
68
- rs: string;
69
- }>;
70
- aggregate(pipeline?: Document[], options?: mongo.AggregateOptions): mongo.AggregationCursor<mongo.BSON.Document>;
71
- /**
72
- * 仅仅支持一级
73
- * @param array 数据名称 比如 items
74
- * @param where 数组内赛选条件 比如 "items.id":1
75
- * @param pre_match 数组上一级赛选条件 比如 "user_id":1
76
- */
77
- getsInArray<T>(array: string, where?: any, pre_match?: any): Promise<T[]>;
78
- /**
79
- * 仅仅支持一级
80
- * @param array 数据名称 比如 items
81
- * @param where 数组内赛选条件 比如 "items.id":1
82
- * @param pre_match 数组上一级赛选条件 比如 "user_id":1
83
- */
84
- getInArray<T>(array: string, where?: any, pre_match?: any): Promise<T>;
85
- }
@@ -1,166 +0,0 @@
1
- import * as mongo from 'mongodb';
2
- export declare class MongoConfig {
3
- open: boolean;
4
- host: string;
5
- port: number;
6
- options: mongo.MongoClientOptions;
7
- database: string;
8
- }
9
- export declare class MongoBaseModel {
10
- _id: mongo.ObjectId;
11
- }
12
- export declare class MrResult {
13
- /**
14
- * select 的数据量
15
- */
16
- length: number;
17
- /**
18
- * 插入数据的自增id
19
- */
20
- insertId: number;
21
- insertIds: {
22
- [key: number]: number;
23
- };
24
- /**
25
- * update 更新数据的影响条数
26
- */
27
- changedRows: number;
28
- /**
29
- * 插入或删除数据的影响条数
30
- */
31
- affectedRows: number;
32
- }
33
- export declare class MgReturn {
34
- error: any;
35
- result: MrResult;
36
- list: any[];
37
- }
38
- export declare class MongoManager {
39
- protected _dbs: {
40
- [key: string]: MongoExt;
41
- };
42
- protected _defdbname: string;
43
- get defdbname(): string;
44
- init(cfgs: MongoConfig[]): Promise<boolean>;
45
- addMongo(cfg: MongoConfig): Promise<boolean>;
46
- removeMongo(dbname: string, force?: boolean): Promise<boolean>;
47
- getMongo(dbname?: string): MongoExt;
48
- }
49
- export declare let GMongoMgr: MongoManager;
50
- export declare class MongoExt {
51
- protected _mongocfg: MongoConfig;
52
- protected _init_cbs: any[];
53
- protected _mongoDb: mongo.Db;
54
- get mongoDb(): mongo.Db;
55
- protected _mongoClient: mongo.MongoClient;
56
- get mongoClient(): mongo.MongoClient;
57
- protected _mongo_init_succ: boolean;
58
- protected _inited: boolean;
59
- get isValid(): boolean;
60
- constructor();
61
- init(cfg: MongoConfig): Promise<boolean>;
62
- close(force?: boolean): void;
63
- registerInitCb(cb: Function): void;
64
- onConnect(): void;
65
- /**
66
- * 获取自增长id
67
- * @param key
68
- * @returns 小于等于0为异常
69
- */
70
- getAutoIds(key: string): Promise<number>;
71
- protected _convertWhere(where?: any): any;
72
- toObjectId(id: string): mongo.BSON.ObjectId;
73
- /**
74
- * 获取单条消息
75
- * @param collection
76
- */
77
- findOne(collection: string, property?: {}, where?: {}): Promise<{
78
- errcode: {
79
- id: number;
80
- des: string;
81
- };
82
- one: any;
83
- }>;
84
- findMany(collection: string, property?: {}, where?: {}, sort?: {}, skip?: number, limit?: number): Promise<{
85
- errcode: {
86
- id: number;
87
- des: string;
88
- };
89
- list: any[];
90
- }>;
91
- countDocuments(collection: string, where?: {}, options?: mongo.CountDocumentsOptions): Promise<{
92
- errcode: {
93
- id: number;
94
- des: string;
95
- };
96
- count: number;
97
- }>;
98
- deleteOne(collection: any, where: any): Promise<{
99
- errcode: {
100
- id: number;
101
- des: string;
102
- };
103
- count: number;
104
- }>;
105
- deleteMany(collection: any, where: any): Promise<{
106
- errcode: {
107
- id: number;
108
- des: string;
109
- };
110
- count: number;
111
- }>;
112
- /**
113
- * 插入数据
114
- * @param collection
115
- * @param data
116
- */
117
- insertOne(collection: string, data: any): Promise<{
118
- errcode: {
119
- id: number;
120
- des: string;
121
- };
122
- rs: mongo.InsertOneResult<any>;
123
- }>;
124
- insertManay(collection: string, data: []): Promise<{
125
- errcode: {
126
- id: number;
127
- des: string;
128
- };
129
- rs: mongo.InsertManyResult<any>;
130
- }>;
131
- updateOne(collection: string, model?: {}, where?: {}, upsert?: boolean): Promise<{
132
- errcode: {
133
- id: number;
134
- des: string;
135
- };
136
- rs: mongo.UpdateResult<mongo.BSON.Document>;
137
- }>;
138
- updateMany(collection: string, model: any, where?: {}, upsert?: boolean): Promise<{
139
- errcode: {
140
- id: number;
141
- des: string;
142
- };
143
- rs: mongo.BSON.Document | mongo.UpdateResult<mongo.BSON.Document>;
144
- }>;
145
- createIndex(collection: string, index: any, options?: mongo.CreateIndexesOptions): Promise<{
146
- errcode: {
147
- id: number;
148
- des: string;
149
- };
150
- rs: string;
151
- }>;
152
- simpleAggregate(collection: string, property?: {}, where?: {}, size?: number, random_size?: number): Promise<{
153
- errcode: {
154
- id: number;
155
- des: string;
156
- };
157
- list: any[];
158
- }>;
159
- aggregate(collection: string, pipeline?: Document[], options?: mongo.AggregateOptions): mongo.AggregationCursor<mongo.BSON.Document>;
160
- /**
161
- * 快速事务
162
- * @param collection
163
- * @param cb
164
- */
165
- quickTransaction(cb: Function, options?: mongo.TransactionOptions): Promise<false | any>;
166
- }
@@ -1,15 +0,0 @@
1
- import { MongoBaseService } from "./MongoBaseService";
2
- import { MongoBaseModel } from "./MongoManager";
3
- declare class MongoServiceManager {
4
- protected _dbservicess: {
5
- [dbname: string]: {
6
- [classname: string]: MongoBaseService<MongoBaseModel>;
7
- };
8
- };
9
- add(ser: MongoBaseService<MongoBaseModel>): void;
10
- getService<T extends MongoBaseService<MongoBaseModel>>(type: {
11
- new (): T;
12
- }, dbname?: string): T;
13
- }
14
- export declare let GMongoSerMgr: MongoServiceManager;
15
- export {};
@@ -1,7 +0,0 @@
1
- export declare let GOpenSocial: OpenSocial;
2
- declare class OpenSocial {
3
- protected _getNewMsg(): any;
4
- getUser(unionid: string, openid: string): Promise<any>;
5
- updatePwd(unionid: string, openid: string, new_pwd: string): Promise<any>;
6
- }
7
- export {};
@@ -1,13 +0,0 @@
1
- export declare class WechatOAMsg {
2
- toUserName: string;
3
- fromUserName: string;
4
- createTime: number;
5
- msgType: string;
6
- content: string;
7
- msgId: number;
8
- }
9
- export declare let GWechatOATool: WechatOATool;
10
- export declare class WechatOATool {
11
- convertMsg(xmlStr: string): WechatOAMsg;
12
- toReplyXmlStr(msg: WechatOAMsg): string;
13
- }