import api from '../request'
import { getCode } from "@/utils/uniapi.js";
import { authType } from "@/apis/config"
import { json2query } from '@/utils'

export default {
	/**
	 * 登录
	 */
	wLogin: async (json) => {
		try {
			let data = json
			// 获取code
			if (!data) data = await loginData(data)
			let res = await api('v2/auth2/Login', {
				method: 'POST',
				data
			})
			return res
		} catch (e) {
			console.error('wLogin', e)
			throw (e)
		}

	},

	/**
	 * 解析手机号	微信
	 */
	getUserByMiniInfo: async (data) => {
		try {
			let res = await api('v2/auth2/GetUserByMiniInfo', {
				method: 'POST',
				data: {
					data: data.encryptedData,
					iv: data.iv,
					jsCode: await getCode('oauth'),
					appID: 'wx4b5930c13f8b1170'
				},
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
			})
			return res
		} catch (e) {
			console.error('getUserByMiniInfo', e)
			throw (e)
		}
	},

	/**
	 * 解析手机号 快手和抖音
	 * @param {*} sessionKey // 用户 login 返回的 sessionKey
	 * @param {*} msg 		 // 密文 encryptedData
	 * @param {*} iv 		 // 密钥 iv
	 * @param {*} loginType  // 类型 authType()
	 */
	analyzeMobile: async (data) => {
		try {
			let res = await api('v2/auth2/TiktokDecrypt', {
				method: 'POST',
				data: {
					sessionKey: data.sessionKey,
					msg: data.phone.encryptedData,
					iv: data.phone.iv,
					loginType: await authType()
				}
			})
			return res
		} catch (e) {
			console.error('analyzeMobile', e)
			throw (e)
		}
	},

	/**
	 * 绑定手机号
	 */
	addAuthBindWithMobile: async (data) => {
		try {
			let res = await api('v2/auth2/AddAuthBindWithMobile', {
				method: 'POST',
				data
			})
			return res
		} catch (e) {
			console.error('addAuthBindWithMobile', e)
			throw (e)
		}
	},

	/**
	 * 注册
	 * @param {String} authToken // login返回的临时token
	 * @param {Object} payload
	 * @param {String} payload.userID2
	 * @param {String} payload.name
	 * @param {String} payload.mobile
	 * @param {String} payload.avatar
	 */
	registerUser: async (data) => {
		try {
			let res = await api('v2/user2/RegisterUser', {
				method: 'POST',
				data
			})
			return res
		} catch (e) {
			console.error('registerUser', e)
			throw (e)
		}
	},

	/**
	 * 获取用户信息
	 */
	getSelfInfo: async () => {
		try {
			let res = await api('v2/user2/GetSelfInfo')
			return res
		} catch (e) {
			console.error('getSelfInfo', e)
			throw (e)
		}
	},

	/**
	 * 更新用户信息
	 */
	updateUserLastInfo: async (storeID, brandID) => {
		try {
			let res = await api('v2/user2/UpdateUserLastInfo', {
				method: 'POST',
				data: json2query({
					brandID,
					storeID
				})
			})
			return res
		} catch (e) {
			console.error('updateUserLastInfo', e)
			throw (e)
		}
	},

	/**
	 * 获取服务信息
	 */
	getServiceInfo: async () => {
		try {
			let res = await api('v2/cms/GetServiceInfo')
			return res
		} catch (e) {
			console.error('', e)
			throw (e)
		}
	},

	/**
	 * 获取个人推荐码
	 * data:{scene: json}
	 */
	getTiktokOrWxCode: async (json) => {
		try {
			let res = await api('v2/event/GetWeixinUnlimited', {
				method: 'POST',
				data: json
			})
			return res
		} catch (e) {
			console.error('getTiktokCode', e)
			throw (e)
		}
	},

	/**
	 * 通过商家电话号码或者商家信息
	 */
	getUsers: async (mobile) => {
		try {
			let res = await api(`v2/user2/GetUsers?userType=0&mobile=${mobile}`)
			return res
		} catch (e) {
			console.error('getUsers', e)
			throw (e)
		}
	},

	/**
	 * 聊天记录
	 */
	getImMessageRecord: async (vendorOrderID) => {
		try {
			let res = await api('v2/event/GetImMessageRecord', {
				data: {
					vendorOrderID
				}
			})
			return res
		} catch (e) {
			console.error('getImMessageRecord', e)
			throw (e)
		}
	},

	// /**
	//  * 获取临时token
	//  */
	// getTemToken: async () => {
	// 	try {
	// 		let res =await api('v2/app_need/getToken4Jxc4', {
	// 			method: 'POST',
	// 		}, 'print')
	// 		return res
	// 	}catch(e){
	// 	   console.error('', e)
	// 	   throw(e)
	// 	}
	// },

	/**
	 * 获取版本信息
	 */
	getVersionController: async (appId) => {
		try {
			let res = await api('v2/version/GetVersionController', {
				data: {
					appId
				}
			})
			return res
		} catch (e) {
			console.error('getVersionController', e)
			throw (e)
		}
	}

}

/**
 * 组合登录的参数
 */
const loginData = async (code) => {
	let data = {
		authType: authType(),
		authSecret: code ? code : await getCode('oauth')
	}
	return data
}