common.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. module.exports = {
  2. // 电话号码中间加*
  3. formatPhone(e) {
  4. if (this.$isNull(e)) return
  5. var tel = e;
  6. tel = "" + tel;
  7. var newTel = tel.substr(0, 3) + "****" + tel.substr(7)
  8. return newTel
  9. },
  10. formatName(e) {
  11. if (this.$isNull(e)) return
  12. if (e != null || e != undefined) {
  13. var name = e;
  14. if (name.length > 1) {
  15. name = "" + name;
  16. var newName = name.substr(0, 1) + "*" + name.substr(2,name.length-1)
  17. return newName
  18. }
  19. else {
  20. return name
  21. }
  22. } else {
  23. return e
  24. }
  25. },
  26. //姓名星号-只留首尾
  27. formatNameAll(name) {
  28. if (this.$isNull(name)) return
  29. let newStr;
  30. if (name.length === 2) {
  31. newStr = name.substr(0, 1) + '*';
  32. } else if (name.length > 2) {
  33. let seat = ''
  34. for(let i = 0; i < name.length - 2; i++){
  35. seat += '*'
  36. }
  37. newStr = name.substr(0, 1) + seat + name.substr(-1, 1);
  38. } else {
  39. newStr = name;
  40. }
  41. return newStr;
  42. },
  43. // 时间格式
  44. timefilter(value,type) {
  45. if (this.$isNull(value)) return
  46. if(this.$isNull(type)) {
  47. type = 'YY.MM.DD HH:MM:SS'
  48. }
  49. var newDate = ""
  50. if (String(value).indexOf("-") != -1) {
  51. newDate = new Date(value.replace(/-/g, "/"))
  52. } else {
  53. newDate = new Date(Number(value))
  54. }
  55. var hours = newDate.getHours().toString()
  56. var minutes = newDate.getMinutes().toString()
  57. var seconds = newDate.getSeconds().toString()
  58. var year = newDate.getFullYear().toString();
  59. var month = (newDate.getMonth() + 1).toString();
  60. var day = (newDate.getDate()).toString();
  61. if (month.length == 1) {
  62. month = "0" + month;
  63. }
  64. if (day.length == 1) {
  65. day = "0" + day;
  66. }
  67. if (hours.length == 1) {
  68. hours = "0" + hours;
  69. }
  70. if (minutes.length == 1) {
  71. minutes = "0" + minutes;
  72. }
  73. if (seconds.length == 1) {
  74. seconds = "0" + seconds;
  75. }
  76. if (type == '年月日') {
  77. return year + '年' + month + '月' + day + '日 '
  78. } else if (type == 'YY-MM-DD') {
  79. return year + '-' + month + '-' + day
  80. } else if (type == 'YY.MM.DD') {
  81. return year + '.' + month + '.' + day
  82. } else if (type == 'YY/MM/DD') {
  83. return year + '/' + month + '/' + day
  84. } else if (type == '年月日 HH:MM:SS') {
  85. return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes + ':' + seconds
  86. } else if (type == 'YY-MM-DD HH:MM:SS') {
  87. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  88. } else if (type == 'YY.MM.DD HH:MM:SS') {
  89. return year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
  90. } else if (type == 'YY/MM/DD HH:MM:SS') {
  91. return year + '/' + month + '/' + day + ' ' + hours + ':' + minutes + ':' + seconds
  92. } else if (type == '年月日 HH:MM') {
  93. return year + '年' + month + '月' + day + '日 ' + hours + ':' + minutes
  94. } else if (type == 'YY-MM-DD HH:MM') {
  95. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
  96. } else if (type == 'YY.MM.DD HH:MM') {
  97. return year + '.' + month + '.' + day + ' ' + hours + ':' + minutes
  98. } else if (type == 'YY/MM/DD HH:MM') {
  99. return year + '/' + month + '/' + day + ' ' + hours + ':' + minutes
  100. } else if (type == 'HH:MM') {
  101. return hours + ':' + minutes
  102. }
  103. },
  104. isNull(param) {
  105. if (typeof param == "boolean") {
  106. return false;
  107. }
  108. if (param == null || param == undefined || param == '' || param.length == 0) {
  109. return true;
  110. }
  111. return false;
  112. },
  113. //同步取本地缓存数据,为了解决数据没取到之前请求接口报错
  114. syncGetStor(keyName,name) {
  115. return new Promise((resolve,reject)=>{
  116. //接口调用的方法放在这里,成功的话调用resolve
  117. // console.log(uni.getStorageSync(keyName)[name])
  118. // setTimeout(function(){
  119. let val
  120. if(name) {
  121. val = uni.getStorageSync(keyName)[name]
  122. } else {
  123. val = uni.getStorageSync(keyName)
  124. }
  125. resolve(val)
  126. // },3000)
  127. // if(!this.$isNull(val)) {
  128. // resolve(val)//res是你自己定义的返回值
  129. // } else {
  130. // reject('')
  131. // }
  132. })
  133. },
  134. }