import { createPinia, defineStore } from 'pinia' import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化 import { upDateAppApi } from '@/api/system'; import { hasNewVersion, onClickUpdate } from '@/utils/index'; const store = createPinia() store.use( createPersistedState({ storage: { getItem: uni.getStorageSync, setItem: uni.setStorageSync, }, }), ) export const useAppStore = defineStore('app', { state: () => ({ isGray: 0 as 0 | 1, // 0=正常,1=灰化 }), actions: { setIsGray(value : 0 | 1) { this.isGray = value }, }, persist: true, // 启用持久化 }) export const useUpdateApp = defineStore('updateApp', () => { const updateOptions = reactive({ force: false, hasNew: false, content: '', url: '', apkUrl: '', wgtUrl: '' }) const systemInfo = uni.getSystemInfoSync() /** * 当前处于APP_PLUS时 检查是否有新版本 * * @param to 是否跳转新页面显示更新 默认值false */ function checkAppUpdate(to = false) { try { upDateAppApi().then(async (res : any) => { let { result } = res result.apkUrl = import.meta.env.VITE_SERVER_BASEURL + '/sys/common/static/' + result.apkUrl; result.wgtUrl = import.meta.env.VITE_SERVER_BASEURL + '/sys/common/static/' + result.wgtUrl updateOptions.wgtUrl = result.wgtUrl if (systemInfo.osName === 'android') { // Android updateOptions.apkUrl = result.apkUrl // #ifdef APP_PLUS updateOptions.hasNew = await hasNewVersion(result.versionCode, result.update == 'wgt') as boolean // #endif } else { // IOS updateOptions.url = `itms-apps://itunes.apple.com/cn/app/id${123456}?mt=8` } updateOptions.hasNew && uni.showModal({ title: '更新', content: '发现新版本,请更新', success(res) { if (res.confirm) { onClickUpdate(result.update, result) } else { plus.runtime.quit() } } }) }) } catch (error) { updateOptions.hasNew = false } } return { checkAppUpdate, ...toRefs(updateOptions), systemInfo } }) export default store // 模块统一导出 export * from './user'