import config from '../config/common' // 配置文件
import handle401 from './handle401'
// import MD5 from 'md5'


export default {
	send(params) {
		let tenantId = uni.getStorageSync('OrgId').slice(0,3)
		if (!tenantId || tenantId.length < 2)
			tenantId = '0001'
		let url = params.url;
		let method = params.method || "get";
		let data = params.data || {};
		let isAll = false
		if (params.isAll)
			isAll = params.isAll
		let header = {
			'Content-Type': params.method === 'post' ? 'application/json' : 'application/json;charset=UTF-8',
			'Tenant': tenantId, // avue配置相关
			//测试对接
			// 'signature':'snJ0cOfn7aZ54nKcMQnz3tG1kL8IyN5H/FHsVyDpJxWyH2QNgZlCE+KSHQqcKR4PzZnpaqp7/awwGvleUsoFRn6pc1of6UZsCTEeWF2zG5ciHosp+jRhNGp55bLXCnqt+9bj9KU4Ca+lpHP/9B8nE6OW/9zoRkccFXxeZBSg32fBmlHlkjvi9cmTc0oQ/osdoRZ2HdiLJ6WZNXr9Uz9nUj7Du/qXvN5BZN7V8pxxdm9mgGnCMLMwz7cglB9mYyt/tKp9ur2UjO3bwFJ7ypZWWCQ9Af/dJe0Z8i9muTQptJCuXJ6kHjYaFHm1jny0rtO2pbaO7WPyYJQeke8d16yVNg==', 
			...params.header
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: config.serviceHost(url),
				method: method,
				header: header,
				data: data,
				// timeout，
				success(response) {
					const res = response
					// 根据返回的状态码做出对应的操作
					//获取成功
					if (res.statusCode == 200) {
						if (isAll)
							resolve(res);
						else
							resolve(res.data);
					} else {
						uni.clearStorageSync()
						switch (res.statusCode) {
							case 401:
								setTimeout(() => {
									uni.showToast({
										icon: 'error',
										title: '请稍后重试...',
										duration: 3000,
									})
								}, 10)

								break;
							case 404:
								setTimeout(() => {
									uni.showToast({
										icon: 'error',
										title: '请求地址不存在...',
										duration: 3000,
									})
								}, 10)

								break;
							default:
								setTimeout(() => {
									uni.showToast({
										icon: 'error',
										title: '服务器内部错误...',
										duration: 3000,
									})
								}, 10)

								break;
						}
					}
				},
				fail(err) {
					console.log(err)
					if (err.errMsg.indexOf('request:fail') !== -1) {
						setTimeout(() => {
							uni.showToast({
								title: '网络异常',
								icon: "error",
								duration: 2000
							})
						}, 10)

					} else {
						setTimeout(() => {
							uni.showToast({
								title: '未知异常',
								duration: 2000
							})
						}, 10)

					}
					reject(err);

				},
				complete() {
					// 不管成功还是失败都会执行
					// uni.hideLoading();
					uni.hideToast();
				}
			});
		}).catch(() => {});
	},
	upload(url = '', filePath) {
		const appInfoData = uni.getStorageSync('appInfo')
		const tenant = uni.getStorageSync('Tenant') || ''
		const userId = appInfoData?.User?.ID || ''
		const orgId = appInfoData?.User?.ORG_ID || ''
		return new Promise((resolve, reject) => {
			uni.showLoading()
			uni.uploadFile({
				url: url,
				filePath: filePath[0],
				name: 'file',
				formData: {
					OrgId: orgId,
					userID: userId
				},
				header: {
					Tenant: tenant,
					userid: userId
				},
				success: (res) => {
					if (res.statusCode === 200) {
						uni.hideLoading()
						resolve(JSON.parse(res.data))
						const uploadResult = JSON.parse(res.data)
						if (uploadResult.IsSuccessful) {
							resolve(uploadResult.Data)
						} else {
							reject(uploadResult.ErrorMessage)
						}
					}
				},
				fail: (err) => {
					console.log(err)
				}
			})
		})
	},

}