util.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //清除空键值对
  2. //自定义判断元素类型
  3. function toType(obj) {
  4. return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
  5. }
  6. export const filter_param = (o) => {
  7. for (var key in o) {
  8. if (o[key] == null)
  9. delete o[key];
  10. if (toType(o[key]) == "string") {
  11. o[key] = o[key].trim();
  12. if (o[key].length == 0) {
  13. delete o[key];
  14. }
  15. }
  16. }
  17. return o;
  18. }
  19. export const toChinesNum = (num) => {
  20. let changeNum = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  21. let unit = ["", "拾", "佰", "仟", "万"];
  22. let sUnit = ["角", "分", "厘"];
  23. let newSNum = "";
  24. let otNum = num;
  25. num = parseInt(num);
  26. let getWan = (temp) => {
  27. let strArr = temp.toString().split("").reverse();
  28. let newNum = "";
  29. for (let i = 0; i < strArr.length; i++) {
  30. newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (unit[i]))) + newNum;
  31. }
  32. return newNum;
  33. }
  34. let overWan = Math.floor(num / 10000);
  35. let noWan = num % 10000;
  36. if (noWan.toString().length < 4) {
  37. noWan = "0" + noWan;
  38. }
  39. if (otNum.toString().split('.').length > 1) {
  40. let sNum = otNum.toString().split('.')[1].split("");
  41. for (let i = 0; i < sNum.length; i++) {
  42. newSNum += i > 0 && sNum[i] == 0 && sNum[i - 1] == 0 ? "" : changeNum[sNum[i]] + sUnit[i];
  43. }
  44. if (otNum.toString().split('.')[0] == "0") {
  45. return '零' + (overWan ? getWan(overWan) + "万" + getWan(noWan) + '元' + newSNum : getWan(num) + '元' + newSNum);
  46. } else {
  47. return overWan ? getWan(overWan) + "万" + getWan(noWan) + '元' + newSNum : getWan(num) + '元' + newSNum;
  48. }
  49. } else {
  50. return overWan ? getWan(overWan) + "万" + getWan(noWan) + '元整' : getWan(num) + "元整";
  51. }
  52. }
  53. export const phoneFun = function (phones) {
  54. var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  55. if (!myreg.test(phones)) {
  56. console.log('手机号格式不正确')
  57. return false;
  58. } else {
  59. console.log('手机号格式正确')
  60. return true;
  61. }
  62. }