modalForm.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. let btnStop = false;
  16. function getModalInstance(render = undefined) {
  17. modalInstance =
  18. modalInstance ||
  19. Modal.newInstance({
  20. closable: true,
  21. maskClosable: false,
  22. footerHide: true,
  23. render: render,
  24. // zIndex: 2000,
  25. });
  26. return modalInstance;
  27. }
  28. function alert(options) {
  29. const render = 'render' in options ? options.render : undefined;
  30. let instance = getModalInstance(render);
  31. options.onRemove = function () {
  32. modalInstance = null;
  33. };
  34. instance.show(options);
  35. }
  36. export default function (formRequestPromise, { width = '700' } = { width: '700' }) {
  37. return new Promise((resolve) => {
  38. const msg = Message.loading({
  39. content: 'Loading...',
  40. duration: 0,
  41. });
  42. formRequestPromise
  43. .then(({ data }) => {
  44. if (data.status === false) {
  45. msg();
  46. return Notice.warning({
  47. title: data.title,
  48. duration: 3,
  49. desc: data.info,
  50. render: (h) => {
  51. return h('div', [
  52. h(
  53. 'a',
  54. {
  55. attrs: {
  56. href: 'http://www.crmeb.com',
  57. },
  58. },
  59. data.info,
  60. ),
  61. ]);
  62. },
  63. });
  64. }
  65. data.config = {};
  66. data.config.global = {
  67. upload: {
  68. props: {
  69. onSuccess(res, file) {
  70. if (res.status === 200) {
  71. file.url = res.data.src;
  72. } else {
  73. Message.error(res.msg);
  74. }
  75. },
  76. },
  77. },
  78. frame: {
  79. props: {
  80. closeBtn: false,
  81. okBtn: false,
  82. },
  83. },
  84. };
  85. data.config.onSubmit = (formData, $f) => {
  86. $f.btn.loading(true);
  87. $f.btn.disabled(true);
  88. request[data.method.toLowerCase()](data.action, formData)
  89. .then((res) => {
  90. modalInstance.remove();
  91. Message.success(res.msg || '提交成功');
  92. resolve(res);
  93. })
  94. .catch((err) => {
  95. Message.error(err.msg || '提交失败');
  96. })
  97. .finally(() => {
  98. $f.btn.loading(false);
  99. $f.btn.disabled(false);
  100. });
  101. };
  102. data.config.submitBtn = false;
  103. data.config.resetBtn = false;
  104. if (!data.config.form) data.config.form = {};
  105. // data.config.form.labelWidth = 100
  106. let fApi;
  107. data = Vue.observable(data);
  108. alert({
  109. title: data.title,
  110. width,
  111. loading: false,
  112. render: function (h) {
  113. return h('div', { class: 'common-form-create' }, [
  114. h('formCreate', {
  115. props: {
  116. rule: data.rules,
  117. option: data.config,
  118. },
  119. on: {
  120. mounted: ($f) => {
  121. fApi = $f;
  122. msg();
  123. },
  124. },
  125. }),
  126. h('div', { class: 'common-form-create-footer' }, [
  127. h(
  128. 'Button',
  129. {
  130. class: 'common-form-button',
  131. props: {
  132. long: false,
  133. },
  134. on: {
  135. click: () => {
  136. modalInstance.remove();
  137. },
  138. },
  139. },
  140. ['取消'],
  141. ),
  142. h(
  143. 'Button',
  144. {
  145. class: 'common-form-button',
  146. props: {
  147. type: 'primary',
  148. long: false,
  149. },
  150. on: {
  151. click: () => {
  152. if (btnStop) return;
  153. btnStop = true;
  154. fApi.submit();
  155. setTimeout(() => {
  156. btnStop = false;
  157. }, 1000);
  158. },
  159. },
  160. },
  161. ['提交'],
  162. ),
  163. ]),
  164. ]);
  165. },
  166. });
  167. })
  168. .catch((res) => {
  169. Spin.hide();
  170. msg();
  171. Message.error(res.msg || '表单加载失败');
  172. });
  173. });
  174. }