util.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. const install = (Vue, vm) => {
  86. console.log('初始化挂载($util)工具方法 util.js ')
  87. Vue.prototype.$util = { msg, prePage, modal, makeQueryStr }
  88. }
  89. export default install