common.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * 获取url参数值
  3. * @param {*} url
  4. * @param {*} name
  5. */
  6. export function getUrlParameter(url, name) {
  7. var regexSearch = '[\\?&#]' + name + '=([^&#]*)'
  8. var regex = new RegExp(regexSearch)
  9. var results = regex.exec(url)
  10. return results ? window.decodeURIComponent(results[1]) : ''
  11. }
  12. export function showToast(e) {
  13. uni.showToast({
  14. title: e,
  15. icon: 'none',
  16. mask: true,
  17. duration: 1000
  18. });
  19. }
  20. export default {
  21. transDate: function (val, pattern) {
  22. if (val) {
  23. let time = new Date()
  24. time.setTime(val)
  25. if (Date.parse(val)) {
  26. time = new Date(val)
  27. }
  28. if (time instanceof Date) {
  29. return this.formatDate((pattern) || 'yyyy-MM-dd', time)
  30. }
  31. }
  32. return ''
  33. },
  34. transBaseDateTime: function (val, pattern) {
  35. if (val) {
  36. let time = new Date()
  37. time.setTime(val)
  38. if (Date.parse(val)) {
  39. time = new Date(val)
  40. }
  41. if (time instanceof Date) {
  42. return this.formatDate((pattern) || 'yyyy年MM月dd日 hh:mm', time)
  43. }
  44. }
  45. return ''
  46. },
  47. transBaseToDateTime: function (val, pattern) {
  48. if (val) {
  49. let time = new Date()
  50. time.setTime(val)
  51. if (Date.parse(val)) {
  52. time = new Date(val)
  53. }
  54. if (time instanceof Date) {
  55. return this.formatDate((pattern) || 'MM月dd日 hh:mm', time)
  56. }
  57. }
  58. return ''
  59. },
  60. transServDate: function (val) {
  61. return this.transDate(val, 'yyyy-MM-dd hh:mm:ss')
  62. },
  63. transServDay: function (val) {
  64. return this.transDate(val, 'yyyy-MM-dd')
  65. },
  66. isArrayFn: function (value) {
  67. if (typeof Array.isArray === 'function') {
  68. return Array.isArray(value)
  69. } else {
  70. return Object.prototype.toString.call(value) === '[object Array]'
  71. }
  72. },
  73. transDcMap: function (arr) {
  74. const tMap = {}
  75. arr.forEach(item => {
  76. tMap[item.value] = item.label
  77. })
  78. return tMap
  79. },
  80. castEval: function (val) {
  81. // return JSON.parse(val)
  82. if (val) {
  83. return eval('(' + val + ')')
  84. } else {
  85. return ''
  86. }
  87. },
  88. castString: function (val) {
  89. return JSON.stringify(val)
  90. },
  91. // 金额格式化,整数部分每3位用逗号分隔,支持带有正负号以及小数部分
  92. formatMoney: function (amt) {
  93. if (!amt) return ''
  94. if (amt.length <= 3) {
  95. return amt
  96. }
  97. if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(amt)) {
  98. return amt
  99. }
  100. var a = RegExp.$1;
  101. var b = RegExp.$2;
  102. var c = RegExp.$3
  103. var re = new RegExp()
  104. re.compile('(\\d)(\\d{3})(,|$)')
  105. while (re.test(b)) {
  106. b = b.replace(re, '$1,$2$3')
  107. }
  108. return a + '' + b + '' + c
  109. },
  110. formatDate: function (fmt, date) { // author: meizz
  111. var o = {
  112. 'M+': date.getMonth() + 1, // 月份
  113. 'd+': date.getDate(), // 日
  114. 'h+': date.getHours(), // 小时
  115. 'm+': date.getMinutes(), // 分
  116. 's+': date.getSeconds(), // 秒
  117. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  118. 'S': date.getMilliseconds() // 毫秒
  119. }
  120. if (/(y+)/.test(fmt)) {
  121. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  122. }
  123. for (var k in o) {
  124. if (new RegExp('(' + k + ')').test(fmt)) {
  125. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  126. }
  127. }
  128. return fmt
  129. },
  130. phoneNumber(phone) {
  131. return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
  132. },
  133. replaceThumbnail: function (urlPath) {
  134. if (urlPath != null) {
  135. urlPath = urlPath.replace('/server/FileController/download/', this.$constant.BASE_URI + '/wx/fileController/downloadThumbnail/')
  136. }
  137. if (urlPath != null) {
  138. urlPath = urlPath.replace('/h5Server/FileController/download/', this.$constant.BASE_URI + '/wx/fileController/downloadThumbnail/')
  139. }
  140. return urlPath
  141. },
  142. goBack: function () {
  143. // window.history.go(-1)
  144. // self.location = document.referrer
  145. history.back()
  146. },
  147. uuid: function (len, radix) {
  148. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  149. const uuid = []
  150. let i
  151. radix = radix || chars.length
  152. if (len) {
  153. // Compact form
  154. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
  155. } else {
  156. // rfc4122, version 4 form
  157. let r
  158. // rfc4122 requires these characters
  159. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  160. uuid[14] = '4'
  161. // Fill in random data. At i==19 set the high bits of clock sequence as
  162. // per rfc4122, sec. 4.1.5
  163. for (i = 0; i < 36; i++) {
  164. if (!uuid[i]) {
  165. r = 0 | Math.random() * 16
  166. uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
  167. }
  168. }
  169. }
  170. return uuid.join('')
  171. },
  172. }