committed by
Gitee
1 changed files with 71 additions and 57 deletions
@ -1,62 +1,76 @@ |
|||||
import Vue from 'vue' |
import Vue from 'vue'; |
||||
import store from '@/store' |
import store from '@/store'; |
||||
|
|
||||
let NEWAPIURL = process.env.NODE_ENV == 'development' ? 'https://testtaihu.xmainc.com' : 'https://testtaihu.xmainc.com' |
// 定义 API URL
|
||||
Vue.prototype.NEWAPIURL = NEWAPIURL |
const DEV_API_URL = 'https://api.cloud.sz-trip.com'; |
||||
|
const PROD_API_URL = 'https://api.cloud.sz-trip.com'; |
||||
|
const NEWAPIURL = process.env.NODE_ENV === 'development' ? DEV_API_URL : PROD_API_URL; |
||||
|
|
||||
|
// 获取token
|
||||
|
const getToken = () => { |
||||
|
const userInfoFromStorage = uni.getStorageSync('userInfo'); |
||||
|
if (userInfoFromStorage) { |
||||
|
const userInfo = JSON.parse(userInfoFromStorage); |
||||
|
if (userInfo.token) { |
||||
|
return userInfo.token; |
||||
|
} |
||||
|
} |
||||
|
return store.state.user.userInfo.token; |
||||
|
}; |
||||
|
|
||||
|
// 定义错误处理函数
|
||||
|
const handleError = (res, reject) => { |
||||
|
uni.hideLoading(); |
||||
|
setTimeout(() => { |
||||
|
uni.showToast({ |
||||
|
title: res.data?.msg || res.msg, |
||||
|
icon: 'none' |
||||
|
}); |
||||
|
reject(res); |
||||
|
}, 0); |
||||
|
if (res.data?.code === 401) { |
||||
|
store.commit('changeLoginPath'); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
// 挂载到 Vue 原型上
|
||||
|
Vue.prototype.NEWAPIURL = NEWAPIURL; |
||||
// #ifdef H5
|
// #ifdef H5
|
||||
NEWAPIURL = '/api' |
Vue.prototype.NEWAPIURL = '/api'; |
||||
// #endif
|
// #endif
|
||||
|
|
||||
Vue.prototype.Post = (params, apiurl) => { |
Vue.prototype.Post = (params = {}, apiurl) => { |
||||
// if (uni.getStorageSync('userInfo')) params.token = JSON.parse(uni.getStorageSync('userInfo')).token
|
const token = getToken(); |
||||
if (uni.getStorageSync('userInfo') && JSON.parse(uni.getStorageSync('userInfo')).token) params.token = JSON.parse(uni.getStorageSync('userInfo')).token |
if (token) { |
||||
else if (store.state.user.userInfo.token) params.token = store.state.user.userInfo.token |
params.token = token; |
||||
return new Promise((resolve, reject) => { |
} |
||||
uni.showLoading({ |
return new Promise((resolve, reject) => { |
||||
title: '加载中' |
uni.showLoading({ |
||||
}) |
title: '加载中' |
||||
uni.request({ |
}); |
||||
method: params.method || 'GET', |
uni.request({ |
||||
url: NEWAPIURL + apiurl, |
method: params.method || 'GET', |
||||
data: params || {}, |
url: Vue.prototype.NEWAPIURL + apiurl, |
||||
header: params.header || { |
data: params, |
||||
'content-type': 'application/json', |
header: { |
||||
'token': params.token || '' |
'content-type': 'application/json', |
||||
}, |
'token': token || '' |
||||
success: res => { |
}, |
||||
uni.hideLoading() |
success: (res) => { |
||||
console.log('success', res.data) |
console.log('success', res.data); |
||||
if (res.data.code === 200) { |
if (res.data.code === 200) { |
||||
resolve(res.data) |
resolve(res.data); |
||||
} else { |
} else { |
||||
setTimeout(() => { |
handleError(res, reject); |
||||
uni.showToast({ |
} |
||||
title: res.data.msg, |
}, |
||||
icon: 'none' |
fail: (err) => { |
||||
}) |
console.log('err', err); |
||||
reject(null) |
handleError(err, reject); |
||||
}, 0) |
} |
||||
if (res.data.code === 401) { |
}); |
||||
store.commit('changeLoginPath') |
}); |
||||
} |
}; |
||||
} |
|
||||
}, |
|
||||
fail: err => { |
|
||||
console.log('err', err) |
|
||||
uni.hideLoading() |
|
||||
setTimeout(() => { |
|
||||
uni.showToast({ |
|
||||
title: err.msg || err.data.msg, |
|
||||
icon: 'none' |
|
||||
}) |
|
||||
}, 0) |
|
||||
if (err.data.code === 401) { |
|
||||
store.commit('changeLoginPath') |
|
||||
} |
|
||||
reject(err) |
|
||||
} |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
export default NEWAPIURL |
export default NEWAPIURL; |
||||
|
|
||||
Loading…
Reference in new issue