小说小程序前端代码仓库(小程序)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
2.6 KiB

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex); //vue的插件机制
import api from '@/api/api.js'
//Vuex.Store 构造器选项
const store = new Vuex.Store({
state: {
configList: {}, //配置列表
userInfo: {}, //用户信息
themeMode: uni.getStorageSync('themeMode') || 'light', // 主题模式:light(白天)或dark(黑夜)
isLogin : !!uni.getStorageSync('token'),
},
getters: {
// 当前主题模式
currentTheme(state) {
return state.themeMode
},
// 是否为黑夜模式
isDarkMode(state) {
return state.themeMode === 'dark'
}
},
mutations: {
// 初始化配置
initConfig(state) {
api('getConfig', res => {
if (res.code == 200) {
// state.configList = res.result
res.result.forEach(n => {
state.configList[n.keyName] = n.keyContent
})
}
})
},
// 切换主题模式
toggleThemeMode(state) {
state.themeMode = state.themeMode === 'light' ? 'dark' : 'light'
// 保存到本地存储,以便下次打开时恢复
uni.setStorageSync('themeMode', state.themeMode)
},
// 设置为指定主题模式
setThemeMode(state, mode) {
if (mode === 'light' || mode === 'dark') {
state.themeMode = mode
// 保存到本地存储,以便下次打开时恢复
uni.setStorageSync('themeMode', state.themeMode)
}
},
// 初始化主题模式
initThemeMode(state) {
// 从本地存储中读取主题设置
const savedMode = uni.getStorageSync('themeMode')
if (savedMode) {
state.themeMode = savedMode
}
},
login(state) {
uni.showLoading({
title: '登录中...'
})
uni.login({
success(res) {
if (res.errMsg != "login:ok") {
return
}
api('wxLogin', {
code: res.code
}, res => {
uni.hideLoading()
if (res.code != 200) {
return
}
state.userInfo = res.result.userInfo
uni.setStorageSync('token', res.result.token)
state.isLogin = true
if (!state.userInfo.nickName || !state.userInfo.headImage) {
uni.navigateTo({
url: '/pages_order/auth/wxUserInfo'
})
} else {
uni.navigateBack(-1)
}
})
}
})
},
getUserInfo(state) {
api('getUserInfo', res => {
if (res.code == 200) {
state.userInfo = res.result
}
})
},
// 退出登录
logout(state, title = '确认退出登录吗') {
uni.showModal({
title,
success(r) {
if (r.confirm) {
state.userInfo = {}
state.isLogin = false
uni.removeStorageSync('token')
uni.reLaunch({
url: '/pages/index/index'
})
}
}
})
},
},
actions: {},
})
export default store