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.
97 lines
2.7 KiB
97 lines
2.7 KiB
import Request from '@/js_sdk/luch-request/luch-request/index.js'
|
|
import {API} from '@/config'
|
|
import {getUserInfo, clearUserInfo} from "@/util/user";
|
|
|
|
const http = new Request();
|
|
http.config.baseURL = API
|
|
|
|
http.interceptors.request.use((config) => {
|
|
// uni.showLoading({title: '加载中'})
|
|
const token = getUserInfo().token
|
|
if (token && token !== 'undefined') {
|
|
config.header.Authorization = token
|
|
}
|
|
config.header.tenantId = 10049
|
|
return config
|
|
}, config => {
|
|
return Promise.reject(config)
|
|
})
|
|
|
|
http.interceptors.response.use((response) => {
|
|
const url = response.config.url
|
|
// uni.hideLoading()
|
|
switch (parseInt(response.data.code)) {
|
|
case 401: {
|
|
if (['user-cart-num', 'new-order', 'user-cart-add', 'user-info'].indexOf(url) === -1) {
|
|
uni.showToast({
|
|
title: '请登录',
|
|
icon: 'error',
|
|
duration: 2000,
|
|
})
|
|
setTimeout(() => {
|
|
clearUserInfo()
|
|
}, 2000)
|
|
}
|
|
}
|
|
break;
|
|
case 500: {
|
|
if (['/user/change-pay-password', 'new-order-from-goods-list'].indexOf(url) === -1) {
|
|
console.log(response.data)
|
|
uni.showToast({
|
|
icon: 'none',
|
|
title: response.data.content.msg,
|
|
duration: 2000,
|
|
})
|
|
}
|
|
}
|
|
break;
|
|
case 502: {
|
|
uni.showToast({
|
|
icon: 'error',
|
|
title: '系统维护中',
|
|
duration: 2000,
|
|
})
|
|
}
|
|
}
|
|
return response
|
|
}, (error) => {
|
|
if (error.code === 502) {
|
|
uni.showToast({
|
|
icon: 'error',
|
|
title: '系统维护中',
|
|
duration: 2000,
|
|
})
|
|
}
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
const post = (url, formData, options) => {
|
|
return new Promise((resolve, reject) => {
|
|
http.post(url, formData, options).then(res => {
|
|
if (res.data.code === 0) resolve(res.data)
|
|
else if (res.data.code === 401) reject(res.data)
|
|
else reject(res.data)
|
|
})
|
|
})
|
|
}
|
|
|
|
export const get = (url, options) => {
|
|
console.log(options)
|
|
return new Promise((resolve, reject) => {
|
|
http.get(url, options).then(res => {
|
|
resolve(res.data)
|
|
})
|
|
})
|
|
}
|
|
|
|
export const put = (url, formData, options) => {
|
|
return new Promise((resolve, reject) => {
|
|
http.put(url, formData, options).then(res => {
|
|
if (res.data.code === 0) resolve(res.data)
|
|
else if (res.data.code === 401) reject(res.data)
|
|
else reject(res.data)
|
|
})
|
|
})
|
|
}
|
|
|
|
export default post
|