
import globalConfig from '@/config/global.config.js'
import { useUserStore } from '@/store/useUserStore.js'
import { useAppStore } from '@/store/useAppStore'
import { useLangStore } from '@/store/useLangStore'


// http状态吗
const HttpStatus = {
	SUCCESS: 200,
	CLIENTERROR: 400,
	AUTHENTICATE: 401,
	FORBIDDEN: 403,
	NOTFOUND: 404,
	SERVERERROR: 500,
	BADGATEWAY: 502,
	SERVICEUNAVAILABLE: 503,
	GATEWAYTIMEOUT: 504
}

// 后台返回状态码
const HttpResponseCode = {
	SUCCESS: "200",
	TOKENERROR: "401",
	TOKENERROR2: "-1",
	SERVERERROR: "500"
}

// 请求返回log
const HttpRequestLog = (url, data, res) => {
	// console.log('request', url, data)
	// console.log('response:', res)
}

// 错误提示弹窗
const ShowErrorMessage = (message) => {
	if (!message) { return }
	setTimeout(() => {
		uni.showToast({
			title: message,
			icon: 'none',
			duration: 3000
		})
	})
}

// 请求头处理
const HttpRequestHeader = (option) => {
	if (option.isClearHeader) {
		return {}
	}
	const header = {
	}
	return Object.assign({}, header, option.header)
}

// 请求参数处理
const HttpRequestData = (option) => {
	if (Array.isArray(option.data)) {
		return option.data
	}

	const userStore = useUserStore()
	const appStore = useAppStore()
	const langStore = useLangStore()

	const commonData = {
		appId: globalConfig.appId,
		deviceNumber: appStore.deviceId,
		androidId: appStore.androidId,
		locale: langStore.requestLocale,
		// appPackageName: `com.luck.h5.${globalConfig.appId}`,
		appPackageName: appStore.packageName,
		h5No: isNaN(appStore.h5No) ? 1 : Number(appStore.h5No),
		// adid: appStore.adjustAdid,
		channel: appStore.channelId,
		deviceType: appStore.isNative ? 3 : 1
	}
	if (userStore.isLogin) {
		commonData.sign = userStore.sign
		commonData.uid = userStore.uid
	}
	return Object.assign({}, commonData, option.data)
}

export {
	HttpStatus,
	HttpResponseCode,
	HttpRequestLog,
	ShowErrorMessage,
	HttpRequestHeader,
	HttpRequestData
}
