// import store from '../store'
import {
	handleLoginFailure
} from '@/utils'
import {
	baseImagePath
} from '@/config'
// wx2527c0aabd4401d4
const app = getApp();
const baseUrl = "https://api.biaodianfuhao.net"
// const baseUrl = "http://192.168.1.14:8080" //chang
// const baseUrl = "http://192.168.31.193:8000"
// const baseUrl = "http://rjding.natappfree.cc"
const defaultOpt = {
	login: true
}
const baseRequest = (options) => {
	let showLoading = options.loading;
	const token = app.globalData.requestToken;
	// const token ="eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjE2NzM2MTQ5MDQwOTI3ODY2ODksImFjY291bnQiOiIxMzI1MjgwNjY2MCIsInV1aWQiOiI4MTc3NDg0OS1kZDk0LTRiZmMtOTRmNy1kZTJlYWQ4OWMwOTEiLCJyZW1lbWJlck1lIjp0cnVlLCJleHBpcmF0aW9uRGF0ZSI6MTY5MDQ0NDY5MTcyMCwiY2FUb2tlbiI6bnVsbCwib3RoZXJzIjpudWxsLCJzdWIiOiIxNjczNjE0OTA0MDkyNzg2Njg5IiwiaWF0IjoxNjg4NjkxNjU5LCJleHAiOjE2OTA0NDQ2OTF9.qGR9O5AHgnzlkeDkyWJOFm5p6mdzAaUk1LOIQ2lTeAxSqL1yRsZFhYu2CoBDp-k3UMmvqGcClNDNpficDjpTeg"
	// const token =
	// 	"eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjE1NjIyNTk1MTQ2ODA0NTUxNjksImFjY291bnQiOiIxNTE2NDAzNTkxMCIsInV1aWQiOiJkMGI2YTI1Yy1jY2M1LTRiZTYtOGNhMC1jMWNmY2E5ZTRjMmEiLCJyZW1lbWJlck1lIjp0cnVlLCJleHBpcmF0aW9uRGF0ZSI6MTY4OTQwODAzOTcyOCwiY2FUb2tlbiI6bnVsbCwib3RoZXJzIjpudWxsLCJzdWIiOiIxNTYyMjU5NTE0NjgwNDU1MTY5IiwiaWF0IjoxNjg3NjU1MDA3LCJleHAiOjE2ODk0MDgwMzl9.12ZiVUyjFGhXCOlsx_927RrQVYG43Ke7Gk4-peYzpJkLDKLBW0XATWcxL-wCSYRaAgXEr6GqyR0jhDOHfc1eqA"
	// 合并传参过来的 headers
	// 如果接口需要登录，携带 token 去请求
	options.header = {
		...options.header,
	}
	options.url = baseUrl + options.url;
	// console.log('请求参数=======>',options.url,options.data);
	if (options.login === true) {
		options.header = {
			...options.header,
			token: token

		}
	}
	// console.log(options);
	return new Promise((resolve, reject) => {
		if (showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		// console.log(options.data);
		uni.request({
			url: options.url,
			method: options.method,
			data: options.data || options.params,
			header: options.header,
			success(res) {
				// console.log('url='+options.url,'请求响应',res);
				uni.getNetworkType({
					success: function(res) {
						showLoading = false;
						if (res.networkType == 'none') {
							uni.showToast({
								icon: 'none',
								title: '接口请求出错，请检查手机网络',
								success(res) {}
							});
						}
					}
				});
				if (res.data.code != "00000") {
					if (res.data.code == "A0104") {
						uni.showToast({
							icon: 'none',
							title: res.data.message + '',
							success() {},
							complete() {

							}
						});
						reject(res.data.message);
					} else if (res.data.code == "00001") {
						uni.showToast({
							icon: 'none',
							title: res.data.data,
							success() {},
							complete() {

							}
						});
						reject(res.data);
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.message + '',
							success() {},
							complete() {

							}
						});
						reject(res.data.message);
					}
				}
				resolve(res.data);
			},
			fail(error) {
				uni.getNetworkType({
					success: function(res) {
						if (res.networkType == 'none') {
							uni.showToast({
								icon: 'none',
								title: '接口请求出错，请检查手机网络',
								success(res) {}
							});
						}
					}
				});
				console.log(error);
				uni.showToast({
					icon: 'none',
					title: '接口请求出错：' + error.errMsg,
					success(res) {}
				});
				reject(error);
			},
			complete(res) {
				uni.hideLoading();
			}
		});
	});
};
/**
 * http 请求基础类
 * 参考文档 https://www.kancloud.cn/yunye/axios/234845
 *
 */
const request = ['post', 'put', 'patch'].reduce((request, method) => {
	/**
	 *
	 * @param url string 接口地址
	 * @param data object get参数
	 * @param options object axios 配置项
	 * @returns {AxiosPromise}
	 */
	request[method] = (url, data = {}, options = {}) => {
		return baseRequest(Object.assign({
			url,
			data,
			method
		}, defaultOpt, options))
	}
	return request
}, {});

['get', 'delete', 'head'].forEach(method => {
	/**
	 *
	 * @param url string 接口地址
	 * @param params object get参数
	 * @param options object axios 配置项
	 * @returns {AxiosPromise}
	 */
	request[method] = (url, params = {}, options = {}) => {
		return baseRequest(Object.assign({
			url,
			params,
			method
		}, defaultOpt, options))
	}
})

export default request