util.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* 常用工具函数 */
  2. export const msg = (title, duration = 1500, mask = true, icon = 'none') => {
  3. //统一提示方便全局修改
  4. if (Boolean(title) === false) { return }
  5. uni.showToast({
  6. title,
  7. duration,
  8. mask,
  9. icon
  10. })
  11. }
  12. export const modal = (title, content, confirmText, cancelText, showCancel = false, callBack) => {
  13. uni.showModal({
  14. title,
  15. content,
  16. confirmText,
  17. cancelText,
  18. confirmColor: '#F3B574',
  19. showCancel,
  20. success: function(res) {
  21. if (res.confirm) {
  22. callBack()
  23. }
  24. }
  25. })
  26. }
  27. export const json = type => {
  28. //模拟异步请求数据
  29. return new Promise(resolve => {
  30. setTimeout(() => {
  31. // console.log(resolve+'======='+type);
  32. resolve(Json[type])
  33. }, 500)
  34. })
  35. }
  36. export const prePage = () => {
  37. let pages = getCurrentPages()
  38. let prePage = pages[pages.length - 2]
  39. // #ifdef H5
  40. return prePage
  41. // #endif
  42. return prePage.$vm
  43. }
  44. // 获取节点元素信息
  45. export const boundingClientRect = (component, selector, flag = false) => {
  46. return new Promise((resolve, reject) => {
  47. const query = uni.createSelectorQuery().in(component)
  48. if (!flag) {
  49. query
  50. .select(selector)
  51. .boundingClientRect(data => {
  52. resolve(data)
  53. })
  54. .exec()
  55. } else {
  56. query
  57. .selectAll(selector)
  58. .boundingClientRect(data => {
  59. resolve(data)
  60. })
  61. .exec()
  62. }
  63. })
  64. }
  65. /**
  66. * @description 时间日期格式化
  67. * @param {dateTime} date 标准时间格式 -> new Date()
  68. * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
  69. * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
  70. */
  71. export function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') {
  72. var o = {
  73. 'M+': date.getMonth() + 1, // month
  74. 'd+': date.getDate(), // day
  75. 'h+': date.getHours(), // hour
  76. 'm+': date.getMinutes(), // minute
  77. 's+': date.getSeconds(), // second
  78. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  79. S: date.getMilliseconds(), // millisecond
  80. }
  81. if (/(y+)/.test(format)) {
  82. format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  83. }
  84. for (var k in o) {
  85. if (new RegExp('(' + k + ')').test(format)) {
  86. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k])
  87. .length))
  88. }
  89. }
  90. return format
  91. }