const baseRequest = async (url, method, data = {}, loading = true, header = {}) => { url = url.indexOf('https://') > -1 ? url : 'https://xnh.xazhima.com/prod-api/wap/business/weixin' + url return new Promise((reslove, reject) => { loading && uni.showLoading({ title: '加载中' }) uni.request({ url, method: method || 'GET', header, timeout: 10000, data: data || {}, success: (successData) => { const res = successData.data if (successData.statusCode == 200) { if (res.code === 200) { reslove(res) } else { uni.showToast({ title: '未知错误', icon: 'none' }) } } else { uni.showToast({ title: '网络连接失败,请稍后重试', icon: 'none' }) reject(res) } }, fail: (msg) => { uni.showToast({ title: '网络连接失败,请稍后重试', icon: 'none' }) reject(msg) }, complete: () => { uni.hideLoading() }, }) }) } const request = { get: (api, data, loading) => baseRequest(api, 'GET', data, loading), getPath: (api, data, keys, loading) => { let newRequestPath = api; Object.keys(keys).forEach(function(key) { newRequestPath = newRequestPath.replace( "{" + key + "}", keys[key] ); }); return baseRequest(newRequestPath, 'GET', data, loading) }, post: (api, data, loading) => baseRequest(api, 'POST', data, loading), postForm: (api, data, loading) => baseRequest(api, 'POST', data, loading, { 'content-type': 'application/x-www-form-urlencoded' }), } export default request