util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import $C from './config.js'
  2. const {
  3. brandId,
  4. branchId
  5. } = uni.getStorageSync('brandInfo')
  6. export default {
  7. // 获取存储列表数据
  8. getStorage(key) {
  9. // let data = null;
  10. // // #ifdef H5
  11. // if($C.env === 'dev'){
  12. // data = window.sessionStorage.getItem(key)
  13. // } else {
  14. // data = uni.getStorageSync(key)
  15. // }
  16. // // #endif
  17. // // #ifndef H5
  18. // data = uni.getStorageSync(key)
  19. // // #endif
  20. return uni.getStorageSync(key)
  21. },
  22. // 设置存储
  23. setStorage(key, data) {
  24. // // #ifdef H5
  25. // if($C.env === 'dev'){
  26. // return window.sessionStorage.setItem(key,data)
  27. // } else {
  28. // return uni.setStorageSync(key,data)
  29. // }
  30. // // #endif
  31. // // #ifndef H5
  32. return uni.setStorageSync(key, data)
  33. // // #endif
  34. },
  35. // 删除存储
  36. removeStorage(key) {
  37. // // #ifdef H5
  38. // if($C.env === 'dev'){
  39. // return window.sessionStorage.removeItem(key);
  40. // } else {
  41. // return uni.removeStorageSync(key)
  42. // }
  43. // // #endif
  44. // // #ifndef H5
  45. return uni.removeStorageSync(key)
  46. // // #endif
  47. },
  48. setUserInfo(userInfoData) {
  49. // console.log(userInfoData);
  50. var userInfoStr = uni.setStorageSync("userInfo", JSON.stringify(userInfoData))
  51. console.log(userInfoStr);
  52. },
  53. getUserInfo() {
  54. var userInfoData = uni.getStorageSync(`userInfo_${brandId}`);
  55. return userInfoData;
  56. },
  57. getConfigName(data, key, value, nameKey) {
  58. if (Array.isArray(data)) {
  59. let result = data.find(item => item[key] == value)
  60. return result && result[nameKey] || ''
  61. }
  62. },
  63. getDataName(key) {
  64. console.log(9000, key, `${key}_${brandId}_${branchId}`)
  65. return `${key}_${brandId}_${branchId}`
  66. },
  67. /**
  68. * 获取当前页面路由
  69. * @param index
  70. * @returns {(() => void)|null}
  71. */
  72. getCurrentPageRoute(index=0) {
  73. const pages = getCurrentPages();
  74. if (pages.length === 0) return null;
  75. return pages[pages.length - 1-index].route; // 不带 /
  76. }
  77. ,
  78. /**
  79. * 判断当前页面是否是指定页面
  80. * @param pagePath
  81. * @returns {boolean}
  82. */
  83. isCurrentPage(pagePath) {
  84. const current = this.getCurrentPageRoute();
  85. return current === pagePath;
  86. },
  87. /**
  88. * 获取当前页面参数
  89. * @param index
  90. * @returns {{}|*|null}
  91. */
  92. getCurrentPageOptions(index) {
  93. const pages = getCurrentPages();
  94. if (pages.length === 0) return null;
  95. const currentPage = pages[pages.length - 1-index]; // 当前页面实例
  96. // 小程序 & App 平台:参数在 options 属性上
  97. if (currentPage.options) {
  98. return currentPage.options;
  99. }
  100. // H5 可能需要从 $page 取(uni-app 特有)
  101. if (currentPage.$page && currentPage.$page.options) {
  102. return currentPage.$page.options;
  103. }
  104. // 兜底:尝试 onLoad 时传入的参数(部分平台)
  105. return {};
  106. },
  107. /**
  108. * 获取上一页完整路径
  109. * @returns {string|null}
  110. */
  111. getPrevPageFullUrl() {
  112. const pages = getCurrentPages();
  113. let str=""
  114. pages.forEach(
  115. (page, index) => {
  116. str += page.route
  117. }
  118. )
  119. // 至少要有两个页面(当前页 + 上一页)
  120. if (pages.length < 2) {
  121. return null;
  122. }
  123. const prevPage = pages[pages.length - 2]; // 上一页(倒数第二个)
  124. const route = prevPage.route; // 页面路径,如 'pages/detail/detail'
  125. const options = prevPage.options || {}; // 参数对象,如 { id: '123' }
  126. if (!route) return null;
  127. // 拼接查询字符串
  128. const queryStr = Object.keys(options)
  129. .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(options[key])}`)
  130. .join('&');
  131. return `/${route}${queryStr ? '?' + queryStr : ''}`;
  132. }
  133. }