You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.5 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import axios from 'axios';
  2. import { Modal } from 'ant-design-vue';
  3. import { h } from 'vue';
  4. export default class ApiClient {
  5. // instancec of client
  6. static instance = null;
  7. // get client instance
  8. static getClient() {
  9. if ( null === ApiClient.instance ) {
  10. ApiClient.instance = new ApiClient();
  11. }
  12. return ApiClient.instance;
  13. }
  14. // call api
  15. async call( name, params={} ) {
  16. let response = await axios({
  17. method: 'post',
  18. // url: `/api/${name}`,
  19. // url: `http://localhost:80/api/${name}`,
  20. //根据不同的环境配置不同的url,生产环境下使用/api/${name},开发环境下使用http://localhost:80/api/${name}
  21. url: process.env.NODE_ENV === 'production' ? `/api/${name}` : `http://localhost:8080/api/${name}`,
  22. data: params
  23. });
  24. if ( !response.data.success ) {
  25. let errorInfo = response.data.ecode;
  26. delete errorInfo.codeChName;
  27. let content = h('div', {}, [
  28. h('p',{class:'mb-5 text-red-400 whitespace-pre'},response.data.message),
  29. h('div', {
  30. style:{height:'20px',overflow:'hidden'},
  31. onClick:(event) => {
  32. let elem = event.target.parentElement;
  33. if ( '20px' === elem.style.height ) {
  34. elem.style.height = '600px';
  35. elem.style.overflow = 'auto';
  36. } else {
  37. elem.style.height = '20px';
  38. elem.scrollTop = 0;
  39. elem.style.overflow = 'hidden';
  40. }
  41. }
  42. }, [
  43. h('div','详情'),
  44. h('pre', JSON.stringify(errorInfo, null, 2)),
  45. h('pre', {class:'overflow-auto h-96 mt-5'}, response.data.traceInfo),
  46. ]),
  47. ]);
  48. Modal.error({title: '请求错误',content:content,width:800});
  49. throw new Error(`API【${name}】调用失败 : ${response.data}`);
  50. }
  51. if ( 'MESSAGE' === response.data.appRetType ) {
  52. Modal.info({title:'通知', content:response.data.message});
  53. }
  54. if ( null !== response.data.data && 'object' === typeof(response.data.data) ) {
  55. response.data.data.$dataType = response.data.dataType;
  56. }
  57. return response.data.data;
  58. }
  59. }