helper.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. 'use strict';
  2. /**
  3. * 辅助方法扩展
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/9/28
  7. * @version
  8. */
  9. const zeroRange = 0.0000000001;
  10. const fs = require('fs');
  11. const path = require('path');
  12. const streamToArray = require('stream-to-array');
  13. const _ = require('lodash');
  14. const bc = require('../lib/base_calc.js');
  15. const Decimal = require('decimal.js');
  16. Decimal.set({precision: 50, defaults: true});
  17. module.exports = {
  18. _: _,
  19. /**
  20. * 生成随机字符串
  21. *
  22. * @param {Number} length - 需要生成字符串的长度
  23. * @param {Number} type - 1为数字和字符 2为纯数字 3为纯字母
  24. * @return {String} - 返回生成结果
  25. */
  26. generateRandomString(length, type = 1) {
  27. length = parseInt(length);
  28. length = isNaN(length) ? 1 : length;
  29. let randSeed = [];
  30. let numberSeed = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  31. let stringSeed = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  32. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  33. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
  34. switch (type) {
  35. case 1:
  36. randSeed = stringSeed.concat(numberSeed);
  37. stringSeed = numberSeed = null;
  38. break;
  39. case 2:
  40. randSeed = numberSeed;
  41. break;
  42. case 3:
  43. randSeed = stringSeed;
  44. break;
  45. default:
  46. break;
  47. }
  48. const seedLength = randSeed.length - 1;
  49. let result = '';
  50. for (let i = 0; i < length; i++) {
  51. const index = Math.ceil(Math.random() * seedLength);
  52. result += randSeed[index];
  53. }
  54. return result;
  55. },
  56. /**
  57. * 字节转换
  58. * @param {number} bytes - 字节
  59. * @return {string} - 大小
  60. */
  61. bytesToSize(bytes) {
  62. if (parseInt(bytes) === 0) return '0 B';
  63. const k = 1024;
  64. const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  65. const i = Math.floor(Math.log(bytes) / Math.log(k));
  66. // return (bytes / Math.pow(k, i)) + ' ' + sizes[i];
  67. return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
  68. },
  69. /**
  70. * 浮点乘法计算
  71. * @param {number} arg1 - 乘数
  72. * @param {number} arg2 - 被乘数
  73. * @return {string} - 结果
  74. */
  75. accMul(arg1, arg2) {
  76. let m = 0;
  77. const s1 = arg1.toString();
  78. const s2 = arg2.toString();
  79. try {
  80. m += s1.split('.')[1] !== undefined ? s1.split('.')[1].length : 0;
  81. } catch (e) {
  82. throw e;
  83. }
  84. try {
  85. m += s2.split('.')[1] !== undefined ? s2.split('.')[1].length : 0;
  86. } catch (e) {
  87. throw e;
  88. }
  89. return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
  90. },
  91. accAdd(arg1, arg2) {
  92. let r1;
  93. let r2;
  94. try {
  95. r1 = arg1.toString().split('.')[1].length;
  96. } catch (e) {
  97. r1 = 0;
  98. }
  99. try {
  100. r2 = arg2.toString().split('.')[1].length;
  101. } catch (e) {
  102. r2 = 0;
  103. }
  104. const c = Math.abs(r1 - r2);
  105. const m = Math.pow(10, Math.max(r1, r2));
  106. if (c > 0) {
  107. const cm = Math.pow(10, c);
  108. if (r1 > r2) {
  109. arg1 = Number(arg1.toString().replace('.', ''));
  110. arg2 = Number(arg2.toString().replace('.', '')) * cm;
  111. } else {
  112. arg1 = Number(arg1.toString().replace('.', '')) * cm;
  113. arg2 = Number(arg2.toString().replace('.', ''));
  114. }
  115. } else {
  116. arg1 = Number(arg1.toString().replace('.', ''));
  117. arg2 = Number(arg2.toString().replace('.', ''));
  118. }
  119. return (arg1 + arg2) / m;
  120. },
  121. // 四舍五入或末尾加零,实现类似php的 sprintf("%.".decimal."f", val);
  122. roundNum(val, decimals) {
  123. if (val !== '') {
  124. val = parseFloat(val);
  125. if (decimals < 1) {
  126. val = (Math.round(val)).toString();
  127. } else {
  128. let num = val.toString();
  129. if (num.lastIndexOf('.') === -1) {
  130. num += '.';
  131. num += this.makezero(decimals);
  132. val = num;
  133. } else {
  134. const valdecimals = num.split('.')[1].length;
  135. if (parseInt(valdecimals) < parseInt(decimals)) {
  136. num += this.makezero(parseInt(decimals) - parseInt(valdecimals));
  137. val = num;
  138. } else if (parseInt(valdecimals) > parseInt(decimals)) {
  139. val = parseFloat(val) !== 0 ? Math.round(this.accMul(val, this.makemultiple(decimals))) / this.makemultiple(decimals) : this.makedecimalzero(decimals);
  140. let num = val.toString();
  141. if (num.lastIndexOf('.') === -1) {
  142. num += '.';
  143. num += this.makezero(decimals);
  144. val = num;
  145. } else {
  146. const valdecimals = num.split('.')[1].length;
  147. if (parseInt(valdecimals) < parseInt(decimals)) {
  148. num += this.makezero(parseInt(decimals) - parseInt(valdecimals));
  149. val = num;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. return val;
  157. },
  158. // 生成num位的0
  159. makezero(num) {
  160. const arr = new Array(num);
  161. for (let i = 0; i < num; i++) {
  162. arr[i] = 0;
  163. }
  164. return arr.join('');
  165. },
  166. // 生成num位的10倍数
  167. makemultiple(num) {
  168. return Math.pow(10, parseInt(num));
  169. },
  170. // 根据单位获取小数位数
  171. findDecimal(unit) {
  172. let value = this.ctx.tender.info.precision.other.value;
  173. const changeUnits = this.ctx.tender.info.precision;
  174. for (const d in changeUnits) {
  175. if (changeUnits[d].unit !== undefined && changeUnits[d].unit === unit) {
  176. value = changeUnits[d].value;
  177. break;
  178. }
  179. }
  180. return value;
  181. },
  182. /**
  183. * 显示排序符号
  184. *
  185. * @param {String} field - 字段名称
  186. * @return {String} - 返回字段排序的符号
  187. */
  188. showSortFlag(field) {
  189. const sort = this.ctx.sort;
  190. if (!(sort instanceof Array) || sort.length !== 2) {
  191. return '';
  192. }
  193. sort[1] = sort[1].toUpperCase();
  194. return (sort[0] === field && sort[1] === 'DESC') ? '' : '-';
  195. },
  196. /**
  197. * 判断是否为ajax请求
  198. *
  199. * @param {Object} request - 请求数据
  200. * @return {boolean} 判断结果
  201. */
  202. isAjax(request) {
  203. let headerInfo = request.headers['x-requested-with'] === undefined ? '' : request.headers['x-requested-with'];
  204. headerInfo = headerInfo.toLowerCase();
  205. return headerInfo === 'xmlhttprequest';
  206. },
  207. /**
  208. * 模拟发送请求
  209. *
  210. * @param {String} url - 请求地址
  211. * @param {Object} data - 请求数据
  212. * @param {String} type - 请求类型(POST) POST | GET
  213. * @param {String} dataType - 数据类型 json|text
  214. * @return {Object} - 请求结果
  215. */
  216. async sendRequest(url, data, type = 'POST', dataType = 'json') {
  217. // 发起请求
  218. try {
  219. const response = await this.ctx.curl(url, {
  220. method: type,
  221. data,
  222. dataType,
  223. });
  224. if (response.status !== 200) {
  225. throw '请求失败';
  226. }
  227. return response.data;
  228. } catch (err) {
  229. throw '请求失败';
  230. }
  231. },
  232. /**
  233. * 深度验证数据
  234. *
  235. * @param {Object} rule - 数据规则
  236. * @return {void}
  237. */
  238. validate(rule) {
  239. // 先用内置的验证器验证数据
  240. this.ctx.validate(rule);
  241. // 然后再验证是否有多余的数据
  242. const postData = this.ctx.request.body;
  243. delete postData._csrf;
  244. const postDataKey = Object.keys(postData);
  245. const ruleKey = Object.keys(rule);
  246. // 自动增加字段则填充上,以防判断出错
  247. if (postData.create_time !== undefined) {
  248. ruleKey.push('create_time');
  249. }
  250. for (const tmp of postDataKey) {
  251. // 规则里面没有定义则抛出异常
  252. if (ruleKey.indexOf(tmp) < 0) {
  253. throw '参数不正确';
  254. }
  255. }
  256. },
  257. /**
  258. * 拆分path
  259. *
  260. * @param {String|Array} paths - 拆分字符
  261. * @param {String} symbol - 拆分符号
  262. * @return {Array} - 拆分结果
  263. */
  264. explodePath(paths, symbol = '-') {
  265. const result = [];
  266. paths = paths instanceof Array ? paths : [paths];
  267. for (const path of paths) {
  268. // 拆分数据
  269. const pathArray = path.split(symbol);
  270. // 用户缓存循环的数据
  271. const tmpArray = [];
  272. for (const tmp of pathArray) {
  273. // 每次循环都追加一个数据进去
  274. tmpArray.push(tmp);
  275. const tmpPathString = tmpArray.join(symbol);
  276. // 判断是否已经存在有对应数据
  277. if (result.indexOf(tmpPathString) >= 0) {
  278. continue;
  279. }
  280. result.push(tmpPathString);
  281. }
  282. }
  283. return result;
  284. },
  285. /**
  286. * 基于obj, 拷贝sObj中的内容
  287. * obj = {a: 1, b: 2}, sObj = {a: 0, c: 3}, 返回{a: 0, b: 2, c: 3}
  288. * @param obj
  289. * @param sObj
  290. * @returns {any}
  291. */
  292. updateObj(obj, sObj) {
  293. if (!obj) {
  294. return JSON.parse(JSON.stringify(sObj));
  295. }
  296. const result = JSON.parse(JSON.stringify(obj));
  297. if (sObj) {
  298. for (const prop in sObj) {
  299. result[prop] = sObj[prop];
  300. }
  301. }
  302. return result;
  303. },
  304. /**
  305. * 在数组中查找
  306. * @param {Array} arr
  307. * @param name -
  308. * @param value
  309. * @returns {*}
  310. */
  311. findData(arr, name, value) {
  312. if (!arr instanceof Array) {
  313. throw '该方法仅用于数组查找';
  314. }
  315. if (arr.length === 0) { return undefined; }
  316. for (const data of arr) {
  317. if (data[name] == value) {
  318. return data;
  319. }
  320. }
  321. return undefined;
  322. },
  323. /**
  324. * 检查数字是否为0
  325. * @param {Number} value
  326. * @return {boolean}
  327. */
  328. checkZero(value) {
  329. return !(value && Math.abs(value) > zeroRange);
  330. },
  331. /**
  332. * 检查数字是否相等
  333. * @param {Number} value1
  334. * @param {Number} value2
  335. * @returns {boolean}
  336. */
  337. checkNumberEqual(value1, value2) {
  338. if (value1 && value2) {
  339. return Math.abs(value2 - value1) > zeroRange;
  340. } else {
  341. return (!value1 && !value2)
  342. }
  343. },
  344. /**
  345. * 比较编码
  346. * @param str1
  347. * @param str2
  348. * @param symbol
  349. * @returns {number}
  350. */
  351. compareCode(str1, str2, symbol = '-') {
  352. if (!str1) {
  353. return -1;
  354. } else if (!str2) {
  355. return 1;
  356. }
  357. const path1 = str1.split(symbol);
  358. const path2 = str2.split(symbol);
  359. const reg = /^[0-9]*$/;
  360. for (let i = 0, iLen = Math.min(path1.length, path2.length); i < iLen; i++) {
  361. if (reg.test(path1[i]) && reg.test(path2[i])) {
  362. const num1 = parseInt(path1[i]);
  363. const num2 = parseInt(path2[i]);
  364. if (num1 !== num2) {
  365. return num1 - num2;
  366. }
  367. } else if (path1[i] < path2[i]) {
  368. return -1;
  369. } else if (path1[i] > path2[i]) {
  370. return 1;
  371. }
  372. }
  373. return path1.length - path2.length;
  374. },
  375. /**
  376. * 树结构节点排序,要求最顶层节点须在同一父节点下
  377. * @param treeNodes
  378. * @param idField
  379. * @param pidField
  380. */
  381. sortTreeNodes (treeNodes, idField, pidField) {
  382. const result = [];
  383. const getFirstLevel = function (nodes) {
  384. let result;
  385. for (const node of nodes) {
  386. if (!result || result > node.level) {
  387. result = node.level;
  388. }
  389. }
  390. return result;
  391. };
  392. const getLevelNodes = function (nodes, level) {
  393. const children = nodes.filter(function (a) {
  394. return a.level = level;
  395. });
  396. children.sort(function (a, b) {
  397. return a.order - b.order;
  398. });
  399. return children;
  400. };
  401. const getChildren = function (nodes, node) {
  402. const children = nodes.filter(function (a) {
  403. return a[pidField] = node[idField];
  404. });
  405. children.sort(function (a, b) {
  406. return a.order - b.order;
  407. });
  408. return children;
  409. };
  410. const addSortNodes = function (nodes) {
  411. for (let i = 0; i< nodes.length; i++) {
  412. result.push(nodes[i]);
  413. addSortNodes(getChildren(nodes[i]));
  414. }
  415. };
  416. const firstLevel = getFirstLevel(treeNodes);
  417. addSortNodes(getLevelNodes(treeNodes, firstLevel));
  418. },
  419. /**
  420. * 判断当前用户是否有指定权限
  421. *
  422. * @param {Number|Array} permission - 权限id
  423. * @return {Boolean} - 返回判断结果
  424. */
  425. hasPermission(permission) {
  426. let result = false;
  427. try {
  428. const sessionUser = this.ctx.session.sessionUser;
  429. if (sessionUser.permission === undefined) {
  430. throw '不存在权限数据';
  431. }
  432. let currentPermission = sessionUser.permission;
  433. if (currentPermission === '') {
  434. throw '权限数据为空';
  435. }
  436. // 管理员则直接返回结果
  437. if (currentPermission === 'all') {
  438. return true;
  439. }
  440. currentPermission = currentPermission.split(',');
  441. permission = permission instanceof Array ? permission : [permission];
  442. let counter = 0;
  443. for (const tmp of permission) {
  444. if (currentPermission[tmp] !== undefined) {
  445. counter++;
  446. }
  447. }
  448. result = counter === permission.length;
  449. } catch (error) {
  450. result = false;
  451. }
  452. return result;
  453. },
  454. /**
  455. * 递归创建文件夹(fs.mkdirSync需要上一层文件夹已存在)
  456. * @param pathName
  457. * @returns {Promise<void>}
  458. */
  459. async recursiveMkdirSync(pathName) {
  460. const upperPath = path.dirname(pathName);
  461. if (!fs.existsSync(upperPath)) {
  462. await this.recursiveMkdirSync(upperPath);
  463. }
  464. await fs.mkdirSync(pathName);
  465. },
  466. /**
  467. * 字节 保存至 本地文件
  468. * @param buffer - 字节
  469. * @param fileName - 文件名
  470. * @returns {Promise<void>}
  471. */
  472. async saveBufferFile(buffer, fileName) {
  473. // 检查文件夹是否存在,不存在则直接创建文件夹
  474. const pathName = path.dirname(fileName);
  475. if (!fs.existsSync(pathName)) {
  476. await this.recursiveMkdirSync(pathName);
  477. }
  478. await fs.writeFileSync(fileName, buffer);
  479. },
  480. /**
  481. * 将文件流的数据保存至本地文件
  482. * @param stream
  483. * @param fileName
  484. * @returns {Promise<void>}
  485. */
  486. async saveStreamFile(stream, fileName) {
  487. // 读取字节流
  488. const parts = await streamToArray(stream);
  489. // 转化为buffer
  490. const buffer = Buffer.concat(parts);
  491. // 写入文件
  492. await this.saveBufferFile(buffer, fileName);
  493. },
  494. /**
  495. * 检查code是否是指标模板数据
  496. * @param {String} code
  497. * @returns {boolean}
  498. */
  499. validBillsCode(code) {
  500. const reg1 = /(^[0-9]+)([a-z0-9\-]*)/i;
  501. const reg2 = /([a-z0-9]+$)/i;
  502. return reg1.test(code) && reg2.test(code);
  503. },
  504. getNumberFormatter(decimal) {
  505. if (decimal <= 0) {
  506. return "0";
  507. }
  508. let pre = "0.";
  509. for (let i = 0; i < decimal; i++) {
  510. pre += "#"
  511. }
  512. return pre;
  513. },
  514. /**
  515. * 根据单位查找对应的清单精度
  516. * @param {tenderInfo.precision} list - 清单精度列表
  517. * @param {String} unit - 单位
  518. * @returns {number}
  519. */
  520. findPrecision(list, unit) {
  521. if (unit) {
  522. for (const p in list) {
  523. if (list[p].unit && list[p].unit === unit) {
  524. return list[p];
  525. }
  526. }
  527. }
  528. return list.other;
  529. },
  530. /**
  531. * 检查数据中的精度
  532. * @param {Object} Obj - 检查的数据
  533. * @param {Array} fields - 检查的属性
  534. * @param {Number} precision - 精度
  535. * @constructor
  536. */
  537. checkFieldPrecision(Obj, fields, precision = 2) {
  538. if (Obj) {
  539. for (const field of fields) {
  540. if (Obj[field]) {
  541. Obj[field] = this.round(Obj[field], precision);
  542. }
  543. }
  544. }
  545. },
  546. /**
  547. * 过滤无效数据
  548. *
  549. * @param obj
  550. * @param fields - 有效数据的数组
  551. */
  552. filterValidFields(data, fields) {
  553. if (data) {
  554. const result = {};
  555. for (const prop in data) {
  556. if (fields.indexOf(prop) !== -1) {
  557. result[prop] = data[prop];
  558. }
  559. }
  560. return result;
  561. } else {
  562. return data;
  563. }
  564. },
  565. // 加减乘除方法,为方便调用,兼容num为空的情况
  566. // 加减法使用base_calc,乘除法使用Decimal(原因详见demo/calc_test)
  567. /**
  568. * 加法 num1 + num2
  569. * @param num1
  570. * @param num2
  571. * @returns {number}
  572. */
  573. add(num1, num2) {
  574. return bc.add(num1 ? num1 : 0, num2 ? num2: 0);
  575. },
  576. /**
  577. * 减法 num1 - num2
  578. * @param num1
  579. * @param num2
  580. * @returns {number}
  581. */
  582. sub(num1, num2) {
  583. return bc.sub(num1 ? num1 : 0, num2 ? num2 : 0);
  584. },
  585. /**
  586. * 乘法 num1 * num2
  587. * @param num1
  588. * @param num2
  589. * @returns {*}
  590. */
  591. mul(num1, num2, digit = 6) {
  592. return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber();
  593. },
  594. /**
  595. * 除法 num1 / num2
  596. * @param num1 - 被除数
  597. * @param num2 - 除数
  598. * @returns {*}
  599. */
  600. div(num1, num2, digit = 6) {
  601. if (num2 && !this.checkZero(num2)) {
  602. return Decimal.div(num1 ? num1: 0, num2).toDecimalPlaces(digit).toNumber();
  603. } else {
  604. return null;
  605. }
  606. },
  607. /**
  608. * 四舍五入(统一,方便以后万一需要置换)
  609. * @param {Number} value - 舍入的数字
  610. * @param {Number} decimal - 要保留的小数位数
  611. * @returns {*}
  612. */
  613. round(value, decimal) {
  614. //return value ? bc.round(value, decimal) : null;
  615. return value ? new Decimal(value).toDecimalPlaces(decimal).toNumber() : null;
  616. },
  617. /**
  618. * 汇总
  619. * @param array
  620. * @returns {number}
  621. */
  622. sum(array) {
  623. let result = 0;
  624. for (const a of array) {
  625. result = this.add(result, a);
  626. }
  627. return result;
  628. },
  629. /**
  630. * 使用正则替换字符
  631. * @param str
  632. * @param reg
  633. * @param subStr
  634. * @returns {*}
  635. */
  636. replaceStr(str, reg, subStr) {
  637. return str ? str.replace(reg, subStr) : str;
  638. },
  639. /**
  640. * 替换字符串中的 换行符回车符
  641. * @param str
  642. * @returns {*}
  643. */
  644. replaceReturn(str) {
  645. return (str && typeof str === 'string') ? str.replace(/[\r\n]/g, '') : str;
  646. },
  647. /**
  648. * 获取 字符串 数组的 mysql 筛选条件
  649. *
  650. * @param arr
  651. * @returns {*}
  652. */
  653. getInArrStrSqlFilter(arr) {
  654. let result = '';
  655. for (const a of arr) {
  656. if (result !== '') {
  657. result = result + ','
  658. }
  659. result = result + this.ctx.app.mysql.escape(a);
  660. }
  661. return result;
  662. },
  663. /**
  664. * 合并相关数据
  665. * @param {Array} main - 主数据
  666. * @param {Array[]}rela - 相关数据 {data, fields, prefix, relaId}
  667. */
  668. assignRelaData(main, rela) {
  669. const index = {}, indexPre = 'id_';
  670. const loadFields = function (datas, fields, prefix, relaId) {
  671. for (const d of datas) {
  672. const key = indexPre + d[relaId];
  673. const m = index[key];
  674. if (m) {
  675. for (const f of fields) {
  676. if (d[f] !== undefined) {
  677. m[prefix + f] = d[f];
  678. }
  679. }
  680. }
  681. }
  682. };
  683. for (const m of main) {
  684. index[indexPre + m.id] = m;
  685. }
  686. for (const r of rela) {
  687. loadFields(r.data, r.fields, r.prefix, r.relaId);
  688. }
  689. },
  690. whereSql (where, as) {
  691. if (!where) {
  692. return '';
  693. }
  694. const wheres = [];
  695. const values = [];
  696. for (const key in where) {
  697. const value = where[key];
  698. if (Array.isArray(value)) {
  699. wheres.push('?? IN (?)');
  700. } else {
  701. wheres.push('?? = ?');
  702. }
  703. values.push((as && as !== '') ? as + '.' + key : key);
  704. values.push(value);
  705. }
  706. if (wheres.length > 0) {
  707. return this.ctx.app.mysql.format(' WHERE ' + wheres.join(' AND '), values);
  708. }
  709. return '';
  710. },
  711. formatMoney(s, dot = ',') {
  712. if (!s) return '0.00';
  713. s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";
  714. var l = s.split(".")[0].split("").reverse(),
  715. r = s.split(".")[1];
  716. let t = "";
  717. for(let i = 0; i < l.length; i ++ ) {
  718. t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? dot : "");
  719. }
  720. return t.split("").reverse().join("") + "." + r;
  721. }
  722. };