You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import $config from '../config' import $util from './util'
export default function http(url, param = {}, extraConfig) { //默认配置
let defaultConfig = { token: true, //携带令牌
loading: true, //显示加载
mock: false, //模拟数据
} //请求配置
let httpConfig = { ...defaultConfig, ...extraConfig } httpConfig.mock ? httpConfig.config = $config.mock : httpConfig.config = $config[process.env.NODE_ENV] //携带令牌
let token = uni.getStorageSync('token') if (token) { param.token = token } else if (httpConfig.token) { uni.showModal({ title: '提示', content: '登录后才能操作哦~', showCancel: true, confirmText: '去登录', success: (e) => { if (e.confirm) this.$util.toast('点击登录') } }) return Promise.reject('未登录') } //显示加载
httpConfig.loading && $util.loading('努力加载中') //进行请求
let baseUrl = httpConfig.config.baseUrl let header = { 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': uni.getStorageSync('cookie') ? `foxphp_ok=${uni.getStorageSync('cookie')}` : '', } return new Promise((resolve, reject) => { uni.request({ url: `${baseUrl}/${url}`, data: param, header: header, method: 'POST', // method: Object.keys(param).length ? 'POST' : 'GET',
success: (res) => { uni.setStorageSync('cookie', res.data.foxphp_ok) httpConfig.loading && uni.hideLoading() switch (res.statusCode) { case 200: if (res.data.code == 1) { //正常请求
resolve(res.data) } else { //其他状况
if(!res.data.msg=='notask'){ res.data.msg && $util.toast(res.data.msg) } resolve(res.data) } break case 403: $util.toast('服务器拒绝') reject(JSON.stringify(res)) break case 404: $util.toast('资源不存在') reject(JSON.stringify(res)) break case 500: $util.toast('服务器异常') reject(JSON.stringify(res)) break default: $util.toast('未知错误码') reject(JSON.stringify(res)) } }, fail: (err) => { httpConfig.loading && uni.hideLoading() $util.toast('网络异常') reject(JSON.stringify(err)) } }) }) }
|