|
|
import axios from 'axios'; import { Modal } from 'ant-design-vue'; import { h } from 'vue'; export default class ApiClient { // instancec of client
static instance = null;
// get client instance
static getClient() { if ( null === ApiClient.instance ) { ApiClient.instance = new ApiClient(); } return ApiClient.instance; }
// call api
async call( name, params={} ) { let response = await axios({ method: 'post', // url: `/api/${name}`,
// url: `http://localhost:80/api/${name}`,
//根据不同的环境配置不同的url,生产环境下使用/api/${name},开发环境下使用http://localhost:80/api/${name}
url: process.env.NODE_ENV === 'production' ? `/api/${name}` : `http://localhost:8080/api/${name}`, data: params }); if ( !response.data.success ) { let errorInfo = response.data.ecode; delete errorInfo.codeChName; let content = h('div', {}, [ h('p',{class:'mb-5 text-red-400 whitespace-pre'},response.data.message), h('div', { style:{height:'20px',overflow:'hidden'}, onClick:(event) => { let elem = event.target.parentElement; if ( '20px' === elem.style.height ) { elem.style.height = '600px'; elem.style.overflow = 'auto'; } else { elem.style.height = '20px'; elem.scrollTop = 0; elem.style.overflow = 'hidden'; } } }, [ h('div','详情'), h('pre', JSON.stringify(errorInfo, null, 2)), h('pre', {class:'overflow-auto h-96 mt-5'}, response.data.traceInfo), ]), ]); Modal.error({title: '请求错误',content:content,width:800}); throw new Error(`API【${name}】调用失败 : ${response.data}`); } if ( 'MESSAGE' === response.data.appRetType ) { Modal.info({title:'通知', content:response.data.message}); } if ( null !== response.data.data && 'object' === typeof(response.data.data) ) { response.data.data.$dataType = response.data.dataType; } return response.data.data; } }
|