log-form.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.excelId" @render="tableFormRender" />
  4. </div>
  5. </template>
  6. <script setup>
  7. import { onMounted, ref, watch } from 'vue'
  8. import { useAppStore } from '~src/store'
  9. import queryApi from '~api/ledger/query'
  10. import { getArrValue, 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. getDataApi()
  29. }, { deep: true })
  30. //渲染完成
  31. onMounted(() => {
  32. //设置主题
  33. useAppState.setTheme('light')
  34. useAppState.setThemeVal('light')
  35. if (getObjVal(appItem.value)) {
  36. getDataApi()
  37. }
  38. editTypeClick('form')
  39. //接受app传递过来的消息
  40. window.addEventListener('message', (event) => {
  41. if (event.data.source === 'app') {
  42. setMessage(event.data)
  43. }
  44. })
  45. })
  46. //设置消息
  47. const setMessage = ({ data, type }) => {
  48. if (type === 'editTypeClick') {
  49. editTypeClick(data)
  50. } else if (type === 'formSave') {
  51. toSaveClick()
  52. } else if (type === 'pageTap') {
  53. getBussDataInfo(data)
  54. } else if (type === 'addForm') {
  55. addTableFormClick()
  56. } else if (type === 'delForm') {
  57. delTableFormClick()
  58. } else if (type === 'linkIds') {
  59. let ids = data ? JSON.parse(data) : []
  60. linkTableFormClick(ids)
  61. } else if (type === 'copyFormData') {
  62. copyTableFormClick()
  63. } else if (type === 'getPdfUrl') {
  64. getBussPdfInfo()
  65. }
  66. }
  67. const getDataApi = async () => {
  68. const { excelId } = appItem.value
  69. if (excelId) {
  70. await getTableFormInfo()
  71. await getExcelHtml()
  72. }
  73. }
  74. //获取表单初始数据
  75. const getFormDataInit = (data = {}) => {
  76. const { projectId, contractId, excelId, date } = appItem.value
  77. return {
  78. linkTabIds: [],
  79. ...data,
  80. projectId: projectId,
  81. contractId: contractId,
  82. recordTime: date,
  83. pkeyId: excelId,
  84. isTheLog: '1',
  85. theLogId: '',
  86. classify: 1,
  87. }
  88. }
  89. //获取已填写的数据
  90. const formLogDataList = ref([])
  91. const getTableFormInfo = async () => {
  92. const { contractId, excelId, pkeyId, date } = appItem.value
  93. const { data } = await queryApi.getTheLogBusinessData({
  94. contractId: contractId,
  95. pkeyId: excelId,
  96. nodePrimaryKeyId: pkeyId,
  97. recordTime: date,
  98. theLogId: '',
  99. }, false)
  100. let res = getArrValue(data), formArrData = []
  101. if (res.length > 0) {
  102. for (let i = 0; i < res.length; i++) {
  103. formArrData.push(getFormDataInit(res[i]))
  104. }
  105. } else {
  106. formArrData.push(getFormDataInit())
  107. }
  108. formLogDataList.value = formArrData
  109. setFormLength()
  110. getBussDataInfo()
  111. }
  112. //获取表单初始数据
  113. const formLogIndex = ref(0)
  114. const tableFormInfo = ref({})
  115. const getBussDataInfo = (index = 0) => {
  116. const formLog = formLogDataList.value
  117. const info = getObjValue(formLog[index])
  118. formLogIndex.value = index
  119. if (getObjVal(info)) {
  120. tableFormInfo.value = info
  121. } else {
  122. tableFormInfo.value = {}
  123. }
  124. window?.postMessage({
  125. type: 'formIndex',
  126. source: 'web',
  127. data: {
  128. id: info?.id ?? '',
  129. page: index + 1,
  130. },
  131. })
  132. }
  133. //获取模板标签数据
  134. const excelHtml = ref('')
  135. const isTableForm = ref(false)
  136. const getExcelHtml = async () => {
  137. const { contractId, excelId } = appItem.value
  138. if (excelId) {
  139. //获取数据
  140. const { error, code, data } = await queryApi.getExcelHtml({
  141. contractId: contractId,
  142. pkeyId: excelId,
  143. }, false)
  144. //处理数据
  145. const resData = isString(data) ? data || '' : ''
  146. if (!error && code === 200 && resData) {
  147. excelHtml.value = resData
  148. isTableForm.value = true
  149. } else {
  150. excelHtml.value = ''
  151. isTableForm.value = false
  152. postMsg('暂无表单', 'error')
  153. }
  154. } else {
  155. isTableForm.value = false
  156. postMsg('参数异常', 'error')
  157. }
  158. }
  159. //渲染完成
  160. const tableFormRender = (form) => {
  161. tableFormInfo.value = form
  162. window?.postMessage({
  163. type: 'formRender',
  164. source: 'web',
  165. data: {},
  166. })
  167. }
  168. //新增表格
  169. const addTableFormClick = () => {
  170. const defaultData = getFormDataInit()
  171. formLogDataList.value.push(defaultData)
  172. const index = formLogDataList.value.length - 1
  173. setFormLength()
  174. getBussDataInfo(index)
  175. postMsg('新增成功', 'success')
  176. }
  177. //删除当前页
  178. const delTableFormClick = () => {
  179. const index = formLogIndex.value
  180. formLogDataList.value.splice(index, 1)
  181. const logIndex = index <= 0 ? 0 : index - 1
  182. formLogIndex.value = logIndex
  183. setFormLength()
  184. getBussDataInfo(logIndex)
  185. postMsg('删除成功', 'success')
  186. }
  187. //复制当前表格及内容
  188. const copyTableFormClick = () => {
  189. const index = formLogIndex.value
  190. const formLog = formLogDataList.value
  191. const info = getObjValue(formLog[index])
  192. const defaultData = getFormDataInit(info)
  193. formLogDataList.value.push({
  194. ...defaultData,
  195. id: '',
  196. })
  197. setFormLength()
  198. getBussDataInfo(formLogDataList.value.length - 1)
  199. postMsg('复制成功', 'success')
  200. }
  201. //更新表单数量
  202. const setFormLength = () => {
  203. window?.postMessage({
  204. type: 'formLength',
  205. source: 'web',
  206. data: formLogDataList.value.length,
  207. })
  208. }
  209. //更新关联工序
  210. const linkTableFormClick = (data) => {
  211. const index = formLogIndex.value
  212. const formLog = formLogDataList.value
  213. const info = getObjValue(formLog[index])
  214. info.linkTabIds = data
  215. formLogDataList.value[index] = info
  216. tableFormInfo.value.linkTabIds = data
  217. }
  218. //预览PDF
  219. const getBussPdfInfo = async () => {
  220. const { contractId, excelId, pkeyId, date } = appItem.value
  221. const { data } = await queryApi.getBussPdfInfo({
  222. contractId: contractId,
  223. pkeyId: excelId,
  224. nodePrimaryKeyId: pkeyId,
  225. recordTime: date,
  226. theLogId: '',
  227. }, false)
  228. //处理数据
  229. const resData = isString(data) ? data ?? '' : ''
  230. window?.postMessage({
  231. type: 'formPdfUrl',
  232. source: 'web',
  233. data: resData,
  234. })
  235. }
  236. //切换显示模式
  237. const tableWidth = ref(0)
  238. const editTypeClick = (type) => {
  239. let metaEle = document.getElementsByName('viewport')[0]
  240. if (type === 'form') {
  241. metaEle.setAttribute('content', 'width=device-width,user-scalable=no,initial-scale=1.0')
  242. tableWidth.value = 0
  243. } else if (type === 'table') {
  244. metaEle.setAttribute('content', 'width=1080, initial-scale=0.5,user-scalable=yes')
  245. tableWidth.value = document.body.offsetWidth
  246. }
  247. editType.value = type
  248. }
  249. //保存表单
  250. const toSaveClick = async () => {
  251. const formLog = formLogDataList.value
  252. let isLink = await isLinkTabIds(formLog)
  253. if (!isLink) {
  254. postMessage({
  255. type: 'saveRes',
  256. data: false,
  257. })
  258. return
  259. }
  260. //发起请求
  261. const { error, code } = await queryApi.saveExcelBussData({
  262. dataInfo: { orderList: formLog },
  263. }, false)
  264. if (!error && code === 200) {
  265. postMessage({
  266. type: 'saveRes',
  267. data: true,
  268. })
  269. postMsg('保存成功', 'success')
  270. getBussPdfInfo().then()
  271. } else {
  272. postMessage({
  273. type: 'saveRes',
  274. data: false,
  275. })
  276. postMsg('保存失败', 'error')
  277. }
  278. }
  279. //判断工序节点
  280. const isLinkTabIds = async (data) => {
  281. const { nodeType } = appItem.value
  282. if (nodeType === 7 || nodeType === 11) {
  283. const isLink = data.some(({ linkTabIds }) => {
  284. const linkIds = getArrValue(linkTabIds)
  285. return linkIds.length > 0
  286. })
  287. if (isLink) {
  288. return true
  289. } else {
  290. postMsg('请先关联工序', 'error')
  291. return false
  292. }
  293. } else {
  294. return true
  295. }
  296. }
  297. //发送消息
  298. const postMessage = ({ type, data = {} }) => {
  299. window?.postMessage({
  300. type: type,
  301. source: 'web',
  302. data: data,
  303. })
  304. }
  305. //弹出提示
  306. const postMsg = (title, icon) => {
  307. window?.postMessage({
  308. type: 'msg',
  309. source: 'web',
  310. data: {
  311. title: title,
  312. icon: icon,
  313. },
  314. })
  315. }
  316. </script>
  317. <style lang="scss">
  318. @import "~src/styles/uni-app/table-form.scss";
  319. </style>