
// 用户数据模块
import http from '@/common/request/index'
import store from '@/common/store'
import tools from '@/common/utils/tools'
// import {
// 	bindUser,
// 	getUserInfo
// } from '@/common/request/apis/user.js';
// import {
// 	logout
// } from '@/common/request/apis/account.js';
// import {
// 	Openid,
// 	getShareImage,
// 	appShareConfig
// } from '@/common/request/apis/index';
// state 静态
const state = {
	dynamic_switch: uni.getStorageSync("dynamic_switch") || uni.getStorageSync("dynamic_switch") === 0 ? uni.getStorageSync("dynamic_switch") : 1,
	alumni_switch: uni.getStorageSync("alumni_switch") || uni.getStorageSync("alumni_switch") === 0 ? uni.getStorageSync("alumni_switch") : 1,
	product_switch: uni.getStorageSync("product_switch") || uni.getStorageSync("product_switch") === 0 ? uni.getStorageSync("product_switch") : 1,
	token: uni.getStorageSync("token") || "",
	isStart: false, // 是否开屏
	isLogin: uni.getStorageSync("isLogin") || false, // 是否登陆
	isOneLogin: 0, // 是否登陆
	userInfo: uni.getStorageSync("userInfo") || {}, // 用户信息
	userEquity: uni.getStorageSync("userEquity") || {}, // 权益信息
	imgUrl: '', //服务器图片baseUrl
	videoHistory: uni.getStorageSync("videoHistory") || [], //查找视频历史记录
	helpHistory: uni.getStorageSync("helpHistory") || [], //查找帮助历史记录
	pageParams: {},
	cangName: uni.getStorageSync('cangName') || '-',//暂时的仓库字段
}
// getters 可以获取 computer 进行动态刷新
const getters = {
	dynamic_switch: state => state.dynamic_switch,
	alumni_switch: state => state.alumni_switch,
	product_switch: state => state.product_switch,
	token: state => state.token,
	isStart: state => state.isStart,
	isLogin: state => state.isLogin,
	isOneLogin: state => state.isOneLogin,
	userInfo: state => state.userInfo,
	userEquity: state => state.userEquity,
	imgUrl: imgURL => state.imgUrl,
	videoHistory: videoHistory => state.videoHistory,
	helpHistory: helpHistory => state.helpHistory,
	pageParams: pageParams => state.pageParams,
	cangName: cangName=>state.cangName
}
// actions 异步数据  接口
const actions = {
	// 登录
	userLogin({
		commit,
		dispatch
	}, userInfo) {
		commit('setToken', userInfo.token);
		commit('setLogin', true);
		commit('setisOneLogin', 1);

		commit('setUserInfo', userInfo);
		uni.$u.toast("登录成功!");
		setTimeout(()=>{
			uni.redirectTo({
				url:'/pages/index/index'
			})
		},1500)
		// dispatch('chat/start', '', {
		// 	root: true
		// })
		// const user_id = uni.getStorageSync('user_id')
	},
	// 退出登录
	userLogout({
		commit,
		dispatch
	}) {
		// if(isPort) {
		// 	logout({}).then(res => {
		// 		if (res.code == 1) {
		// 			commit('setToken', "");
		// 			commit('setLogin', false);
		// 			commit('setisOneLogin', 0);
		// 			commit('setUserInfo', {});
		// 			uni.$u.toast(res.msg);
		// 		} else {
		// 			uni.$u.toast(res.msg);
		// 		}
		// 	}).catch(err => {
		// 		uni.$u.toast(err.msg);
		// 	})
		// }else{
			commit('setToken', "");
			commit('setLogin', false);
			commit('setisOneLogin', 0);
			commit('setUserInfo', {});
			uni.$u.toast("退出成功!");
			setTimeout(()=>{
				uni.redirectTo({
					url:'/pages/login/login'
				})
			},1500)
		// }
		
	},
	// 获取权益信息
	getUserEquity({
		commit,
		dispatch
	}) {
		getRemainNumber().then(res => {
			commit('setUserEquity', res.data);
		}).catch(err => {
			uni.$u.toast(err.msg);
			// 错误提示框
		});
	},
	// 更新用户信息
	updateUserInfo({
		commit,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			//调用登陆接口
			getUserInfo().then(res => {
				if (res.code == 1) {
					res.data.tagList = res.data.tag ? res.data.tag.split(",") : []
					commit('setUserInfo', res.data)
					resolve(res)
				} else {
					uni.$u.toast(res.msg);
					reject(res)
				}
			}).catch(err => {
				uni.$u.toast(err.msg);
				reject(err)
			})
		})
	},
	getPagePath({
		commit,
		dispatch
	}) {
		let curPage = getCurrentPages();
		let route = curPage[curPage.length - 1].route; //获取当前页面的路由
		let params = curPage[curPage.length - 1].options; //获取当前页面参数，如果有则返回参数的对象，没有参数返回空对象{}
		// 需要返回格式  /pages/XX?id=XX 打开下面的
		let query = '';
		let keys = Object.keys(params); //获取对象的key 返回对象key的数组
		if (keys.length > 0) {
			query = keys.reduce((pre, cur) => {
				return pre + cur + '=' + params[cur] + '&';
			}, '?').slice(0, -1);
		}
		return new Promise((resolve, reject) => {
			//调用登陆接口
			getShareImage({
				url: '/' + route
			}).then(res => {
				if (res.code == 1) {
					let obj = {
						pathObj: {
							url: route,
							params
						},
						shareImg: res.data,
						path:route + query
					}
					//存储信息在state中
					commit('setPageParams', obj)
					resolve(obj)
				}
			}).catch(err => {
				reject(err)
			})
		})
	},
}
//静态数据 方法 可更改数据
const mutations = {
	set_dynamic_switch(state, payload) {
		state.dynamic_switch = +payload;
		uni.setStorageSync("dynamic_switch", +payload);
	},
	set_alumni_switch(state, payload) {
		state.alumni_switch = +payload;
		uni.setStorageSync("alumni_switch", +payload);
	},
	set_product_switch(state, payload) {
		state.product_switch = +payload;
		uni.setStorageSync("product_switch", +payload);
	},
	setToken(state, payload) {
		state.token = payload;
		uni.setStorageSync("token", payload);
	},
	// 开屏动画
	setStart(state, data) {
		state.isStart = data;
	},
	// 登录态
	setLogin(state, data) {
		state.isLogin = data;
		uni.setStorageSync('isLogin', data);
	},
	// 是否第一次登录
	setisOneLogin(state, data) {
		state.isOneLogin = data;
		uni.setStorageSync('isOneLogin', data);
	},
	// 用户信息
	setUserInfo(state, data) {
		state.userInfo = data;
		uni.setStorageSync("userInfo", data);
	},
	// 权益信息
	setUserEquity(state, data) {
		state.userEquity = data;
		uni.setStorageSync("userEquity", data);
	},
	//搜索视频历史记录
	setVideoHistory(state, data) {
		if (!data) {
			console.log(data)
			// let arr=[]
			state.videoHistory = []
			uni.setStorageSync("videoHistory", state.videoHistory);
		} else {
			state.videoHistory.push(data);
			uni.setStorageSync("videoHistory", state.videoHistory);
		}
	},
	//搜索帮助历史记录
	setHelpHistory(state, data) {
		if (!data) {
			console.log(data)
			// let arr=[]
			state.helpHistory = []
			uni.setStorageSync("helpHistory", state.helpHistory);
		} else {
			state.helpHistory.push(data);
			uni.setStorageSync("helpHistory", state.helpHistory);
		}
	},
	// 缓存当前路径和分享参数
	setPageParams(state, payload) {
		state.pageParams = payload;
	},
	// 缓存仓库
	setcangName(state,payload) {
		state.cangName = payload;
		uni.setStorageSync('cangName',payload)
	}
}



export default {
	state,
	mutations,
	actions,
	getters
}