12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- //引入vue和vuex
- import Vue from 'vue'
- import Vuex from 'vuex'
- Vue.use(Vuex)
- // 用户是否登录
- let hasLogin = uni.getStorageSync('hasLogin') || false
- // 用户信息
- let userinfo = uni.getStorageSync('userinfo') || {}
- // 历史搜搜
- let searchData = uni.getStorageSync('searchData') || []
- const store = new Vuex.Store({ //全局变量定义
- state: {
- forcedLogin: false, //是否需要强制登录
- userLoginFlag: hasLogin, //用户是否登录
- userinfo: userinfo, //存放用户账号数据
- searchData: searchData ,// 历史搜索
- searchItem: '' // 导航栏搜索内容
- },
- getters: {
- // 用户信息
- info(state) {
- return state.userinfo
- },
- // 历史搜搜
- historySearch(state) {
- return state.searchData
- },
- // 历史搜索 5条之内
- historySearchInFive(state) {
- return state.searchData.slice(0,5)
- },
- // 历史搜索 超出5条
- historySearchOverFive(state) {
- return state.searchData.slice(5)
- },
- searchItemData(state) {
- return state.searchItem
- }
- },
- mutations: {
- // 登录
- login(state, provider) {
- state.userLoginFlag = true;
- state.userInfo = provider
- uni.setStorage({ // 异步缓存用户信息
- key: "userinfo",
- data: provider
- })
- },
- // 退出
- logout(state) {
- // state.userLoginFlag = false;
- // state.userInfo = {};
- // uni.removeStorage({ // 清除用户信息
- // key: "userinfo"
- // })
- uni.navigateTo({
- url: '/pages/login/index'
- })
- },
- // 修改导航栏搜索内容的值
- updateSearchItemData(state, data) {
- state.searchItem = data
- },
- // 清空历史搜索或者储存
- clearSearchData(state, data) {
- state.searchData = data
- },
- }
- })
- export default store
|