|
|
- import Vue from 'vue'
- import Vuex from 'vuex'
-
- Vue.use(Vuex); //vue的插件机制
-
- import api from '@/api/api.js'
-
- const ROLE_TYPE_AND_DESC_FIELDS_MAPPING = {
- default: 'member-personal',
- 0: 'member-personal', //个人会员
- 1: 'member-business', //企业会员
- }
-
- //Vuex.Store 构造器选项
- const store = new Vuex.Store({
- state: {
- configList: {}, //配置列表
- userInfo: {}, //用户信息
- userCenterData: {},
- vipInfo: {},
- riceInfo: {}, //用户相关信息
- payOrderProduct: [], //支付订单中的商品
- },
- getters: {
- role(state) {
- const { validTime, massageVipCombo } = state.vipInfo || {}
-
- if (!validTime || new Date().getTime() > new Date(validTime).getTime()) {
- return ''
- }
-
- const { type } = massageVipCombo || {}
-
- return ROLE_TYPE_AND_DESC_FIELDS_MAPPING[type] || ROLE_TYPE_AND_DESC_FIELDS_MAPPING.default
- },
- },
- mutations: {
- // 初始化配置
- initConfig(state) {
- api('getConfig', res => {
- const configList = {
- ...state.configList,
- }
- if (res.code == 200) {
- res.result.records.forEach(n => {
- configList[n.paramCode] = n.paramValueImage || n.paramValueText || n.paramValueArea;
- // configList[n.paramCode + '_keyValue'] = n.keyValue;
- });
- }
-
- state.configList = configList
- uni.$emit('initConfig', state.configList)
- })
-
- // let config = ['getPrivacyPolicy', 'getUserAgreement']
- // config.forEach(k => {
- // api(k, res => {
- // if (res.code == 200) {
- // state.configList[k] = res.result
- // }
- // })
- // })
- },
- login(state, config) {
- uni.showLoading({
- title: '登录中...'
- })
- uni.login({
- success(res) {
- if (res.errMsg != "login:ok") {
- return
- }
-
- let data = {
- code: res.code,
- }
-
- if (uni.getStorageSync('shareId')) {
- data.shareId = uni.getStorageSync('shareId')
- }
-
- api('wxLogin', data, res => {
-
- uni.hideLoading()
-
- if (res.code != 200) {
- return
- }
-
- Vue.set(state, 'userInfo', res.result.userInfo)
- uni.setStorageSync('token', res.result.token)
-
- if (!state.userInfo.nickName ||
- !state.userInfo.headImage ||
- !state.userInfo.phone
- ) {
- uni.navigateTo({
- url: '/pages_order/auth/wxUserInfo'
- })
- } else {
- uni.navigateBack(-1)
- }
- })
- }
- })
- },
- getUserInfo(state) {
- api('getInfo', res => {
- if (res.code == 200) {
- Vue.set(state, 'userInfo', res.result)
-
- 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'
- })
- }
- }
- })
- }
- }
- })
- },
- getUserCenterInfo(state) {
- api('getUserCenterData', res => {
- if (res.code == 200) {
- Vue.set(state, 'vipInfo', res.result?.vipInfo || {})
- Vue.set(state.userCenterData, 'score', res.result?.score || 0)
- }
- })
- },
- getRiceInfo(state) {
- // todo
-
- return
- 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 = {}
- uni.removeStorageSync('token')
-
- if(reLaunch){
- uni.reLaunch({
- url: '/pages/index/index'
- })
- }
-
- },
- // 设置支付订单中的商品
- setPayOrderProduct(state, data) {
- state.payOrderProduct = data
- },
- },
- actions: {},
- })
-
- export default store
|