import store from '@/store' import apiBaseConfig from '@/config/index.js' function httpBaseUrl() { // #ifdef MP-WEIXIN return '/dev-api' // #endif } function switchMethod(method) { let header = {}; switch (method) { case 'POST': header = { // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Content-Type': 'application/json;charset=UTF-8', } break; case 'GET': header = { 'Content-Type': 'application/json;charset=UTF-8', } break; } return header } /** * url 接口地址 * params 参数 * method 请求方式,必须全大写 * isToken 请求头是否设置token,默认true **/ const ApiHttp = (url, params, method, isToken = true) => { let header = {}, token = uni.getStorageSync('token') || null; // token = store.state.login.token || null; header = switchMethod(method) if (isToken) { header = { ...header, 'Authorization': `Bearer ${token}` } } return new Promise((resolve, reject) => { // uni.showLoading({ // title: '加载中...', // mask: true, // }); uni.request({ url: `${apiBaseConfig.domain}${url}`, method, data: params, header, success: (res) => { const { data } = res; const { code, msg } = data; if (code === 200) { resolve(data); } else if (code === 401) { uni.clearStorageSync(); uni.showModal({ title: '请登录', content: '您需要登录才能使用该功能', success: (res) => { if (res.confirm) { uni.reLaunch({ url: '/my/login/login' }) } else if (res.cancel) { uni.navigateBack() } } }) resolve(data); } else { if (url == '/group-buy/by-consumer-order/leader/refunde' || url == '/group-buy/by-collective-leader/releaseOrder' || url == '/group-buy/by-collective-leader-authentication/apply/leader') { resolve(data); return } uni.showToast({ title: msg || '系统内部错误', icon: "none" }) if (url == '/system/xcxLogin' || url == 'system/getPhone') { uni.clearStorageSync(); store.dispatch('loginOut') } } }, fail: (err) => { console.log(err, 'pppppppppppp') uni.showToast({ title: err.errMsg, icon: "none" }) if (url == '/system/xcxLogin' || url == 'system/getPhone') { uni.clearStorageSync(); store.dispatch('loginOut') } reject(err) }, complete: () => { // uni.hideLoading(); } }) }) } export { ApiHttp }