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: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwiaWF0IjoxNjUyMjM0OTM2LCJleHAiOjE2NTIyNzgxMzYsImFjY291bnRJZCI6IjEifQ.QwmlBM6uDfRpW4xV8b0r3aBOqXQu5gWLj5Jbrlnrtcg' }) return new Promise((resolve, reject)=>{ uni.request({ header, url: config.baseURL + options.url, method: options.method || 'GET', // 请求类型,默认为GET data: options.data || {}, // 请求参数,默认空对象 success: ({ data }) => { // 状态判断,根据后台定义并提示 if (data.status === 200) { resolve(data) } else { uni.showToast({ title: data.message, icon: 'none' }) reject(data) } }, fail: err => { uni.showToast({ title: '请求失败!', icon: 'fail' }) 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 }