import http from './index.js';
import {
	serverUrl,
	apiUrl,
	fileUrl,
	accessKey,
	userId
} from '@/config.js';
import {
	ACCESS_TOKEN,
	USER_ID,
	USER_INFO
} from '@/store/mutation-types'
import store from '@/store'
import storage from '@/utils/storage'

// 免密登录
export const getToken = () => http.get(apiUrl + '/token/' + userId + '/' + accessKey + '/').then(result => {
	console.log("免密登录: ", result);
	const {
		access_token,
		user
	} = result.data

	// 过期时间30天
	const expiryTime = 30 * 86400
	// 保存tokne和userId到缓存
	storage.set(USER_ID, user.userId, expiryTime)
	storage.set(USER_INFO, user, expiryTime)
	storage.set(ACCESS_TOKEN, access_token, expiryTime)
	store.dispatch('setToken', access_token)
	store.dispatch('setUserId', user.userId)
})

// 获取图形验证码
export const getCaptcha = (params) => http.get('/captcha', {
	params
})

// 发送短信验证码
export const sendSmsCaptcha = (params) => http.post(serverUrl + '/open/sendSmsCaptcha', {
	params
})

// 获取微信openId
export const getWxOpenId = (data) => http.post(serverUrl + '/wx-login/getWxOpenId', data)

// 支付宝授权码换取userId
export const getAuthCode = (data) => http.post('/open/login-alipay/getAuthCode', data)

// 微信手机号码登录
export const loginMpWxMobile = (data) => http.post(serverUrl + '/wx-login/loginByMpWxPhone', data)

// 获取支付宝手机号码
export const getPhoneNumber = (data, config) => http.post('/shop/payment/getPhoneNumber', data, config)

// 支付宝登录
export const alipayLogin = (data) => http.post('/login-alipay/login', data)

// 支付宝用户注册
export const register = (data) => http.post('/login-alipay/register', data)

// 游客登录
export const login = (data) => http.post('/login', data)

export default {
	getToken,
	getWxOpenId,
	getCaptcha,
	sendSmsCaptcha,
	getAuthCode,
	getPhoneNumber,
	alipayLogin,
	login
}
