modalForm.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2022 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. $f.btn.loading(true);
  87. $f.btn.disabled(true);
  88. if (btnStop) return;
  89. btnStop = true;
  90. request[data.method.toLowerCase()](data.action, formData)
  91. .then((res) => {
  92. modalInstance.remove();
  93. Message.success(res.msg || '提交成功');
  94. resolve(res);
  95. })
  96. .catch((err) => {
  97. Message.error(err.msg || '提交失败');
  98. })
  99. .finally(() => {
  100. btnStop = false;
  101. $f.btn.loading(false);
  102. $f.btn.disabled(false);
  103. });
  104. };
  105. data.config.submitBtn = false;
  106. data.config.resetBtn = false;
  107. if (!data.config.form) data.config.form = {};
  108. // data.config.form.labelWidth = 100
  109. let fApi;
  110. data = Vue.observable(data);
  111. alert({
  112. title: data.title,
  113. width,
  114. loading: false,
  115. render: function (h) {
  116. return h('div', { class: 'common-form-create' }, [
  117. h('formCreate', {
  118. props: {
  119. rule: data.rules,
  120. option: data.config,
  121. },
  122. on: {
  123. mounted: ($f) => {
  124. fApi = $f;
  125. msg();
  126. },
  127. },
  128. }),
  129. h(
  130. 'Button',
  131. {
  132. class: 'common-form-button',
  133. props: {
  134. type: 'primary',
  135. long: true,
  136. },
  137. on: {
  138. click: () => {
  139. fApi.submit();
  140. },
  141. },
  142. },
  143. ['提交'],
  144. ),
  145. ]);
  146. },
  147. });
  148. })
  149. .catch((res) => {
  150. Spin.hide();
  151. msg();
  152. Message.error(res.msg || '表单加载失败');
  153. });
  154. });
  155. }