import {
	api_config
} from "../config.js"

class ReqestConfig {
	constructor() {
		this.params = {
			'content-type': "application/json",
            timeout: 16000,
            header: {
                appId: uni.getStorageSync('storage_users').uid,
                authorization: uni.getStorageSync('storage_token')
            }
		}

	}
}

class RequestFun extends ReqestConfig {
	constructor(arg) {
		super(arg)
	}
	fetch(url, method, data, complete = null, responseType = 'text') {
        // console.log(uni.getStorageSync("xjappserver").url+':'+uni.getStorageSync("xjappserver").port)
		return new Promise((resolve, reject) => {
			uni.request({
				url: "http://"+uni.getStorageSync("xjappserver").url+':'+uni.getStorageSync("xjappserver").port + url, //
				...this.params,
				responseType: responseType,
				method: method,
				data: data,
				success: (res) => {
					try {
						if (res.data.code == 0) {
							resolve(res.data.data)
						} else if (res.data.code == 205) {
							reject(res)
						} else {
							// let value =
							// 	`参数:${JSON.stringify(data)}\n\n接口:${method}=>${api_config+url}\n\n接口报错信息:${res.data.msg}`
							// uni.showModal({
							// 	title: '开发错误提示(仅供测试使用)',
							// 	showCancel: true,
							// 	content: value,
							// 	confirmText: '复制内容',
							// 	success(resp) {
							// 		if (resp.confirm) {
							// 			uni.setClipboardData({
							// 				data: value,
							// 				success: () => {
							// 					uni.showToast({
							// 						title: '复制成功'
							// 					})
							// 				}
							// 			});
							// 		} else if (resp.cancel) {
							// 			console.log('用户点击取消');
							// 		}
							// 	}
							// });
							plus.nativeUI.toast(res.data.msg); //可定制化
							reject(res)
						}
					} catch (e) {
						console.log(e)
						plus.nativeUI.toast(res.data.message);
						reject(res)
					}

				},
				fail: (error) => {
					plus.nativeUI.toast('连接超时，请检查您的网络~');
					reject(error)
				},
				complete: complete
			});
		})

	}
}

export default RequestFun
