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.
91 lines
2.0 KiB
91 lines
2.0 KiB
import config from '@/config/request' |
|
let HTTP_COUNT = 0 // loading次数 |
|
|
|
const request = options => { |
|
HTTP_COUNT++ |
|
if (config.showLoading) { |
|
// 请求数据时的loading |
|
uni.showToast({ |
|
title: '加载中', |
|
duration: 200, |
|
icon: 'loading' |
|
}) |
|
} |
|
const header = Object.assign({}, config.headers, { |
|
token: uni.getStorageSync('token') |
|
}) |
|
return new Promise((resolve, reject)=>{ |
|
const { url } = options |
|
uni.request({ |
|
header, |
|
url: ((url.includes('getTeamsByAccountId') || url.includes('getTheBusinessManagerIdsUnderTheTeam') || url.includes('treeList')) ? 'http://192.168.31.151:9000/' : config.baseURL) + url, |
|
method: options.method || 'GET', // 请求类型,默认为GET |
|
data: options.data || {}, // 请求参数,默认空对象 |
|
success: ({ data }) => { |
|
const { status, message } = data |
|
// 状态判断,根据后台定义并提示 |
|
if (status === 200) { |
|
resolve(data) |
|
} else if (status == 401) { |
|
// 登录过期 |
|
uni.removeStorageSync('token') |
|
uni.removeStorageSync('userName') |
|
uni.removeStorageSync('avatar') |
|
uni.removeStorageSync('sessionKey') |
|
uni.removeStorageSync('team') |
|
uni.showToast({ |
|
title: message, |
|
icon: 'none' |
|
}) |
|
setTimeout(() => { |
|
uni.navigateTo({ |
|
url: '../login/login' |
|
}) |
|
}, 1500) |
|
reject(data) |
|
} else if (!status) { |
|
resolve(data) |
|
} else { |
|
uni.showToast({ |
|
title: message, |
|
icon: 'none' |
|
}) |
|
reject(data) |
|
} |
|
}, |
|
fail: err => { |
|
uni.showToast({ |
|
title: '请求失败!', |
|
icon: 'none' |
|
}) |
|
reject(err) |
|
}, |
|
complete: () => { |
|
if (config.showLoading) { |
|
HTTP_COUNT-- |
|
HTTP_COUNT || uni.hideLoading() |
|
} |
|
} |
|
}) |
|
}) |
|
} |
|
|
|
const get = (url, data, options = {}) => { |
|
options.method = 'GET' |
|
options.data = data |
|
options.url = url |
|
return request(options) |
|
} |
|
|
|
const post = (url, data, options = {}) => { |
|
options.method = 'POST' |
|
options.data = data |
|
options.url = url |
|
return request(options) |
|
} |
|
|
|
export default { |
|
request, |
|
get, |
|
post |
|
} |