data-form.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <div class="hc-uni-app-table-form" :class="[editType]">
  3. <hc-table-form ref="tableFormRef" :form="tableFormInfo" :html="excelHtml" :scroll="false" :pkey="appItem.pkeyId" @render="tableFormRender" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import { onMounted, ref, watch } from 'vue'
  8. import wbsApi from '~api/data-fill/wbs'
  9. import { useAppStore } from '~src/store'
  10. import { getObjVal, getObjValue, isString } from 'js-fast-way'
  11. const props = defineProps({
  12. option: {
  13. type: Object,
  14. default: () => ({}),
  15. },
  16. })
  17. //初始变量
  18. const useAppState = useAppStore()
  19. //基础变量
  20. const appItem = ref(props.option)
  21. const tableFormRef = ref(null)
  22. const editType = ref('form')
  23. //深度监听
  24. watch(() => [
  25. props.option,
  26. ], ([option]) => {
  27. appItem.value = getObjValue(option)
  28. getInitData()
  29. }, { deep: true })
  30. //渲染完成
  31. onMounted(() => {
  32. //设置主题
  33. useAppState.setTheme('light')
  34. useAppState.setThemeVal('light')
  35. if (getObjVal(appItem.value)) {
  36. getInitData()
  37. }
  38. //接受app传递过来的消息
  39. window.addEventListener('message', (event) => {
  40. const { source, data, type } = event.data
  41. if (source === 'app') {
  42. if (type === 'editTypeClick') {
  43. editTypeClick(data)
  44. } else if (type === 'formSave') {
  45. toSaveClick()
  46. }
  47. }
  48. })
  49. })
  50. const getInitData = () => {
  51. //获取相关数据
  52. getWbsContractById()
  53. getDataApi()
  54. editTypeClick('form')
  55. }
  56. const getDataApi = async () => {
  57. if (appItem.value.pkeyId) {
  58. await getTableFormInfo()
  59. await getExcelHtml()
  60. }
  61. }
  62. //获取详情
  63. const wbsInfo = ref({})
  64. const getWbsContractById = async () => {
  65. const { data } = await wbsApi.getWbsContractById({
  66. pKeyId: appItem.value.pkeyId,
  67. })
  68. wbsInfo.value = getObjValue(data)
  69. }
  70. //获取表单初始数据
  71. const getFormDataInit = (data = {}) => {
  72. const { projectId, contractId, classify, treeId, pkeyId } = appItem.value
  73. return {
  74. projectId: projectId,
  75. contractId: contractId,
  76. classify: classify,
  77. pkeyId: pkeyId,
  78. nodeId: treeId,
  79. isRenderForm: false,
  80. ...data,
  81. }
  82. }
  83. //获取已填写的数据
  84. const tableFormInfo = ref({})
  85. const getTableFormInfo = async () => {
  86. const { error, code, data } = await wbsApi.getBussDataInfo({
  87. pkeyId: appItem.value.pkeyId,
  88. }, false)
  89. const resData = getObjVal(data)
  90. if (!error && code === 200 && resData) {
  91. tableFormInfo.value = getFormDataInit(resData)
  92. } else {
  93. tableFormInfo.value = getFormDataInit()
  94. }
  95. }
  96. //获取模板标签数据
  97. const excelHtml = ref('')
  98. const getExcelHtml = async () => {
  99. const { error, code, data } = await wbsApi.getExcelHtml({
  100. pkeyId: appItem.value.pkeyId,
  101. }, false)
  102. const resData = isString(data) ? data || '' : ''
  103. if (!error && code === 200 && resData) {
  104. excelHtml.value = resData
  105. } else {
  106. excelHtml.value = ''
  107. tableFormInfo.value.isRenderForm = false
  108. postMsg('暂无表单', 'error')
  109. }
  110. }
  111. //渲染完成
  112. const tableFormRender = (form) => {
  113. tableFormInfo.value = form
  114. window?.postMessage({
  115. type: 'formRender',
  116. source: 'web',
  117. data: {},
  118. })
  119. }
  120. //切换显示模式
  121. const tableWidth = ref(0)
  122. const editTypeClick = (type) => {
  123. let metaEle = document.getElementsByName('viewport')[0]
  124. if (type === 'form') {
  125. metaEle.setAttribute('content', 'width=device-width,user-scalable=no,initial-scale=1.0')
  126. tableWidth.value = 0
  127. } else if (type === 'table') {
  128. metaEle.setAttribute('content', 'width=1080, initial-scale=0.5,user-scalable=yes')
  129. tableWidth.value = document.body.offsetWidth
  130. }
  131. editType.value = type
  132. }
  133. //保存表单
  134. const toSaveClick = async () => {
  135. const { pkeyId, status } = appItem.value
  136. if (!pkeyId) {
  137. postMsg('pkeyId为空', 'error')
  138. return
  139. } else if (status === '3') {
  140. postMsg('已上报的资料,不允许保存。', 'error')
  141. return
  142. }
  143. const formData = tableFormRef.value?.getFormData()
  144. const { error, code, msg } = await wbsApi.saveExcelBussData(formData, false)
  145. if (!error && code === 200) {
  146. postForm('saveSuccess')
  147. postMsg('保存成功', 'success')
  148. }
  149. }
  150. //发送消息
  151. const postForm = (name, data = {}) => {
  152. window?.postMessage({
  153. type: name,
  154. source: 'web',
  155. data: data,
  156. })
  157. }
  158. //弹出提示
  159. const postMsg = (title, icon) => {
  160. window?.postMessage({
  161. type: 'msg',
  162. source: 'web',
  163. data: {
  164. title: title,
  165. icon: icon,
  166. },
  167. })
  168. }
  169. </script>
  170. <style lang="scss">
  171. @import "~src/styles/uni-app/table-form.scss";
  172. </style>