| | |
| | | import { defineStore } from 'pinia'; |
| | | import storage from '@/utils/storage'; |
| | | import { defineStore } from 'pinia' |
| | | import storage from '@/utils/storage' |
| | | |
| | | // 缓存的主题 |
| | | const THEME_KEY = 'app-theme'; |
| | | const THEME_KEY = 'app-theme' |
| | | |
| | | const useAppStore = defineStore('app', { |
| | | state: () => ({ |
| | |
| | | theme: storage.get(THEME_KEY) || 'light' |
| | | }), |
| | | getters: { |
| | | getSystemInfo(state) { |
| | | return state.systemInfo; |
| | | getSystemInfo (state) { |
| | | return state.systemInfo |
| | | }, |
| | | getTheme(state) { |
| | | return state.theme; |
| | | getTheme (state) { |
| | | return state.theme |
| | | } |
| | | }, |
| | | actions: { |
| | | setSystemInfo(info) { |
| | | this.systemInfo = info; |
| | | setSystemInfo (info) { |
| | | this.systemInfo = info |
| | | }, |
| | | initSystemInfo() { |
| | | initSystemInfo () { |
| | | uni.getSystemInfo({ |
| | | success: (res) => { |
| | | this.setSystemInfo(res); |
| | | this.setSystemInfo(res) |
| | | }, |
| | | fail: (err) => { |
| | | console.error(err); |
| | | console.error(err) |
| | | } |
| | | }); |
| | | }) |
| | | }, |
| | | /** |
| | | * 设置主题 |
| | | */ |
| | | setTheme(theme) { |
| | | this.theme = theme; |
| | | setTheme (theme) { |
| | | this.theme = theme |
| | | // 保存到本地存储 |
| | | storage.set(THEME_KEY, this.theme); |
| | | storage.set(THEME_KEY, this.theme) |
| | | }, |
| | | checkUpdate() { |
| | | const updateManager = uni.getUpdateManager(); |
| | | checkUpdate () { |
| | | const updateManager = uni.getUpdateManager() |
| | | updateManager.onCheckForUpdate((res) => { |
| | | // 请求完新版本信息的回调 |
| | | console.log(res.hasUpdate); |
| | | }); |
| | | console.log(res.hasUpdate) |
| | | }) |
| | | updateManager.onUpdateReady(() => { |
| | | uni.showModal({ |
| | | title: '更新提示', |
| | | content: '新版本已经准备好,是否重启应用?', |
| | | success(res) { |
| | | success (res) { |
| | | if (res.confirm) { |
| | | // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 |
| | | updateManager.applyUpdate(); |
| | | updateManager.applyUpdate() |
| | | } |
| | | } |
| | | }); |
| | | }); |
| | | }) |
| | | }) |
| | | updateManager.onUpdateFailed((res) => { |
| | | console.error(res); |
| | | console.error(res) |
| | | // 新的版本下载失败 |
| | | uni.showToast({ |
| | | title: '更新失败', |
| | | icon: 'error' |
| | | }); |
| | | }); |
| | | }) |
| | | }) |
| | | } |
| | | } |
| | | }); |
| | | }) |
| | | |
| | | export default useAppStore; |
| | | export default useAppStore |