const serverHost = 'https://pupil.hchyun.com/api' // https://api-ugo-web.itheima.net'
function isOutTime(res) {
	if (res.data.message === '请先登录') {
		uni.showToast('登录信息已过期,请重新登录')
		setTimeout(() => {
			uni.redirectTo({
				url: '/pages/login/login'
			})
		}, 1000)
		uni.removeStorageSync('userInfo')
		uni.removeStorageSync('token')
	} else {
		// uni.showToast(res.data.message);
	}
}

const service = {
	get(url, data) {
		const header = {}
		// header['Authorization'] = 'Bearer ' + uni.getStorageSync('token')
		header['token'] = uni.getStorageSync('loginToken')
		header['content-type'] = 'application/json'
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'get',
				url: serverHost + url,
				data: data,
				header: header,
				success: res => {
					// // 调用接口成功
					// if (!res.data.data) {
					//   isOutTime(res)
					//   reject(res)
					// }
					resolve(res)
				},
				fail: err => {
					// 调用接口失败
					reject(err)
				}
			})
		})
	},
	post(url, data, isLogin) {
		const header = {}
		header['token'] = uni.getStorageSync('loginToken')
		header['content-type'] = 'application/json'
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'post',
				url: serverHost + url,
				data: data,
				header: header,
				timeout: 30000,
				success: res => {
					uni.hideLoading()
					// if (res.data.exception === 'UnAuthorizedException') {
					//   let pages = getCurrentPages()
					//   let page = (pages[pages.length - 1]).route
					//   if (!page.includes('login')) {
					//     uni.navigateTo({
					//       url: `/pages/login/login`
					//     })
					//   }
					// }
					// if (res.data.message) {
					//   isOutTime(res);
					//   toast.error(res.data.message)
					//   reject(res);
					// }

					// if (isLogin && !res.data.access_token) {
					//   reject(res);
					// } else {
					//   resolve(res);
					// }
					resolve(res)
				},
				fail: err => {
					console.log('err', err)
					uni.hideLoading()
					// 调用接口失败
					try {
						toast.error(err.data.message)
					} catch (errr) {
						toast.error('服务器出错')
					}
					reject(err)
				}
			})
		})
	},
	put(url, data, isLogin) {
		const header = {}
		header['token'] = uni.getStorageSync('loginToken')
		header['content-type'] = 'application/json'
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'put',
				url: serverHost + url,
				header: header,
				success: res => {
					// if (!res.data.flag) {
					// 	isOutTime(res)
					// 	reject(res)
					// }
					resolve(res)
				},
				data: data,
				fail: err => {
					// 调用接口失败
					//   toast.error(res.data.message)
					uni.hideLoading()
					reject(err)
				}
			})
		})
	},
	putWithFormData(url, data, isLogin) {
		const header = {}
		header['token'] = uni.getStorageSync('loginToken')
		header['content-type'] = 'application/x-www-form-urlencoded'
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'put',
				url: serverHost + url,
				header: header,
				success: res => {
					// if (!res.data.flag) {
					// 	isOutTime(res)
					// 	reject(res)
					// }
					resolve(res)
				},
				data: data,
				fail: err => {
					// 调用接口失败
					//   toast.error(res.data.message)
					uni.hideLoading()
					reject(err)
				}
			})
		})
	},
	delete(url, data, isLogin) {
		const header = {}
		header['token'] = uni.getStorageSync('loginToken')
		header['content-type'] = 'application/json'
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'delete',
				url: serverHost + url,
				data: data,
				header: header,
				success: res => {
					resolve(res)
				},
				fail: err => {
					// 调用接口失败
					//   toast.error(res.data.message)
					uni.hideLoading()
					reject(err)
				}
			})
		})
	}
}
const toastDuration = 1500
const toast = {
	// duration: 1500,
	success: (text, dur) => {
		// $wuxToptips().success({
		//   hidden: false,
		//   text: text || '成功',
		//   duration: dur || toastDuration
		// });
		uni.showToast({
			title: text || '成功',
			duration: dur || toastDuration
		})
	},
	warn: text => {
		// $wuxToptips().warn({
		//   hidden: false,
		//   text: text || '未知警告',
		//   duration: toastDuration
		// });
		uni.showToast({
			icon: 'none',
			title: text || '未知警告',
			duration: toastDuration
		})
	},
	error: text => {
		// $wuxToptips().error({
		//   hidden: false,
		//   text: text || '未知错误',
		//   duration: toastDuration
		// });
		uni.showToast({
			icon: 'none',
			title: text || '未知错误',
			duration: toastDuration
		})
	}
}
const apiService = {
  uploadImgUrl: serverHost + `/upload/image/`,
	imgUrl: serverHost,
	// login: data => {
	// 	data = Object.assign(data || {}, {})
	// 	const url = `/api`
	// 	return new Promise((resolve, reject) => {
	// 		resolve(service.post(url, data, true))
	// 	})
	// },
	//登录接口
	login(data) {
		const url = `/login/mnpLogin`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	// 获取首页轮播图
	getBanner(data) {
		const url = `/home/banner`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, data))
		})
	},
	// 获取产品列表
	getProducts(data) {
		const url = `/home/product`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, data))
		})
	},
	// 获取产品详情
	getProductDetail(id) {
		const url = `/home/pdetail/?productId=${id}`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, id))
		})
	},
	// 获取行业新闻
	getNews() {
		const url = `/news/nlists/?pageSize=50&pageNum=1`
		return new Promise((resolve, reject) => {
			resolve(service.get(url))
		})
	},
	// 获取行业新闻详情
	getNewsDetail(id) {
		const url = `/news/ndetail?newsId=${id}`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, id))
		})
	},
	// 获取认证厂家列表
	getCertifiedCj() {
		const url = `/news/flists?pageSize=50&pageNum=1`
		return new Promise((resolve, reject) => {
			resolve(service.get(url))
		})
	},
	// 获取认证厂家详情
	getCertifiedCjDetail(id) {
		const url = `/news/fdetail?fId=${id}`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, id))
		})
	},
	//绑定公司和升级VIP
	bindComponyAndUpdate(data) {
		const url = `/center/update`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	//获取历史记录或收藏
	getHistoryOrCollection(type) {
		const url = `/center/favlist/?type=${type}`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, type))
		})
	},
	//删除历史记录或收藏
	deleteHistoryOrCollection(id) {
		const url = `/center/delfav/?id=${id}`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, id))
		})
	},
	//获取创意发布&需求发布
	getIdeasAndNeeds(type) {
		const url = `/center/rlist?type=${type}`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, type))
		})
	},
	//删除创意发布&需求发布
	deleteIdeasAndNeeds(id) {
		const url = `/center/delreq/?id=${id}`
		return new Promise((resolve, reject) => {
			resolve(service.delete(url, id))
		})
	},
	//收藏按钮
	collect(pid) {
		const url = `/center/addfav/?pid=${pid}`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, pid))
		})
	},
	//点赞按钮
	thumb(pid) {
		const url = `/center/thumb/?pid=${pid}`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, pid))
		})
	},
	//获取个人信息
	getUser() {
		const url = `/center/userinfo`
		return new Promise((resolve, reject) => {
			resolve(service.get(url))
		})
	},
	//修改个人信息
	updateUser(data) {
		const url = `/center/userupdate`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	// 获取首页分类Tag
	getTags(data) {
		const url = `/home/tags`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, data))
		})
	},
	// 获取首页分类
	getCategories(data) {
		const url = `/home/classlist`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, data))
		})
	},
	// 搜索发现   热搜
	getDiscover() {
		const url = `/home/discover`
		return new Promise((resolve, reject) => {
			resolve(service.get(url))
		})
	},
	// 搜索产品
	getProductList(data) {
		const url = `/home/product`
		return new Promise((resolve, reject) => {
			resolve(service.get(url, data))
		})
	},
	// 图片基本地址
	getImgUrl() {
		const url = `/center/cfg?key=img_url`
		return new Promise((resolve, reject) => {
			resolve(service.get(url))
		})
	},
	// 新增
	addOutboundOrder(data) {
		const url = `/api`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	// 提交
	deliveryOrderConfirm(data) {
		const url = `/api`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	// 提交
	submitIdeasAndNeeds(data) {
		let url = `/require/release/`
		if (data.id) {
			url = `/require/update/`
		}
		return new Promise((resolve, reject) => {
			resolve(service.putWithFormData(url, data))
		})
	},
	getIdeasAndNeedsFormdata(data) {
		const url = `/require/formdata/`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	postMobileByMnp(data) {
		const url = `/user/getMobileByMnp`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	postImage(data) {
		const url = `/upload/image/`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, data))
		})
	},
	// 解密
	decrypt(data) {
		const url = `/api`
		return new Promise((resolve, reject) => {
			resolve(service.post(url, {
				data
			}))
		})
	},
	// 国密sm4加密
	sm4Encrypt(data) {
		const url = `/api`
		return new Promise((resolve, reject) => {
			resolve(service.sm(url, data))
		})
	}
}
export {
	apiService,
	toast
}
