jazz
2023-12-28 583e1038163d7b95f7927f83b19d81f6eac32020
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * @Author: 丘骏_jun
 * @Date: 2019-11-21 10:59:17
 * @Last Modified by: 丘骏_jun
 * @Last Modified time: 2021-05-14 18:28:18
 */
import Vue from 'vue'
import Vuex from 'vuex'
// import fn from '../utils/fn.js'
Vue.use(Vuex)
 
const state = {}
 
 
// 显式更改state,内部只允许同步,调用方式:$store.commit(key, value)
const mutations = {}
 
// 相当于computed,调用方式:store.getters.xxx
const getters = {}
 
// 允许异步,需通过store.dispath分发,调用store.commit更改state
const actions = {}
 
/**
 * 首字母变大写
 * @param {string} name
 */
function getBigName(name){
    return [name[0].toUpperCase(), ...name.substring(1)].join('')
}
 
/**
 * 使用create,快捷创建简易字段
 * 通用创建store字段,包含state,getters,mutations
 * @param {string} key
 * @param {*} default_value
 * @param {function} mutation
 */
function create (key, default_value, mutation) {
    state[key] = default_value
    mutations['set' + getBigName(key)] = mutation || ((state, value) => {state[key] = value})
    getters['get' + getBigName(key)] = state => state[key]
}
 
// 用户信息
create('userData', {})
// danmuData: [], // 弹幕数据
// luckNum: 0, // 幸运值数据
// shareNum: 0, // 分享次数
// leftNum: 0, // 剩余抽奖次数
 
const store = new Vuex.Store({
    state,
    getters,
    mutations,
    actions,
})
 
export default store