util.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. const Constants = require('../utils/Constants.js')
  2. // 转换时间
  3. const getDate = (year, month, day, hour, minute) => {
  4. const newyear = year.substr(0, year.length - 1);
  5. const setmonth = month.substr(0, month.length - 1);
  6. const newmonth = setmonth < 10 ? '0' + setmonth : setmonth;
  7. const setday = day.substr(0, day.length - 1);
  8. const newday = setday < 10 ? '0' + setday : setday;
  9. // const sethour = hour.substr(0, hour.length - 1);
  10. const newhour = hour < 10 ? '0' + hour : hour;
  11. // const setminute = minute.substr(0, minute.length - 1);
  12. const newminute = minute < 10 ? '0' + minute : minute;
  13. return newyear + '-' + newmonth + '-' + newday + ' ' + newhour + ":" + newminute;
  14. }
  15. // 将时间戳转换为时间
  16. const getobjDate = (date) => {
  17. let now;
  18. if (date) {
  19. now = new Date(date)
  20. } else {
  21. now = new Date()
  22. }
  23. let y = now.getFullYear(),
  24. m = now.getMonth() + 1,
  25. d = now.getDate(),
  26. h = now.getHours(), //获取当前小时数(0-23)
  27. f = now.getMinutes(),
  28. n = (Math.ceil((now.getMinutes()) / 10)) * 10; //获取当前分钟数(0-59) 取整数
  29. return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + (h < 10 ? "0" + h : h) + ":" + (f < 10 ? "0" + f : f);
  30. }
  31. // 将时间戳转换为日期
  32. const toDateStr = (date) => {
  33. let now;
  34. if (date) {
  35. now = new Date(date)
  36. } else {
  37. now = new Date()
  38. }
  39. let y = now.getFullYear(),
  40. m = now.getMonth() + 1,
  41. d = now.getDate()
  42. return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
  43. }
  44. const getobjDateApple = (date) => {
  45. let now;
  46. if (date) {
  47. now = new Date(date)
  48. } else {
  49. now = new Date()
  50. }
  51. let y = now.getFullYear(),
  52. m = now.getMonth() + 1,
  53. d = now.getDate(),
  54. h = now.getHours(), //获取当前小时数(0-23)
  55. f = now.getMinutes(),
  56. n = (Math.ceil((now.getMinutes()) / 10)) * 10; //获取当前分钟数(0-59) 取整数
  57. return y + "/" + (m < 10 ? "0" + m : m) + "/" + (d < 10 ? "0" + d : d) + " " + (h < 10 ? "0" + h : h) + ":" + (f < 10 ? "0" + f : f);
  58. }
  59. //根据年月 获取天数
  60. const mGetDate = (year, month) => {
  61. var d = new Date(year, month, 0);
  62. return d.getDate();
  63. }
  64. //根据时间2019-01-02 09:12 得到 ['2019','1','2','9','12']
  65. const getarrWithtime = (str) => {
  66. let arr = [];
  67. let arr1 = str.split(' ');
  68. let arr2 = (arr1[0]).split('-');
  69. let arr3 = arr1[1].split(':');
  70. arr = arr2.concat(arr3);
  71. arr[1] = arr[1].startsWith('0') ? arr[1].substr(1, arr[1].length) : arr[1];
  72. arr[2] = arr[2].startsWith('0') ? arr[2].substr(1, arr[2].length) : arr[2];
  73. arr[3] = arr[3].startsWith('0') ? arr[3].substr(1, arr[3].length) : arr[3];
  74. arr[4] = arr[4].startsWith('0') ? arr[4].substr(1, arr[4].length) : arr[4];
  75. return arr;
  76. }
  77. const getTime = date => {
  78. const year = date.getFullYear()
  79. const month = date.getMonth() + 1
  80. const day = date.getDate()
  81. const hour = date.getHours()
  82. const minute = date.getMinutes()
  83. const second = date.getSeconds()
  84. return [hour, minute, second].map(formatNumber).join(':')
  85. }
  86. const formatTime = date => {
  87. const year = date.getFullYear()
  88. const month = date.getMonth() + 1
  89. const day = date.getDate()
  90. const hour = date.getHours()
  91. const minute = date.getMinutes()
  92. const second = date.getSeconds()
  93. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  94. }
  95. const formatDataTime = date => {
  96. const year = date.getFullYear()
  97. const month = date.getMonth() + 1
  98. const day = date.getDate()
  99. const hour = date.getHours()
  100. const minute = date.getMinutes()
  101. const second = date.getSeconds()
  102. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  103. }
  104. const transformTimestamp = timestamp => {
  105. let a = new Date(timestamp).getTime();
  106. const date = new Date(a);
  107. const year = date.getFullYear()
  108. const month = date.getMonth() + 1
  109. const day = date.getDate()
  110. const hour = date.getHours()
  111. const minute = date.getMinutes()
  112. const second = date.getSeconds()
  113. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  114. }
  115. const formatDayTime = date => {
  116. const year = date.getFullYear()
  117. const month = date.getMonth() + 1
  118. const day = date.getDate()
  119. return [year, month, day].map(formatNumber).join('-')
  120. }
  121. const formatDayTimeMoth = date => {
  122. const year = date.getFullYear()
  123. const month = date.getMonth() + 1
  124. const day = '01'
  125. return [year, month, day].map(formatNumber).join('-')
  126. }
  127. const formatDayTimeApple = date => {
  128. const year = date.getFullYear()
  129. const month = date.getMonth() + 1
  130. const day = date.getDate()
  131. return [year, month, day].map(formatNumber).join('/')
  132. }
  133. const formatDayYm = date => {
  134. const year = date.getFullYear()
  135. const month = date.getMonth() + 1
  136. return year + '年' + month + '月'
  137. }
  138. const getGuid = function () {
  139. var s = [];
  140. var hexDigits = "0123456789abcdef";
  141. for (var i = 0; i < 36; i++) {
  142. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  143. }
  144. s[14] = "4";
  145. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  146. s[8] = s[13] = s[18] = s[23] = "-";
  147. var uuid = s.join("");
  148. return uuid;
  149. }
  150. function getSpecifiedYear(num) {
  151. var myDate = new Date();
  152. var year = myDate.getFullYear() + num;
  153. var beginDate = year + "-01-01";
  154. var endDate = year + "-12-31";
  155. return [beginDate, endDate]
  156. }
  157. const formatNumber = n => {
  158. n = n.toString()
  159. return n[1] ? n : '0' + n
  160. }
  161. //浮点数加法运算解决精度问题
  162. function AddNumber(arg1, arg2) {
  163. arg1 = arg1.toString(), arg2 = arg2.toString();
  164. var arg1Arr = arg1.split("."), arg2Arr = arg2.split("."), d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
  165. var maxLen = Math.max(d1.length, d2.length);
  166. var m = Math.pow(10, maxLen);
  167. var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
  168. var d = arguments[2];
  169. return typeof d === "number" ? Number((result).toFixed(d)) : result;
  170. }
  171. // 两个浮点数相减
  172. function SubNumber(num1, num2) {
  173. var r1, r2, m;
  174. try {
  175. r1 = num1.toString().split('.')[1].length;
  176. } catch (e) {
  177. r1 = 0;
  178. }
  179. try {
  180. r2 = num2.toString().split(".")[1].length;
  181. } catch (e) {
  182. r2 = 0;
  183. }
  184. var m = Math.pow(10, Math.max(r1, r2));
  185. var n = (r1 >= r2) ? r1 : r2;
  186. return (Math.round(num1 * m - num2 * m) / m).toFixed(n);
  187. }
  188. function accMul(arg1, arg2) {
  189. var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
  190. try { m += s1.split(".")[1].length } catch (e) { }
  191. try { m += s2.split(".")[1].length } catch (e) { }
  192. return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
  193. }
  194. function except(arg1, arg2) {
  195. var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
  196. try { m += s1.split(".")[1].length } catch (e) { }
  197. try { m += s2.split(".")[1].length } catch (e) { }
  198. return Number(s1.replace(".", "")) / Number(s2.replace(".", "")) / Math.pow(10, m)
  199. }
  200. //获取当前时间 返回格式2021-12-14
  201. function getTodayTime() {
  202. var date = new Date();
  203. var time = (date.getFullYear() + 1) + "-" + (date.getMonth() + 1) + "-" + date.getDate()
  204. return time;
  205. }
  206. function fun_date(num) {
  207. var date1 = new Date();
  208. //今天时间
  209. var date2 = new Date(date1);
  210. date2.setDate(date1.getDate() + num);
  211. //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  212. var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
  213. return time2;
  214. }
  215. function getCurrentMonth() {
  216. var firstDate = new Date();
  217. var startDate = firstDate.getFullYear() + "-" + ((firstDate.getMonth() + 1) < 10 ? "0" : "") + (firstDate.getMonth() + 1) + "-" + "01";
  218. var date = new Date();
  219. var currentMonth = date.getMonth();
  220. var nextMonth = ++currentMonth;
  221. var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  222. var oneDay = 1000 * 60 * 60 * 24;
  223. var lastDate = new Date(nextMonthFirstDay - oneDay);
  224. var endDate = lastDate.getFullYear() + "-" + ((lastDate.getMonth() + 1) < 10 ? "0" : "") + (lastDate.getMonth() + 1) + "-" + (lastDate.getDate() < 10 ? "0" : "") + lastDate.getDate();
  225. return [startDate, endDate]
  226. }
  227. function getCurrentDateMonth(cdate) {
  228. var firstDate = new Date(cdate);
  229. var startDate = firstDate.getFullYear() + "-" + ((firstDate.getMonth() + 1) < 10 ? "0" : "") + (firstDate.getMonth() + 1) + "-" + "01";
  230. var date = new Date(cdate);
  231. var currentMonth = date.getMonth();
  232. var nextMonth = ++currentMonth;
  233. var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  234. var oneDay = 1000 * 60 * 60 * 24;
  235. var lastDate = new Date(nextMonthFirstDay - oneDay);
  236. var endDate = lastDate.getFullYear() + "-" + ((lastDate.getMonth() + 1) < 10 ? "0" : "") + (lastDate.getMonth() + 1) + "-" + (lastDate.getDate() < 10 ? "0" : "") + lastDate.getDate();
  237. return [startDate, endDate]
  238. }
  239. function calc10000(arr, keySet) {
  240. arr.forEach(it => {
  241. keySet.forEach(kIt => {
  242. if (it[kIt]) {
  243. it[kIt] = parseFloat(it[kIt] / 10000).toFixed(2)
  244. }
  245. })
  246. })
  247. return arr
  248. }
  249. function parseFloatFixed2(num) {
  250. return parseFloat(num).toFixed(2)
  251. }
  252. function getCurrentWeek(n) {
  253. /***参数都是以周一为基准的***/
  254. //上周的开始时间
  255. // //上周的结束时间
  256. // //本周的开始时间
  257. // //本周的结束时间
  258. var now = new Date();
  259. var year = now.getFullYear();
  260. //因为月份是从0开始的,所以获取这个月的月份数要加1才行
  261. var month = now.getMonth() + 1;
  262. var date = now.getDate();
  263. var day = now.getDay();
  264. //判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
  265. if (day !== 0) {
  266. n = n + (day - 1);
  267. } else {
  268. n = n + day;
  269. }
  270. if (day) {
  271. //这个判断是为了解决跨年的问题
  272. if (month > 1) {
  273. month = month;
  274. }
  275. //这个判断是为了解决跨年的问题,月份是从0开始的
  276. else {
  277. year = year - 1;
  278. month = 12;
  279. }
  280. }
  281. now.setDate(now.getDate() - n);
  282. return now;
  283. }
  284. function drawAndShareImage(param) {
  285. const ctx = wx.createCanvasContext('qrcode')
  286. ctx.beginPath()
  287. ctx.clearRect(0, 0, param.canvasWidth, param.canvasHeight)
  288. ctx.setFillStyle('#fff')
  289. ctx.fillRect(0, 0, param.canvasWidth, param.canvasHeight)
  290. /** 背景图 */
  291. ctx.beginPath()
  292. ctx.drawImage(param.background, 0, 0, param.canvasWidth, param.canvasHeight)
  293. ctx.save();
  294. /** 二维码 */
  295. ctx.beginPath()
  296. ctx.arc(param.qrX, param.qrY, 0, 0, 0)
  297. ctx.fill()
  298. ctx.clip()
  299. ctx.drawImage(
  300. param.qrcode,
  301. 0, 0, 375, 375
  302. )
  303. ctx.restore()
  304. ctx.save()
  305. ctx.draw(false, () => {
  306. wx.canvasToTempFilePath({
  307. x: 100,
  308. y: 200,
  309. width: 50,
  310. height: 50,
  311. destWidth: 100,
  312. destHeight: 100,
  313. canvasId: 'qrcode',
  314. complete(res) { }
  315. })
  316. })
  317. }
  318. //日期转换
  319. Date.prototype.format = function (format) {
  320. var o = {
  321. "M+": this.getMonth() + 1, //month
  322. "d+": this.getDate(), //day
  323. "h+": this.getHours(), //hour
  324. "m+": this.getMinutes(), //minute
  325. "s+": this.getSeconds(), //second
  326. "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
  327. "S": this.getMilliseconds() //millisecond
  328. }
  329. if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
  330. (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  331. for (var k in o)
  332. if (new RegExp("(" + k + ")").test(format))
  333. format = format.replace(RegExp.$1,
  334. RegExp.$1.length == 1 ? o[k] :
  335. ("00" + o[k]).substr(("" + o[k]).length));
  336. return format;
  337. }
  338. Date.prototype.before = function (day) {
  339. var timestamp = Date.parse(this);
  340. var beforeTimestamp = timestamp - 86400000 * day;
  341. return new Date(beforeTimestamp);
  342. }
  343. Date.prototype.after = function (day) {
  344. var timestamp = Date.parse(this);
  345. var afterTimestamp = timestamp + 86400000 * day;
  346. return new Date(afterTimestamp);
  347. }
  348. //时间戳换成日期时间转
  349. function js_date_time(unixtime) {
  350. var date = new Date(unixtime);
  351. var y = date.getFullYear();
  352. var m = date.getMonth() + 1;
  353. m = m < 10 ? ('0' + m) : m;
  354. var d = date.getDate();
  355. d = d < 10 ? ('0' + d) : d;
  356. return y + '-' + m + '-' + d;
  357. }
  358. //时间戳换成日期时间转
  359. function js_month_time(unixtime) {
  360. var date = new Date(unixtime);
  361. var y = date.getFullYear();
  362. var m = date.getMonth() + 1;
  363. m = m < 10 ? ('0' + m) : m;
  364. var d = date.getDate();
  365. d = d < 10 ? ('0' + d) : d;
  366. let a = {
  367. first: y + '-' + m,
  368. second: y + m
  369. }
  370. return a;
  371. }
  372. function getYear(type, dates) {
  373. var dd = new Date();
  374. var n = dates || 0;
  375. var year = dd.getFullYear() + Number(n);
  376. var month = dd.getMonth() + Number(n);
  377. if (month != null && month != 11) {
  378. if (type == "s") {
  379. var day = year - 1 + "-11-30";
  380. };
  381. if (type == "e") {
  382. var day = year + "-12-01";
  383. };
  384. if (!type) {
  385. var day = year + "-01-01/" + year + "-12-31";
  386. };
  387. return day;
  388. } else if (month != null && month == 11) {
  389. if (type == "s") {
  390. var day = year + "-11-30";
  391. };
  392. if (type == "e") {
  393. var day = year + 1 + "-12-01";
  394. };
  395. if (!type) {
  396. var day = year + "-01-01/" + year + "-12-31";
  397. };
  398. return day;
  399. }
  400. }
  401. function timeForMat(count) {
  402. // 拼接时间
  403. const time1 = new Date()
  404. const time2 = new Date()
  405. if (count === 1) {
  406. time1.setTime(time1.getTime() - (24 * 60 * 60 * 1000))
  407. } else {
  408. if (count >= 0) {
  409. time1.setTime(time1.getTime())
  410. } else {
  411. if (count === -2) {
  412. time1.setTime(time1.getTime() + (24 * 60 * 60 * 1000) * 2)
  413. } else {
  414. time1.setTime(time1.getTime() + (24 * 60 * 60 * 1000))
  415. }
  416. }
  417. }
  418. const Y1 = time1.getFullYear()
  419. const M1 = ((time1.getMonth() + 1) > 9 ? (time1.getMonth() + 1) : '0' + (time1.getMonth() + 1))
  420. const D1 = (time1.getDate() > 9 ? time1.getDate() : '0' + time1.getDate())
  421. const timer1 = Y1 + '-' + M1 + '-' + D1 // 当前时间
  422. time2.setTime(time2.getTime() - (24 * 60 * 60 * 1000 * count))
  423. const Y2 = time2.getFullYear()
  424. const M2 = ((time2.getMonth() + 1) > 9 ? (time2.getMonth() + 1) : '0' + (time2.getMonth() + 1))
  425. const D2 = (time2.getDate() > 9 ? time2.getDate() : '0' + time2.getDate())
  426. const timer2 = Y2 + '-' + M2 + '-' + D2 // 以前的7天或者30天
  427. return [timer2, timer1]
  428. }
  429. /**
  430. * @desc : 复制
  431. * @date : 2022/7/29 16:49
  432. * @author : 周兴
  433. */
  434. function toCopy(value, label) {
  435. if (!value || !label) return;
  436. wx.setClipboardData({
  437. data: value,
  438. success: res => {
  439. wx.showToast({
  440. title: label + '已复制',
  441. duration: 1000,
  442. })
  443. }
  444. })
  445. }
  446. function numAdd(num1, num2) {
  447. var baseNum, baseNum1, baseNum2;
  448. try {
  449. baseNum1 = num1.toString().split(".")[1].length;
  450. } catch (e) {
  451. baseNum1 = 0;
  452. }
  453. try {
  454. baseNum2 = num2.toString().split(".")[1].length;
  455. } catch (e) {
  456. baseNum2 = 0;
  457. }
  458. baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  459. return (num1 * baseNum + num2 * baseNum) / baseNum;
  460. }
  461. function numSub(num1, num2) {
  462. var baseNum, baseNum1, baseNum2;
  463. var precision;// 精度
  464. try {
  465. baseNum1 = num1.toString().split(".")[1].length;
  466. } catch (e) {
  467. baseNum1 = 0;
  468. }
  469. try {
  470. baseNum2 = num2.toString().split(".")[1].length;
  471. } catch (e) {
  472. baseNum2 = 0;
  473. }
  474. baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
  475. precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
  476. return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision);
  477. }
  478. function jumpPageDate(type) {
  479. let date = []
  480. if (type == 1) {
  481. date = [formatTime(new Date()).substring(0, 10), formatTime(new Date()).substring(0, 10)]
  482. }
  483. else if (type == 2) {
  484. date = [formatTime(getCurrentWeek(0)).substring(0, 10), formatTime(getCurrentWeek(-6)).substring(0, 10)]
  485. }
  486. else if (type == 3) {
  487. date = getCurrentMonth();
  488. } else if (type == 4) {
  489. let startYear = getYear("s", 0)
  490. let endYear = getYear("e", 0)
  491. //本年
  492. date = [formatTime(new Date(startYear)).substring(0, 10),
  493. formatTime(new Date(endYear)).substring(0, 10)]
  494. }
  495. else if (type == 6) {
  496. //近7
  497. date = timeForMat(7)
  498. }
  499. else if (type == 5) {
  500. //近30天
  501. date = timeForMat(30)
  502. } else if (type == -1) {
  503. let dateEnd = formatTime(new Date()).substring(0, 10)
  504. let startYear = new Date().getFullYear() - 1;
  505. let startMonth = new Date().getMonth() + 1 > 9 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1);
  506. let startDay = new Date().getDate() > 9 ? new Date().getDate() : '0' + new Date().getDate();
  507. if (startMonth === 2 && startDay === 29) {
  508. startDay = 28
  509. }
  510. date = [startYear + '/' + startMonth + '/' + startDay, dateEnd]
  511. } else if (type == -2) {
  512. let dateEnd = formatTime(new Date()).substring(0, 10)
  513. let startYear = new Date().getFullYear() + 1;
  514. let startMonth = new Date().getMonth() + 1 > 9 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1);
  515. let startDay = new Date().getDate() > 9 ? new Date().getDate() : '0' + new Date().getDate();
  516. if (startMonth === 2 && startDay === 29) {
  517. startDay = 28
  518. }
  519. date = [dateEnd, startYear + '/' + startMonth + '/' + startDay]
  520. }
  521. return date;
  522. }
  523. /**
  524. * @desc : 判断手机号格式
  525. * @author : 于继渤
  526. * @date : 2022/8/19 10:16
  527. */
  528. function isPoneAvailable(poneInput) {
  529. var myreg = Constants.Mobile_Phone_Number_Regular_Expression
  530. if (!myreg.test(poneInput)) {
  531. return false;
  532. } else {
  533. return true;
  534. }
  535. }
  536. /**
  537. * @desc : 深度拷贝
  538. * @author : 于继渤
  539. * @date : 2022/8/19 10:16
  540. */
  541. function copyObj(obj) {
  542. var _obj = JSON.stringify(obj);
  543. var objClone = JSON.parse(_obj);
  544. return objClone;
  545. }
  546. /**
  547. * @desc : 直辖市更新
  548. * @author : 于继渤
  549. * @date : 2022/8/19 10:16
  550. */
  551. function updateProvince(location) {
  552. if (location.province && location.province != '' && typeof location.province == 'string') {
  553. let province = location.province
  554. if (province.indexOf('北京') != -1) {
  555. province = '北京'
  556. return location.province = province
  557. }
  558. if (province.indexOf('上海') != -1) {
  559. province = '上海'
  560. return location.province = province
  561. }
  562. if (province.indexOf('天津') != -1) {
  563. province = '天津'
  564. return location.province = province
  565. }
  566. if (province.indexOf('重庆') != -1) {
  567. province = '重庆'
  568. return location.province = province
  569. }
  570. } else {
  571. if (location.city) {
  572. let province = location.city.replace('市', '')
  573. return location.province = province
  574. }
  575. }
  576. }
  577. //判断数字是否为负数并转为负数
  578. function isNumberNegative(n) {
  579. if (!isNaN(n)) {
  580. let numberTemp = Number(0)
  581. if (n > 0) {
  582. numberTemp = (Number(0) - Number(n))
  583. return numberTemp
  584. } else if (n < 0) {
  585. return n
  586. } else {
  587. return n
  588. }
  589. } else {
  590. return Number(n)
  591. }
  592. }
  593. /**
  594. * @desc : 合并实体(供main.js)调用,其他地方,调用上面那个
  595. * @author : 周兴
  596. * @date : 2022/11/21 14:25
  597. */
  598. function objectMergeByMainJs(source, target) {
  599. if (typeof source !== 'object') {
  600. source = {};
  601. }
  602. if (Array.isArray(target)) {
  603. return source.slice();
  604. }
  605. if (!source['key']) {
  606. Object.keys(target).forEach((property) => {
  607. const targetProperty = target[property];
  608. if (typeof targetProperty === 'object') {
  609. source[property] = this.objectMergeByMainJs(source[property], targetProperty);
  610. } else {
  611. source[property] = targetProperty;
  612. }
  613. });
  614. }
  615. return source;
  616. }
  617. /**
  618. * @desc : id parentId扁平结构转变为children层级结构
  619. * @author : 周兴
  620. * @date : 2022/12/30 15:55
  621. */
  622. function convertToChildren(arr, pId = 'parentId', id = 'id', path = null) {
  623. if (!arr || arr.length === 0) return arr;
  624. let data = arr.filter(it => !it[pId]) // 获取最顶级
  625. if (data && data.length > 0) {
  626. data.forEach(it => {
  627. _convertToChildren(it, arr, it[id], pId, id);
  628. })
  629. }
  630. return data;
  631. }
  632. /**
  633. * @desc : 转成children格式数据(私有方法)
  634. * @author : 周兴
  635. * @date : 2022/12/30 17:06
  636. */
  637. function _convertToChildren(item, arr, idValue, pId, id, path = null) {
  638. // console.log('t1',item,idValue,)
  639. let filters = arr.filter(it => it[pId] === idValue);
  640. if (!filters || filters.length === 0) {
  641. return [];
  642. } else {
  643. item.children = filters;
  644. item.children.forEach(it => {
  645. _convertToChildren(it, arr, it[id], pId, id, path);
  646. })
  647. return item;
  648. }
  649. }
  650. /**
  651. * @desc : 根据枚举值获取相应的键名
  652. * @author : 周兴
  653. * @date : 2024/2/29 17:06
  654. */
  655. function getKeyByValue(enumObj, value) {
  656. for (let key in enumObj) {
  657. if (enumObj[key] === value) {
  658. return key;
  659. }
  660. }
  661. return null;
  662. }
  663. // 判断是否为空
  664. function isEmpty(obj) {
  665. if (typeof obj === 'undefined' || obj === null || obj === '') return true;
  666. return false
  667. }
  668. /**
  669. * @desc : 过滤掉数组中的空值行
  670. * @author : 周兴
  671. * @date : 2024/1/26 11:46
  672. */
  673. function filterArrayEmpty(table) {
  674. if (!table || table.length == 0) return table;
  675. let keys = Object.keys(table[0]);
  676. let returnTable = []
  677. table.forEach(row => {
  678. let flag = true
  679. keys.forEach(k => {
  680. if (k.indexOf('_errMsg') < 0 && !isEmpty(row[k])) {
  681. flag = false;
  682. }
  683. })
  684. if (!flag) {
  685. returnTable.push(row)
  686. }
  687. })
  688. return returnTable;
  689. }
  690. /**
  691. * @desc : 提示信息
  692. * @author : 周兴
  693. * @date : 2024/1/26 11:46
  694. */
  695. function showToast(info,duration = 2000) {
  696. if (info) {
  697. // 默认2s
  698. wx.showToast({
  699. title: info,
  700. icon: 'none',
  701. duration: duration
  702. })
  703. }
  704. }
  705. /**
  706. * @desc : 根据小数位数处理数据
  707. * @author : 周兴
  708. * @date : 2024/1/26 11:46
  709. */
  710. function handleQty(qty, decimalPlace = 2) {
  711. // 确保qty是数值类型
  712. const numQty = Number(qty);
  713. // 检查decimalPlace是否为有效数字,并避免非数字值
  714. if (isNaN(decimalPlace) || decimalPlace < 0) {
  715. decimalPlace = 2; // 默认设置为2,或者根据需要设置其他默认值
  716. }
  717. // 计算应该乘以的数值(10的decimalPlace次方)
  718. const multiplier = Math.pow(10, decimalPlace);
  719. // 执行四舍五入操作
  720. const roundedQty = Math.round(numQty * multiplier) / multiplier;
  721. return roundedQty;
  722. }
  723. /**
  724. * @desc : 防抖函数
  725. * @author : 于继渤
  726. * @date : 2024/6/28
  727. */
  728. function debounce(func, that, wait) {
  729. let timeout;
  730. if(!wait){
  731. wait = 400
  732. }
  733. return function() {
  734. const context = that;
  735. const args = arguments;
  736. clearTimeout(timeout);
  737. timeout = setTimeout(() => {
  738. func.apply(context, args);
  739. }, wait);
  740. };
  741. }
  742. /**
  743. * @desc : 处理菜单
  744. * @author : 于继渤
  745. * @date : 2024/6/28
  746. */
  747. function handleMenu(funPackage,menuList){
  748. // 没有功能包就把vip功能都去掉
  749. if(!funPackage || funPackage.length == 0){
  750. menuList = menuList.filter(it=>!it.isVip);
  751. }else{
  752. // 如果有功能包,就过滤掉功能包之外的vip功能
  753. let funUuids = []
  754. funPackage.forEach(it=>{
  755. if(it.funUuids && it.funUuids.length > 0){
  756. funUuids = funUuids.union(it.funUuids)
  757. }
  758. })
  759. menuList = menuList.filter(it=>!it.isVip || funUuids.includes(it.funUuid));
  760. }
  761. let finalList = menuList.filter(it=>it.menuType == 1);
  762. // 处理一级菜单没有子级
  763. let filters = menuList.filter(it=>it.menuType == 0);
  764. filters.forEach(it=>{
  765. let tfilter = menuList.filter(t=>t.parentUuid == it.menuUuid)
  766. if(tfilter && tfilter.length > 0){
  767. finalList.push(it);
  768. }
  769. })
  770. return finalList
  771. }
  772. /**
  773. * @desc : 设置查询提示值
  774. * @author : 周兴
  775. * @date : 2024/4/18 11:46
  776. */
  777. function setSearchPlaceholder(lang, cols) {
  778. var text = '';
  779. if (typeof cols == 'object') {
  780. cols.forEach(it=>{
  781. if(lang[it]){
  782. text += lang[it] + '/';
  783. }
  784. })
  785. text = text.substring(0, text.length - 1);
  786. } else {
  787. text = lang[cols];
  788. }
  789. return lang['search'] + ' ' + text;
  790. }
  791. module.exports = {
  792. handleMenu:handleMenu,
  793. debounce:debounce,
  794. handleQty:handleQty,
  795. setSearchPlaceholder:setSearchPlaceholder,
  796. showToast:showToast,
  797. filterArrayEmpty: filterArrayEmpty,
  798. getKeyByValue: getKeyByValue,
  799. convertToChildren: convertToChildren,
  800. objectMergeByMainJs: objectMergeByMainJs,
  801. updateProvince: updateProvince,
  802. isPoneAvailable: isPoneAvailable,
  803. formatTime: formatTime,
  804. drawAndShareImage,
  805. fun_date,
  806. getCurrentWeek,
  807. getCurrentMonth,
  808. getCurrentDateMonth,
  809. getTime,
  810. getGuid,
  811. js_date_time,
  812. js_month_time,
  813. getDate,
  814. toDateStr,
  815. getobjDate,
  816. mGetDate,
  817. getarrWithtime,
  818. getSpecifiedYear,
  819. getTodayTime,
  820. formatDataTime,
  821. formatDayTime,
  822. transformTimestamp,
  823. getYear,
  824. timeForMat,
  825. AddNumber,
  826. accMul,
  827. except,
  828. toCopy,
  829. numAdd,
  830. numSub, formatDayYm, jumpPageDate, calc10000, parseFloatFixed2,
  831. formatDayTimeApple, getobjDateApple,
  832. formatDayTimeMoth: formatDayTimeMoth,
  833. copyObj,
  834. SubNumber,
  835. isNumberNegative
  836. }