Dialog.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!--
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-09-04 11:21:05
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-09-18 12:38:09
  6. * @FilePath: \Game-Backstage-Management-System\src\components\common\Dialog.vue
  7. * @Description:
  8. *
  9. -->
  10. <script setup lang="ts">
  11. import Form from '../form/Form.vue'
  12. import { useDialog } from '@/hooks/useDialog'
  13. import { nextTick, onMounted, reactive, ref } from 'vue'
  14. import type { DialogConfig } from '@/types/dialog'
  15. interface DialogProps {
  16. config: DialogConfig
  17. }
  18. // 临时用的close
  19. const { dialogClose2 } = useDialog()
  20. const emits = defineEmits(['formSubmit'])
  21. // 新增的URL与更新URL,因为有时候更新和新增在一起,有时候是分开的,所以在这里去区分
  22. const addUrl = ref<string>('')
  23. const updateUrl = ref<string>('')
  24. // 对话框ref
  25. const dialogFormRef = ref<InstanceType<typeof Form>>()
  26. // props
  27. const props = withDefaults(defineProps<DialogProps>(), {})
  28. // props中的关于配置的信息
  29. const dialogConfigInfo = props.config
  30. // 游戏配置对话框设置
  31. const dialogConfig = reactive({
  32. dialogVisible: false,
  33. title: '',
  34. formLabelWidth: '150px',
  35. type: 0 // 0 是新增 1是修改
  36. })
  37. // 游戏配置提交
  38. const submiteGameChange = () => {
  39. dialogFormRef.value?.submitFormData().then(() => {
  40. // console.log(dialogConfigInfo.reqConfig.otherOptions.formData.gid )
  41. dialogConfig.dialogVisible = false
  42. })
  43. }
  44. // 表单关闭
  45. const closeDialog = () => {
  46. dialogClose2(dialogFormRef.value, dialogConfig)
  47. }
  48. // 表单添加
  49. const addForm = () => {
  50. dialogConfigInfo.reqConfig.url = addUrl.value
  51. dialogConfig.dialogVisible = true
  52. }
  53. // 表单修改
  54. const editForm = (row: any, updateURL?: string) => {
  55. dialogConfig.dialogVisible = true
  56. if (updateURL) {
  57. updateUrl.value = updateURL
  58. dialogConfigInfo.reqConfig.url = updateUrl.value
  59. }
  60. nextTick(() => {
  61. dialogFormRef.value?.fillForm(row)
  62. })
  63. }
  64. onMounted(() => {
  65. dialogConfig.title = dialogConfigInfo.title
  66. addUrl.value = props.config.reqConfig.url // 保存一下新增的URL
  67. })
  68. /**
  69. * @description: 对字段进行加密
  70. * @param {*} fields 字段名
  71. * @param {*} useFormField 是否对表单的字段加密
  72. * @param {*} encryptMsg 加密的消息
  73. * @return {*}
  74. */
  75. const encrypt = (fields: string, useFormField: boolean, encryptMsg: Array<string>) => {
  76. dialogFormRef.value?.encryptData(fields, useFormField, encryptMsg).finally(() => {
  77. dialogConfig.dialogVisible = false
  78. })
  79. }
  80. const subForm = () => {
  81. emits('formSubmit')
  82. }
  83. defineExpose({
  84. addForm,
  85. editForm,
  86. encrypt
  87. })
  88. </script>
  89. <template>
  90. <div class="dialog">
  91. <el-dialog
  92. @close="closeDialog"
  93. v-model="dialogConfig.dialogVisible"
  94. :title="dialogConfig.title"
  95. :close-on-click-modal="false"
  96. style="width: 40%"
  97. append-to-body
  98. >
  99. <Form
  100. :disabled="true"
  101. @sub-form="subForm"
  102. ref="dialogFormRef"
  103. :config="{
  104. fieldsInfo: dialogConfigInfo.fieldsInfo,
  105. reqConfig: dialogConfigInfo.reqConfig,
  106. rules: dialogConfigInfo.rules
  107. }"
  108. ></Form>
  109. <template #footer>
  110. <div class="dialog-footer">
  111. <slot name="otherBtn"></slot>
  112. <slot name="btnGroup">
  113. <el-button class="dialogBtn" type="primary" @click="submiteGameChange()">
  114. 确认
  115. </el-button>
  116. <el-button class="dialogBtn" @click="closeDialog">取消</el-button>
  117. </slot>
  118. </div>
  119. </template>
  120. </el-dialog>
  121. </div>
  122. </template>
  123. <style scoped>
  124. .dialogBtn {
  125. box-sizing: border-box;
  126. margin-right: 10px;
  127. }
  128. </style>