modalForm.js 5.8 KB

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