罗广辉
2026-06-18 44dd9c4eb8a2489c63a5eb53fd4254bbb3510e95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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