util.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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: '#E15616',
  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. }