modalForm.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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: 'http://www.crmeb.com'
  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. data.config.onSubmit = function (formData, $f) {
  77. $f.btn.loading(true);
  78. $f.btn.disabled(true);
  79. request[data.method.toLowerCase()](data.action, formData).then((res) => {
  80. modalInstance.remove()
  81. Message.success(res.msg || '提交成功')
  82. resolve(res)
  83. }).catch(err => {
  84. Message.error(err.msg || '提交失败')
  85. }).finally(() => {
  86. $f.btn.loading(false)
  87. $f.btn.disabled(false);
  88. })
  89. }
  90. data.config.submitBtn = false
  91. data.config.resetBtn = false
  92. if (!data.config.form) data.config.form = {}
  93. // data.config.form.labelWidth = 100
  94. let fApi
  95. data = Vue.observable(data);
  96. alert({
  97. title: data.title,
  98. width,
  99. loading: false,
  100. render: function (h) {
  101. return h('div', { class: 'common-form-create' }, [
  102. h('formCreate', {
  103. props: {
  104. rule: data.rules,
  105. option: data.config
  106. },
  107. on: {
  108. mounted: ($f) => {
  109. fApi = $f
  110. msg()
  111. }
  112. }
  113. }),
  114. h('div', {style:'display:flex;justify-content: flex-end;'}, [
  115. h('Button', {
  116. class: 'common-form-button',
  117. style: 'margin-right:14px',
  118. props: {
  119. type: 'default',
  120. },
  121. on: {
  122. click: () => {
  123. modalInstance.remove();
  124. }
  125. }
  126. }, ['取消']),
  127. h('Button', {
  128. class: 'common-form-button',
  129. props: {
  130. type: 'primary',
  131. },
  132. on: {
  133. click: () => {
  134. fApi.submit()
  135. }
  136. }
  137. }, ['确认'])
  138. ])
  139. ])
  140. }
  141. })
  142. }).catch(res => {
  143. Spin.hide();
  144. msg();
  145. Message.error(res.msg || '表单加载失败');
  146. })
  147. })
  148. }