ming_node 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ var M=require("../index");
2
+ Db= M.getMySql({
3
+ database: "lj_node"
4
+ })
5
+ const BaseMapper=require("../module/BaseMapper");
6
+ //mi_api 是表名
7
+ file_chat_msgMapper = new BaseMapper("file_chat_msg");
8
+ async function main(){
9
+
10
+ r=await file_chat_msgMapper.selectList({});
11
+ await BaseMapper.appendListName({
12
+ tableName:"vip_basic",
13
+ list:r,
14
+ list_idkey:"create_user",
15
+ list_namekey:"create_user_name",
16
+ db_idkey:"unionid",
17
+ db_namekey:"name"
18
+ })
19
+ console.log(r);
20
+ }
21
+
22
+ main().then(d=>{
23
+ process.exit()
24
+ })
@@ -0,0 +1,10 @@
1
+ var M=require("../index");
2
+
3
+ CollectionUtils= require("../utils/common/CollectionUtils")
4
+
5
+ r1= CollectionUtils.u([[{name:"zs"}],[{name:"zws"}]],"name")
6
+ r2= CollectionUtils.n([[{name:"zs"}],[{name:"zs"}]],"name")
7
+ console.log(r1)
8
+ console.log(r2)
9
+
10
+ console.log(MIO)
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /**
2
- * File : MemoryBaseRpcApi.js
2
+ * File : index.js
3
3
  * By : Minglie
4
4
  * QQ: 934031452
5
5
  * Date :2021.12.01
6
- * version :2.3.0
6
+ * version :2.4.0
7
7
  */
8
8
  var http = require('http');
9
9
  var https = require('https');
@@ -46,14 +46,14 @@
46
46
  M._globle_plugin_url_cacheMap={};
47
47
  //全局插件
48
48
  M._globle_plugin=new Set();
49
- M._node_lib_path=process.env.NODE_PATH;
49
+ M._node_lib_path=process.cwd()+"/.ming_node_cacke";
50
50
  //远程静态资源路径
51
51
  M.remoteStaticPath = "https://minglie.gitee.io/mingpage/static";
52
52
  M.remoteStaticPathEnable = true;
53
53
  //代理服务器配置
54
54
  M.proxyHost = "http://127.0.0.1:8888"
55
55
  M.proxyHost = "";
56
-
56
+ M.IO={}
57
57
  M.setModule=function (key,module){
58
58
  M._moduleMap.set(key,module);
59
59
  }
@@ -88,7 +88,7 @@ M.getGloblePlugin=(pluginKey)=>{
88
88
  }
89
89
  return Object.assign(obj, c1);
90
90
  }
91
-
91
+
92
92
  //将对象追加到url上
93
93
  privateObj.appendDataToUrl = function (url, data) {
94
94
  let getData = "";
@@ -104,10 +104,10 @@ M.getGloblePlugin=(pluginKey)=>{
104
104
  let r = url + getData;
105
105
  return r;
106
106
  }
107
-
107
+
108
108
  M.get = function (url, callback, data, headers) {
109
109
  if (typeof callback == "function") {
110
-
110
+
111
111
  } else {
112
112
  headers = data || {};
113
113
  data = callback;
@@ -153,7 +153,7 @@ M.get = function (url, callback, data, headers) {
153
153
  if (url.startsWith("https")) {
154
154
  reqHttp = https;
155
155
  }
156
-
156
+
157
157
  return new Promise((resolve, reject) => {
158
158
  options = M.httpBefore(options);
159
159
  if (options == false) {
@@ -186,21 +186,21 @@ M.get = function (url, callback, data, headers) {
186
186
  req.on('error', function (err) {
187
187
  reject(err);
188
188
  console.error(err);
189
-
189
+
190
190
  });
191
191
  req.end();
192
192
  })
193
193
  }
194
194
  M._request = function (url, callback, data, headers,methed) {
195
195
  if (typeof callback == "function") {
196
-
196
+
197
197
  } else {
198
198
  headers = data || {};
199
199
  data = callback;
200
200
  callback = () => {
201
201
  };
202
202
  }
203
-
203
+
204
204
  url = privateObj.appendDataToUrl(url, M.reqComQueryparams);
205
205
  var html = '';
206
206
  var urlObj = url_module.parse(url)
@@ -238,7 +238,7 @@ M._request = function (url, callback, data, headers,methed) {
238
238
  if (url.startsWith("https")) {
239
239
  reqHttp = https;
240
240
  }
241
-
241
+
242
242
  return new Promise((resolve, reject) => {
243
243
  var req = reqHttp.request(options, function (res) {
244
244
  options = M.httpBefore(options);
@@ -268,9 +268,9 @@ M._request = function (url, callback, data, headers,methed) {
268
268
  M.httpEnd(html);
269
269
  resolve(html);
270
270
  });
271
-
271
+
272
272
  });
273
-
273
+
274
274
  req.on('error', function (err) {
275
275
  console.error(err);
276
276
  });
@@ -283,7 +283,7 @@ M.delete=(url, callback, data, headers)=>M._request(url, callback, data, headers
283
283
  M.put=(url, callback, data, headers)=>M._request(url, callback, data, headers,"PUT")
284
284
  M.postJson = function (url, callback, data, headers) {
285
285
  if (typeof callback == "function") {
286
-
286
+
287
287
  } else {
288
288
  headers = data || {};
289
289
  data = callback;
@@ -321,9 +321,9 @@ M.postJson = function (url, callback, data, headers) {
321
321
  if (url.startsWith("https")) {
322
322
  reqHttp = https;
323
323
  }
324
-
324
+
325
325
  return new Promise((resolve, reject) => {
326
-
326
+
327
327
  var req = reqHttp.request(options, function (res) {
328
328
  options = M.httpBefore(options);
329
329
  if (options == false) {
@@ -352,9 +352,9 @@ M.postJson = function (url, callback, data, headers) {
352
352
  M.httpEnd(html);
353
353
  resolve(html);
354
354
  });
355
-
355
+
356
356
  });
357
-
357
+
358
358
  req.on('error', function (err) {
359
359
  console.error(err);
360
360
  });
@@ -411,7 +411,7 @@ M.request.put=M.put;
411
411
  });
412
412
  return promise;
413
413
  }
414
-
414
+
415
415
  M.import=async function (url,callback){
416
416
  if(M._globle_lib_cacheMap[url]){
417
417
  return M._globle_lib_cacheMap[url];
@@ -426,9 +426,9 @@ M.request.put=M.put;
426
426
  M._globle_lib_cacheMap[url]=r;
427
427
  return r
428
428
  }
429
-
429
+
430
430
  }
431
-
431
+
432
432
  /**
433
433
  *下载图片
434
434
  */
@@ -477,23 +477,23 @@ M.request.put=M.put;
477
477
  });
478
478
  req.end();
479
479
  }
480
-
480
+
481
481
  /**
482
482
  *打印结果前钩子
483
483
  */
484
484
  M.beforeLogData = function (res, desc) {
485
485
  console.log("-----" + desc + "-----" + res.req.path + "-------------");
486
486
  }
487
-
488
-
487
+
488
+
489
489
  /**
490
490
  *打印结果后钩子
491
491
  */
492
492
  M.afterLogData = function () {
493
-
493
+
494
494
  console.log("--END")
495
495
  }
496
-
496
+
497
497
  /**
498
498
  *简化get请求
499
499
  */
@@ -516,9 +516,9 @@ M.request.put=M.put;
516
516
  }, data
517
517
  );
518
518
  }
519
-
519
+
520
520
  }
521
-
521
+
522
522
  /**
523
523
  *简化post请求
524
524
  */
@@ -530,7 +530,7 @@ M.request.put=M.put;
530
530
  }, data
531
531
  );
532
532
  }
533
-
533
+
534
534
  M.postJson0 = function (url, data) {
535
535
  M.postJson(
536
536
  M.host + url,
@@ -539,28 +539,28 @@ M.request.put=M.put;
539
539
  }, data
540
540
  );
541
541
  }
542
-
542
+
543
543
  M.template = function (str) {
544
544
  return eval("`" + str + "`");
545
545
  }
546
-
547
-
546
+
547
+
548
548
  /**
549
549
  * ----------------------客户端END--------------------------------------------
550
550
  */
551
-
552
-
551
+
552
+
553
553
  /**
554
554
  * ----------------------数据持久化读写START--------------------------------------------
555
555
  */
556
-
556
+
557
557
  /**
558
558
  *递归创建文件夹
559
559
  */
560
560
  M.mkdir = function (dirpath, dirname) {
561
561
  //判断是否是第一次调用
562
562
  if (typeof dirname === "undefined") {
563
-
563
+
564
564
  if (dirpath.indexOf(".") > 0) {
565
565
  dirpath = path.dirname(dirpath);
566
566
  }
@@ -603,7 +603,7 @@ M.request.put=M.put;
603
603
  });
604
604
  });
605
605
  }
606
-
606
+
607
607
  privateObj.checkDirectory = function (src, dst, callback) {
608
608
  fs.access(dst, fs.constants.F_OK, (err) => {
609
609
  if (err) {
@@ -614,7 +614,7 @@ M.request.put=M.put;
614
614
  }
615
615
  });
616
616
  };
617
-
617
+
618
618
  M.readFile = function (file) {
619
619
  if (fs.existsSync(file)) {
620
620
  return fs.readFileSync(file, "utf-8");
@@ -639,7 +639,7 @@ M.request.put=M.put;
639
639
  M.writeObjToFile = function (file, obj) {
640
640
  M.writeFile(file, JSON.stringify(obj));
641
641
  }
642
-
642
+
643
643
  M.addObjToFile = function (file, obj) {
644
644
  try {
645
645
  var d = M.getObjByFile(file);
@@ -667,7 +667,7 @@ M.request.put=M.put;
667
667
  }
668
668
  M.writeObjToFile(file, d);
669
669
  }
670
-
670
+
671
671
  M.deleteObjByPropFile = function (file, o) {
672
672
  let o_key = Object.keys(o)[0];
673
673
  let o_val = o[o_key]
@@ -683,7 +683,7 @@ M.request.put=M.put;
683
683
  M.writeObjToFile(file, d);
684
684
  return d_num;
685
685
  }
686
-
686
+
687
687
  M.updateObjByIdFile = function (file, obj) {
688
688
  var d = M.getObjByFile(file);
689
689
  for (let i = 0; i < d.length; i++) {
@@ -791,10 +791,10 @@ M.request.put=M.put;
791
791
  } catch (e) {
792
792
  preObj = {};
793
793
  }
794
-
794
+
795
795
  M.writeObjToFile(M.map_path, Object.assign(preObj, a));
796
796
  }
797
-
797
+
798
798
  M.getAttribute = function (k) {
799
799
  return M.getObjByFile(M.map_path)[k];
800
800
  }
@@ -825,14 +825,14 @@ M.request.put=M.put;
825
825
  });
826
826
  } )
827
827
  }
828
-
829
-
828
+
829
+
830
830
  M.readCsvLine =async function (file, callback) {
831
831
  return M.readLine(file, function (line) {
832
832
  callback(line.replace("\r", "").split(/(?<!\"[^,]+),(?![^,]+\")/));
833
833
  })
834
834
  }
835
-
835
+
836
836
  M.getFileNameByUrl=function (url){
837
837
  let split= url.split("/");
838
838
  return split[split.length-1]
@@ -843,7 +843,7 @@ M.getFileList = function (path) {
843
843
  function readFile(path, filesList, targetObj) {
844
844
  files = fs.readdirSync(path);//需要用到同步读取
845
845
  files.forEach(walk);
846
-
846
+
847
847
  function walk(file) {
848
848
  states = fs.statSync(path + '/' + file);
849
849
  if (states.isDirectory()) {
@@ -855,7 +855,7 @@ M.getFileList = function (path) {
855
855
  item = {name: file, children: [], value: path + '/' + file};
856
856
  filesList.push(item);
857
857
  }
858
-
858
+
859
859
  readFile(path + '/' + file, filesList, item);
860
860
  } else {
861
861
  //创建一个对象保存信息
@@ -863,7 +863,7 @@ M.getFileList = function (path) {
863
863
  obj.size = states.size;//文件大小,以字节为单位
864
864
  obj.name = file;//文件名
865
865
  obj.path = path + '/' + file; //文件绝对路径
866
-
866
+
867
867
  if (targetObj["children"]) {
868
868
  var item = {name: file, value: obj.path}
869
869
  targetObj["children"].push(item);
@@ -874,7 +874,7 @@ M.getFileList = function (path) {
874
874
  }
875
875
  }
876
876
  }
877
-
877
+
878
878
  var filesList = [];
879
879
  var targetObj = {};
880
880
  readFile(path, filesList, targetObj);
@@ -953,8 +953,8 @@ M.log = function (...params) {
953
953
  if (M.log_file_enable) M.appendFile(M.log_path, r);
954
954
  }
955
955
  }
956
-
957
-
956
+
957
+
958
958
  M.getSqlite = function (dbName) {
959
959
  if (M.sqlite) {
960
960
  return M.sqlite;
@@ -992,7 +992,7 @@ M.log = function (...params) {
992
992
  M.sqlite = Db;
993
993
  return Db;
994
994
  }
995
-
995
+
996
996
  ///////////////////////////////
997
997
 
998
998
 
@@ -1103,12 +1103,12 @@ M.getMongoDB = function (dbConfig) {
1103
1103
  var MongoDB=require('mongodb');
1104
1104
  var MongoClient =MongoDB.MongoClient;
1105
1105
  const ObjectID = MongoDB.ObjectID;
1106
-
1106
+
1107
1107
  var Config={
1108
1108
  dbUrl: dbConfig.dbUrl|| 'mongodb://localhost:27017/',
1109
1109
  dbName: dbConfig.dbName|| 'miapi'
1110
1110
  };
1111
-
1111
+
1112
1112
  class MingMongoClient{
1113
1113
  static connect(){
1114
1114
  return new Promise((resolve,reject)=>{
@@ -1127,7 +1127,7 @@ M.getMongoDB = function (dbConfig) {
1127
1127
  }
1128
1128
  })
1129
1129
  }
1130
-
1130
+
1131
1131
  static find(collectionName,json){
1132
1132
  if(!json){
1133
1133
  json=collectionName
@@ -1143,7 +1143,7 @@ M.getMongoDB = function (dbConfig) {
1143
1143
  }
1144
1144
  resolve(docs);
1145
1145
  })
1146
-
1146
+
1147
1147
  })
1148
1148
  })
1149
1149
  }
@@ -1178,14 +1178,14 @@ M.getMongoDB = function (dbConfig) {
1178
1178
  if(err){
1179
1179
  reject(err);
1180
1180
  }else{
1181
-
1181
+
1182
1182
  resolve(result);
1183
1183
  }
1184
1184
  })
1185
1185
  })
1186
1186
  })
1187
1187
  }
1188
-
1188
+
1189
1189
  static insertMany(collectionName,json){
1190
1190
  if(!json){
1191
1191
  json=collectionName
@@ -1197,14 +1197,14 @@ M.getMongoDB = function (dbConfig) {
1197
1197
  if(err){
1198
1198
  reject(err);
1199
1199
  }else{
1200
-
1200
+
1201
1201
  resolve(result);
1202
1202
  }
1203
1203
  })
1204
1204
  })
1205
1205
  })
1206
1206
  }
1207
-
1207
+
1208
1208
  static remove(collectionName,json){
1209
1209
  if(!json){
1210
1210
  json=collectionName
@@ -1216,14 +1216,14 @@ M.getMongoDB = function (dbConfig) {
1216
1216
  if(err){
1217
1217
  reject(err);
1218
1218
  }else{
1219
-
1219
+
1220
1220
  resolve(result);
1221
1221
  }
1222
1222
  })
1223
1223
  })
1224
1224
  })
1225
1225
  }
1226
-
1226
+
1227
1227
  static getById(collectionName,id){
1228
1228
  if(!id){
1229
1229
  id=collectionName
@@ -1244,7 +1244,7 @@ M.getMongoDB = function (dbConfig) {
1244
1244
  })
1245
1245
  })
1246
1246
  }
1247
-
1247
+
1248
1248
  }
1249
1249
  MingMongoClient.ObjectID=(id)=> new ObjectID(id)
1250
1250
  let Db=MingMongoClient;
@@ -1253,9 +1253,9 @@ M.getMongoDB = function (dbConfig) {
1253
1253
  M.mongoDb=Db;
1254
1254
  return Db;
1255
1255
  }
1256
-
1257
-
1258
-
1256
+
1257
+
1258
+
1259
1259
  /**
1260
1260
  * ----------------------Sql CRUD START-------------------------------------------
1261
1261
  */
@@ -1273,7 +1273,7 @@ M.getMongoDB = function (dbConfig) {
1273
1273
  let sql = "insert into " + tableName + fields + " values " + values;
1274
1274
  return sql;
1275
1275
  }
1276
-
1276
+
1277
1277
  M.getDeleteObjSql = function (tableName, obj) {
1278
1278
  var fields = [];
1279
1279
  for (let field in obj) {
@@ -1283,7 +1283,7 @@ M.getMongoDB = function (dbConfig) {
1283
1283
  sql = sql.replace(/,/g, " and ")
1284
1284
  return sql;
1285
1285
  }
1286
-
1286
+
1287
1287
  M.getUpdateObjSql = function (tableName, obj, caseObj) {
1288
1288
  var fields = [];
1289
1289
  for (let field in obj) {
@@ -1300,11 +1300,11 @@ M.getMongoDB = function (dbConfig) {
1300
1300
  }
1301
1301
  sql = `update ${tableName} set ${fields.map(u => u + "='" + obj[u] + "'")} where ${caseObjfields.map(u => u + "='" + caseObj[u] + "'").join(" and ")}`;
1302
1302
  }
1303
-
1303
+
1304
1304
  return sql;
1305
1305
  }
1306
-
1307
-
1306
+
1307
+
1308
1308
  M.getSelectObjSql = function (tableName, obj) {
1309
1309
  var fields = [];
1310
1310
  for (let field in obj) {
@@ -1314,17 +1314,17 @@ M.getMongoDB = function (dbConfig) {
1314
1314
  sql = sql.replace(/,/g, " and ")
1315
1315
  return sql;
1316
1316
  }
1317
-
1317
+
1318
1318
  /**
1319
1319
  * ----------------------Sql CRUD START-------------------------------------------
1320
1320
  */
1321
-
1322
-
1321
+
1322
+
1323
1323
  /**
1324
1324
  * ----------------------数据持久化读写END--------------------------------------------
1325
1325
  */
1326
-
1327
-
1326
+
1327
+
1328
1328
  /**
1329
1329
  * ----------------------服务器端START--------------------------------------------
1330
1330
  */
@@ -1381,7 +1381,7 @@ M.failResult=(msg,code,d)=>{
1381
1381
  }
1382
1382
  return result;
1383
1383
  }
1384
-
1384
+
1385
1385
  /**
1386
1386
  *获取驼峰式的对象
1387
1387
  */
@@ -1392,7 +1392,7 @@ M.failResult=(msg,code,d)=>{
1392
1392
  }
1393
1393
  return result;
1394
1394
  }
1395
-
1395
+
1396
1396
  M.randomStr = function () {
1397
1397
  return (Math.random().toString(36) + new Date().getTime()).slice(2);
1398
1398
  }
@@ -1430,7 +1430,7 @@ M.urlParse = function (url) {
1430
1430
  }
1431
1431
  return s
1432
1432
  };
1433
-
1433
+
1434
1434
  /**
1435
1435
  * 异常处理钩子
1436
1436
  * @param e
@@ -1442,9 +1442,9 @@ M.urlParse = function (url) {
1442
1442
  }
1443
1443
  return true;
1444
1444
  }
1445
-
1446
-
1447
-
1445
+
1446
+
1447
+
1448
1448
  M.server = function () {
1449
1449
  var G = this; /*全局变量,也就是M*/
1450
1450
  //静态资源路径
@@ -1495,7 +1495,7 @@ M.urlParse = function (url) {
1495
1495
  }
1496
1496
  return isRest;
1497
1497
  }
1498
-
1498
+
1499
1499
  req.ip = req.headers['x-forwarded-for'] ||
1500
1500
  req.connection.remoteAddress ||
1501
1501
  req.socket.remoteAddress ||
@@ -1571,7 +1571,7 @@ M.urlParse = function (url) {
1571
1571
  res.end();
1572
1572
  G._end(req,text);
1573
1573
  }
1574
-
1574
+
1575
1575
  res.render = async function (url) {
1576
1576
  res.alreadySend = true;
1577
1577
  let text="";
@@ -1719,12 +1719,12 @@ M.urlParse = function (url) {
1719
1719
  M["_gloable_exception_handle"](e,req,res);
1720
1720
  }
1721
1721
  }
1722
-
1723
-
1722
+
1723
+
1724
1724
  app.begin = function (callback) {
1725
1725
  G._begin = callback;
1726
1726
  }
1727
-
1727
+
1728
1728
  app.end = function (callback) {
1729
1729
  G._end = callback;
1730
1730
  }
@@ -1782,7 +1782,7 @@ M.urlParse = function (url) {
1782
1782
  url = url.substr(0, url.indexOf(":"));
1783
1783
  G._rest[url] = realUrl;
1784
1784
  }
1785
-
1785
+
1786
1786
  G._get[url] = callback;
1787
1787
  }
1788
1788
  /**
@@ -1829,8 +1829,8 @@ M.urlParse = function (url) {
1829
1829
  }
1830
1830
  G._mapping[url] = callback;
1831
1831
  }
1832
-
1833
-
1832
+
1833
+
1834
1834
  M.formatUrl = function (url) {
1835
1835
  if (!url.endsWith('/')) {
1836
1836
  url = url + '/';
@@ -1847,7 +1847,7 @@ M.urlParse = function (url) {
1847
1847
  req.url = url;
1848
1848
  app(req, res);
1849
1849
  }
1850
-
1850
+
1851
1851
  /**
1852
1852
  *重定向
1853
1853
  */
@@ -1855,12 +1855,12 @@ M.urlParse = function (url) {
1855
1855
  res.writeHead(302, {'Content-Type': 'text/html; charset=utf-8', 'Location': url});
1856
1856
  res.end();
1857
1857
  }
1858
-
1858
+
1859
1859
  app.set = function (k, v) {
1860
1860
  M["_" + k] = v;
1861
1861
  }
1862
-
1863
-
1862
+
1863
+
1864
1864
  //全局异常钩子
1865
1865
  app.set("gloable_exception_handle",(err,req,res)=>{
1866
1866
  console.error(err.stack)
@@ -1869,12 +1869,12 @@ M.urlParse = function (url) {
1869
1869
  res.send(M.result(err.message,false,-1));
1870
1870
  }
1871
1871
  })
1872
-
1872
+
1873
1873
  //render异常钩子
1874
1874
  app.set("render_exception_handle",(err,req,res)=>{
1875
1875
  console.error(err.stack)
1876
1876
  })
1877
-
1877
+
1878
1878
  //没有对应接口时的处理器
1879
1879
  app.set("no_router_handle",(req,res)=>{
1880
1880
  res.end('no router')
@@ -1903,19 +1903,19 @@ M.urlParse = function (url) {
1903
1903
  res.end();
1904
1904
  })
1905
1905
 
1906
-
1907
-
1908
1906
  app.listen = function (port) {
1909
1907
  const server= http.createServer(app).listen(port);
1910
1908
  console.log("listen on port:" + port);
1911
1909
  return server;
1912
1910
  }
1911
+
1912
+ global.app=app;
1913
1913
  return app;
1914
1914
  }
1915
1915
  M["_gloable_exception_handle"]=(err)=>{
1916
1916
  console.error(err)
1917
1917
  }
1918
-
1918
+
1919
1919
  //异常捕获
1920
1920
  process.on('uncaughtException', function (err) {
1921
1921
  M["_gloable_exception_handle"](err,M.req,M.res);
@@ -1924,8 +1924,8 @@ M.urlParse = function (url) {
1924
1924
  process.on('unhandledRejection',function(err,promise){
1925
1925
  M["_gloable_exception_handle"](err,M.req,M.res);
1926
1926
  });
1927
-
1928
-
1927
+
1928
+
1929
1929
  /**
1930
1930
  * 代理服务器start
1931
1931
  */
@@ -1956,7 +1956,7 @@ M.urlParse = function (url) {
1956
1956
  })
1957
1957
  })
1958
1958
  }
1959
-
1959
+
1960
1960
  M.axios = function (axiosConfig) {
1961
1961
  axiosConfig.headers.host = "";
1962
1962
  var urlObj = url_module.parse(axiosConfig.url)
@@ -1986,7 +1986,7 @@ M.urlParse = function (url) {
1986
1986
  M.httpEnd(html);
1987
1987
  resolve(html);
1988
1988
  });
1989
-
1989
+
1990
1990
  });
1991
1991
  req.on('error', function (err) {
1992
1992
  console.error(err);
@@ -2020,7 +2020,7 @@ privateObj.dealUseServer = async function (req, res) {
2020
2020
  let fileName = pathname.replace("/", "");
2021
2021
  //获取文件的后缀名
2022
2022
  var extname = path.extname(pathname);
2023
-
2023
+
2024
2024
  if (fileName.startsWith("__default_")) {
2025
2025
  res.setHeader("Access-Control-Allow-Origin", "*");
2026
2026
  res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
@@ -2035,7 +2035,7 @@ privateObj.dealUseServer = async function (req, res) {
2035
2035
  if (!res.alreadySend) await res.renderUrl(M.remoteStaticPath + pathname)
2036
2036
  return;
2037
2037
  }
2038
-
2038
+
2039
2039
  if (pathname != '/favicon.ico') { /*过滤请求favicon.ico*/
2040
2040
  //文件操作获取 static下面的index.html
2041
2041
  fs.readFile(staticPath + '/' + pathname, function (err, data) {
@@ -2059,7 +2059,7 @@ privateObj.dealUseServer = async function (req, res) {
2059
2059
  res.end();
2060
2060
  }
2061
2061
  }
2062
-
2062
+
2063
2063
  /*SSE SERVER */
2064
2064
  M.sseServer = function () {
2065
2065
  //sse 心跳
@@ -2160,12 +2160,12 @@ privateObj.dealUseServer = async function (req, res) {
2160
2160
  }
2161
2161
  return app;
2162
2162
  }
2163
-
2163
+
2164
2164
  /**
2165
2165
  * ----------------------服务器端END--------------------------------------------
2166
2166
  */
2167
-
2168
-
2167
+
2168
+
2169
2169
  /**
2170
2170
  * ----------------------其他工具函数START--------------------------------------------
2171
2171
  */
@@ -2175,11 +2175,11 @@ privateObj.dealUseServer = async function (req, res) {
2175
2175
  if (err || stderr) console.error(err, stderr);
2176
2176
  reslove(stdout);
2177
2177
  });
2178
-
2178
+
2179
2179
  })
2180
2180
  return promise;
2181
2181
  }
2182
-
2182
+
2183
2183
  M.getMyIp = function () {
2184
2184
  var interfaces = require('os').networkInterfaces();
2185
2185
  for (var devName in interfaces) {
@@ -2192,17 +2192,17 @@ privateObj.dealUseServer = async function (req, res) {
2192
2192
  }
2193
2193
  }
2194
2194
  }
2195
-
2195
+
2196
2196
  /**
2197
2197
  *对象转JSON key不用引号括起来,因兼容性不好,所以去掉
2198
2198
  */
2199
-
2199
+
2200
2200
  /**
2201
2201
  M.JSOM_Stringify=function(obj){
2202
2202
  return JSON.stringify(obj).replace(/"(\w+)"(\s*:\s*)/gis, '$1$2');
2203
2203
  }
2204
2204
  */
2205
-
2205
+
2206
2206
  M.sleep = function (numberMillis) {
2207
2207
  var now = new Date();
2208
2208
  var exitTime = now.getTime() + numberMillis;
@@ -2224,7 +2224,7 @@ M.delayMs=async function (ms){
2224
2224
  /**
2225
2225
  * ----------------------其他工具函数END--------------------------------------------
2226
2226
  */
2227
-
2227
+
2228
2228
  /**
2229
2229
  * 静态资源对应表
2230
2230
  */
@@ -2742,11 +2742,11 @@ M.delayMs=async function (ms){
2742
2742
  ".conf": "text/conf",
2743
2743
  ".sql": "text/sql"
2744
2744
  }
2745
-
2745
+
2746
2746
  M.test = function () {
2747
2747
  console.log(privateObj.staticMime[".jssson"] || "aa")
2748
2748
  }
2749
-
2749
+
2750
2750
  M.getRemoteCacheByUrl = async function (url) {
2751
2751
  if (url in M._globle_cacheMap) {
2752
2752
  return M._globle_cacheMap[url];
@@ -2761,7 +2761,7 @@ M.delayMs=async function (ms){
2761
2761
  M._globle_cacheMap[url] = text;
2762
2762
  return text;
2763
2763
  }
2764
-
2764
+
2765
2765
  M.init = function () {
2766
2766
  /***
2767
2767
  * 下划线命名转为驼峰命名
@@ -2773,7 +2773,7 @@ M.delayMs=async function (ms){
2773
2773
  });
2774
2774
  return str;
2775
2775
  }
2776
-
2776
+
2777
2777
  /***
2778
2778
  * 驼峰命名转下划线
2779
2779
  */
@@ -2782,7 +2782,7 @@ M.delayMs=async function (ms){
2782
2782
  str = this.replace(/([A-Z])/g, "_$1").toLowerCase();
2783
2783
  return str;
2784
2784
  }
2785
-
2785
+
2786
2786
  //首字母变大写
2787
2787
  String.prototype.firstChartoUpper = function () {
2788
2788
  return this.replace(/^([a-z])/g, function (word) {
@@ -2816,9 +2816,12 @@ M.delayMs=async function (ms){
2816
2816
  }
2817
2817
  return fmt;
2818
2818
  }
2819
-
2820
-
2819
+
2820
+
2821
2821
  }
2822
2822
  M.init();
2823
-
2823
+
2824
+ global.M=M;
2825
+ global.MIO=M.IO;
2826
+
2824
2827
  module.exports = M;
@@ -320,7 +320,44 @@ class BaseMapper {
320
320
  return sql;
321
321
  }
322
322
 
323
-
323
+ /**
324
+ * 给 list 加name
325
+ * @param tableName 表名
326
+ * @param list 列表
327
+ * @param list_idkey 列表中的idkey
328
+ * @param list_namekey 列表中的namekey
329
+ * @param db_idkey 库中的namekey
330
+ * @param db_namekey 库中的namekey
331
+ * @returns {Promise<void>}
332
+ */
333
+ static async appendListName(
334
+ {
335
+ tableName="t_user",
336
+ list,
337
+ list_idkey="create_user",
338
+ list_namekey="name",
339
+ db_idkey="create_user",
340
+ db_namekey="create_user_name",
341
+ }
342
+ ){
343
+ if(list==null || list.length==0){
344
+ return
345
+ }
346
+ const idKeyList=list.map(u=>u[list_idkey]);
347
+ let sql=`select ${db_idkey}, ${db_namekey} from ${tableName} where ${db_idkey} in (?)`
348
+ let dbDataList=await Db.doSql(sql,[idKeyList]);
349
+ const userMap = {};
350
+ for (let i = 0; i < dbDataList.length; i++) {
351
+ const idkeyV = dbDataList[i][db_idkey];
352
+ userMap[idkeyV] = dbDataList[i];
353
+ }
354
+ for (let i=0;i<list.length;i++){
355
+ if(userMap[list[i][list_idkey]]){
356
+ list[i][list_namekey]= userMap[list[i][list_idkey]][db_namekey];
357
+ }
358
+ }
359
+ return;
360
+ }
324
361
 
325
362
  }
326
363
 
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "scripts": {
47
47
  "test": "echo \"Error: no test specified\" && exit 1"
48
48
  },
49
- "version": "2.3.0"
49
+ "version": "2.4.0"
50
50
  }
@@ -23,6 +23,117 @@ class CollectionUtils{
23
23
  }
24
24
 
25
25
 
26
+ static list2Csv({list,titlesKeys,titlesNames}){
27
+ if(list==null||list.length==0){
28
+ return;
29
+ }
30
+ if(!titlesKeys){
31
+ titlesKeys=Object.keys(list[0])
32
+ }
33
+ if(!titlesNames){
34
+ titlesNames=titlesKeys
35
+ }
36
+ let bodyStr="";
37
+ list.forEach((items, index) => {
38
+ for(let key of titlesKeys){
39
+ items[key]=items[key]+"";
40
+ bodyStr=bodyStr+`"${!items[key]?'null': ""+items[key].replace(/\s/g,"")}"`+","
41
+ }
42
+ // console.log(bodyStr)
43
+ bodyStr=bodyStr+"\n";
44
+ })
45
+ return `\ufeff`+titlesNames+'\n'+bodyStr;
46
+ }
47
+
48
+ static n(arr,key="unionId"){
49
+ function n1(r1,r2,key){
50
+ if(r1==null || r2==null ||r1==undefined||r2==undefined || r1.length==0 || r2.length==0){
51
+ return []
52
+ }
53
+ let r2keyList=r2.map(u=>u[key])
54
+ let result= r1.filter(u=>r2keyList.indexOf(u[key])!==-1)
55
+ return result;
56
+ }
57
+ let allResult=[]
58
+ try{
59
+ if(arr==null || arr==undefined || arr.length==0){
60
+ return []
61
+ }
62
+ for (let i=0;i<arr.length;i++){
63
+ if(arr[i]==null||arr[i]==undefined||arr[i].length==0){
64
+ return []
65
+ }
66
+ }
67
+ allResult= arr.reduce((r1,r2)=>{
68
+ let ri= n1(r1,r2,key)
69
+ if(ri==null||ri==undefined||ri.length==0){
70
+ throw []
71
+ }
72
+ return ri;
73
+ },arr[0])
74
+ }catch (e){
75
+ allResult=e;
76
+ }
77
+ return allResult;
78
+ }
79
+
80
+ static u(arr,key="unionId"){
81
+ function u1(r1,r2,key){
82
+ if(r1==null && r2==null){
83
+ return []
84
+ }
85
+ let r=[...r1,...r2];
86
+ return r;
87
+ }
88
+ let allResult=[]
89
+ try{
90
+ if(arr==null || arr==undefined || arr.length==0){
91
+ return []
92
+ }
93
+ for (let i=0;i<arr.length;i++){
94
+ if(arr[i]==null||arr[i]==undefined||arr[i].length==0){
95
+ return []
96
+ }
97
+ }
98
+ allResult= arr.reduce((r1,r2)=>{
99
+ let ri= u1(r1,r2,key)
100
+ if(ri==null||ri==undefined||ri.length==0){
101
+ return [];
102
+ }
103
+ return ri;
104
+ },arr[0])
105
+ }catch (e){
106
+ allResult=[];
107
+ }
108
+ let allResultKeys=allResult.map(u=>u[key]);
109
+ let allResult1=[];
110
+ let allResult1KeyObj={};
111
+ for (let i=0;i<allResult.length;i++){
112
+ let allResultObj=allResult[i];
113
+ let k= allResultObj[key];
114
+ if(allResult1KeyObj[k]){
115
+ continue;
116
+ }else {
117
+ allResult1.push(allResultObj);
118
+ allResult1KeyObj[k]=true;
119
+ }
120
+ }
121
+ return allResult1;
122
+ }
123
+
124
+ static dif(arr1,arr2,key="unionId"){
125
+ if(arr1==null || arr1==null|| arr1.length==0 ){
126
+ return []
127
+ }
128
+ if(arr2==null || arr2==null|| arr2.length==0 ){
129
+ return arr1;
130
+ }
131
+ let r2keyList=arr2.map(u=>u[key])
132
+ let result= arr1.filter(u=>r2keyList.indexOf(u[key])==-1)
133
+ return result;
134
+
135
+ }
136
+
26
137
  }
27
138
 
28
139
  module.exports = CollectionUtils;
@@ -0,0 +1,36 @@
1
+
2
+
3
+ class DateUtils {
4
+ static difDate(startDate="2021-04-01",endDate="2022-02-10"){
5
+ let flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2];
6
+ let start = new Date(startDate);
7
+ let end = new Date(endDate);
8
+ let year = end.getFullYear() - start.getFullYear();
9
+ let month = end.getMonth() - start.getMonth();
10
+ let day = end.getDate() - start.getDate();
11
+ if (month < 0) {
12
+ year--;
13
+ month = end.getMonth() + (12 - start.getMonth());
14
+ }
15
+ if (day < 0) {
16
+ month--;
17
+ let index = flag.findIndex((temp) => {
18
+ return temp === start.getMonth() + 1
19
+ });
20
+ let monthLength;
21
+ if (index <= 6) {
22
+ monthLength = 31;
23
+ } else if (index > 6 && index <= 10) {
24
+ monthLength = 30;
25
+ } else {
26
+ monthLength = 28;
27
+ }
28
+ day = end.getDate() + (monthLength - start.getDate());
29
+ }
30
+ return {year,month,day}
31
+ }
32
+
33
+ }
34
+
35
+
36
+ module.exports = DateUtils;