import config from '@/config'
import {
	toast
} from '@/uni_modules/uview-plus'

export const http = (url, params, method) => {
	return new Promise((resolve, reject) => {
		let Authorization = uni.getStorageSync('token') || ''
		let version = uni.getStorageSync('version') || uni.getSystemInfoSync()
		let isInvest = uni.getStorageSync('isInvest') || config.isInvest;
		
		if (!uni.getStorageSync('version')) uni.setStorageSync('version', version);
		let {
			deviceId,
			deviceBrand,
			deviceModel,
			osVersion,
		} = version
		let header = {
			deviceId,
			deviceBrand,
			deviceModel,
			osVersion,
			platform: 1,
			Authorization
		}
		uni.request({
			url: config.baseUrl + url,
			data: params || {},
			params: {},
			method: method || 'POST',
			header: header,
			success: (res) => {
				// uni.hideLoading()
				if (res.statusCode == 401) {
					uni.removeStorageSync('token');
					uni.removeStorageSync('user');
					uni.hideLoading()
					uni.showModal({
						title: '提示',
						content: '为提供更好的服务，请前往登录',
						success: (res) => {
							if (res.confirm) {
								if(isInvest){
									uni.navigateTo({
										url: '/pageInvest/login/login'
									})
								}else{
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
								
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return
				}
				const data = res.data
				if (data.code == 10 && !data.success) {
					reject(data)
					return
				}
				if (data.code !== 1 && !data.success) {
					toast(data.msg)
					reject(data)
					return
				}
				resolve(data.data || null)
			},
			fail: (err) => {
				if (err.statusCode == 401) {
					uni.removeStorageSync('token');
					uni.removeStorageSync('user');
					uni.hideLoading()
					uni.showModal({
						title: '提示',
						content: '为提供更好的服务，请前往登录',
						success: (res) => {
							if (res.confirm) {
								
								
								
								if(isInvest){
									uni.navigateTo({
										url: '/pageInvest/login/login'
									})
								}else{
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
								
								
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return
				}
				const data = err.data
				if (data.code !== 1 && !data.success) {
					toast(data.msg)
					reject(data)
					return
				}
				uni.hideLoading()
				reject(err)
			}
		})
	});
};





// import {requestInterceptors,responseInterceptors} from './interceptors.js'
// // 引入luch-request
// import { http } from '@/uni_modules/uview-plus'
// //  初始化请求配置
// const initRequest=(vm)=>{
// 	http.setConfig((defaultConfig) => {
// 		/* defaultConfig 为默认全局配置 */
// 		defaultConfig.baseURL = config.baseUrl /* 根域名 */
// 		return defaultConfig
// 	})
// 	requestInterceptors()
// 	responseInterceptors()
// }
// export {
// 	initRequest
// }