import Vue from 'vue' import Vuex from 'vuex' import utils from '../utils/utils.js' import cartNum from '@/store/modules/cartNum.js' Vue.use(Vuex); //vue的插件机制 import api from '@/api/api.js' //Vuex.Store 构造器选项 const store = new Vuex.Store({ modules: { cartNum }, state: { configList: {}, //配置列表 userInfo: {}, //用户信息 levelInfo: {}, //团员等级信息 cartData: [], //购物车数据 riceInfo: {}, //用户相关信息 category: [], //分类信息 payOrderProduct: [], //支付订单中的商品 promotionUrl : '',//分享二维码 couponData: {} //选中的优惠券数据 }, getters: {}, mutations: { // 初始化配置 initConfig(state) { api('getConfig', res => { const configList = { ...state.configList, } if (res.code == 200) { res.result.records.forEach(n => { state.configList[n.paramCode] = n.paramValueText || n.paramValue || n.paramValueImage || n.paramValueArea }); console.log('configList', state.configList); } // state.configList = configList uni.$emit('initConfig', state.configList) }) }, login(state, config = {}) { uni.showLoading({ title: '登录中...' }) uni.login({ success : res => { if (res.errMsg != "login:ok") { return } let data = { code: res.code, } // 如果通过分享者链接进入小程序时,会将分享者ID存储在本地 if (uni.getStorageSync('shareId')) { data.shareId = uni.getStorageSync('shareId') } api('wxLogin', data, res => { uni.hideLoading() if (res.code != 200) { return } state.userInfo = res.result.userInfo uni.setStorageSync('token', res.result.token) if(config.path){ let path = config.path delete config.path delete config.shareId let para = utils.objectToUrlParams(config) uni.reLaunch({ url: `${path}?${para}`, }) return } if (!state.userInfo.nickName || !state.userInfo.headImage || !state.userInfo.phone ) { uni.navigateTo({ url: '/pages_order/auth/wxUserInfo' }) } else { // 直接登录成功 store.commit('getUserInfo') utils.navigateBack(-1) } }) } }) }, getUserInfo(state) { api('getUserCenterData', res => { if (res.code == 200) { state.userInfo = res.result.member state.levelInfo = res.result.level // console.log(state.levelInfo); if (!state.userInfo.nickName || !state.userInfo.headImage || !state.userInfo.phone ) { uni.showModal({ title: '申请获取您的信息!', cancelText: '稍后补全', confirmText: '现在补全', success(e) { if (e.confirm) { uni.navigateTo({ url: '/pages_order/auth/wxUserInfo' }) } } }) } } }) }, getRiceInfo(state) { api('getRiceInfo', { token: uni.getStorageSync('token') || '' }, res => { if (res.code == 200) { state.riceInfo = res.result } }) }, // 退出登录 logout(state, reLaunch = false) { // uni.showModal({ // title: '确认退出登录吗', // success(r) { // if (r.confirm) { // state.userInfo = {} // uni.removeStorageSync('token') // uni.reLaunch({ // url: '/pages/index/index' // }) // } // } // }) state.userInfo = {} state.levelInfo = {} uni.removeStorageSync('token') if(reLaunch){ uni.reLaunch({ url: '/pages/index/index' }) } }, getQrCode(state) { let that = this; // 注释掉登录检查 if(!uni.getStorageSync('token')){ return } uni.getImageInfo({ src: `${Vue.prototype.$config.baseUrl}/info_common/getInviteCode?token=${uni.getStorageSync('token')}`, success : res => { that.commit('setPromotionUrl', res.path) }, fail : err => { } }) }, // 查询分类接口 getCategoryList(state, fn) { api('queryCategoryList', { pageNo: 1, pageSize: 9999, }, res => { if (res.code == 200) { state.category = res.result.records fn && fn(state.category) } }) }, // 设置支付订单中的商品 setPayOrderProduct(state, data) { state.payOrderProduct = data }, setPromotionUrl(state, data){ state.promotionUrl = data }, // 设置购物车数据 setCartData(state, data){ console.log('data', data) state.cartData = data }, // 检查绑定团长与否 checkBindLeader(){ api('queryMyLeader', {}, res => { if(res.code == 500 || !res.result) { uni.showModal({ title: '提示', content: '您还未绑定团长,请先绑定团长', confirmText: '去绑定', showCancel: false, success: (res) => { if (res.confirm) { uni.navigateTo({ url: '/pages_order/mine/unbindTeam' }) } } }) }else { return res.result } }) }, // 设置选中的优惠券数据 setCouponData(state, data){ state.couponData = data }, // 清除选中的优惠卷数据 clearCouponData(state){ state.couponData = {} } }, actions: {}, }) export default store