jeecgBootUniapp/src/store/index.ts

89 lines
2.1 KiB
Vue
Raw Normal View History

2025-04-29 08:37:17 +00:00
import { createPinia, defineStore } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化
2025-04-30 01:16:10 +00:00
import { upDateAppApi } from '@/api/system';
import { hasNewVersion, onClickUpdate } from '@/utils/index';
2025-04-29 08:37:17 +00:00
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, // 启用持久化
})
2025-04-30 01:16:10 +00:00
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
}
})
2025-04-29 08:37:17 +00:00
export default store
// 模块统一导出
export * from './user'