初始化仓库
This commit is contained in:
8
store/getters.js
Normal file
8
store/getters.js
Normal file
@ -0,0 +1,8 @@
|
||||
export default {
|
||||
userInfo: state => state.app.userInfo || {},
|
||||
token: state => state.app.token,
|
||||
isLogin: state => !!state.app.token,
|
||||
cartNum: state => state.app.cartNum,
|
||||
inviteCode: state => state.app.userInfo.distribution_code || "",
|
||||
appConfig: state => state.app.config
|
||||
};
|
||||
12
store/index.js
Normal file
12
store/index.js
Normal file
@ -0,0 +1,12 @@
|
||||
import Vue from "vue";
|
||||
import Vuex from "vuex";
|
||||
import modules from "./modules";
|
||||
import getters from "./getters";
|
||||
|
||||
Vue.use(Vuex);
|
||||
const debug = process.env.NODE_ENV !== "production";
|
||||
export default new Vuex.Store({
|
||||
modules,
|
||||
getters,
|
||||
strict: debug
|
||||
});
|
||||
91
store/modules/app.js
Normal file
91
store/modules/app.js
Normal file
@ -0,0 +1,91 @@
|
||||
import {getUser} from '@/api/user'
|
||||
import {getCartNum} from '@/api/store';
|
||||
import {
|
||||
USER_INFO,
|
||||
TOKEN,
|
||||
CONFIG
|
||||
} from '@/config/cachekey';
|
||||
import Cache from '@/utils/cache'
|
||||
const state = {
|
||||
config: Cache.get(CONFIG) || {
|
||||
app_agreement: 0,
|
||||
center_setting: {},
|
||||
index_setting: {},
|
||||
navigation_menu: [],
|
||||
navigation_setting: {}
|
||||
},
|
||||
userInfo: {
|
||||
user_money: 0,
|
||||
user_integral: 0,
|
||||
coupon: 0
|
||||
},
|
||||
token: Cache.get(TOKEN) || null,
|
||||
cartNum: "",
|
||||
};
|
||||
|
||||
const mutations = {
|
||||
LOGIN(state, opt) {
|
||||
state.token = opt.token;
|
||||
Cache.set(TOKEN, opt.token, 59 * 24 * 60 * 60);
|
||||
this.dispatch('getUser')
|
||||
},
|
||||
LOGOUT(state) {
|
||||
state.token = undefined;
|
||||
state.userInfo = {
|
||||
user_money: 0,
|
||||
user_integral: 0,
|
||||
coupon: 0
|
||||
}
|
||||
Cache.remove(TOKEN);
|
||||
},
|
||||
SETCARTNUM(state, num) {
|
||||
state.cartNum = num
|
||||
},
|
||||
SETUSERINFO(state, user) {
|
||||
state.userInfo = user
|
||||
},
|
||||
SETCONFIG(state, data) {
|
||||
state.config = Object.assign(state.config, data)
|
||||
Cache.set(CONFIG, state.config);
|
||||
}
|
||||
};
|
||||
|
||||
const actions = {
|
||||
getCartNum({state, commit}) {
|
||||
return new Promise(resolve => {
|
||||
if (!state.token) return uni.removeTabBarBadge({
|
||||
index: 2
|
||||
})
|
||||
getCartNum().then(res => {
|
||||
if (res.code == 1) {
|
||||
commit('SETCARTNUM', res.data.num)
|
||||
if (!res.data.num) return uni.removeTabBarBadge({
|
||||
index: 2
|
||||
})
|
||||
uni.setTabBarBadge({
|
||||
index: 2,
|
||||
text: String(res.data.num)
|
||||
})
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
getUser({state, commit}) {
|
||||
return new Promise(resolve => {
|
||||
getUser().then(res => {
|
||||
if (res.code == 1) {
|
||||
commit('SETUSERINFO', res.data)
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
},
|
||||
};
|
||||
|
||||
export default {
|
||||
state,
|
||||
mutations,
|
||||
actions
|
||||
};
|
||||
5
store/modules/index.js
Normal file
5
store/modules/index.js
Normal file
@ -0,0 +1,5 @@
|
||||
import app from "./app";
|
||||
|
||||
export default {
|
||||
app
|
||||
};
|
||||
Reference in New Issue
Block a user