util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* 常用工具函数 */
  2. export const msg = (title, duration = 1500, mask = true, icon = 'none') => {
  3. //统一提示方便全局修改
  4. if (Boolean(title) === false) {
  5. return
  6. }
  7. uni.showToast({
  8. title,
  9. duration,
  10. mask,
  11. icon
  12. })
  13. }
  14. export const modal = (title, content, confirmText, cancelText, showCancel = false, callBack) => {
  15. uni.showModal({
  16. title,
  17. content,
  18. confirmText,
  19. cancelText,
  20. confirmColor: '#E15616',
  21. showCancel,
  22. success: function(res) {
  23. if (res.confirm) {
  24. callBack()
  25. }
  26. }
  27. })
  28. }
  29. export const json = type => {
  30. //模拟异步请求数据
  31. return new Promise(resolve => {
  32. setTimeout(() => {
  33. // console.log(resolve+'======='+type);
  34. resolve(Json[type])
  35. }, 500)
  36. })
  37. }
  38. export const prePage = () => {
  39. let pages = getCurrentPages()
  40. let prePage = pages[pages.length - 2]
  41. // #ifdef H5
  42. return prePage
  43. // #endif
  44. return prePage.$vm
  45. }
  46. /** 时间格式化
  47. * @param {dateTime} date 标准时间格式 -> new Date()
  48. * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
  49. * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
  50. */
  51. export const dateFormat = (date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') => {
  52. var o = {
  53. 'M+': date.getMonth() + 1, // month
  54. 'd+': date.getDate(), // day
  55. 'h+': date.getHours(), // hour
  56. 'm+': date.getMinutes(), // minute
  57. 's+': date.getSeconds(), // second
  58. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  59. S: date.getMilliseconds(), // millisecond
  60. }
  61. if (/(y+)/.test(format)) {
  62. format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  63. }
  64. for (var k in o) {
  65. if (new RegExp('(' + k + ')').test(format)) {
  66. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k])
  67. .length))
  68. }
  69. }
  70. return format
  71. }
  72. // 两个简单数组是否交集
  73. export const isIntersect = (arr1 = [], arr2 = []) => {
  74. // console.log(arr1, arr2)
  75. return arr1.some(v => arr2.indexOf(v.toString()) > -1)
  76. }
  77. // 将对象转为querystring参数
  78. export const makeQueryStr = (obj) => {
  79. const strs = []
  80. for (let key in obj) {
  81. strs.push(key + '=' + obj[key])
  82. }
  83. return strs.join('&')
  84. }
  85. /**
  86. * 倒计时
  87. * @param diff 倒计时时间/s
  88. * @param loadTime 运行时的当前时间
  89. * @param item 倒计时对象
  90. * @param callback 回调
  91. */
  92. const countDown = function(diff, loadTime, item, callback) {
  93. function round($diff) {
  94. let dd = parseInt($diff / 1000 / 60 / 60 / 24, 10) // 计算剩余的天数
  95. let hh = parseInt(($diff / 1000 / 60 / 60) % 24, 10) // 计算剩余的小时数
  96. let mm = parseInt(($diff / 1000 / 60) % 60, 10) // 计算剩余的分钟数
  97. let ss = parseInt(($diff / 1000) % 60, 10) // 计算剩余的秒数
  98. function checkTime(_a) {
  99. let a = _a
  100. if (a < 10) {
  101. a = '0' + a
  102. }
  103. return a.toString()
  104. }
  105. item.conttainer = {
  106. dd: checkTime(dd),
  107. hh: checkTime(hh),
  108. mm: checkTime(mm),
  109. ss: checkTime(ss),
  110. }
  111. if (item.conttainer.dd > 0 || item.conttainer.hh > 0 || item.conttainer.mm > 0 || item.conttainer.ss > 0) {
  112. item.t = setTimeout(() => {
  113. callback && callback(item)
  114. round($diff - 1000)
  115. }, 1000)
  116. } else {
  117. callback && callback(item)
  118. }
  119. }
  120. round(diff - loadTime)
  121. }
  122. const install = (Vue, vm) => {
  123. console.log('初始化挂载($util)工具方法 util.js ')
  124. Vue.prototype.$util = { msg, prePage, modal, makeQueryStr, countDown }
  125. }
  126. export default install