import {
	_login,
	_queryUser,
	_refreshToken
} from '../api/UserApi'
import {
	_query,
	_getters,
	_dispatch
} from './BaseService'
import Cache from '@/cache'

const service = {
	/**
	 * 解析为本地使用格式
	 * @param goods
	 */
	parseL: (goods = {}) => {
	  return goods;
	},
	cacheAppUser: (appUser) => Cache.setSync(Cache.$Keys.app.APP_USER, appUser),				// 缓存app信息
	cacheWxInfo: (wxInfo) => Cache.setSync(Cache.$Keys.app.WX_INFO, wxInfo),					// 缓存微信信息
	cacheUserInfo: (userInfo) => Cache.setSync(Cache.$Keys.app.USER_INFO, userInfo),			// 缓存用户信息
	getCacheAppUser: () => Cache.getSync(Cache.$Keys.app.APP_USER),								// 获取缓存中的app信息
	getCacheWxInfo: () => Cache.getSync(Cache.$Keys.app.wxInfo),								// 获取缓存中的微信信息
	getCacheUserInfo: () => Cache.getSync(Cache.$Keys.app.USER_INFO),							// 获取缓存中的用户信息
	setAppUser: (appUser = {}) => _dispatch('setAppUser', {
		openid: appUser.openId,
		token: appUser.token,
		tokenHead: appUser.tokenHead,
		userid: appUser.memberId,
		currentTime: new Date().getTime()
	}),																							// 保存aap信息到store中
	setToken: (token = null) => _dispatch('setToken', token),									// 设置新Token到store中
	setUserInfo: (user = {}) => _dispatch('setUserInfo', user),									// 保存用户信息到store中
	setWxInfo: (wxInfo = {}) => _dispatch('setWxInfo', wxInfo), 								// 保存微信信息到store中
	getAppUser: () => _getters('appUser', {}), 													// 获取store中的app信息
	getWxInfo: () => _getters('wxInfo', {}), 													// 获取store中的微信信息
	getUserInfo: () => _getters('userInfo', {}),												// 获取store中的用户信息
	/**
	 * login			登录
	 * queryUser		查询用户信息
	 * refreshToken		刷新用户Token
	 */
	login: (user = {}) => _query({api: _login, params: user, callBack: (data = {}) => {
		service.setAppUser(data);
		service.cacheAppUser(service.getAppUser());
		service.setUserInfo(data);
		service.cacheUserInfo(service.getUserInfo());
	}}),
}

export default service

// 接口解读
/**
 * 	登录接口
 * url:'/lite/index'
 * query：{s: 'Init.init',code: ,nickname: ,gender:,portrait: ,encryptedData:,iv:,promoter_id:machine_id:}
 */