// store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
	  coureLogin: uni.getStorageSync('coureLogin') || 0,
	  webviewUrl: '',
	  wsDanmuUrl: 'wss://userapp.moonxiang.com', // 弹幕评论接口地址
	  //红包领取规则:
	  answerType: 1, //红包领取规则 0：完课且最后一分钟（第二次无需最后一分钟）， 1:按完课百分比答题领红包
	  isSpare:0, // 0，主要小程序，1：备选
	  uploadFile: 'https://userapp.moonxiang.com',
	imgpath: 'https://bjczwh.oss-cn-beijing.aliyuncs.com',//北京存在文化
	logoname:'食养生活',
	appid:'wx94951f52d3ac5e25',//家常食养记
	// appid:'wx3fb7ca188c55e2e8',//存梦食道养集
	// appid:'wxe9d944625a5ce247',//食养早安生活
	// appid:'wx0611e784c0309386',//存梦健食养身
	// appid:'wx1c32ab023bfd109e',//存梦清香养身心
	// appid:'wxcebb0a6606f3fbcf',//其他
  },
  getters: {
    coureLogin: (state) => state.coureLogin,
  },
  mutations: {
    setCount(state, value) {
      state.count = value;
    },
    setUserInfo(state, info) {
      state.userInfo = info;
    },
	setWebviewUrl(state, value) {
		state.webviewUrl = value;
	},
	setCoureLogin(state, payload) {
		uni.setStorageSync('coureLogin', payload);
		state.coureLogin = payload;
	}
  },
  actions: {
    fetchUser({ commit }) {
      // const res = await uni.request({ url: '/api/user' });
      commit('setUserInfo', 'noe');
    },
	getWebviewUrl({commit}) {
		var that = this;
		var data = {
			key: 'course.config'
		}
		return new Promise((resolve, reject) => {
			getConfigByKey(data).then(res => {
				if (res.code == 200) {
					console.log("getConfigByKey====", JSON.parse(res.data))
					let data = JSON.parse(res.data)
					commit('setWebviewUrl', data.userCourseAuthDomain);
					resolve()
				}
			}).catch(error => {
				reject(error)
			});
		})
	}
  }
});