import Config from './config.js' import Util from './util.js' import store from '@/store/index.js' import Platform from './platform.js' const { appType, appTypeName } = Platform.getPlatformInfo(); // 惰性加载 Lang,避免与 lang.js 的循环依赖 let _Lang = null; function getLang() { if (!_Lang) { _Lang = require('./lang.js').default; } return _Lang; } export default { sendRequest(params) { if (!Config.baseUrl) { uni.showToast({ title: getLang().lang('http.baseUrlNotConfigured'), 'icon': 'none', duration: 10000 }); return; } // remote-only boot 门控: 所有业务请求必须等国际化数据就绪后才发送 // 这样业务请求返回时 lang() 已有翻译数据,不会造成 UI 污染 // bundle 请求走 lang-loader.js 的 uni.request 直接发送,不受此门控影响 const _isRemoteOnly = getLang().isRemoteOnly && getLang().isRemoteOnly(); const _bootDone = getLang().isBootCompleted && getLang().isBootCompleted(); if (_isRemoteOnly && !_bootDone) { // 获取 boot promise(优先从 App 实例拿,避免重复 boot) let bootPromise = null; try { const app = getApp(); if (app && app.$langReady) bootPromise = app.$langReady; } catch (e) {} if (!bootPromise) bootPromise = getLang().boot(); if (params.async === false) { // 同步 Promise 模式: 链式等待 boot 后再发请求 return bootPromise.then(() => this._sendRequestNow(params)); } else { // 回调模式: boot 完成后再执行请求 bootPromise.then(() => this._sendRequestNow(params)); return; } } return this._sendRequestNow(params); }, _sendRequestNow(params) { var method = params.data != undefined ? 'POST' : 'GET', url = (Config.baseUrl + params.url).replace(/(? { uni.request({ url: url, method: method, data: data, header: params.header || { // 'Accept': 'application/json', 'content-type': 'application/x-www-form-urlencoded;application/json' }, dataType: params.dataType || 'json', responseType: params.responseType || 'text', success: (res) => { // try { // res.data = JSON.parse(res.data); // } catch (e) { // //TODO handle the exception // console.log('api error:', e); // } if (res.data.code == -3 && store.state.siteState > 0) { store.commit('setSiteState', -3); Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch'); return; } if (res.data.refreshtoken) { store.commit('setToken', res.data.refreshtoken); } if (res.data.code == -10009 || res.data.code == -10010) { store.commit('setToken', ''); store.commit('setMemberInfo', ''); } resolve(res.data); }, fail: (res) => { if (res.errMsg && res.errMsg == 'request:fail url not in domain list') { uni.showToast({ title: getLang().lang('http.domainNotInList', { url: Config.baseUrl }), 'icon': 'none', duration: 10000 }); return; } reject(res); }, complete: (res) => { // success/fail 已处理 resolve/reject,complete 只负责兜底提示非预期状态 if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(res.statusCode) == -1)) { uni.showToast({ title: getLang().lang('http.requestFailed', { url: Config.baseUrl }), 'icon': 'none', duration: 10000 }); } } }); }); } else { //异步 uni.request({ url: url, method: method, data: data, header: params.header || { // 'Accept': 'application/json', 'content-type': 'application/x-www-form-urlencoded;application/json' }, dataType: params.dataType || 'json', responseType: params.responseType || 'text', success: (res) => { // try { // res.data = JSON.parse(res.data); // } catch (e) { // //TODO handle the exception // console.log('api error:', e); // } if (res.data.code == -3 && store.state.siteState > 0) { store.commit('setSiteState', -3); Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch'); return; } if (res.data.refreshtoken) { store.commit('setToken', res.data.refreshtoken); } if (res.data.code == -10009 || res.data.code == -10010) { store.commit('setToken', ''); store.commit('setMemberInfo', ''); } typeof params.success == 'function' && params.success(res.data); }, fail: (res) => { if (res.errMsg && res.errMsg == 'request:fail url not in domain list') { uni.showToast({ title: getLang().lang('http.domainNotInList', { url: Config.baseUrl }), 'icon': 'none', duration: 10000 }); return; } typeof params.fail == 'function' && params.fail(res); }, complete: (res) => { if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(res.statusCode) == -1)) { uni.showToast({ title: getLang().lang('http.requestFailed', { url: Config.baseUrl }), 'icon': 'none', duration: 10000 }); return; } typeof params.complete == 'function' && params.complete(res.data); } }); } }, uploadBase64(params) { uni.request({ url: Config.baseUrl + '/api/upload/headimgBase64', method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded;application/json' }, data: { app_type: appType, app_type_name: appTypeName, images: params.base64 }, dataType: 'json', responseType: 'text', success: res => { typeof params.success == 'function' && params.success(res.data); }, fail: () => { typeof params.fail == 'function' && params.fail(res); } }); }, pullImage(params) { uni.request({ url: Config.baseUrl + '/api/upload/headimgPull', method: 'POST', header: { 'content-type': 'application/x-www-form-urlencoded;application/json' }, data: { app_type: appType, app_type_name: appTypeName, path: params.path }, dataType: 'json', responseType: 'text', success: res => { typeof params.success == 'function' && params.success(res.data); }, fail: () => { typeof params.fail == 'function' && params.fail(res); } }); }, upload(params) { uni.uploadFile({ url: Config.baseUrl + params.url, filePath: params.filePath, name: params.name || 'file', fileType: params.fileType || 'image', formData: { app_type: appType, app_type_name: appTypeName, }, header: { 'content-type': 'application/x-www-form-urlencoded;application/json' }, success: (res) => { typeof params.success == 'function' && params.success(JSON.parse(res.data)); }, fail: (res) => { typeof params.fail == 'function' && params.fail(res); } }); } }