吉安感知网项目-前端
chenyao
yesterday 4c390d209b45d516fbe2f7552d26048687c83972
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
82
83
84
85
86
87
88
89
90
91
92
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
    contactList: [], // 通讯录列表,用于快速查找联系人信息
  }),
  getters: {
    getContactByUserId: (state) => (userId) => {
      return state.contactList.find(contact => String(contact.userId) === String(userId))
    },
    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'
        })
      })
    },
    /**
     * 更新通讯录列表
     * @param {Array} contacts 通讯录列表
     */
    updateContactList (contacts) {
      this.contactList = contacts
    }
  }
})
 
export default useAppStore