Blame view

src/store/index.js 1.57 KB
289f85d9e   Adam   提交
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  import Vue from 'vue'
  import Vuex from 'vuex'
  
  Vue.use(Vuex)
  
  const store = new Vuex.Store({
  	state: {
  		hasLogin: false,
  		loginProvider: "",
  		openid: null,
  		testvuex:false,
          colorIndex: 0,
          colorList: ['#FF0000','#00FF00','#0000FF']
  	},
  	mutations: {
  		login(state, provider) {
  			state.hasLogin = true;
  			state.loginProvider = provider;
  		},
  		logout(state) {
  			state.hasLogin = false
  			state.openid = null
  		},
  		setOpenid(state, openid) {
  			state.openid = openid
  		},
  		setTestTrue(state){
  			state.testvuex = true
  		},
  		setTestFalse(state){
  			state.testvuex = false
  		},
          setColorIndex(state,index){
              state.colorIndex = index
          }
  	},
      getters:{
          currentColor(state){
              return state.colorList[state.colorIndex]
          }
      },
  	actions: {
  		// lazy loading openid
  		getUserOpenId: async function ({
  			commit,
  			state
  		}) {
  			return await new Promise((resolve, reject) => {
  				if (state.openid) {
  					resolve(state.openid)
  				} else {
  					uni.login({
  						success: (data) => {
  							commit('login')
  							setTimeout(function () { //模拟异步请求服务器获取 openid
  								const openid = '123456789'
  								console.log('uni.request mock openid[' + openid + ']');
  								commit('setOpenid', openid)
  								resolve(openid)
  							}, 1000)
  						},
  						fail: (err) => {
  							console.log('uni.login 接口调用失败,将无法正常使用开放接口等服务', err)
  							reject(err)
  						}
  					})
  				}
  			})
  		}
  	}
  })
  
  export default store