util.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // 验证手机号码
  2. export function validatePhoneNumber(data) {
  3. var reg = /^1[3-9]\d{9}$/
  4. return reg.test(data)
  5. }
  6. export function lastfour(num) {
  7. // 截取后4位
  8. let lastFourDigits = num.slice(-4);
  9. return lastFourDigits
  10. }
  11. export function isValidBankCard(cardNumber) {
  12. const reg = /^(\d{16}|\d{19})$/
  13. // 使用正则表达式测试银行卡号
  14. return reg.test(cardNumber)
  15. }
  16. /**
  17. * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
  18. * @param param 当前时间,new Date() 格式或者字符串时间格式
  19. * @param format 需要转换的时间格式字符串
  20. * @description param 10秒: 10 * 1000
  21. * @description param 1分: 60 * 1000
  22. * @description param 1小时: 60 * 60 * 1000
  23. * @description param 24小时:60 * 60 * 24 * 1000
  24. * @description param 3天: 60 * 60* 24 * 1000 * 3
  25. * @returns 返回拼接后的时间字符串
  26. */
  27. export function formatPast(param, format = 'YYYY-mm-dd HH:MM:SS') {
  28. if(!param) return '从未下单'
  29. // 传入格式处理、存储转换值
  30. let t, s
  31. // 获取js 时间戳
  32. let time = new Date().getTime()
  33. // 是否是对象
  34. typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
  35. // 当前时间戳 - 传入时间戳
  36. time = Number.parseInt(`${time - t}`)
  37. if (time < 10000) {
  38. // 10秒内
  39. return '刚刚' + '下单'
  40. } else if (time < 60000 && time >= 10000) {
  41. // 超过10秒少于1分钟内
  42. s = Math.floor(time / 1000)
  43. return `${s}秒前` + '下单'
  44. } else if (time < 3600000 && time >= 60000) {
  45. // 超过1分钟少于1小时
  46. s = Math.floor(time / 60000)
  47. return `${s}分钟前` + '下单'
  48. } else if (time < 86400000 && time >= 3600000) {
  49. // 超过1小时少于24小时
  50. s = Math.floor(time / 3600000)
  51. return `${s}小时前` + '下单'
  52. } else if (time < 259200000 && time >= 86400000) {
  53. // 超过1天少于3天内
  54. s = Math.floor(time / 86400000)
  55. return `${s}天前` + '下单'
  56. } else {
  57. // 超过3天
  58. const date = typeof param === 'string' || 'object' ? new Date(param) : param
  59. return formatDateTime(param) + '下单'
  60. }
  61. }
  62. export function formatTime(time) {
  63. if (typeof time !== 'number' || time < 0) {
  64. return time
  65. }
  66. var hour = parseInt(time / 3600)
  67. time = time % 3600
  68. var minute = parseInt(time / 60)
  69. time = time % 60
  70. var second = time
  71. return ([hour, minute, second]).map(function(n) {
  72. n = n.toString()
  73. return n[1] ? n : '0' + n
  74. }).join(':')
  75. }
  76. export function formatDateTime(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
  77. if(!date) {
  78. return ''
  79. }
  80. if (typeof (date) === 'number') {
  81. date = new Date(date)
  82. }
  83. var o = {
  84. "M+": date.getMonth() + 1, //月份
  85. "d+": date.getDate(), //日
  86. "h+": date.getHours(), //小时
  87. "m+": date.getMinutes(), //分
  88. "s+": date.getSeconds(), //秒
  89. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  90. "S": date.getMilliseconds() //毫秒
  91. }
  92. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
  93. for (var k in o)
  94. if (new RegExp("(" + k + ")").test(fmt))
  95. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
  96. return fmt
  97. }
  98. export function formatLocation(longitude, latitude) {
  99. if (typeof longitude === 'string' && typeof latitude === 'string') {
  100. longitude = parseFloat(longitude)
  101. latitude = parseFloat(latitude)
  102. }
  103. longitude = longitude.toFixed(2)
  104. latitude = latitude.toFixed(2)
  105. return {
  106. longitude: longitude.toString().split('.'),
  107. latitude: latitude.toString().split('.')
  108. }
  109. }
  110. var dateUtils = {
  111. UNITS: {
  112. '年': 31557600000,
  113. '月': 2629800000,
  114. '天': 86400000,
  115. '小时': 3600000,
  116. '分钟': 60000,
  117. '秒': 1000
  118. },
  119. humanize: function(milliseconds) {
  120. var humanize = '';
  121. for (var key in this.UNITS) {
  122. if (milliseconds >= this.UNITS[key]) {
  123. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  124. break;
  125. }
  126. }
  127. return humanize || '刚刚';
  128. },
  129. format: function(dateStr) {
  130. var date = this.parse(dateStr)
  131. var diff = Date.now() - date.getTime();
  132. if (diff < this.UNITS['天']) {
  133. return this.humanize(diff);
  134. }
  135. var _format = function(number) {
  136. return (number < 10 ? ('0' + number) : number);
  137. };
  138. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  139. _format(date.getHours()) + ':' + _format(date.getMinutes());
  140. },
  141. parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  142. var a = str.split(/[^0-9]/);
  143. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  144. }
  145. };
  146. // 返回上一页
  147. export function prePage(page = null){
  148. let pages = getCurrentPages();
  149. //console.log('pages:',pages);
  150. let prePage = pages[pages.length - 2];
  151. if (page !== null) {
  152. prePage = pages[page];
  153. }
  154. // #ifdef H5
  155. return prePage;
  156. // #endif
  157. return prePage.$vm;
  158. }
  159. export function kmUnit(m){
  160. var v;
  161. if(typeof m != 'number'){
  162. m = Number(m)
  163. }
  164. if(typeof m === 'number' && !isNaN(m)){
  165. if (m >= 1000) {
  166. v = (m / 1000).toFixed(2) + 'km'
  167. } else {
  168. v = m + 'm'
  169. }
  170. }else{
  171. v = '0m'
  172. }
  173. return v;
  174. }
  175. export function isWeixin() {
  176. if (navigator && navigator.userAgent && navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1) {
  177. return true
  178. }
  179. return false
  180. }
  181. export function parseQuery() {
  182. let res = {}
  183. // #ifdef H5
  184. const query = (location.href.split('?')[1] || '').trim().replace(/^(\?|#|&)/, '')
  185. if (!query) {
  186. return res
  187. }
  188. query.split('&').forEach(param => {
  189. const parts = param.replace(/\+/g, ' ').split('=')
  190. const key = decodeURIComponent(parts.shift())
  191. const val = parts.length > 0 ? decodeURIComponent(parts.join('=')) : null
  192. if (res[key] === undefined) {
  193. res[key] = val
  194. } else if (Array.isArray(res[key])) {
  195. res[key].push(val)
  196. } else {
  197. res[key] = [res[key], val]
  198. }
  199. })
  200. // #endif
  201. // #ifndef H5
  202. var pages = getCurrentPages() //获取加载的页面
  203. var currentPage = pages[pages.length - 1] //获取当前页面的对象
  204. var url = currentPage.route //当前页面url
  205. res = currentPage.options //如果要获取url中所带的参数可以查看options
  206. // #endif
  207. return res
  208. }
  209. export function urlDecode(query) {
  210. if (!query) return null
  211. let hash, object = {}
  212. const hashes = query.slice(query.indexOf('?') + 1).split('&')
  213. for (let i = 0; i < hashes.length; i++) {
  214. hash = hashes[i].split('=')
  215. object[hash[0]] = hash[1]
  216. }
  217. return object;
  218. }
  219. /**
  220. * 将值复制到目标对象,且以目标对象属性为准,例:target: {a:1} source:{a:2,b:3} 结果为:{a:2}
  221. * @param target 目标对象
  222. * @param source 源对象
  223. */
  224. export const copyValueToTarget = (target, source) => {
  225. const newObj = Object.assign({}, target, source);
  226. // 删除多余属性
  227. Object.keys(newObj).forEach((key) => {
  228. // 如果不是target中的属性则删除
  229. if (Object.keys(target).indexOf(key) === -1) {
  230. delete newObj[key];
  231. }
  232. });
  233. // 更新目标对象值
  234. Object.assign(target, newObj);
  235. };