import Vue from 'vue' import Vuex from 'vuex' import login from '@/apis/login' import order from '@/apis/order' import user from '@/apis/user' import common from '@/apis/common' import tools from '@/common/tools' import axios from 'axios' Vue.use(Vuex); export default new Vuex.Store({ //所有的数据都放在state中 state: { TENANT_NANE: "", msgCount: 0, cartCount: 0, applyData: {}, //预约考试数据存放 currentRouter: {}, token: '', user_account: '', userInfo: null, examResult: {}, header: { serviceTel: {} },//页头配置 footer: [],//页尾配置 links: null,//友情链接 mobile:null,//移动端设置 sysTime: 0, businessItem: null, businessList: [], isDesktop: !navigator.userAgent.match( /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i ) }, getters: { businessList: state => state.businessList, businessItem: state => state.businessItem, sysTime: state => state.sysTime, userInfo: state => state.userInfo, token: state => state.token, header: state => state.header, footer: state => state.footer, links: state => state.links, mobile: state => state.mobile, getApplyData: state => state.applyData, examResult: state => state.examResult, currentRouter: state => state.currentRouter, cartCount: state => state.cartCount, msgCount: state => state.msgCount }, //操作数据,唯一的通道是mutations mutations: { setTENANT_NANE(state, id) { state.TENANT_NANE = id }, setBusinessList(state, list) { state.businessList = list }, setBusinessItem(state, item) { console.log(item) state.businessItem = item }, commonSystemTime(state, time) { state.sysTime = time; }, setCurrentRouter(state, data) { state.currentRouter = data; }, setExamResult(state, data) { state.examResult = data; }, updateApplyData(state, data) { state.applyData = data; }, setUserInfo(state, data) { state.userInfo = data; }, setHomeSetList(state, data) { data.forEach(item => { if (item.configKey === 'home.header') { state.header = JSON.parse(item.configValue) console.log(state.header) } if (item.configKey === 'home.footer') { state.footer = JSON.parse(item.configValue) console.log(state.footer) } if (item.configKey === 'home.links') { state.links = JSON.parse(item.configValue) console.log(state.links) } if (item.configKey === 'home.mobile') { state.mobile = JSON.parse(item.configValue) console.log(state.mobile) } }) }, getCartCount(state) { user.cartList().then((res) => { state.cartCount = res.total; }); }, getMsgCount(state) { user.getappinformUserlist({ systemStatusList: '1,2', receiptStatus: 0, }).then(res => { state.msgCount = res.total }) } }, //actions,可以来做异步操作,然后提交给mutations,而后再对state(数据)进行操作 actions: { findTenantId({ commit }) { return new Promise(resolve => { var urlReg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/; var urls=urlReg.exec(location.origin)[0]; console.log(urls) if (location.origin.includes("192.168.1") || location.origin.includes("120.79.166.78")) { urls = "web.xyyxt.net" } axios.get(process.env.BASE_URL + '/app/common/findTenantId?hostPc=' + urls) .then(function (response) { commit('setTENANT_NANE', response.data.data) resolve() }) .catch(function (error) { console.log("TenantIDerror:", error); }); }) }, /** * 设置系统时间 */ setSystemTime({ commit }) { return new Promise(resolve => { common.commonSystemTime().then(res => { if (res.code == 200) { commit('commonSystemTime', res.data) resolve() } }) }) }, getbusinessList({ commit }) { return new Promise(resolve => { order.orderUserAllBusinessList().then(res => { if (res.code == 200) { commit('setBusinessList', res.rows) if (res.rows && res.rows.length) { commit('setBusinessItem', res.rows[0]) } resolve() } }) }) }, /** * * @param {*} context * @returns * 获取用户信息 */ getUserInfo(context) { return new Promise(resolve => { login.getInfo({ fromPlat: 2 }).then(res => { context.commit('setUserInfo', res.data) if (!tools.getUuid()) { tools.setUuid(new Date().valueOf() + "") } resolve() }).catch(err => { }) }) }, /** * * @param {*} context * @returns * 获取首页配置 */ getCommonBaseHomeList(context) { return new Promise(resolve => { common.getCommonBaseHomeList().then(res => { context.commit('setHomeSetList', res.rows) resolve() }) }) } } })