|
| 1 | +import { wxToPromise, isPC, isIOS, emitter } from './utils/index' |
| 2 | + |
| 3 | +interface AppOption extends AppData { |
| 4 | + getSetting: () => void |
| 5 | + getSyetemInfo: () => void |
| 6 | + registerCommand: () => void |
| 7 | + setLaunchShowOption: (option: WechatMiniprogram.App.LaunchShowOption, type: string) => void |
| 8 | + setSetting: () => void |
| 9 | +} |
| 10 | + |
| 11 | +/** 全局数据初始值 */ |
| 12 | +const globalData : AppData['globalData'] = { |
| 13 | + apiCategory: 'default', |
| 14 | + authRecord: false, |
| 15 | + authWritePhotosAlbum: false, |
| 16 | + envVersion: 'release', |
| 17 | + isWeakNet: false, |
| 18 | + keyboardHeight: 0, |
| 19 | + navBarHeight: 0, |
| 20 | + safeAreaBottom: 0, |
| 21 | + scene: 0, |
| 22 | + shareTicket: '', |
| 23 | + systemInfo: undefined, |
| 24 | + uuid: '', |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @see https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.html |
| 29 | + */ |
| 30 | +App<AppOption>({ |
| 31 | + globalData, |
| 32 | + async onLaunch() { |
| 33 | + /** |
| 34 | + * 限制频率的接口,必须在这里调用 |
| 35 | + * @see https://developers.weixin.qq.com/miniprogram/dev/framework/performance/api-frequency.html |
| 36 | + */ |
| 37 | + this.getSyetemInfo() |
| 38 | + |
| 39 | + this.getSetting() |
| 40 | + this.setSetting() |
| 41 | + }, |
| 42 | + |
| 43 | + /** |
| 44 | + * 注册全局的便利函数 |
| 45 | + */ |
| 46 | + registerCommand () { |
| 47 | + |
| 48 | + }, |
| 49 | + /** |
| 50 | + * 获取系统信息 |
| 51 | + */ |
| 52 | + getSyetemInfo () { |
| 53 | + const systemInfo = wx.getSystemInfoSync() |
| 54 | + this.globalData.systemInfo = systemInfo |
| 55 | + |
| 56 | + // 计算底部安全区高度,mac screenHeight 比实际显示的高,这里做个修正 |
| 57 | + // windows 2.26.1 开始 screenHeight 和 windowHeight 不同了,需要用 systemInfo.windowHeight - systemInfo.safeArea.bottom |
| 58 | + this.globalData.safeAreaBottom = isPC(this) ? 0 : systemInfo.screenHeight - systemInfo.safeArea.bottom |
| 59 | + |
| 60 | + // 计算导航栏高度 |
| 61 | + if (wx.getMenuButtonBoundingClientRect) { |
| 62 | + const rect = wx.getMenuButtonBoundingClientRect() |
| 63 | + if (rect) { |
| 64 | + const {top, bottom} = rect! |
| 65 | + const navbarHeight = isIOS(this) ? 44 : 48; |
| 66 | + const navbarPaddingTop = (bottom + top) / 2 - navbarHeight / 2 |
| 67 | + this.globalData.navBarHeight = navbarHeight + navbarPaddingTop |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + console.log('systemInfo', this.globalData.systemInfo) |
| 72 | + }, |
| 73 | + /** |
| 74 | + * 检查权限状态 |
| 75 | + */ |
| 76 | + async getSetting () { |
| 77 | + const [res] = await wxToPromise(wx.getSetting) |
| 78 | + if (res) { |
| 79 | + this.globalData.authRecord = res.authSetting['scope.record']! |
| 80 | + this.globalData.authWritePhotosAlbum = res.authSetting['scope.writePhotosAlbum']! |
| 81 | + } |
| 82 | + }, |
| 83 | + /** |
| 84 | + * 设置小程序配置 |
| 85 | + */ |
| 86 | + async setSetting () { |
| 87 | + // 转发设置,重置分享参数 |
| 88 | + wx.updateShareMenu({ |
| 89 | + withShareTicket: false, |
| 90 | + isPrivateMessage: false |
| 91 | + }) |
| 92 | + |
| 93 | + // 弱网状态 |
| 94 | + wx.onNetworkWeakChange && wx.onNetworkWeakChange((res) => { |
| 95 | + this.globalData.isWeakNet = res.weakNet |
| 96 | + }) |
| 97 | + |
| 98 | + // 键盘高度变化事件 |
| 99 | + wx.onKeyboardHeightChange((res) => { |
| 100 | + console.log('app.ts keyboardHeightChange', res.height) |
| 101 | + this.globalData.keyboardHeight = res.height |
| 102 | + emitter.emit('keyboardHeightChange', res) |
| 103 | + }) |
| 104 | + |
| 105 | + // 窗口尺寸变化事件(微信 bug: mac 客户端 <= 3.7 版本不能触发) |
| 106 | + wx.onWindowResize((res) => { |
| 107 | + if (!res.size || !this.globalData.systemInfo) return |
| 108 | + |
| 109 | + // 数据相同时,不触发事件 |
| 110 | + if ( |
| 111 | + this.globalData.systemInfo.screenHeight === res.size.screenHeight && |
| 112 | + this.globalData.systemInfo.screenWidth === res.size.screenWidth && |
| 113 | + this.globalData.systemInfo.windowHeight === res.size.windowHeight && |
| 114 | + this.globalData.systemInfo.windowWidth === res.size.windowWidth |
| 115 | + ) return |
| 116 | + |
| 117 | + this.globalData.systemInfo.screenHeight = res.size.screenHeight |
| 118 | + this.globalData.systemInfo.screenWidth = res.size.screenWidth |
| 119 | + this.globalData.systemInfo.windowHeight = res.size.windowHeight |
| 120 | + this.globalData.systemInfo.windowWidth = res.size.windowWidth |
| 121 | + |
| 122 | + // 事件触发器,去广播给页面 |
| 123 | + emitter.emit('windowResize') |
| 124 | + }) |
| 125 | + }, |
| 126 | + /** |
| 127 | + * 设置启动参数 |
| 128 | + */ |
| 129 | + setLaunchShowOption (option, type) { |
| 130 | + console.log(type, 'option', option) |
| 131 | + |
| 132 | + // 重定向到首页 |
| 133 | + if (option.query?.redirect === 'home') { |
| 134 | + wx.reLaunch({url: '/pages/home/index'}) |
| 135 | + } |
| 136 | + |
| 137 | + this.globalData.scene = option.scene |
| 138 | + console.log(type, 'scene', option.scene) |
| 139 | + |
| 140 | + // @ts-ignore |
| 141 | + this.globalData.apiCategory = option.apiCategory |
| 142 | + |
| 143 | + this.globalData.shareTicket = option.shareTicket || '' |
| 144 | + console.log(type, 'shareTicket', option.shareTicket) |
| 145 | + |
| 146 | + } |
| 147 | +}) |
0 commit comments