| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /**
- * 生命周期类型映射
- */
- const loadTypeMaps = new Map()
- loadTypeMaps.set(enums.LoadType.onLoad, onLoad)
- loadTypeMaps.set(enums.LoadType.onShow, onShow)
- loadTypeMaps.set(enums.LoadType.onReady, onReady)
- type goPageExt = {
- /** 默认navigateTo */
- goPageType?: GoPageTypeKeys
- params?: AnyObject
- }
- const staticData = {
- args: {} as AyContainerEntryArgs,
- goPageExt: {} as goPageExt,
- }
- type DecorateMethods<T> = T & {
- /** 点击执行的方法 */
- handler: T
- }
- export default {
- staticData,
- initData<T extends object>(data: T) {
- const rd = reactive(data)
- // const presetCurPage = store.curPage.initPageInfo({ data: rd, preset: true })
- // rd.pageConfig = {
- // title: presetCurPage.title,
- // style: presetCurPage.style,
- // }
- return rd
- },
- /** 初始化对象内的方法,自动为对象内所有方法增加防重提功能(同步方法、异步方法执行中再次触发不执行) */
- initMethods<T extends AnyObject>(
- methods: T,
- extEach?: Record<string, { showLoading?: boolean; ba?: () => void }>,
- ext?: {
- /**
- * 前置通知,返回false时不执行目标方法
- * @param methodName:方法名
- */ ba?: (methodName: keyof T, isHandler: boolean) => boolean | void
- },
- ): DecorateMethods<T> {
- /** 正在执行中的方法 */
- const executings = {}
- /** 是被点击执行的方法 */
- const handlering = {}
- const rv = {} as DecorateMethods<T>
- for (const [k, v] of Object.entries(methods)) {
- if (typeof v === 'function') {
- rv[k] = new Proxy(v, {
- apply(target, arg, argArray) {
- if (executings[k] === true) return
- executings[k] = true
- const bar = ext?.ba(k, handlering[k])
- if (bar === false) {
- executings[k] = false
- return
- }
- const rv = target(...argArray)
- // 异步方法
- if (rv?.finally) {
- if (extEach[k]?.showLoading) {
- uni.showLoading({ title: config.common.RequestExtDef.loadingText })
- }
- rv.finally(() => {
- executings[k] = false
- if (extEach[k]?.showLoading) {
- // 在调用hideLoading与hideToast时配置参数noConflict: true取消混用特性,解决hideLoading隐藏showToast提示的问题。
- uni.hideLoading({ noConflict: true })
- }
- })
- } else {
- executings[k] = false
- }
- return rv
- },
- })
- }
- }
- rv.handler = new Proxy(
- {},
- {
- get(target, p) {
- return (...args) => {
- console.log('handler')
- handlering[p] = true
- Promise.resolve().then(() => (handlering[p] = false))
- return rv[p](...args)
- }
- },
- },
- ) as T
- return rv
- },
- /** 获取资源完整地址 */
- getResourceUrl(url) {
- return import.meta.env.VITE_RESOURCE_BASEURL + '/' + url
- },
- /** 获取资源完整地址 */
- getWebsocketUrl(url) {
- return import.meta.env.VITE_WEBSOCKET_BASEURL + '/' + url
- },
- goHome() {
- ay.goPage(config.pages.index_index, { goPageType: enums.GoPageType.reLaunch })
- },
- /** 跳登录
- * @params type 0:手机号快捷 1:账号、密码
- */
- goLogin(type = 0) {
- if (type === 0) ay.goPage(config.pages.login_index, { goPageType: enums.GoPageType.reLaunch }) // { params: { backPage: curPage } })
- // func.face.showToast({
- // title: '请登录',
- // icon: 'error',
- // })
- if (type === 1) {
- ay.goPage(config.pages.webview_login, { goPageType: enums.GoPageType.reLaunch })
- }
- },
- /** 关闭当前页面,返回上一页面或多级页面 */
- async navigateBack({
- args,
- aceas,
- }: {
- /** uni.navigateBack透传参数 */
- args?: UniApp.NavigateBackOptions
- /** 有值时,返回页面时执行AyEntranceCallBack-只执行一次 */
- aceas?: AyContainerEntryArgs
- } = {}) {
- if (store.curPage.pageConfig.prePage && aceas) {
- store.curPage.pageConfig.prePage.onceBackReloadArgs = aceas
- }
- await uni.navigateBack(args)
- },
- /** 页面跳转
- *
- */
- async goPage(page?: ayPage, ext?: goPageExt) {
- let goPageFunc = uni.navigateTo
- if (page._type === enums.PageType.tabPage) {
- goPageFunc = uni.switchTab
- }
- if (ext?.goPageType) {
- goPageFunc = uni[ext.goPageType]
- }
- // staticData.goPageExt = ext
- // 防止重复跳转
- func.antiShake({
- func: async () => {
- goPageFunc({ url: page._url })
- if (ext?.params) {
- store.curPage.initPageInfo({ page, params: ext.params, preset: true })
- }
- },
- immediately: true,
- mark: page._url,
- })
- // await goPageFunc({ url: page._url })
- },
- /**
- 页面生命周期入口,初始化页面,默认拦截enums.LoadType.onLoad、enums.LoadType.refresh, 可配置ext扩展参数
- */
- entrance(
- /** 页面生命周期入口的回调函数 */
- callBack: AyEntranceCallBack = () => {
- uni.stopPullDownRefresh()
- },
- ext?: AyEntranceExt,
- ) {
- /** 页面实例的唯一标识 */
- const tempid = Math.random().toString()
- const args: AyContainerEntryArgs = {
- loadType: enums.LoadType.onLoad,
- cras: config.common.defAyContainerRefreshArgs,
- }
- staticData.args = args
- const init = () => {
- store.curPage.initPageInfo()
- let isLogined = true
- const curPage = store.curPage.pageConfig
- if (store.common.data._releaser) {
- store.common.data._releaser.p.then(() => {
- if (curPage.identity) {
- if (!store.user.isLogined) {
- isLogined = false
- ay.goLogin()
- }
- }
- })
- } else {
- if (curPage.identity) {
- if (!store.user.isLogined) {
- isLogined = false
- ay.goLogin()
- }
- }
- }
- uni.setNavigationBarTitle({ title: store.curPage.pageConfig.title })
- return isLogined
- }
- onHide(() => {})
- onUnload(() => {
- store.curPage.removeCurPagerInfo()
- })
- onLoad(async () => {
- if (!func.antiShake({ func: init, immediately: true, mark: tempid })) {
- return
- }
- if (store.curPage?.pageConfig?.isPager) {
- store.curPage.pageConfig.curCallBack = (cras: AyContainerRefreshArgs) => {
- args.cras = {
- ...args.cras,
- ...cras,
- }
- args.loadType = enums.LoadType.refresh
- callBack(args)
- }
- // 在onload中 初始化原生方法时,之前的代码不能有await,否则小程序环境'重新进入小程序' 无法触发
- // onReachBottom(() => {
- // store.curPage.pagerMethods.refresh({ isAdd: true })
- // })
- // onPullDownRefresh(() => {
- // store.curPage.pagerMethods.refresh({ isAdd: false })
- // })
- // } else {
- // onPullDownRefresh(() => {
- // args.loadType = enums.LoadType.refresh
- // callBack(args)
- // })
- }
- // if (options) {
- // args.options = options
- // }
- // 等待数据开关接口
- if (store.common.data._releaser) {
- await store.common.data._releaser.p
- }
- callBack(args)
- })
- onShow(() => {
- if (!func.antiShake({ func: init, immediately: true, mark: tempid })) {
- return
- }
- if (store.curPage.pageConfig.onceBackReloadArgs) {
- callBack({
- ...args,
- ...store.curPage.pageConfig.onceBackReloadArgs,
- loadType: enums.LoadType.backReload,
- })
- delete store.curPage.pageConfig.onceBackReloadArgs
- }
- })
- if (ext?.addLoadTypes && ext?.addLoadTypes.length) {
- ext.addLoadTypes.forEach((f) => {
- const curLt = loadTypeMaps.get(f)
- if (curLt) {
- curLt(() => {
- if (f === enums.LoadType.onShow && !store.curPage.pageConfig.onShowed) {
- store.curPage.pageConfig.onShowed = true
- return
- }
- callBack({
- ...args,
- loadType: f,
- })
- })
- }
- })
- }
- if (ext?.defAyContainerRefreshArgs) {
- args.cras = {
- ...args.cras,
- ...ext.defAyContainerRefreshArgs,
- }
- }
- },
- /**
- * 通知容器数据加载完毕
- */
- containerLoaded(args?: AyContainerMethodsLoadedArgs) {
- if (store.curPage?.pageConfig?.isPager) {
- store.curPage.pagerMethods.loaded({
- ...store.curPage.pageConfig.pagerInfo,
- ...args,
- })
- } else {
- uni.stopPullDownRefresh()
- }
- },
- }
|