/* 
	http 请求工具方法
 */
const querystring = require('querystring');
const Signture = require('crypto')
import { baseUrl } from '@/config/baseConfig.js'
import msgUtils from '@/utils/msgUtils.js'

// import store from '@/store/index.js'

/* 
 * 格式化http url
 * @params {String} url 请求地址
 * @params {String} nameSpace 请求api的空间
 */
export function httpUrlFormat (url, nameSpace = 'exchange') {
	return baseUrl(nameSpace) + url
}

/* 
	发起异步请求
 */
export function ajaxSync(config) {
	return new Promise(function(resolve, reject) {
		const success = config.success;
		const error = config.error;
		// 发起请求
		uni.request(Object.assign(config, {
			success(data) {
				if(data.header.authorization){
					uni.setStorageSync('box-token',data.header.authorization)
				}
				// 401 未登录 或token已过期
				// 403 没有相关权限
				// 10000 登录失败
				if(data.data.code === 11000 || data.data.code === 401){
					msgUtils.msg(data.data.message);
					uni.navigateTo({
						url:`/pages/login/login?type=back`
					})
				}else if(data.data.code === 10001){
					msgUtils.msg(data.data.message);
					uni.reLaunch({
						url:`/pages/login/past-due`
					})
				}else if(data.data.code === 11010){
					msgUtils.msg(data.data.message);
					uni.reLaunch({
						url:`/pages/login/shut-down`
					})
				}
				if(data.data.code === 403){
					msgUtils.msg(data.data.message);
				}
				if(data.data.code === 10000){
					msgUtils.msg(data.data.message);
				}
				
				// if(data.data.code === 401){
				// 	msgUtils.msg("请登录");
				// 	uni.navigateTo({
				// 		url:`/pages/login/login?type='back'`
				// 	})
				// }
				success && success(data.data);
				resolve(data.data);
			},
			fail(data) {
				// console.log(data)
				// config.showTips && msgUtils.msg(data.data.msg);
				error && error(data.data);
				reject(data.data);
			}
		}));
	});
}

/* 
	发起row格式数据请求
 */
export function ajaxForRawData(config) {
	config.header = Object.assign(config.header || {}, {
		// 'Content-Type': 'application/json; charset=UTF-8'
		'Content-Type': 'application/x-www-form-urlencoded'
	});

	if (typeof config.data === "object") {
		config.data = JSON.stringify(config.data);
	}
	return ajaxSync(config);
};

/* 
	发起需要携带校验凭证的请求：oauthAccessToken
 */
function getAuthorization(url, type, param) {
	try {
		// console.log("uni.getStorageSync('box-token'):",uni.getStorageSync('box-token'))
		// return `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTc2ODQ3OTg2MDAxMjk1MzYwMSwibmlja25hbWUiOiLojonov7jmjqPnjJznl4UiLCJ1c2VybmFtZSI6IjE1MjgwMTE2MzgzIiwicGFzc3dvcmQiOiJxd2VyMTIzNDUiLCJzdGF0dXMiOjF9.ahpZGI8CdmPYYkywF62tNrpGniVrYKSWWh1XXfISUN8`
		return `${uni.getStorageSync('box-token')}`
	} catch {
		// 跳转登录页
		// uni.navigateTo({
		// 	url: '/pages/login/login?type=back'
		// });
	}
}

export function ajaxCarryAuthorization(config) {
	config.header = Object.assign(config.header || {}, {
		Authorization: getAuthorization(config.url, config.method, config.data)
	});
	if (config.isRawData) {
		return ajaxForRawData(config);
	}
	return ajaxSync(config);
}