squni.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import gloablConfig from '@/common/config.js'
  2. import { getUrlQuery, objToUrlQuery } from '@/util/squ.js'
  3. const globalKeyPrefix = gloablConfig.key ? gloablConfig.key + '-' : 'squni-'
  4. /**
  5. * 缓存全局APP实例对象(vm)
  6. */
  7. let APP = null
  8. // ====== 存储
  9. // uni.getStorageInfo uni.getStorageInfoSync uni.clearStorageSync 略
  10. export const setStorage = config =>
  11. uni.setStorage({
  12. key: globalKeyPrefix + config.key,
  13. data: config.data,
  14. success: config.success,
  15. fail: config.fail,
  16. complete: config.complete
  17. })
  18. export const setStorageSync = (key, value) => uni.setStorageSync(globalKeyPrefix + key, value)
  19. export const getStorage = config =>
  20. uni.getStorage({
  21. key: globalKeyPrefix + config.key,
  22. success: config.success,
  23. fail: config.fail,
  24. complete: config.complete
  25. })
  26. export const getStorageSync = key => uni.getStorageSync(globalKeyPrefix + key)
  27. export const removeStorage = config =>
  28. uni.removeStorage({
  29. key: globalKeyPrefix + config.key,
  30. success: config.success,
  31. fail: config.fail,
  32. complete: config.complete
  33. })
  34. export const removeStorageSync = key => uni.removeStorageSync(globalKeyPrefix + key)
  35. // ====== 路由
  36. export const setApp = (vm) => {
  37. APP = vm
  38. }
  39. export const getApp = () => {
  40. return APP
  41. }
  42. export const switchTab = (path, config) => {
  43. uni.switchTab({
  44. url: path,
  45. success: config && config.success,
  46. fail: config && config.fail,
  47. complete: config && config.complete
  48. })
  49. }
  50. export const navigateTo = (path, config) => {
  51. uni.navigateTo({
  52. url: path,
  53. animationType: config && config.animationType,
  54. animationDuration: config && config.animationDuration,
  55. events: config && config.events,
  56. success: config && config.success,
  57. fail: config && config.fail,
  58. complete: config && config.complete
  59. })
  60. }
  61. export const navigateBack = (vm) => {
  62. // 从分享打开的,返回首页. 可引入wx-share.js
  63. if (vm.share == true) {
  64. redirectHome()
  65. } else {
  66. // 不是从分享打开的,返回上一页
  67. uni.navigateBack({
  68. delta: 1,
  69. })
  70. }
  71. }
  72. export const redirectTo = (path, config) => {
  73. uni.redirectTo({
  74. url: path,
  75. success: config && config.success,
  76. fail: config && config.fail,
  77. complete: config && config.complete
  78. })
  79. }
  80. export const redirectHome = () => {
  81. if (gloablConfig.indexType === 'Tab') {
  82. uni.switchTab({
  83. url: gloablConfig.indexPath
  84. })
  85. } else {
  86. uni.redirectTo({
  87. url: gloablConfig.indexPath
  88. })
  89. }
  90. }
  91. export const routeWithParams = (path, method) => {
  92. method = method || 'redirectTo'
  93. const params = getCurQueryAll()
  94. const _originHref = params['_originHref'] || window.location.href
  95. params['_originHref'] = _originHref
  96. uni[method]({
  97. url: path + '?' + objToUrlQuery(params)
  98. })
  99. }
  100. /**
  101. * 获取当前页面请求路径
  102. * 返回: { $vm, route, options, ... }
  103. */
  104. export const getCurPage = () => {
  105. // uni-app内置函数: https://uniapp.dcloud.net.cn/api/window/window.html#getcurrentpages
  106. const pages = getCurrentPages()
  107. return (pages && pages.length > 0) ? pages[pages.length - 1] : {}
  108. }
  109. /**
  110. * 获取上个页面
  111. * 返回: { $vm, route, options, ... } | null
  112. */
  113. export const getPrePage = () => {
  114. const pages = getCurrentPages()
  115. return pages && pages.length > 1 ? pages[pages.length - 2] : null
  116. }
  117. /**
  118. * 获取当前页面请求路径所有参数
  119. */
  120. export const getCurQueryAll = () => {
  121. const curPage = getCurPage()
  122. // 在微信小程序或是app中,通过curPage.options;如果是H5,则需要curPage.$route.query
  123. let params = curPage.options || (curPage.$route && curPage.$route.query)
  124. // 有时候H5会出现 curPage.options = {}
  125. if (params == null
  126. // #ifdef H5
  127. || Object.getOwnPropertyNames(params).length === 0
  128. // #endif
  129. ) {
  130. params = getUrlQuery()
  131. }
  132. return params
  133. }
  134. /**
  135. * 获取当前页面请求路径参数. 可参考 squ.js 中的 getUrlQuery
  136. */
  137. export const getCurQuery = (name) => {
  138. const query = getCurQueryAll()
  139. return query ? query[name] : null
  140. }
  141. // ====== 其他
  142. export const copy = (value, msg) => {
  143. uni.setClipboardData({
  144. data: value,
  145. success: () => {
  146. uni.showToast({
  147. title: msg || '复制成功',
  148. duration: 800
  149. })
  150. }
  151. })
  152. }
  153. /**
  154. * 信息提示更简洁
  155. */
  156. export const toast = (message, icon, config) => {
  157. config = config || {}
  158. config.title = message
  159. config.icon = icon || 'none'
  160. uni.showToast(config)
  161. }
  162. /**
  163. * 滚动页面到底部。如聊天时
  164. */
  165. export const scrollToBottom = () => {
  166. // 要加点延迟, 不然有可能不生效
  167. setTimeout(() => {
  168. uni.pageScrollTo({
  169. scrollTop: 999999,
  170. duration: 0
  171. })
  172. }, 50)
  173. }
  174. export const shareWeb = () => {
  175. let url = window.location.protocol + '//' + window.location.host + window.location.pathname
  176. const query = getCurQueryAll()
  177. query.inviterUserId = getStorageSync('userId')
  178. url = url + '?' + objToUrlQuery(query)
  179. copy(url, '已复制分享链接,快去分享吧~')
  180. }
  181. export default {
  182. globalKeyPrefix,
  183. setStorageSync,
  184. getStorageSync,
  185. removeStorageSync,
  186. setApp,
  187. getApp,
  188. switchTab,
  189. navigateTo,
  190. navigateBack,
  191. redirectTo,
  192. redirectHome,
  193. routeWithParams,
  194. getCurPage,
  195. getPrePage,
  196. getCurQuery,
  197. getCurQueryAll,
  198. copy,
  199. toast,
  200. shareWeb
  201. }