import Event from '../typings/event'; import Component, { ingoreEmptyFunction } from './Component'; /* eslint-disable @typescript-eslint/no-empty-function */ interface Favorites { /** * 自定义标题 * @description 页面标题或账号名称 */ title: string; /** * 自定义图片 * @description 显示图片长宽比为1:1 页面截图 */ imageUrl: string; /** * 自定义query字段 * @description 当前页面的query */ query: string } export interface ShareAppMessage extends Omit { path: string; promise?: Promise; } abstract class Page { constructor(data: State) { this.data = data; } public data: Readonly; /** 到当前页面的路径,类型为String。 */ protected route!: Readonly; /** * 函数用于将数据从逻辑层发送到视图层(异步),同时改变对应的 this.data 的值(同步) * @description 其中 key 可以以数据路径的形式给出,支持改变数组中的某一项或对象的某个属性,如 array[2].message,a.b.c.d,并且不需要在 this.data 中预先定义。 * @description 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致。 * @description 仅支持设置可 JSON 化的数据。 * @description 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据。 * @description 请不要把 data 中任何一项的 value 设为 undefined ,否则这一项将不被设置并可能遗留一些潜在问题。 */ public setData!: (newData: State | Record, callback?: () => void) => void; /** * 页面加载时触发。一个页面只会调用一次,可以在 onLoad 的参数中获取打开当前页面路径中的参数。 * @param query 打开当前页面路径中的参数 */ protected onLoad(query: Record) {} /** 页面显示/切入前台时触发 */ protected onShow() {} /** * 页面初次渲染完成时触发。一个页面只会调用一次,代表页面已经准备妥当,可以和视图层进行交互。 * @description 注意:对界面内容进行设置的 API 如wx.setNavigationBarTitle,请在onReady之后进行。 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/page-life-cycle.html | 详见生命周期} */ protected onReady() {} /** * 页面隐藏/切入后台时触发。 * @description 如 wx.navigateTo 或底部 tab 切换到其他页面,小程序切入后台等 */ protected onHide() {} /** * 页面卸载时触发 * @description 如wx.redirectTo或wx.navigateBack到其他页面时 */ protected onUnload() {} /** * 监听用户下拉刷新事件。 * @description 需要在app.json的window选项中或页面配置中开启enablePullDownRefresh。 * @description 可以通过wx.startPullDownRefresh触发下拉刷新,调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。 * @description 当处理完数据刷新后,wx.stopPullDownRefresh可以停止当前页面的下拉刷新。 */ protected onPullDownRefresh() {} /** * 监听用户上拉触底事件 * @description 可以在app.json的window选项中或页面配置中设置触发距离onReachBottomDistance。 * @description 在触发距离内滑动期间,本事件只会被触发一次。 */ protected onReachBottom() {} /** 监听用户滑动页面事件 */ protected onPageScroll(scrollTop: number) {} /** * 小程序屏幕旋转时触发。详见 响应显示区域变化 * @see {@link https://developers.weixin.qq.com/miniprogram/dev/framework/view/resizable.html#%E5%9C%A8%E6%89%8B%E6%9C%BA%E4%B8%8A%E5%90%AF%E7%94%A8%E5%B1%8F%E5%B9%95%E6%97%8B%E8%BD%AC%E6%94%AF%E6%8C%81 | 响应显示区域变化} */ protected onResize() {} /** * 监听用户点击右上角菜单“收藏”按钮的行为,并自定义收藏内容。 * @description 本接口为 Beta 版本,安卓 7.0.15 版本起支持,暂只在安卓平台支持 * @param params 页面中包含web-view组件时,返回当前web-view的url */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore protected onAddToFavorites(params: { webviewUrl: string }): Favorites {} /** * 监听用户点击页面内转发按钮(button 组件 open-type="share")或右上角菜单“转发”按钮的行为,并自定义转发内容。 * @description 注意:只有定义了此事件处理函数,右上角菜单才会显示“转发”按钮 */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore protected async onShareAppMessage(params: { from: string; taget: Event.Target; webViewUrl: string }): ShareAppMessage { return '__此处是函数定义__' as any; } /** * 监听右上角菜单“分享到朋友圈”按钮的行为,并自定义分享内容。 * @description 注意:只有定义了此事件处理函数,右上角菜单才会显示“转发”按钮 */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore protected onShareTimeline(params: { from: string; taget: Event.Target; webViewUrl: string }): Favorites { return '__此处是函数定义__' as any; } /** * 点击 tab 时触发 * @version 1.9.0 */ protected onTabItemTap(params: { index: string; pagePath: string; text: string }) {} /** * 被打开的页面可以通过 this.getOpenerEventChannel() 方法来获得一个 EventChannel 对象 * 这两个 EventChannel 对象间可以使用 emit 和 on 方法相互发送、监听事件。 */ protected getOpenerEventChannel(): WechatMiniprogram.EventChannel { return '__此处是函数定义__' as any; } /** 使用选择器选择组件实例节点,返回匹配到的第一个组件实例对象(会被 wx://component-export 影响) */ public selectComponent!: (selector: string) => Component | T; /** * 导出实例 * @description 过滤不需要导出的成员以及空函数 */ private exportInstance>(params: T): T { const instance = {}; const ingore = ['init', 'constructor', 'exportInstance']; for (const key in params) { const k = key as keyof typeof params; const item = params[k] as any; if (!ingore.includes(key) && ingoreEmptyFunction(item)) { // 避免把定义的空函数传递出去 (instance as any)[k] = item; } } return instance as any as T; } /** 初始化实例,导出给小程序Page函数 */ public init(mixins?: string[]): any { return this.exportInstance(this); } /** 获取模板实例 */ protected getTemplate}>(header: T): InstanceType { return this as any as InstanceType; } } export default Page;