import { base_url } from './config.js' const timeout = 5000; let showModal = false // 定义公开API白名单 const publicApis = [ '/beer/list', // 啤酒列表 '/beer/detail', // 啤酒详情 '/activity/list', // 活动列表 '/activity/detail', // 活动详情 '/bar/detail', // 酒吧详情 '/bar/list', // 酒吧列表 '/common/', // 公共接口 ] // 检查是否是公开API const isPublicApi = (url) => { return publicApis.some(api => url.includes(api)) } export default (params) => { let url = params.url; let method = params.method || "get"; let data = params.data || {}; let header = {} let token = uni.getStorageSync('token') console.log(token) if (token) { header['Authorization'] = 'Bearer ' + token } // if (method == "post") { // header['Content-Type'] = 'multipart/form-data' // } return new Promise((resolve, reject) => { uni.request({ url: base_url +'/api'+ url, method: method, header: header, data: data, dataType: 'json', timeout, success(response) { const res = response if (res.statusCode == 200) { console.log(res.data, '接口返回值') if (res.data.code == 200) { resolve(res.data); } else if (res.data.code == 401 && !isPublicApi(url)) { uni.clearStorageSync() if (showModal) return showModal = true uni.showModal({ title: "提示", content: "身份已过期,请重新登录", showCancel: false, success() { showModal = false uni.navigateTo({ url: '/pages/index/chooseLogin' }) }, }); } else { uni.showToast({ title: res.data.msg || '请求失败', icon: 'none', duration: 3000, }) reject(res.data) } } else { uni.showToast({ title: '服务器异常', icon: 'none', duration: 2000 }) reject(response) } }, fail(err) { uni.showToast({ title: '网络异常', icon: 'none', duration: 2000 }) reject(err) }, // complete() { // } }); }) }