index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div class="h-full flex custormForm">
  3. <div class="p-5 w-60 pr-0">
  4. <div class="bg-white w-full h-full shadow-md rounded-md flex flex-col const-left">
  5. <el-table ref="customFormTableRef" :data="customFormTableData" border highlight-current-row
  6. @current-change="customFormHandleCurrentChange" style="width: 100%;height: 100%;">
  7. <el-table-column prop="name" label="模块" align="center" />
  8. </el-table>
  9. </div>
  10. </div>
  11. <div class="flex-1 p-5 overflow-auto">
  12. <div class="bg-white w-full h-full shadow-md rounded-md flex flex-col">
  13. <div class="tableHeader">
  14. <div>模块 - {{ currentRow.name }}</div>
  15. <el-button type="primary" @click="showModel()" :disabled="!currentRow.id">编辑表单</el-button>
  16. </div>
  17. <div class="flex-1 p-3" v-loading="AllLoading.elementLading">
  18. <GenerateForm ref="generateForm" :data="generateFormData"></GenerateForm>
  19. </div>
  20. </div>
  21. </div>
  22. <!-- 弹窗 -->
  23. <el-dialog v-model="AllMobelVisible.formMobelVisible" title="创建表单" width="90vw" align-center
  24. :before-close="handleClose">
  25. <div class="createForm">
  26. <DesignForm ref="designForm" :basicFieldsList="letBasicFields" :advanceFieldsList="[]" :layoutFieldsList="[]" />
  27. </div>
  28. <template #footer>
  29. <div class="dialog-footer">
  30. <el-button @click="AllMobelVisible.formMobelVisible = false">取消</el-button>
  31. <el-button type="primary" @click="updateJson()" v-loading="AllLoading.createFormLoading">
  32. 保存
  33. </el-button>
  34. <div @click="setData()">设置数据</div>
  35. </div>
  36. </template>
  37. </el-dialog>
  38. </div>
  39. </template>
  40. <script lang="ts" setup>
  41. import { ref, reactive, onMounted, inject } from 'vue';
  42. import { Delete } from '@element-plus/icons-vue'
  43. import { FormInstance, FormRules, ElMessageBox, ElTable } from 'element-plus'
  44. import { DesignForm, GenerateForm, basicFields } from '@zmjs/form-design';
  45. import { useStore } from '@/store/index'
  46. import { post, get } from '@/utils/request';
  47. import { GETLIST, GETLISTBYCODE, UPDATEADDOR } from './api'
  48. import { getFromValue, resetFromValue } from '@/utils/tools'
  49. const { routers } = useStore()
  50. type customFormTab = {
  51. id: string | number,
  52. config: string,
  53. name: string,
  54. code: string,
  55. }
  56. interface createForm {
  57. id: string | number,
  58. code: string,
  59. name: string,
  60. config: string,
  61. companyId: string | number,
  62. createTime: any,
  63. isCurrent: string | number,
  64. }
  65. const globalPopup = inject<GlobalPopup>('globalPopup')
  66. const designForm = ref<any>(null);
  67. const generateForm = ref<any>(null)
  68. const currentRow: any = ref({})
  69. const customFormTableData = ref<customFormTab[]>([])
  70. const customFormTableRef = ref<InstanceType<typeof ElTable>>()
  71. const AllLoading = reactive({
  72. createFormLoading: false,
  73. elementLading: false
  74. })
  75. const AllMobelVisible = reactive({
  76. formMobelVisible: false
  77. })
  78. const listByCodeArr = ref<any>({})
  79. const generateFormData = ref<any>({
  80. list: [],
  81. config: {}
  82. })
  83. const createForm = reactive<createForm>({
  84. id: '',
  85. code: '',
  86. name: '',
  87. config: '',
  88. companyId: '',
  89. createTime: '',
  90. isCurrent: ''
  91. })
  92. // const letBasicFields:Partial<typeof basicFields> = ["input", "select", "textarea", "time", "date"]
  93. const letBasicFields: any = ["input", "select", "textarea", "time", "date"]
  94. function updateJson() {
  95. createForm.config = JSON.stringify(designForm.value.getJson())
  96. let newForm = getFromValue(createForm)
  97. AllLoading.createFormLoading = true
  98. post(UPDATEADDOR, { ...newForm }).then((res: any) => {
  99. AllLoading.createFormLoading = false
  100. if(res.code != 'ok') {
  101. globalPopup?.showError(res.msg)
  102. return
  103. }
  104. globalPopup?.showSuccess ('保存成功')
  105. getListByCode()
  106. AllMobelVisible.formMobelVisible = false
  107. }).catch((err: any) => {
  108. AllLoading.createFormLoading = false
  109. console.log(err)
  110. })
  111. }
  112. function getList() {
  113. post(GETLIST, {}).then((res: any) => {
  114. customFormTableData.value = res.data.map((item: any) => {
  115. return {
  116. id: item.id,
  117. config: item.config,
  118. name: item.name,
  119. code: item.code
  120. }
  121. })
  122. }).catch((err: any) => {
  123. console.log(err)
  124. })
  125. }
  126. function getListByCode() {
  127. AllLoading.elementLading = true
  128. get(`${GETLISTBYCODE}/${currentRow.value.code}`, {}).then((res: any) => {
  129. listByCodeArr.value = res.data[0]
  130. generateFormData.value = JSON.parse(res.data[0].config)
  131. setTimeout(() => {
  132. AllLoading.elementLading = false
  133. }, 1000)
  134. }).catch((_err: any) => {
  135. setTimeout(() => {
  136. AllLoading.elementLading = false
  137. }, 500)
  138. })
  139. }
  140. function showModel() {
  141. const { id, code, name, companyId, createTime, isCurrent, config } = listByCodeArr.value
  142. Object.assign(createForm, {
  143. id,
  144. code,
  145. name,
  146. companyId,
  147. isCurrent,
  148. config
  149. })
  150. AllMobelVisible.formMobelVisible = true
  151. setTimeout(() => {
  152. designForm.value.setJson(JSON.parse(config))
  153. // designForm.value.setJson(JSON.parse(localStorage.getItem('threadDataJson') || ''))
  154. // designForm.value.setJson(JSON.parse(localStorage.getItem('business') || ''))
  155. // designForm.value.setJson(JSON.parse(localStorage.getItem('tasks') || ''))
  156. }, 500)
  157. }
  158. function customFormHandleCurrentChange(val: any) {
  159. currentRow.value = val;
  160. getListByCode()
  161. }
  162. function handleClose(done: any) {
  163. done();
  164. }
  165. function setData() {
  166. // let data = JSON.parse(localStorage.getItem('threadDataJson') || '')
  167. // let data = JSON.parse(localStorage.getItem('business') || '')
  168. // let data = JSON.parse(localStorage.getItem('tasks') || '')
  169. let data = JSON.parse(localStorage.getItem('product') || '')
  170. data.list.forEach((element: any) => {
  171. element.allDisable = true
  172. });
  173. console.log(data, '<=== 赋值好的')
  174. designForm.value.setJson(data)
  175. }
  176. onMounted(() => {
  177. getList()
  178. });
  179. </script>
  180. <style lang="scss" scoped>
  181. .custormForm {
  182. .createForm {
  183. width: 100%;
  184. height: 65vh;
  185. }
  186. .tableHeader {
  187. display: flex;
  188. width: 100%;
  189. justify-content: space-between;
  190. align-items: center;
  191. padding: 10px;
  192. background-color: #F5F7FA;
  193. }
  194. .el-dialog__body {
  195. padding: 0 !important;
  196. }
  197. }
  198. </style>