index.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /**
  2. * 生命周期类型映射
  3. */
  4. const loadTypeMaps = new Map()
  5. loadTypeMaps.set(enums.LoadType.onLoad, onLoad)
  6. loadTypeMaps.set(enums.LoadType.onShow, onShow)
  7. loadTypeMaps.set(enums.LoadType.onReady, onReady)
  8. type goPageExt = {
  9. /** 默认navigateTo */
  10. goPageType?: GoPageTypeKeys
  11. params?: AnyObject
  12. }
  13. const staticData = {
  14. args: {} as AyContainerEntryArgs,
  15. goPageExt: {} as goPageExt,
  16. }
  17. type DecorateMethods<T> = T & {
  18. /** 点击执行的方法 */
  19. handler: T
  20. }
  21. export default {
  22. staticData,
  23. initData<T extends object>(data: T) {
  24. const rd = reactive(data)
  25. // const presetCurPage = store.curPage.initPageInfo({ data: rd, preset: true })
  26. // rd.pageConfig = {
  27. // title: presetCurPage.title,
  28. // style: presetCurPage.style,
  29. // }
  30. return rd
  31. },
  32. /** 初始化对象内的方法,自动为对象内所有方法增加防重提功能(同步方法、异步方法执行中再次触发不执行) */
  33. initMethods<T extends AnyObject>(
  34. methods: T,
  35. extEach?: Record<string, { showLoading?: boolean; ba?: () => void }>,
  36. ext?: {
  37. /**
  38. * 前置通知,返回false时不执行目标方法
  39. * @param methodName:方法名
  40. */ ba?: (methodName: keyof T, isHandler: boolean) => boolean | void
  41. },
  42. ): DecorateMethods<T> {
  43. /** 正在执行中的方法 */
  44. const executings = {}
  45. /** 是被点击执行的方法 */
  46. const handlering = {}
  47. const rv = {} as DecorateMethods<T>
  48. for (const [k, v] of Object.entries(methods)) {
  49. if (typeof v === 'function') {
  50. rv[k] = new Proxy(v, {
  51. apply(target, arg, argArray) {
  52. if (executings[k] === true) return
  53. executings[k] = true
  54. const bar = ext?.ba(k, handlering[k])
  55. if (bar === false) {
  56. executings[k] = false
  57. return
  58. }
  59. const rv = target(...argArray)
  60. // 异步方法
  61. if (rv?.finally) {
  62. if (extEach[k]?.showLoading) {
  63. uni.showLoading({ title: config.common.RequestExtDef.loadingText })
  64. }
  65. rv.finally(() => {
  66. executings[k] = false
  67. if (extEach[k]?.showLoading) {
  68. // 在调用hideLoading与hideToast时配置参数noConflict: true取消混用特性,解决hideLoading隐藏showToast提示的问题。
  69. uni.hideLoading({ noConflict: true })
  70. }
  71. })
  72. } else {
  73. executings[k] = false
  74. }
  75. return rv
  76. },
  77. })
  78. }
  79. }
  80. rv.handler = new Proxy(
  81. {},
  82. {
  83. get(target, p) {
  84. return (...args) => {
  85. console.log('handler')
  86. handlering[p] = true
  87. Promise.resolve().then(() => (handlering[p] = false))
  88. return rv[p](...args)
  89. }
  90. },
  91. },
  92. ) as T
  93. return rv
  94. },
  95. /** 获取资源完整地址 */
  96. getResourceUrl(url) {
  97. return import.meta.env.VITE_RESOURCE_BASEURL + '/' + url
  98. },
  99. /** 获取资源完整地址 */
  100. getWebsocketUrl(url) {
  101. return import.meta.env.VITE_WEBSOCKET_BASEURL + '/' + url
  102. },
  103. goHome() {
  104. ay.goPage(config.pages.index_index, { goPageType: enums.GoPageType.reLaunch })
  105. },
  106. /** 跳登录
  107. * @params type 0:手机号快捷 1:账号、密码
  108. */
  109. goLogin(type = 0) {
  110. if (type === 0) ay.goPage(config.pages.login_index, { goPageType: enums.GoPageType.reLaunch }) // { params: { backPage: curPage } })
  111. // func.face.showToast({
  112. // title: '请登录',
  113. // icon: 'error',
  114. // })
  115. if (type === 1) {
  116. ay.goPage(config.pages.webview_login, { goPageType: enums.GoPageType.reLaunch })
  117. }
  118. },
  119. /** 关闭当前页面,返回上一页面或多级页面 */
  120. async navigateBack({
  121. args,
  122. aceas,
  123. }: {
  124. /** uni.navigateBack透传参数 */
  125. args?: UniApp.NavigateBackOptions
  126. /** 有值时,返回页面时执行AyEntranceCallBack-只执行一次 */
  127. aceas?: AyContainerEntryArgs
  128. } = {}) {
  129. if (store.curPage.pageConfig.prePage && aceas) {
  130. store.curPage.pageConfig.prePage.onceBackReloadArgs = aceas
  131. }
  132. await uni.navigateBack(args)
  133. },
  134. /** 页面跳转
  135. *
  136. */
  137. async goPage(page?: ayPage, ext?: goPageExt) {
  138. let goPageFunc = uni.navigateTo
  139. if (page._type === enums.PageType.tabPage) {
  140. goPageFunc = uni.switchTab
  141. }
  142. if (ext?.goPageType) {
  143. goPageFunc = uni[ext.goPageType]
  144. }
  145. // staticData.goPageExt = ext
  146. // 防止重复跳转
  147. func.antiShake({
  148. func: async () => {
  149. goPageFunc({ url: page._url })
  150. if (ext?.params) {
  151. store.curPage.initPageInfo({ page, params: ext.params, preset: true })
  152. }
  153. },
  154. immediately: true,
  155. mark: page._url,
  156. })
  157. // await goPageFunc({ url: page._url })
  158. },
  159. /**
  160. 页面生命周期入口,初始化页面,默认拦截enums.LoadType.onLoad、enums.LoadType.refresh, 可配置ext扩展参数
  161. */
  162. entrance(
  163. /** 页面生命周期入口的回调函数 */
  164. callBack: AyEntranceCallBack = () => {
  165. uni.stopPullDownRefresh()
  166. },
  167. ext?: AyEntranceExt,
  168. ) {
  169. /** 页面实例的唯一标识 */
  170. const tempid = Math.random().toString()
  171. const args: AyContainerEntryArgs = {
  172. loadType: enums.LoadType.onLoad,
  173. cras: config.common.defAyContainerRefreshArgs,
  174. }
  175. staticData.args = args
  176. const init = () => {
  177. store.curPage.initPageInfo()
  178. let isLogined = true
  179. const curPage = store.curPage.pageConfig
  180. if (store.common.data._releaser) {
  181. store.common.data._releaser.p.then(() => {
  182. if (curPage.identity) {
  183. if (!store.user.isLogined) {
  184. isLogined = false
  185. ay.goLogin()
  186. }
  187. }
  188. })
  189. } else {
  190. if (curPage.identity) {
  191. if (!store.user.isLogined) {
  192. isLogined = false
  193. ay.goLogin()
  194. }
  195. }
  196. }
  197. uni.setNavigationBarTitle({ title: store.curPage.pageConfig.title })
  198. return isLogined
  199. }
  200. onHide(() => {})
  201. onUnload(() => {
  202. store.curPage.removeCurPagerInfo()
  203. })
  204. onLoad(async () => {
  205. if (!func.antiShake({ func: init, immediately: true, mark: tempid })) {
  206. return
  207. }
  208. if (store.curPage?.pageConfig?.isPager) {
  209. store.curPage.pageConfig.curCallBack = (cras: AyContainerRefreshArgs) => {
  210. args.cras = {
  211. ...args.cras,
  212. ...cras,
  213. }
  214. args.loadType = enums.LoadType.refresh
  215. callBack(args)
  216. }
  217. // 在onload中 初始化原生方法时,之前的代码不能有await,否则小程序环境'重新进入小程序' 无法触发
  218. // onReachBottom(() => {
  219. // store.curPage.pagerMethods.refresh({ isAdd: true })
  220. // })
  221. // onPullDownRefresh(() => {
  222. // store.curPage.pagerMethods.refresh({ isAdd: false })
  223. // })
  224. // } else {
  225. // onPullDownRefresh(() => {
  226. // args.loadType = enums.LoadType.refresh
  227. // callBack(args)
  228. // })
  229. }
  230. // if (options) {
  231. // args.options = options
  232. // }
  233. // 等待数据开关接口
  234. if (store.common.data._releaser) {
  235. await store.common.data._releaser.p
  236. }
  237. callBack(args)
  238. })
  239. onShow(() => {
  240. if (!func.antiShake({ func: init, immediately: true, mark: tempid })) {
  241. return
  242. }
  243. if (store.curPage.pageConfig.onceBackReloadArgs) {
  244. callBack({
  245. ...args,
  246. ...store.curPage.pageConfig.onceBackReloadArgs,
  247. loadType: enums.LoadType.backReload,
  248. })
  249. delete store.curPage.pageConfig.onceBackReloadArgs
  250. }
  251. })
  252. if (ext?.addLoadTypes && ext?.addLoadTypes.length) {
  253. ext.addLoadTypes.forEach((f) => {
  254. const curLt = loadTypeMaps.get(f)
  255. if (curLt) {
  256. curLt(() => {
  257. if (f === enums.LoadType.onShow && !store.curPage.pageConfig.onShowed) {
  258. store.curPage.pageConfig.onShowed = true
  259. return
  260. }
  261. callBack({
  262. ...args,
  263. loadType: f,
  264. })
  265. })
  266. }
  267. })
  268. }
  269. if (ext?.defAyContainerRefreshArgs) {
  270. args.cras = {
  271. ...args.cras,
  272. ...ext.defAyContainerRefreshArgs,
  273. }
  274. }
  275. },
  276. /**
  277. * 通知容器数据加载完毕
  278. */
  279. containerLoaded(args?: AyContainerMethodsLoadedArgs) {
  280. if (store.curPage?.pageConfig?.isPager) {
  281. store.curPage.pagerMethods.loaded({
  282. ...store.curPage.pageConfig.pagerInfo,
  283. ...args,
  284. })
  285. } else {
  286. uni.stopPullDownRefresh()
  287. }
  288. },
  289. }