| | |
| | | // store/index.js |
| | | import { createStore } from 'vuex' |
| | | import http from '../plugins/http' |
| | | import storage from '../plugins/storage.js' |
| | | import message from '../plugins/message.js' |
| | | |
| | | const store = createStore({ |
| | | state() { |
| | | return { |
| | | 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: [], |
| | | appMenu: [], |
| | | univerifyErrorMsg: '', |
| | | currentInfo: {}, |
| | | cache_address: {}, |
| | | sign: { |
| | | enterprise: 0, |
| | | info: 0, |
| | | flower: 0, |
| | | shopping: 0, |
| | | follow: 0, |
| | | delivery: 0, |
| | | order: 0, |
| | | shopnum: 0, |
| | | coupon: 0, |
| | | cache_topay: 0, |
| | | }, |
| | | cache: { |
| | | coupon: {}, |
| | | goods: [], |
| | | cache_coupon_select_cancel: 0, |
| | | }, |
| | | defaultaddress: {}, |
| | | addressDesc: '', |
| | | } |
| | | }, |
| | | mutations: { |
| | | updateLogin(state, provider) { |
| | | state.hasLogin = !!provider |
| | | if (!state.hasLogin) { |
| | | state.currentInfo = {} |
| | | storage.removeItem('token') |
| | | } |
| | | }, |
| | | login(state, provider) { |
| | | state.hasLogin = true |
| | | state.loginProvider = provider |
| | | }, |
| | | logout(state) { |
| | | state.hasLogin = false |
| | | state.openid = null |
| | | state.currentInfo = {} |
| | | state.appMenu = [] |
| | | storage.removeItem('token') |
| | | storage.removeItem('appMenu') |
| | | message.showToast('退出登录成功') |
| | | }, |
| | | setOpenid(state, openid) { |
| | | state.openid = openid |
| | | }, |
| | | setDefaultAddress(state, defaultaddress) { |
| | | state.defaultaddress = defaultaddress |
| | | }, |
| | | setAddressDesc(state, addressDesc) { |
| | | state.addressDesc = addressDesc |
| | | storage.setItem('defaultaddress', addressDesc || '') |
| | | }, |
| | | setTestTrue(state) { |
| | | state.testvuex = true |
| | | }, |
| | | setTestFalse(state) { |
| | | state.testvuex = false |
| | | }, |
| | | setColorIndex(state, index) { |
| | | state.colorIndex = index |
| | | }, |
| | | setActive(state, tabPage) { |
| | | state.active = tabPage |
| | | }, |
| | | setActiveOpen(state, activeOpen) { |
| | | state.activeOpen = activeOpen |
| | | }, |
| | | setMenu(state, menu) { |
| | | state.menu = menu |
| | | }, |
| | | setUniverifyLogin(state, payload) { |
| | | state.isUniverifyLogin = !!payload |
| | | }, |
| | | setUniverifyErrorMsg(state, payload = '') { |
| | | state.univerifyErrorMsg = payload |
| | | } |
| | | }, |
| | | getters: { |
| | | currentColor(state) { |
| | | return state.colorList[state.colorIndex] |
| | | } |
| | | }, |
| | | actions: { |
| | | async sign_add({ state }, key) { |
| | | state.sign[key] = 1 |
| | | }, |
| | | async sign_clear({ state }, key) { |
| | | state.sign[key] = 0 |
| | | }, |
| | | async cache_coupon_select({ state }, dto) { |
| | | state.cache.coupon = dto || {} |
| | | }, |
| | | async cache_coupon_select_cancel({ state }, val) { |
| | | state.cache.cache_coupon_select_cancel = val || 0 |
| | | }, |
| | | async cache_goods_select({ state }, goods) { |
| | | state.cache.goods = goods || [] |
| | | }, |
| | | async logout({ commit }) { |
| | | commit('logout') |
| | | }, |
| | | async getUserOpenId({ state, commit }) { |
| | | if (state.openid) return state.openid |
| | | return new Promise((resolve, reject) => { |
| | | uni.login({ |
| | | success(data) { |
| | | commit('login') |
| | | setTimeout(() => { |
| | | const openid = '123456789' |
| | | commit('setOpenid', openid) |
| | | resolve(openid) |
| | | }, 1000) |
| | | }, |
| | | fail(err) { |
| | | reject(err) |
| | | } |
| | | }) |
| | | }) |
| | | }, |
| | | async getCurrentInfo({ commit, state }) { |
| | | const currentInfo = await http.request('get', '/api/current/user', {}) |
| | | if (currentInfo && currentInfo.code === 0) { |
| | | state.currentInfo = currentInfo.data || {} |
| | | state.type = currentInfo.data.type || '' |
| | | state.spacecode = currentInfo.data.spacecode || '' |
| | | commit('updateLogin', true) |
| | | } else { |
| | | commit('updateLogin', false) |
| | | } |
| | | }, |
| | | async getSwitchSubAccount({ commit, state }, payload) { |
| | | const currentInfo = await http.request('get', '/api/supplierSub/getSwitchById', { |
| | | params: { |
| | | id: payload.id, |
| | | type: payload.type |
| | | } |
| | | }) |
| | | if (currentInfo && currentInfo.code === 0) { |
| | | state.currentInfo = currentInfo.data || {} |
| | | state.type = currentInfo.data.type || '' |
| | | state.spacecode = currentInfo.data.spacecode || '' |
| | | commit('updateLogin', true) |
| | | } else { |
| | | commit('updateLogin', false) |
| | | } |
| | | return currentInfo |
| | | }, |
| | | async getAppMenu({ state }) { |
| | | const menu = await http.request('get', '/api/app/menu/permission/menu', {}) |
| | | storage.setItem('appMenu', []) |
| | | if (menu && menu.code === 0) { |
| | | const appMenu = menu.data || [] |
| | | storage.setItem('appMenu', appMenu) |
| | | } |
| | | }, |
| | | async getAppMenuSupplier({ state }) { |
| | | const menu = await http.request('get', '/api/app/menu/supplier/permission/menu', {}) |
| | | storage.setItem('appMenu', []) |
| | | if (menu && menu.code === 0) { |
| | | const appMenu = menu.data || [] |
| | | storage.setItem('appMenu', appMenu) |
| | | } |
| | | }, |
| | | async getAppMenuPartner({ state }) { |
| | | const menu = await http.request('get', '/api/app/menu/partner/permission/menu', {}) |
| | | storage.setItem('appMenu', []) |
| | | if (menu && menu.code === 0) { |
| | | const appMenu = menu.data || [] |
| | | storage.setItem('appMenu', appMenu) |
| | | } |
| | | }, |
| | | async loginwx({ dispatch, state }, data) { |
| | | const resp = await http.request('post', '/api/login/wechat', { |
| | | data: { |
| | | code: data.code, |
| | | imgurl: data.imgurl || '', |
| | | nickname: data.nickname || '', |
| | | inviter: data.inviter || '', |
| | | phoneNumber: data.phoneNumber || '', |
| | | purePhoneNumber: data.purePhoneNumber || '', |
| | | } |
| | | }) |
| | | if (resp && resp.code === 0) { |
| | | state.isBind = true |
| | | storage.setItem('token', resp.data.access_token || '') |
| | | state.hasLogin = true |
| | | if (data.inviter) { |
| | | storage.removeItem('inviter') |
| | | storage.removeItem('inviterTime') |
| | | storage.removeItem('inviterName') |
| | | } |
| | | await dispatch('getCurrentInfo') |
| | | } else { |
| | | message.showToast('登录失败: ' + (resp && resp.msg)) |
| | | storage.removeItem('openid') |
| | | storage.removeItem('tel') |
| | | storage.removeItem('token') |
| | | } |
| | | return resp |
| | | } |
| | | } |
| | | }) |
| | | |
| | | export default store |