import Vue from 'vue'
import Vuex from 'vuex'
import { checkToken,User } from '@/config/api.js'
import { set } from '@/config/db.js';//缓存操作

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
	},
   mutations: {
		
	},
	actions: {
		checkHandle(type,payload) {
			return new Promise((resolve,reject) => {
				checkToken().then(res => {
					if(res.Ret === 200) {
						resolve(res.Data)
					}else if(res.Ret === 408) {
						/* 无token或token过期 */
						uni.login({
						   success: result=> {
								User.wechatLog({
									Code:result.code
								}).then(res => {
									let token = res.Data.Authorization;
									set('access_token',token);
									let page = getCurrentPages()[0];
									if(payload=='load'){
										page.onLoad();
									}else {
										page.onShow();
									}
								})
						  }
						});
					}
				}).catch(err => {
					reject(err)
				})
			})
		},
		//获取用户的pv/uv
		statistics(type,payload){
			// return new Promise ((resolve,reject)=>{
			// 	User.pageHistory(payload).then(res => {
			// 		if(res.Ret === 200){
			// 			resolve(res.Data)
			// 		}
			// 	}).catch(err => {
			// 		reject(err)
			// 	})
			// })
		}
	},
	getters: {
	}
})

export default store
