committed by
Gitee
1 changed files with 71 additions and 57 deletions
@ -1,62 +1,76 @@ |
|||
import Vue from 'vue' |
|||
import store from '@/store' |
|||
import Vue from 'vue'; |
|||
import store from '@/store'; |
|||
|
|||
let NEWAPIURL = process.env.NODE_ENV == 'development' ? 'https://testtaihu.xmainc.com' : 'https://testtaihu.xmainc.com' |
|||
Vue.prototype.NEWAPIURL = NEWAPIURL |
|||
// 定义 API URL
|
|||
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
|
|||
NEWAPIURL = '/api' |
|||
Vue.prototype.NEWAPIURL = '/api'; |
|||
// #endif
|
|||
|
|||
Vue.prototype.Post = (params, apiurl) => { |
|||
// if (uni.getStorageSync('userInfo')) params.token = JSON.parse(uni.getStorageSync('userInfo')).token
|
|||
if (uni.getStorageSync('userInfo') && JSON.parse(uni.getStorageSync('userInfo')).token) params.token = JSON.parse(uni.getStorageSync('userInfo')).token |
|||
else if (store.state.user.userInfo.token) params.token = store.state.user.userInfo.token |
|||
return new Promise((resolve, reject) => { |
|||
uni.showLoading({ |
|||
title: '加载中' |
|||
}) |
|||
uni.request({ |
|||
method: params.method || 'GET', |
|||
url: NEWAPIURL + apiurl, |
|||
data: params || {}, |
|||
header: params.header || { |
|||
'content-type': 'application/json', |
|||
'token': params.token || '' |
|||
}, |
|||
success: res => { |
|||
uni.hideLoading() |
|||
console.log('success', res.data) |
|||
if (res.data.code === 200) { |
|||
resolve(res.data) |
|||
} else { |
|||
setTimeout(() => { |
|||
uni.showToast({ |
|||
title: res.data.msg, |
|||
icon: 'none' |
|||
}) |
|||
reject(null) |
|||
}, 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) |
|||
} |
|||
}) |
|||
}) |
|||
} |
|||
Vue.prototype.Post = (params = {}, apiurl) => { |
|||
const token = getToken(); |
|||
if (token) { |
|||
params.token = token; |
|||
} |
|||
return new Promise((resolve, reject) => { |
|||
uni.showLoading({ |
|||
title: '加载中' |
|||
}); |
|||
uni.request({ |
|||
method: params.method || 'GET', |
|||
url: Vue.prototype.NEWAPIURL + apiurl, |
|||
data: params, |
|||
header: { |
|||
'content-type': 'application/json', |
|||
'token': token || '' |
|||
}, |
|||
success: (res) => { |
|||
console.log('success', res.data); |
|||
if (res.data.code === 200) { |
|||
resolve(res.data); |
|||
} else { |
|||
handleError(res, reject); |
|||
} |
|||
}, |
|||
fail: (err) => { |
|||
console.log('err', err); |
|||
handleError(err, reject); |
|||
} |
|||
}); |
|||
}); |
|||
}; |
|||
|
|||
export default NEWAPIURL |
|||
export default NEWAPIURL; |
|||
|
Loading…
Reference in new issue