form-item.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <template>
  2. <HcTableForm
  3. ref="tableFormRef"
  4. :cols="colsKeys"
  5. :form="tableFormInfo"
  6. :height="heights"
  7. :html="excelHtml"
  8. :loading="loading"
  9. :pid="activeKey"
  10. :pkey="keyId"
  11. :scroll="scroll"
  12. :width="widths"
  13. @excelBodyTap="excelTableFormClick"
  14. @render="tableFormRender"
  15. @rightTap="tableFormRightTap"
  16. />
  17. </template>
  18. <script setup>
  19. import { onMounted, ref, watch } from 'vue'
  20. import { useAppStore } from '~src/store'
  21. import landApi from '~api/agree/land.js'
  22. import { deepClone, getArrValue, getObjVal, isString } from 'js-fast-way'
  23. //初始
  24. const props = defineProps({
  25. areaId: { // 树节点
  26. type: [String, Number],
  27. default: '',
  28. },
  29. kid: { // pkeyId
  30. type: [String, Number],
  31. default: '',
  32. },
  33. classify: { // 类型1新增,2是编辑
  34. type: [String, Number],
  35. default: '',
  36. },
  37. scroll: {
  38. type: Boolean,
  39. default: true,
  40. },
  41. height: {
  42. type: String,
  43. default: '100%',
  44. },
  45. width: {
  46. type: String,
  47. default: 'auto',
  48. },
  49. datas: {
  50. type: Object,
  51. default: () => ({}),
  52. },
  53. pid: { // 折叠ID
  54. type: String,
  55. default: '',
  56. },
  57. tableId:{
  58. type: [String, Number],
  59. default: '', //表单initTableId
  60. },
  61. agreementId:{
  62. type: [String, Number],
  63. default: '', //协议ID
  64. },
  65. })
  66. //事件
  67. const emit = defineEmits(['rightTap', 'render', 'excelBodyTap'])
  68. //初始变量
  69. const useAppState = useAppStore()
  70. const projectId = ref(useAppState.getProjectId)
  71. const contractId = ref(useAppState.getContractId)
  72. const keyId = ref(props.kid ? props.kid + '' : '')
  73. const areaId = ref(props.areaId)
  74. const agreementId = ref(props.agreementId)
  75. const classify = ref(props.classify)
  76. const loading = ref(false)
  77. const changeData = ref(props.datas)
  78. const heights = ref(props.height)
  79. const widths = ref(props.width)
  80. const activeKey = ref(props.pid)
  81. const tableFormRef = ref(null)
  82. const tableId = ref(props.tableId)
  83. //监听
  84. watch(() => [
  85. useAppState.getProjectId,
  86. useAppState.getContractId,
  87. props.areaId,
  88. props.kid,
  89. props.classify,
  90. props.nodeName,
  91. props.height,
  92. props.width,
  93. props.pid,
  94. props.tableId,
  95. props.agreementId,
  96. ], (
  97. [project_id, contract_id, area_id, key_id, cid, height, width, pid, , table_id, aeement_id],
  98. ) => {
  99. projectId.value = project_id
  100. contractId.value = contract_id
  101. areaId.value = area_id
  102. keyId.value = key_id ? key_id + '' : ''
  103. classify.value = cid
  104. heights.value = height
  105. widths.value = width
  106. activeKey.value = pid
  107. tableId.value = table_id
  108. agreementId.value = aeement_id
  109. })
  110. //深度监听变动的对象数据
  111. watch(() => [
  112. props.datas,
  113. ], ([data]) => {
  114. changeData.value = data
  115. setFormChangeData(data)
  116. }, { deep: true })
  117. //渲染完成
  118. onMounted(async () => {
  119. loading.value = true
  120. //获取已填写的数据
  121. await getTableFormInfo(keyId.value)
  122. //按键key列表
  123. await getHtmlBussColsApi(keyId.value)
  124. //渲染表单
  125. await getExcelHtml(keyId.value)
  126. loading.value = false
  127. })
  128. //表单被点击
  129. const excelTableFormClick = (item) => {
  130. emit('excelBodyTap', item)
  131. }
  132. //获取表单初始数据
  133. const getFormDataInit = () => {
  134. return {
  135. // projectId: projectId.value,
  136. // contractId: contractId.value,
  137. // classify: classify.value,
  138. // pkeyId: keyId.value,
  139. // nodeId: treeId.value,
  140. // isRenderForm: false,
  141. agreementId: agreementId.value, // 协议的id,新增协议为null
  142. areaId:areaId.value, //当前树节点id
  143. projectId: projectId.value,
  144. tableId:tableId.value, //表单的tableId
  145. isRenderForm: false,
  146. linkId: keyId.value,
  147. }
  148. }
  149. //获取已填写的数据
  150. const tableFormInfo = ref({})
  151. const getTableFormInfo = async (pkeyId) => {
  152. if (pkeyId) {
  153. const { error, code, data } = await landApi.getBussInfo({
  154. id: pkeyId,
  155. }, false)
  156. const resData = getObjVal(data)
  157. if (!error && code === 200 && resData) {
  158. tableFormInfo.value = {
  159. ...resData,
  160. ...getFormDataInit(),
  161. ...changeData.value,
  162. }
  163. } else {
  164. tableFormInfo.value = {
  165. ...getFormDataInit(),
  166. ...changeData.value,
  167. }
  168. }
  169. } else {
  170. tableFormInfo.value = {}
  171. window?.$message?.warning('pkeyId为空')
  172. }
  173. }
  174. //获取按键切换输入框的key列表
  175. const colsKeys = ref([])
  176. const getHtmlBussColsApi = async (pkeyId) => {
  177. if (pkeyId) {
  178. const { error, code, data } = await landApi.getBussCols({
  179. id: pkeyId,
  180. }, false)
  181. if (!error && code === 200) {
  182. let keys = getArrValue(data)
  183. for (let i = 0; i < keys.length; i++) {
  184. if (keys[i].length <= 0) {
  185. keys.splice(i, 1)
  186. }
  187. }
  188. colsKeys.value = keys
  189. } else {
  190. colsKeys.value = []
  191. }
  192. } else {
  193. colsKeys.value = []
  194. }
  195. }
  196. //获取模板标签数据
  197. const excelHtml = ref('')
  198. const getExcelHtml = async (pkeyId) => {
  199. if (pkeyId) {
  200. const { error, code, data } = await landApi.getExcelHtml({
  201. id: pkeyId,
  202. }, false)
  203. const resData = isString(data) ? data || '' : ''
  204. if (!error && code === 200 && resData) {
  205. excelHtml.value = resData
  206. } else {
  207. excelHtml.value = ''
  208. tableFormInfo.value.isRenderForm = false
  209. window?.$message?.warning('暂无表单')
  210. }
  211. } else {
  212. excelHtml.value = ''
  213. tableFormInfo.value.isRenderForm = false
  214. window?.$message?.warning('pkeyId为空')
  215. }
  216. }
  217. //渲染完成
  218. const tableFormRender = (form) => {
  219. tableFormInfo.value = form
  220. emit('render', form)
  221. }
  222. //右键点击
  223. const tableFormRightTap = (item) => {
  224. emit('rightTap', item)
  225. }
  226. //设置数据
  227. const setFormChangeData = (data) => {
  228. const form = deepClone(tableFormInfo.value)
  229. tableFormInfo.value = { ...form, ...data }
  230. }
  231. const getFormData = () => {
  232. return tableFormInfo.value
  233. //return tableFormRef.value?.getFormData()
  234. }
  235. const setFormData = (data) => {
  236. setFormChangeData(data)
  237. tableFormRef.value?.setFormData(tableFormInfo.value)
  238. }
  239. const getRegExpJson = () => {
  240. return tableFormRef.value?.getRegExpJson()
  241. }
  242. const isFormRegExp = async () => {
  243. return await tableFormRef.value?.isFormRegExp()
  244. }
  245. //按下ctrl键
  246. const setIsCtrlKey = (data) => {
  247. tableFormRef.value?.setIsCtrlKey(data)
  248. }
  249. //按下复制快捷键
  250. const setCopyKeyList = (event) => {
  251. tableFormRef.value?.setCopyKeyList(event)
  252. }
  253. //按下粘贴快捷键
  254. const setPasteKeyList = async (event) => {
  255. await tableFormRef.value?.setPasteKeyList(event)
  256. }
  257. // 暴露出去
  258. defineExpose({
  259. getFormData,
  260. setFormData,
  261. getRegExpJson,
  262. isFormRegExp,
  263. setIsCtrlKey,
  264. setCopyKeyList,
  265. setPasteKeyList,
  266. })
  267. </script>