import { showCustomModal } from "./util";

const BASE_URL = 'http://192.168.2.14:8080';
const REQUEST_TIMEOUT = 30000;


export default function request(options) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: options.header || {
				'Content-Type': 'application/json',
			},
			success: (res) => {
				console.log(res, 'request')
				if (res.code === 200) {
					resolve(res.data);
				} else {
					reject(res);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
}

uni.addInterceptor("request", {
	invoke(args) {
		// request 触发前拼接 url
		args.url = BASE_URL + args.url
	},
	success(res) {
		const {
			code,
			msg
		} = res.data;
		console.log('addInterceptor', code, msg)
		if (code !== 200) {
			showCustomModal({
				content: msg,
			});
			// uni.navigateTo({
			// 	url: "/pages/login/login"
			// });
			return;
		}
		return Promise.resolve(res.data);
	},
	fail(err) {
		uni.showToast({
			title: "网络异常",
			icon: "error",
			duration: 2000
		});
		return Promise.reject(err);
	}
});