import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); //vue的插件机制 import api from '@/api/api.js' //Vuex.Store 构造器选项 const store = new Vuex.Store({ state: { configList: { phone: "", preferential: 1, price: 6.66, privacyAgreement: "隐私政策", title: "#95", userAgreement: "用户协议", wx: "", }, //配置对象 userInfo : {}, gasStationList : [], gasStation : {}, }, getters: { getConfig(state){ return state.configList } }, mutations: { // 初始化配置 initConfig(state) { // let config = ['preferential', 'wx', 'phone', 'price', 'title'] // config.forEach(k => { // api('getConfig', { // keyValue : k // }, res => { // if (res.code == 200) { // state.configList[k] = res.result // } // }) // }) let apiConfig = [ 'getPrivacyPolicy' , 'getUserAgreement' ] //需要访问不同接口才能得到的配置数据 let key = ['privacyAgreement','userAgreement'] apiConfig.forEach((item,index) => { api(item,res => { state.configList[key[index]] = res.result }) }) }, twogetConfig(state){ api('twogetConfig', { shopId : state.gasStation.id }, res => { if (res.code == 200) { ['preferential', 'wx', 'phone', 'price', 'title'] .forEach(n => { state.configList[n] = res.result[n] }) } }) }, //获取加油站列表 getGasStationList(state){ api('getGasStationList', { pageNo : 1, pageSize : 9999999 }, res => { if(res.code == 200){ state.gasStationList = res.result.records } }) }, setGasStation(state, data){ state.gasStation = data this.commit('twogetConfig') }, setGasStationList(state, data){ state.gasStationList = data }, login(state) { uni.showLoading({ title:"登录中..." }) uni.login({ success(res) { if (res.errMsg != "login:ok") { return } api('loginLogin', { code: res.code }, res => { uni.hideLoading() if (res.code != 200) { return } state.userInfo = res.result.userInfo uni.setStorageSync('token', res.result.token) if (!state.userInfo.nickName || !state.userInfo.headImage) { uni.navigateTo({ url: '/pages/login/wxUserInfo' }) }else{ uni.switchTab({ url: '/pages/payment/payment' }) } }) }, fail(err) { console.error(err) uni.hideLoading() } }) }, getUserInfo(state) { api('infoGetInfo', res => { if(res.code == 200){ state.userInfo = res.result } }) }, }, actions: {}, }) export default store