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