

import {getPhpToken} from "../utils/readAppInfo"
let baseURL = 'http://testshop.hxxfb.com/'
// 请求超出时间
const timeout = 50000

// 需要修改token，和根据实际修改请求头
export function myRequestPhp(options) {
	let url = options.url;
	let method = options.method || "get";
	let data = options.data || {};
	let header = {
		'Content-Type': 'application/json;charset=UTF-8',
		// 'X-Token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDM3MTM1NSwibmJmIjoxNzQwMzcxMzU1LCJleHAiOjE3NTExNzEzNTUsImp0aSI6WzI5OCwibWVyIl19.j2f8VwdwWY4zBpcijHntzUZ9YUms2SxChaVsYf7KnPs',
		"X-Token": uni.getStorageSync('phpToken'), //token
		// "X-Token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuaHh4ZmIuY29tIiwiYXVkIjoiYXBpLmh4eGZiLmNvbSIsImlhdCI6MTc0MDU3MTQ0OCwibmJmIjoxNzQwNTcxNDQ4LCJleHAiOjE3NTEzNzE0NDgsImp0aSI6WzM1NCwibWVyIl19.PVwYv71Gq9RxTxQclUoFMgc8cZt0ju3zQkxk6SGCQJw",
		...options.header
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseURL + url,
			method: method,
			header: header,
			data: data,
			timeout,
			success:async function(response) {
				const res = response
				const message = res.data && res.data.message || ""
				// console.log(res, "resresresresres")
				// console.log(message, "99999message")
				if (res.data.status == 200) { //接口返回200走一下操作
					resolve(res)
				} else if (res.data.status == 500) { //接口返回500走一下操作
					return uni.showToast({
						title: res.data.error,
						icon: 'none'
					})
				} else if ((res.data.status == 400 || res.data.status == 40000) && (message.indexOf('登录') > -1 || message.indexOf('过期') > -1 ||  message.indexOf('token') > -1)) { //接口返回400走一下操作500走一下操作
					await  getPhpToken()
					 resolve(res)
				} else { //除了500都会走以下操作
					// console.log(message, "messag9999e")
					 uni.showToast({
						title: message,
						icon: 'none',
						duration: 6000
					})
					// reject(res)
				}
			},
			fail(err) {
				console.log(err,"接口")
				if (err.errMsg.indexOf('request:fail') !== -1) {
					uni.showToast({
						title: '网络异常',
						icon: "error",
						duration: 2000
					})
				} else {
					uni.showToast({
						title: '未知异常',
						duration: 2000
					})
				}
				uni.hideToast();
				reject(err);

			},
			complete() {
				// uni.hideToast();
			}
		});
	}).catch(() => {
		uni.hideToast();
	});
};