editDataDialog.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <el-dialog
  3. :title="configData.title + configData.afterTitle"
  4. v-model="configData.open"
  5. width="800px"
  6. :close-on-click-modal="false"
  7. append-to-body
  8. destroy-on-close
  9. @close="handleClose"
  10. class="edit-data-dialog"
  11. >
  12. <div class="dialog-container">
  13. <el-form
  14. :model="dataContainer.form"
  15. ref="FormElRef"
  16. :inline="true"
  17. :rules="dataContainer.rules"
  18. label-width="100px"
  19. >
  20. <el-row :gutter="0">
  21. <el-col :span="12" :xs="6">
  22. <el-form-item label="用户组" prop="User_groups">
  23. <el-input
  24. v-model="dataContainer.form.User_groups"
  25. placeholder="请输入"
  26. :disabled="configData.isShow"
  27. clearable
  28. />
  29. </el-form-item>
  30. </el-col>
  31. <el-col :span="12" :xs="6">
  32. <el-form-item label="描述" prop="description">
  33. <el-input
  34. v-model="dataContainer.form.description"
  35. placeholder="请输入"
  36. :disabled="configData.isShow"
  37. clearable
  38. />
  39. </el-form-item>
  40. </el-col>
  41. <el-col :span="12" :xs="6">
  42. <el-form-item label="分组上级" prop="sign">
  43. <el-select
  44. style="width: 100%"
  45. placeholder="请选择"
  46. v-model="dataContainer.form.sign"
  47. :disabled="configData.isShow"
  48. clearable
  49. >
  50. <el-option
  51. v-for="item in [
  52. { label: '分部', value: '1' },
  53. { label: '总部', value: '2' },
  54. ]"
  55. :key="item.value"
  56. :label="item.label"
  57. :value="item.value"
  58. />
  59. </el-select>
  60. </el-form-item>
  61. </el-col>
  62. </el-row>
  63. </el-form>
  64. </div>
  65. <template #footer>
  66. <div class="dialog-footer">
  67. <el-button
  68. @click="
  69. () => {
  70. dataContainer.closeType = 'cancel';
  71. configData.open = false;
  72. }
  73. "
  74. >
  75. 取消
  76. </el-button>
  77. <el-button v-if="!configData.isShow" type="primary" @click="handleSubmit">
  78. 提交
  79. </el-button>
  80. </div>
  81. </template>
  82. </el-dialog>
  83. </template>
  84. <script>
  85. /**
  86. * 数据编辑对话框
  87. * 使用外部调用的方式向内部传递数据
  88. * 使用promise的形式向外部通知状态
  89. */
  90. import { defineComponent, ref, getCurrentInstance, reactive, nextTick } from 'vue';
  91. import { mergeObjProperty } from '@/common/otherTools';
  92. import { getDictItem, initDataByConfig } from '@/common/otherTools';
  93. import { verifiedData } from '@/common/verifiedTools';
  94. import { messageError,messageSuccess } from '@/action/messagePrompt.js';
  95. //配置信息,初始化时使用
  96. const configMap = {
  97. open: {
  98. default: false,
  99. },
  100. title: {
  101. default: '权限管理',
  102. },
  103. afterTitle: {
  104. default: '',
  105. },
  106. isShow: {
  107. //是否只是展示
  108. default: false,
  109. },
  110. };
  111. export default defineComponent({
  112. name: 'EditDataDialog',
  113. components: {},
  114. setup() {
  115. const configData = reactive({});
  116. const FormElRef = ref(null); //组件实例
  117. const dataContainer = reactive({
  118. loading: false,
  119. closeType: 'close', //关闭时的类型,是由确认取消按钮关闭的还是其他地方关闭的 confirm cancel
  120. resolve: undefined, //返给外部promise的回调
  121. reject: undefined,
  122. form: {},
  123. rules: {
  124. User_groups: [{ required: true, message: '请输入用户组名称', trigger: 'blur' }],
  125. },
  126. });
  127. const otherDataContainer = {
  128. castParams: {}, //向外部传递的参数
  129. };
  130. initDataByConfig(configData, {}, configMap);
  131. /**
  132. * 对话框关闭时的回调
  133. * 根据行为类型来判断调用那个回调函数
  134. * */
  135. function handleClose() {
  136. if (dataContainer.closeType == 'confirm') {
  137. dataContainer.resolve(otherDataContainer.castParams);
  138. } else {
  139. dataContainer.reject(dataContainer.closeType, otherDataContainer.castParams);
  140. }
  141. }
  142. /**
  143. * 初始化数据(外部调用)
  144. * 返回一个promise,以提供直接的回调
  145. * */
  146. function initData(show = true, data = {}, option = {}) {
  147. initDataByConfig(configData, option, configMap);
  148. dataContainer.closeType = 'close';
  149. dataContainer.loading = false;
  150. dataContainer.form = {};
  151. otherDataContainer.castParams = {};
  152. configData.open = show;
  153. nextTick(() => {
  154. dataContainer.form = data;
  155. // getDataInfo();
  156. });
  157. return new Promise((r, j) => {
  158. dataContainer.resolve = r;
  159. dataContainer.reject = j;
  160. });
  161. }
  162. /** 获取数据详细 */
  163. function getDataInfo() {
  164. dataContainer.form = {};
  165. }
  166. /** 提交数据 */
  167. function handleSubmit() {
  168. /** 使用组件自带方法验证数据 */
  169. if (!FormElRef.value) return;
  170. FormElRef.value.validate((valid, e) => {
  171. if (e) {
  172. /** 打印报错信息 */
  173. let msg = e[Object.keys(e)[0]][0].message;
  174. messageError(msg);
  175. }
  176. if (!valid) return;
  177. setTimeout(() => {
  178. otherDataContainer.castParams = {
  179. name: '数据保存成功了,向外部通知',
  180. };
  181. dataContainer.closeType = 'confirm';
  182. configData.open = false;
  183. messageSuccess("提交成功")
  184. }, 1000);
  185. });
  186. }
  187. /**
  188. * 数据验证
  189. * 外部可调用
  190. * */
  191. function validData(data) {
  192. const failData = verifiedData(data, {
  193. name: {
  194. msg: '名称 不能为空',
  195. validate(value, option) {
  196. if (!value && value !== 0) return false;
  197. return true;
  198. },
  199. },
  200. });
  201. return failData;
  202. }
  203. return {
  204. configData,
  205. initData,
  206. dataContainer,
  207. handleClose,
  208. getDataInfo,
  209. handleSubmit,
  210. FormElRef,
  211. validData,
  212. };
  213. },
  214. });
  215. </script>
  216. <style lang="scss" scoped>
  217. .edit-data-dialog {
  218. .dialog-container {
  219. padding: 15px 15px 0 15px;
  220. box-sizing: border-box;
  221. }
  222. }
  223. </style>