util.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. import env from "@/http/config/config.js"
  2. /**
  3. * 判断字符串是否为空
  4. * @param obj
  5. * @returns {boolean}
  6. */
  7. export const isStringEmpty = (obj) => {
  8. if (typeof obj == "undefined" || obj == null || obj === "") {
  9. return true;
  10. } else {
  11. return false;
  12. }
  13. }
  14. /**
  15. * 判断对象、字符串是否为空
  16. * @param obj
  17. * @returns {boolean}
  18. */
  19. export const isObjStringEmpty = (obj) => {
  20. if (typeof obj == "object" && JSON.stringify(obj) == "{}") {
  21. return true;
  22. } else {
  23. return isStringEmpty(obj);
  24. }
  25. }
  26. /**
  27. * 判断字符串是否非空
  28. * @param obj
  29. * @returns {boolean}
  30. */
  31. export const isStringNotEmpty = (obj) => {
  32. return !isStringEmpty(obj);
  33. }
  34. /**
  35. * 判断对象、字符串是否非空
  36. * @param obj
  37. * @returns {boolean}
  38. */
  39. export const isObjStringNotEmpty = (obj) => {
  40. return !isObjStringEmpty(obj);
  41. }
  42. /**
  43. * 字符串如果为空,转换字符串为null
  44. * @param obj
  45. * @returns {boolean}
  46. */
  47. export const changeStringEmptyToNull = (obj) => {
  48. if (isStringEmpty(obj)) {
  49. obj = null;
  50. }
  51. return obj;
  52. }
  53. /**
  54. * 字符串如果为空,转换字符串为""
  55. * @param obj
  56. * @returns {boolean}
  57. */
  58. export const changeStringNullToEmpty = (obj) => {
  59. if (typeof obj == "undefined" || obj == null) {
  60. obj = "";
  61. }
  62. return obj;
  63. }
  64. /**
  65. * 字符串如果为空,转换字符串为""
  66. * @param obj
  67. * @returns {boolean}
  68. */
  69. export const stringEndWith = (str) => {
  70. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  71. return false;
  72. if (this.substring(this.length - str.length) == str)
  73. return true;
  74. else
  75. return false;
  76. };
  77. export const formatDate = (shijianchuo) => {
  78. //shijianchuo是整数,否则要parseInt转换
  79. function add0(m) {
  80. return m < 10 ? '0' + m : m
  81. }
  82. var time = new Date(shijianchuo);
  83. var y = time.getFullYear();
  84. var m = time.getMonth() + 1;
  85. var d = time.getDate();
  86. var h = time.getHours();
  87. var mm = time.getMinutes();
  88. var s = time.getSeconds();
  89. return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  90. }
  91. /**
  92. * 格式化时间对象
  93. * @param {string} fmt Date对象的格式化格式:如yyyy-MM-dd HH:mm:ss
  94. * @param {date} date 需要格式化的时间对象
  95. * @return {string} 返回格式化后的字符串,如2020-0101 00:00:00
  96. * */
  97. export const formatDateToStr = function (fmt, date) {
  98. fmt = fmt.replace("hh", "HH")
  99. let ret;
  100. // debugger
  101. const opt = {
  102. "y+": date.getFullYear().toString(),
  103. "M+": (date.getMonth() + 1).toString(),
  104. "d+": date.getDate().toString(),
  105. "H+": date.getHours().toString(),
  106. "m+": date.getMinutes().toString(),
  107. "s+": date.getSeconds().toString(),
  108. };
  109. for (let k in opt) {
  110. ret = new RegExp(`(${k})`).exec(fmt);
  111. if (ret) {
  112. fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"))
  113. }
  114. }
  115. return fmt
  116. }
  117. /**
  118. * 格式化时间对象
  119. * @param {string} str 要转换成时间对象的字符串,如2020-0101 00:00:00
  120. * @return {date} 转换后的时间对象
  121. * */
  122. export function formatStrToDate(str) {
  123. return new Date(Date.parse(str.replace(/-/g, "/")));
  124. }
  125. //数字校验
  126. export const digits = (str, bol) => {
  127. var regexp = /^[0-9]*$/;
  128. if (bol) {
  129. if (regexp.test(str)) {
  130. return false;
  131. }
  132. return true;
  133. } else {
  134. if (typeof str != "undefined" && str.length > 0) {
  135. if (regexp.test(str)) {
  136. return false;
  137. }
  138. return true;
  139. } else {
  140. return false
  141. }
  142. }
  143. };
  144. //校验只能输入大于0的正整数
  145. export const checkNum0 = (str, bol) => {
  146. var regexp = /^[1-9]\d*$/;
  147. if (bol) {
  148. if (regexp.test(str)) {
  149. return false;
  150. }
  151. return true;
  152. } else {
  153. if (typeof str != "undefined" && str.length > 0) {
  154. if (regexp.test(str)) {
  155. return false;
  156. }
  157. return true;
  158. } else {
  159. return false
  160. }
  161. }
  162. };
  163. //校验只能输入大于1的正整数
  164. export const checkNum1 = (str) => {
  165. var regexp = /^[2-9]\d*$/;
  166. if (bol) {
  167. if (regexp.test(str)) {
  168. return false;
  169. }
  170. return true;
  171. } else {
  172. if (typeof str != "undefined" && str.length > 0) {
  173. if (regexp.test(str)) {
  174. return false;
  175. }
  176. return true;
  177. } else {
  178. return false
  179. }
  180. }
  181. };
  182. //校验金额(精确到小数点2位)
  183. export const moneyvalid = (str, bol) => {
  184. var reg = /^^\d+(\.\d{2})+$/;
  185. if (bol) {
  186. if (reg.test(str)) {
  187. return false;
  188. }
  189. return true;
  190. } else {
  191. if (typeof str != "undefined" && str.length > 0) {
  192. if (reg.test(str)) {
  193. return false;
  194. }
  195. return true;
  196. } else {
  197. return false
  198. }
  199. }
  200. };
  201. //校验只能输入字母
  202. export const english = (str, bol) => {
  203. var regexp = /^[A-Za-z]+$/;
  204. if (bol) {
  205. if (regexp.test(str)) {
  206. return false;
  207. }
  208. return true;
  209. } else {
  210. if (typeof str != "undefined" && str.length > 0) {
  211. if (regexp.test(str)) {
  212. return false;
  213. }
  214. return true;
  215. } else {
  216. return false
  217. }
  218. }
  219. };
  220. //检验只能输入汉字
  221. export const chinese = (str, bol) => {
  222. var regexp = /^[\u4e00-\u9fa5]{0,}$/;
  223. if (bol) {
  224. if (regexp.test(str)) {
  225. return false;
  226. }
  227. return true;
  228. } else {
  229. if (typeof str != "undefined" && str.length > 0) {
  230. if (regexp.test(str)) {
  231. return false;
  232. }
  233. return true;
  234. } else {
  235. return false
  236. }
  237. }
  238. };
  239. //校验数字和字母
  240. export const letterOrNumberonly = (str, bol) => {
  241. var regexp = /^[A-Za-z0-9]+$/;
  242. if (bol) {
  243. if (regexp.test(str)) {
  244. return false;
  245. }
  246. return true;
  247. } else {
  248. if (typeof str != "undefined" && str.length > 0) {
  249. if (regexp.test(str)) {
  250. return false;
  251. }
  252. return true;
  253. } else {
  254. return false
  255. }
  256. }
  257. };
  258. //校验数字、字母及下划线
  259. export const letterOrNumberOrUnderline = (str, bol) => {
  260. var regexp = /^\w+$/;
  261. if (bol) {
  262. if (regexp.test(str)) {
  263. return false;
  264. }
  265. return true;
  266. } else {
  267. if (typeof str != "undefined" && str.length > 0) {
  268. if (regexp.test(str)) {
  269. return false;
  270. }
  271. return true;
  272. } else {
  273. return false
  274. }
  275. }
  276. };
  277. //邮箱校验
  278. export const email = (str, bol) => {
  279. var regexp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  280. var regexp2 = /^[A-Za-z0-9._%-]+@([A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$/;
  281. if (bol) {
  282. if (regexp.test(str) || regexp2.test(str)) {
  283. return false;
  284. }
  285. return true;
  286. } else {
  287. if (typeof str != "undefined" && str.length > 0) {
  288. if (regexp.test(str) || regexp2.test(str)) {
  289. return false;
  290. }
  291. return true;
  292. } else {
  293. return false
  294. }
  295. }
  296. };
  297. //校验手机号码
  298. export const mobile = (str, bol) => {
  299. var isMobile = /^1(3|4|5|6|7|8|9)\d{9}$/;
  300. if (bol) {
  301. if (isMobile.test(str))
  302. return false
  303. else
  304. return true;
  305. } else {
  306. if (typeof str != "undefined" && str.length > 0) {
  307. if (isMobile.test(str))
  308. return false
  309. else
  310. return true;
  311. } else {
  312. return false
  313. }
  314. }
  315. };
  316. //校验固定电话
  317. export const phone = (str, bol) => {
  318. var homeMobile = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
  319. if (bol) {
  320. if (homeMobile.test(str))
  321. return false
  322. else
  323. return true;
  324. } else {
  325. if (typeof str != "undefined" && str.length > 0) {
  326. if (homeMobile.test(str))
  327. return false
  328. else
  329. return true;
  330. } else {
  331. return false
  332. }
  333. }
  334. };
  335. //校验电话号码
  336. export const mobileOrPhone = (str, bol) => {
  337. var homeMobile = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
  338. var isMobile = /^1(3|4|5|6|7|8|9)\d{9}$/;
  339. if (bol) {
  340. if (homeMobile.test(str) || isMobile.test(str))
  341. return false
  342. else
  343. return true;
  344. } else {
  345. if (typeof str != "undefined" && str.length > 0) {
  346. if (homeMobile.test(str) || isMobile.test(str))
  347. return false
  348. else
  349. return true;
  350. } else {
  351. return false
  352. }
  353. }
  354. };
  355. //校验身份证号
  356. export const idcard = (str) => {
  357. var idcard1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
  358. var idcard2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
  359. if (idcard1.test(str) || idcard2.test(str))
  360. return false
  361. else
  362. return true;
  363. };
  364. //获取本周
  365. export const getCurrentWeek = (str) => {
  366. //获取当前时间
  367. const currentDate = new Date(str)
  368. //返回date是一周中的某一天
  369. const week = currentDate.getDay()
  370. //一天的毫秒数
  371. const millisecond = 1000 * 60 * 60 * 24
  372. //减去的天数
  373. const minusDay = week != 0 ? week - 1 : 6
  374. //本周 周一
  375. const monday = new Date(currentDate.getTime() - minusDay * millisecond)
  376. //本周 周日
  377. const sunday = new Date(monday.getTime() + 6 * millisecond)
  378. return [monday, sunday]
  379. };
  380. //获取上一周
  381. export const handleGetPrevWeek = (str) => {
  382. const Time = new Date(str)
  383. let weekNum = Time.getDay()
  384. weekNum = weekNum == 0 ? 7 : weekNum
  385. let lastDate = new Date(Time.getTime() - weekNum * 24 * 60 * 60 * 1000)
  386. let fitstDate = new Date(
  387. Time.getTime() - (weekNum + 6) * 24 * 60 * 60 * 1000
  388. )
  389. let startDate =
  390. `${fitstDate.getFullYear()}-${fitstDate.getMonth() + 1 < 10
  391. ? '0' + (fitstDate.getMonth() + 1)
  392. : fitstDate.getMonth() + 1
  393. }-${fitstDate.getDate() < 10
  394. ? '0' + fitstDate.getDate()
  395. : fitstDate.getDate()
  396. }`
  397. let endDate =
  398. `${lastDate.getFullYear()}-${lastDate.getMonth() + 1 < 10
  399. ? '0' + (lastDate.getMonth() + 1)
  400. : lastDate.getMonth() + 1
  401. }-${lastDate.getDate() < 10 ? '0' + lastDate.getDate() : lastDate.getDate()
  402. }`
  403. return [startDate, endDate]
  404. };
  405. //获取下一周
  406. export const handleGetNextvWeek = (str) => {
  407. const Time = new Date(str)
  408. let weekNum = Time.getDay()
  409. weekNum = weekNum == 0 ? 7 : weekNum
  410. let fitstDate = new Date(
  411. Time.getTime() + (7 - weekNum + 1) * 24 * 60 * 60 * 1000
  412. )
  413. let lastDate = new Date(
  414. Time.getTime() + (7 - weekNum + 7) * 24 * 60 * 60 * 1000
  415. )
  416. let startDate =
  417. `${fitstDate.getFullYear()}-${fitstDate.getMonth() + 1 < 10
  418. ? '0' + (fitstDate.getMonth() + 1)
  419. : fitstDate.getMonth() + 1
  420. }-${fitstDate.getDate() < 10
  421. ? '0' + fitstDate.getDate()
  422. : fitstDate.getDate()
  423. }`
  424. let endDate =
  425. `${lastDate.getFullYear()}-${lastDate.getMonth() + 1 < 10
  426. ? '0' + (lastDate.getMonth() + 1)
  427. : lastDate.getMonth() + 1
  428. }-${lastDate.getDate() < 10 ? '0' + lastDate.getDate() : lastDate.getDate()
  429. }`
  430. return [startDate, endDate]
  431. }
  432. // url参数解析
  433. export const getUrlkey = (url) => {
  434. var params = {};
  435. var urls = url.split("?");
  436. var arr = urls[1].split("&");
  437. for (var i = 0, l = arr.length; i < l; i++) {
  438. var a = arr[i].split("=");
  439. params[a[0]] = a[1];
  440. }
  441. return params;
  442. }
  443. //获取上一个年月
  444. export const getPreMonth = () => {
  445. var date = new Date();
  446. var year = date.getFullYear();
  447. var month = date.getMonth()
  448. if (month == 0) {
  449. year = year - 1;
  450. month = 12;
  451. }
  452. month = month <= 9 ? "0" + month : month
  453. return year + '年' + month + '月'
  454. }
  455. //解决IOS日期显示问题
  456. export const formateIOS = (str, pattern) => {
  457. var format = str.replace(/\-/g, "/"); //把“-”,替换成‘/’
  458. var date = new Date(format),
  459. y = date.getFullYear(),
  460. m = (date.getMonth() + 1).toString().padStart(2, 0),
  461. d = date.getDate().toString().padStart(2, 0),
  462. h = date.getHours().toString().padStart(2, 0),
  463. mi = date.getMinutes().toString().padStart(2, 0),
  464. se = date.getSeconds().toString().padStart(2, 0);
  465. if (pattern == "yyyy.mm.dd") {
  466. return `${y}.${m}.${d}`;
  467. } else {
  468. return y + '-' + m + '-' + d;
  469. }
  470. }
  471. //小写金额转大写
  472. export const DX = (n) => {
  473. var newchar = "";
  474. var Num = n;
  475. if (Num == "") {
  476. //输入框删减为空时,将大写金额的内容值设为原始状态,当然也可以根据需求进行修改
  477. newchar = "零元整";
  478. return newchar;
  479. }
  480. var part = String(Num).split(".");
  481. for (let i = part[0].length - 1; i >= 0; i--) {
  482. if (part[0].length > 10) {
  483. newchar = "位数过大,无法计算"; //前面如果有验证位数的,此处判断可去掉
  484. return newchar;
  485. }
  486. var tmpnewchar = ""
  487. var perchar = part[0].charAt(i);
  488. switch (perchar) {
  489. case "0":
  490. tmpnewchar = "零" + tmpnewchar;
  491. break;
  492. case "1":
  493. tmpnewchar = "壹" + tmpnewchar;
  494. break;
  495. case "2":
  496. tmpnewchar = "贰" + tmpnewchar;
  497. break;
  498. case "3":
  499. tmpnewchar = "叁" + tmpnewchar;
  500. break;
  501. case "4":
  502. tmpnewchar = "肆" + tmpnewchar;
  503. break;
  504. case "5":
  505. tmpnewchar = "伍" + tmpnewchar;
  506. break;
  507. case "6":
  508. tmpnewchar = "陆" + tmpnewchar;
  509. break;
  510. case "7":
  511. tmpnewchar = "柒" + tmpnewchar;
  512. break;
  513. case "8":
  514. tmpnewchar = "捌" + tmpnewchar;
  515. break;
  516. case "9":
  517. tmpnewchar = "玖" + tmpnewchar;
  518. break;
  519. }
  520. switch (part[0].length - i - 1) {
  521. case 0:
  522. tmpnewchar = tmpnewchar + "元";
  523. break;
  524. case 1:
  525. if (perchar != 0) tmpnewchar = tmpnewchar + "拾";
  526. break;
  527. case 2:
  528. if (perchar != 0) tmpnewchar = tmpnewchar + "佰";
  529. break;
  530. case 3:
  531. if (perchar != 0) tmpnewchar = tmpnewchar + "仟";
  532. break;
  533. case 4:
  534. tmpnewchar = tmpnewchar + "万";
  535. break;
  536. case 5:
  537. if (perchar != 0) tmpnewchar = tmpnewchar + "拾";
  538. break;
  539. case 6:
  540. if (perchar != 0) tmpnewchar = tmpnewchar + "佰";
  541. break;
  542. case 7:
  543. if (perchar != 0) tmpnewchar = tmpnewchar + "仟";
  544. break;
  545. case 8:
  546. tmpnewchar = tmpnewchar + "亿";
  547. break;
  548. case 9:
  549. tmpnewchar = tmpnewchar + "拾";
  550. break;
  551. }
  552. newchar = tmpnewchar + newchar;
  553. }
  554. if (("" + Num).indexOf(".") != -1) {
  555. if (part[1].length > 2) {
  556. part[1] = part[1].substr(0, 2)
  557. }
  558. for (i = 0; i < part[1].length; i++) {
  559. tmpnewchar = ""
  560. var perchar = part[1].charAt(i)
  561. switch (perchar) {
  562. case "0":
  563. tmpnewchar = "零" + tmpnewchar;
  564. break;
  565. case "1":
  566. tmpnewchar = "壹" + tmpnewchar;
  567. break;
  568. case "2":
  569. tmpnewchar = "贰" + tmpnewchar;
  570. break;
  571. case "3":
  572. tmpnewchar = "叁" + tmpnewchar;
  573. break;
  574. case "4":
  575. tmpnewchar = "肆" + tmpnewchar;
  576. break;
  577. case "5":
  578. tmpnewchar = "伍" + tmpnewchar;
  579. break;
  580. case "6":
  581. tmpnewchar = "陆" + tmpnewchar;
  582. break;
  583. case "7":
  584. tmpnewchar = "柒" + tmpnewchar;
  585. break;
  586. case "8":
  587. tmpnewchar = "捌" + tmpnewchar;
  588. break;
  589. case "9":
  590. tmpnewchar = "玖" + tmpnewchar;
  591. break;
  592. }
  593. if (i == 0) tmpnewchar = tmpnewchar + "角";
  594. if (i == 1) tmpnewchar = tmpnewchar + "分";
  595. newchar = newchar + tmpnewchar;
  596. }
  597. }
  598. while (newchar.search("零元") != -1) {
  599. newchar = newchar.replace("零零", "零");
  600. newchar = newchar.replace("零亿", "亿");
  601. newchar = newchar.replace("亿万", "亿");
  602. newchar = newchar.replace("零万", "万");
  603. newchar = newchar.replace("零元", "元");
  604. newchar = newchar.replace("零角", "");
  605. newchar = newchar.replace("零分", "");
  606. }
  607. if (newchar.charAt(newchar.length - 1) == "元" || newchar.charAt(newchar.length - 1) == "角") {
  608. newchar = newchar + "整";
  609. }
  610. return newchar;
  611. }
  612. //计算数组对象某个属性的总和
  613. export const countTotal = (arr, keyName) => {
  614. let $total = 0;
  615. $total = arr.reduce(function (total, currentValue, currentIndex, arr) {
  616. return parseFloat(currentValue[keyName]) ? (total + parseFloat(currentValue[keyName])) : total;
  617. }, 0);
  618. return $total;
  619. }
  620. //防抖函数
  621. let timeout = null;
  622. export const debounce = (fn, wait) => {
  623. if (timeout != null) clearTimeout(timeout)
  624. timeout = setTimeout(fn, wait)
  625. }
  626. //对象深拷贝
  627. export const deepClone = (obj) => {
  628. let objClone = Array.isArray(obj) ? [] : {};
  629. if (obj && typeof obj === "object") {
  630. for (var key in obj) {
  631. if (obj.hasOwnProperty(key)) {
  632. //判断ojb子元素是否为对象,如果是,递归复制
  633. if (obj[key] && typeof obj[key] === "object") {
  634. objClone[key] = deepClone(obj[key]);
  635. } else {
  636. //如果不是,简单复制
  637. objClone[key] = obj[key];
  638. }
  639. }
  640. }
  641. }
  642. return objClone;
  643. }
  644. //字母大写转小写
  645. export const strChange = (arg) => {
  646. var str=arg.split('');
  647. for(var i = 0; i < str.length; i++) {
  648. if (str[i].charAt() >= "a" && str[i].charAt() <= "z") {
  649. //转换成大写
  650. // str[i] = str[i].toUpperCase();
  651. } else {
  652. //转换成小写
  653. str[i] = str[i].toLowerCase();
  654. }
  655. }
  656. return str.join('');
  657. }
  658. //创建unid
  659. export const createUnid = () => {
  660. var unid = "";
  661. const str = "0 1 2 3 4 5 6 7 8 9 A B C D E F"
  662. var arr = str.split(" ");
  663. var dateStr = getCurDateStr();
  664. for (let i = 0; i < 18; i++) {
  665. unid += arr[Math.round(Math.random() * (arr.length - 1))];
  666. }
  667. return dateStr + unid;
  668. }
  669. const getCurDateStr = () => {
  670. //获取当前时间
  671. var mydate = new Date();
  672. var newyear = mydate.getFullYear();
  673. var newmonth = mydate.getMonth() + 1;
  674. newmonth = (newmonth < 10 ? "0" + newmonth : newmonth);
  675. var newDate = mydate.getDate();
  676. newDate = (newDate < 10 ? "0" + newDate : newDate);
  677. var hours = mydate.getHours(); //获取当前小时数(0-23)
  678. hours = (hours < 10 ? "0" + hours : hours);
  679. var minutes = mydate.getMinutes(); //获取当前分钟数(0-59)
  680. minutes = (minutes < 10 ? "0" + minutes : minutes);
  681. var seconds = mydate.getSeconds(); //获取当前秒数(0-59)
  682. seconds = (seconds < 10 ? "0" + seconds : seconds);
  683. var datetime = "" + newyear + newmonth + newDate + hours + minutes + seconds;
  684. return datetime;
  685. }
  686. //获取短信模板消息体 已使用
  687. export const getmestemplate = async (e) => {
  688. const res = await getSmsTemplate(e)
  689. return res.data.data
  690. }
  691. //通过字典获取值
  692. export const queryType = async (e) => {
  693. const res = await queryByType(e)
  694. return res.data.data[e.dictTypes].map(item => {
  695. return {
  696. label: item.dictname,
  697. value: item.dictvalue,
  698. }
  699. });
  700. }
  701. //把年月日时间格式转换成可以识别的时间格式
  702. export const getDateToNum = (date) => {
  703. let redate = ""
  704. if (date.indexOf("年") > 1) {
  705. redate = date.substring(0, date.indexOf("年"))
  706. }
  707. if (date.indexOf("月") > 1) {
  708. redate = redate + "-" + date.substring(date.indexOf("月") - 2, date.indexOf("月"))
  709. }
  710. if (date.indexOf("日") > 1) {
  711. redate = redate + "-" + date.substring(date.indexOf("日") - 2, date.indexOf("日"))
  712. }
  713. return redate
  714. }
  715. /**
  716. * 弹窗提示方法
  717. * @param {String} msg 提示的消息
  718. * @param {Function} func 成功后执行的方法
  719. */
  720. export async function toastFunc(msg = '', func = null) {
  721. uni.showToast({
  722. icon: "none",
  723. title: msg,
  724. complete() {
  725. setTimeout(async () => {
  726. func && await func()
  727. }, 1000)
  728. }
  729. })
  730. }
  731. /**
  732. * 模态框提示方法
  733. * @param {String} msg 提示的消息
  734. * @param {Function} func 点击确定后执行的方法
  735. */
  736. export function modalFunc(msg = '', func = null, func2 = null) {
  737. uni.showModal({
  738. title: '提示',
  739. content: msg,
  740. complete(e) {
  741. if (e.confirm) {
  742. //点击确定执行的方法
  743. func && func()
  744. }else if(e.cancel){
  745. //点击取消执行的方法
  746. func2 && func2()
  747. }
  748. }
  749. })
  750. }
  751. // 跳转缺省页面
  752. export function jumpResponseHandle(code, msg) {
  753. uni.redirectTo({
  754. url: '/pages/commonview/responceHandle?code=' + code + '&msg=' + msg
  755. })
  756. }
  757. // 判断日期大小
  758. export function computingDate(startDate, endDate) {
  759. startDate = new Date(startDate).getTime()
  760. endDate = new Date(endDate).getTime()
  761. if (startDate == endDate) {
  762. return '等于'
  763. } else if (startDate < endDate) {
  764. return '小于'
  765. } else {
  766. return '大于'
  767. }
  768. }
  769. //获取本周日期 i传0 返回本周第一天
  770. export function getWeek(i) {
  771. var now = new Date();
  772. var firstDay = new Date(now - (now.getDay() - 1) * 86400000);
  773. firstDay.setDate(firstDay.getDate() + i);
  774. let mon = Number(firstDay.getMonth()) + 1;
  775. let day = firstDay.getDate();
  776. if (mon < 10) {
  777. mon = '0' + mon
  778. }
  779. if (day < 10) {
  780. day = '0' + day
  781. }
  782. return now.getFullYear() + "-" + mon + "-" + day;
  783. }
  784. export function changeNumToHan(num) {
  785. var arr1 = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  786. var arr2 = new Array('', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿');//可继续追加更高位转换值
  787. if (!num || isNaN(num)) {
  788. return "零";
  789. }
  790. var english = num.toString().split("")
  791. var result = "";
  792. for (var i = 0; i < english.length; i++) {
  793. var des_i = english.length - 1 - i;//倒序排列设值
  794. result = arr2[i] + result;
  795. var arr1_index = english[des_i];
  796. result = arr1[arr1_index] + result;
  797. }
  798. //将【零千、零百】换成【零】 【十零】换成【十】
  799. result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十');
  800. //合并中间多个零为一个零
  801. result = result.replace(/零+/g, '零');
  802. //将【零亿】换成【亿】【零万】换成【万】
  803. result = result.replace(/零亿/g, '亿').replace(/零万/g, '万');
  804. //将【亿万】换成【亿】
  805. result = result.replace(/亿万/g, '亿');
  806. //移除末尾的零
  807. result = result.replace(/零+$/, '')
  808. //将【零一十】换成【零十】
  809. //result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
  810. //将【一十】换成【十】
  811. result = result.replace(/^一十/g, '十')
  812. return result;
  813. }