const baseUrl = "https://www.hzkjai.com" const httpInterceptor = { invoke(options) { //1.拼接地址 if (!options.url.startsWith('http')) { options.url = (options.baseUrl ? options.baseUrl : baseUrl) + options.url } //2.请求超时 默认60s options.timeout = 10000 //3.添加小程序端请求头标识 options.header = { ...options.header, 'source-client': 'miniapp', 'Content-Type': 'application/x-www-form-urlencoded', } //4.添加token 请求头标识 const token = '' if (token) { options.header.Authorization = token } } } uni.addInterceptor('request', httpInterceptor) const http = (options) => { return new Promise((resolve, reject) => { uni.request({ ...options, //请求成功 success(res) { console.log("success:", res) if (res.statusCode >= 200 && res.statusCode < 300) { //数据获取成功 调用resolve resolve(res.data) } else if (res.statusCode === 401) { reject(res) } else { //通用错误 调用reject uni.showToast({ icon: 'none', title: res.data.msg || '请求错误', }) reject(res) } }, //响应失败 fail(err) { //网络错误 调用reject uni.showToast({ icon: 'none', title: '网络错误,请更换网络试试~', }) reject(err) } }) }) } export default http