import http from './http.js'
export default {
	// 请求方法
	requestEn(url, data = {}, methods) {
		return new Promise((resolve, reject) => {
			// console.log(http.webUrl + url +'---'+ uni.getStorageSync('token'))
			uni.request({
				url: http.webUrl + url,
				method: methods ? methods : 'GET',
				data: data,
				success: res => {
					if (res.data.code === 200) {
						resolve(res.data.data === undefined ? null : res.data.data)
					} else if (res.data.code === 403 ||res.data.code === 401 ) {
						uni.showToast({
							title: 'Token超时,请重新登录',
							icon: 'none'
						})
						if (uni.getStorageSync('token')) {
							uni.removeStorage({
								key: 'token',
								success: () => {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
							})
						} else {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}
					} else {
						uni.showToast({
							title: res.data.msg ? res.data.msg : '系统异常',
							icon: 'none'
						})
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络异常',
						icon: 'none'
					})
				}
			})
		})
	},
	// 请求方法
	requestFn(url, data = {}, methods) {
		return new Promise((resolve, reject) => {
			console.log(http.webUrl + url +'---'+ uni.getStorageSync('token'))
			uni.request({
				url: http.webUrl + url,
				method: methods ? methods : 'GET',
				data: data,
				header: { 
					"MAuthorization": `wxBearer ${uni.getStorageSync('token') ? uni.getStorageSync('token') : ''}`,
					"userType":'MOdBILE'
				},
				success: res => {
					console.log(res)
					
					if (res.data.code === 200) {
						resolve(res.data.data === undefined ? null : res.data.data)
					} else if (res.data.code === 403 ||res.data.code === 401 ) {
						uni.showToast({
							title: 'Token超时,请重新登录',
							icon: 'none'
						})
						if (uni.getStorageSync('token')) {
							uni.removeStorage({
								key: 'token',
								success: () => {
									uni.navigateTo({
										url: '/pages/login/login'
									})
								}
							})
						} else {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						}
					} else {
						uni.showToast({
							title: res.data.msg ? res.data.msg : '系统异常',
							icon: 'none'
						})
					}
				},
				fail: () => {
					uni.showToast({
						title: '网络异常',
						icon: 'none'
					})
				}
			})
		})
	},
 requestLo(url, data = {}, methods) {
  return new Promise((resolve, reject) => {
   console.log(http.webUrl + url)
   uni.request({
    url: http.webUrl + url,
    method: methods ? methods : 'GET',
    data: data,
    header: {
     "userType":'MOBILE'
    },
    success: res => {
     console.log(res)
     if (res.data.code === 200) {
      resolve(res.data === undefined ? null : res.data)
     } else if (res.data.code === 403) {
      uni.showToast({
       title: res.data.msg ? res.data.msg : 'Token超时,请重新登录',
       icon: 'none'
      })
      if (uni.getStorageSync('token')) {
       uni.removeStorage({
        key: 'token',
        success: () => {
         uni.navigateTo({
          url: '/pages/login/login'
         })
        }
       })
      } else {
       uni.navigateTo({
        url: '/pages/login/login'
       })
      }
     } else {
      uni.showToast({
       title: res.data.msg ? res.data.msg : '系统异常',
       icon: 'none'
      })
     }
    },
    fail: () => {
     uni.showToast({
      title: '网络异常',
      icon: 'none'
     })
    }
   })
  })
 }
}
