Blame view

store/index.js 2.8 KB
4b045f7c   刘淇   江阴初始化项目
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  // #ifndef VUE3
  import Vue from 'vue'
  import Vuex from 'vuex'
  Vue.use(Vuex)
  const store = new Vuex.Store({
  // #endif
  
  // #ifdef VUE3
  import { createStore } from 'vuex'
  const store = createStore({
  // #endif
  	state: {
  		hasLogin: false,
  		isUniverifyLogin: false,
  		loginProvider: "",
  		openid: null,
  		testvuex: false,
  		colorIndex: 0,
  		colorList: ['#FF0000', '#00FF00', '#0000FF'],
  		noMatchLeftWindow: true,
  		active: 'componentPage',
  		leftWinActive: '/pages/component/view/view',
  		activeOpen: '',
  		menu: [],
  		univerifyErrorMsg: ''
  	},
  	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
  		},
  		setMatchLeftWindow(state, matchLeftWindow) {
  			state.noMatchLeftWindow = !matchLeftWindow
  		},
  		setActive(state, tabPage) {
  			state.active = tabPage
  		},
  		setLeftWinActive(state, leftWinActive) {
  			state.leftWinActive = leftWinActive
  		},
  		setActiveOpen(state, activeOpen) {
  			state.activeOpen = activeOpen
  		},
  		setMenu(state, menu) {
  			state.menu = menu
  		},
  		setUniverifyLogin(state, payload) {
  			typeof payload !== 'boolean' ? payload = !!payload : '';
  			state.isUniverifyLogin = payload;
  		},
  		setUniverifyErrorMsg(state,payload = ''){
  			state.univerifyErrorMsg = payload
  		}
  	},
  	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)
  						}
  					})
  				}
  			})
  		},
  		getPhoneNumber: function({
  			commit
  		}, univerifyInfo) {
  			return new Promise((resolve, reject) => {
  				uni.request({
  					url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/univerify-login',
  					method: 'POST',
  					data: univerifyInfo,
  					success: (res) => {
  						const data = res.data
  						if (data.success) {
  							resolve(data.phoneNumber)
  						} else {
  							reject(res)
  						}
  
  					},
  					fail: (err) => {
  						reject(res)
  					}
  				})
  			})
  		}
  	}
  })
  
  export default store