增加公共状态管理

This commit is contained in:
YeMingfei666 2025-01-13 18:18:45 +08:00
parent f410719b46
commit 3919dc507c
6 changed files with 100 additions and 11 deletions

View File

@ -19,4 +19,10 @@ export const commonType = (num) => {
return http.request({
url: `/common/type/${num}`,
})
}
export function getCommonConfig(){
return http.request({
url:'common/getAppUseKv'
})
}

View File

@ -119,7 +119,7 @@
</view>
<view class="u-m-t-10">
<text class="font-bold u-font-26 color-333">温馨提示</text>
<text class="u-m-t-10 u-font-24 color-999">一经购买不予退款未满18岁需在监护人的指导同意下进行付费操作</text>
<text class="u-m-t-10 u-font-24 color-999">{{$common.payTips}}</text>
</view>
<view class="u-flex u-flex-row u-m-t-30 u-flex-y-center u-font-28">
<view class="u-flex-y-center">
@ -154,6 +154,10 @@
</template>
<script setup>
import {
useCommonStore
} from '@/store/common.js'
const $common=useCommonStore()
// #ifdef APP
const domModule = uni.requireNativePlugin('dom')
// #endif

17
main.js
View File

@ -1,6 +1,12 @@
import App from './App'
import uviewPlus from 'uview-plus'
import * as Pinia from "pinia";
import {
createUnistorage
} from "pinia-plugin-unistorage";
import {
useCommonStore
} from '@/store/common.js'
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
@ -18,9 +24,16 @@ import {
} from 'vue'
export function createApp() {
const app = createSSRApp(App)
const store = Pinia.createPinia();
store.use(createUnistorage());
app.use(uviewPlus)
app.use(store)
const $common = useCommonStore()
$common.init()
return {
app
app,
Pinia
}
}
// #endif

View File

@ -8,5 +8,8 @@
"pre-commit": "^1.2.2",
"to-arraybuffer": "^1.0.1",
"uview-plus": "^3.3.61"
},
"devDependencies": {
"pinia-plugin-unistorage": "^0.1.2"
}
}

View File

@ -1,6 +1,7 @@
<template>
<view class="min-page">
<my-video-list ref="refVideoList" v-if="state.list.length" @swiperChange="swiperChange" :list="state.list"
@update="update" :info="state"></my-video-list>
</view>
</template>
@ -20,7 +21,7 @@
} from 'lodash'
const sysInfo = uni.getSystemInfoSync()
let isFirstLoad=true
let isFirstLoad = true
let options = {}
const state = reactive({
collect: 0,
@ -33,7 +34,7 @@
async function init() {
try {
const res = await Api.getVideoDetail(options)
isFirstLoad=false
isFirstLoad = false
Object.assign(state, res)
state.list = res.list
} catch (error) {
@ -69,17 +70,18 @@
}) {}
onShow(async () => {
if(!isFirstLoad){
if (!isFirstLoad) {
await init()
refVideoList.value.videoListUpdata()
const drawRes=await Api.getDrawCount()
const drawRes = await Api.getDrawCount()
console.log(drawRes);
const nobuyCourseId=uni.getStorageSync('nobuyCourseId')
const item=state.list.find(v=>v.courseId==nobuyCourseId)
const nobuyCourseId = uni.getStorageSync('nobuyCourseId')
const item = state.list.find(v => v.courseId == nobuyCourseId)
uni.clearStorageSync('nobuyCourseId')
if(drawRes.count*1>0&&nobuyCourseId!==null&&nobuyCourseId!==undefined&&item.videoUrl){
if (drawRes.count * 1 > 0 && nobuyCourseId !== null && nobuyCourseId !== undefined && item
.videoUrl) {
uni.navigateTo({
url:'/pages/me/prizeDraw'
url: '/pages/me/prizeDraw'
})
}
}

61
store/common.js Normal file
View File

@ -0,0 +1,61 @@
import {
defineStore
} from "pinia";
import {getCommonConfig} from '@/api/init.js'
const $map = {
882: 'isWxIosPay',
833: 'checkIosLogin',
834: 'checkIosPay',
835: 'checkWxLogin',
836: 'checkPhoneLogin',
108: 'isOpenWxWebAutoLogin',
817: 'zhengbu',
818: 'danbu',
252: 'adUnitId',
821: 'playType',
251: 'isGuanggao',
254: 'isGuanggaody',
202: 'kefu',
206: 'kefuPhone',
204: 'kefuUrl',
203: 'kefuAppId',
248: 'isVips',
249: 'moreSearch',
49: 'AppUrl',
823: 'OfferID',
824: 'payEnv',
825: 'moneyTips',
855: 'kmPaySel',
849: 'homeTypeSel',
856: 'syPaySel',
857: 'imId',
858: 'isAccountPay',
860: 'dyadUnitId',
881: '',
109: '',
922: 'withdrawNum',
500:'payTips'
}
export const useCommonStore = defineStore("common", {
state() {
return {
payTips:'付款完成后不要忘记抽红包哦'
};
},
actions:{
async init(){
const res=await getCommonConfig()
if(res){
for (let i in $map) {
const key = $map[i]
if (key) {
this[key]=res[i]
}
}
}
console.log(res);
}
},
unistorage: true, // 开启后对 state 的数据读写都将持久化
});