modalForm.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import request from '@/libs/request';
  11. import Modal from './modal';
  12. import Vue from 'vue';
  13. import { Message, Spin, Notice } from 'iview';
  14. let modalInstance;
  15. function getModalInstance(render = undefined) {
  16. modalInstance =
  17. modalInstance ||
  18. Modal.newInstance({
  19. closable: true,
  20. maskClosable: false,
  21. footerHide: true,
  22. render: render,
  23. // zIndex: 2000,
  24. });
  25. return modalInstance;
  26. }
  27. function alert(options) {
  28. const render = 'render' in options ? options.render : undefined;
  29. let instance = getModalInstance(render);
  30. options.onRemove = function () {
  31. modalInstance = null;
  32. };
  33. instance.show(options);
  34. }
  35. export default function (formRequestPromise, { width = '700' } = { width: '700' }) {
  36. return new Promise((resolve) => {
  37. const msg = Message.loading({
  38. content: 'Loading...',
  39. duration: 0,
  40. });
  41. formRequestPromise
  42. .then(({ data }) => {
  43. if (data.status === false) {
  44. msg();
  45. return Notice.warning({
  46. title: data.title,
  47. duration: 3,
  48. desc: data.info,
  49. render: (h) => {
  50. return h('div', [
  51. h(
  52. 'a',
  53. {
  54. attrs: {
  55. href: 'http://www.crmeb.com',
  56. },
  57. },
  58. data.info,
  59. ),
  60. ]);
  61. },
  62. });
  63. }
  64. data.config = {};
  65. data.config.global = {
  66. upload: {
  67. props: {
  68. onSuccess(res, file) {
  69. if (res.status === 200) {
  70. file.url = res.data.src;
  71. } else {
  72. Message.error(res.msg);
  73. }
  74. },
  75. },
  76. },
  77. frame: {
  78. props: {
  79. closeBtn: false,
  80. okBtn: false,
  81. },
  82. },
  83. };
  84. let btnStop = false;
  85. data.config.onSubmit = (formData, $f) => {
  86. console.log('add')
  87. $f.btn.loading(true);
  88. $f.btn.disabled(true);
  89. if (btnStop) return;
  90. btnStop = true;
  91. request[data.method.toLowerCase()](data.action, formData)
  92. .then((res) => {
  93. modalInstance.remove();
  94. Message.success(res.msg || '提交成功');
  95. resolve(res);
  96. })
  97. .catch((err) => {
  98. Message.error(err.msg || '提交失败');
  99. })
  100. .finally(() => {
  101. btnStop = false;
  102. $f.btn.loading(false);
  103. $f.btn.disabled(false);
  104. });
  105. };
  106. data.config.submitBtn = false;
  107. data.config.resetBtn = false;
  108. if (!data.config.form) data.config.form = {};
  109. // data.config.form.labelWidth = 100
  110. let fApi;
  111. data = Vue.observable(data);
  112. alert({
  113. title: data.title,
  114. width,
  115. loading: false,
  116. render: function (h) {
  117. return h('div', { class: 'common-form-create' }, [
  118. h('formCreate', {
  119. props: {
  120. rule: data.rules,
  121. option: data.config,
  122. },
  123. on: {
  124. mounted: ($f) => {
  125. fApi = $f;
  126. msg();
  127. },
  128. },
  129. }),
  130. h(
  131. 'Button',
  132. {
  133. class: 'common-form-button',
  134. props: {
  135. type: 'primary',
  136. long: true,
  137. },
  138. on: {
  139. click: () => {
  140. fApi.submit();
  141. },
  142. },
  143. },
  144. ['提交'],
  145. ),
  146. ]);
  147. },
  148. });
  149. })
  150. .catch((res) => {
  151. Spin.hide();
  152. msg();
  153. Message.error(res.msg || '表单加载失败');
  154. });
  155. });
  156. }