editTable.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <hc-sys id="app-sys" class="h-full hc-uni-app-table-form" :isNavBar="false">
  3. <view id="title-bar" class="title-bar z-24">
  4. <button type="primary" class="title-bar-btn" @click="editTypeClick">切换</button>
  5. <button type="primary" class="title-bar-btn" @click="toCopyClick">复制</button>
  6. <button type="primary" class="title-bar-btn" @click="toHideClick">隐藏</button>
  7. <button type="primary" class="title-bar-btn" @click="previewClick">预览</button>
  8. <button type="primary" class="title-bar-btn" @click="toFileUp">{{pageNode.tabFileType === 2?'已上传':'上传'}}</button>
  9. </view>
  10. <template v-if="webviewShow">
  11. <web-view :update-title="false" :webview-styles="webViewStyle" :style="webViewStyle" :src="webSrc" name="exceliframe" @message="handleMessage"/>
  12. </template>
  13. <view id="action-bar" class="action-bar z-24">
  14. <button type="primary" class="action-bar-btn" @click="formSaveClick">保 存</button>
  15. </view>
  16. </hc-sys>
  17. </template>
  18. <script setup>
  19. import {ref, getCurrentInstance} from "vue";
  20. import {onLoad, onShow, onReady, onUnload} from '@dcloudio/uni-app'
  21. import {errorToast, successToast, toPdfPreview, querySelect} from "@/utils/tools";
  22. import {getStorage} from "@/utils/storage";
  23. import {useAppStore} from "@/store";
  24. import wbsApi from '~api/data-fill/wbs';
  25. import {getFormApiUrl} from '@/config/envApi';
  26. import api from '~api/api';
  27. import {getObjValue} from "js-fast-way";
  28. const store = useAppStore()
  29. const instance = getCurrentInstance().proxy
  30. let wv; //计划创建的webview
  31. //初始变量
  32. const contractInfo = ref(store.contractInfo);
  33. const projectId = ref(store.projectId);
  34. const contractId = ref(store.contractId);
  35. const pageNode = ref({});
  36. const webViewStyle = ref({})
  37. const webviewShow = ref(true)
  38. onLoad((option) => {
  39. // #ifdef H5
  40. window.addEventListener('message', handleMessage);
  41. // #endif
  42. const user = encodeURIComponent(JSON.stringify(getStorage('login_user_info')))
  43. if (option.node) {
  44. uni.showLoading({title: '加载中...', mask: true});
  45. const res = JSON.parse(decodeURIComponent(option.node));
  46. uni.setNavigationBarTitle({
  47. title: res.title
  48. })
  49. pageNode.value = res
  50. webSrc.value = `${htmlsrc}&user=${user}&node=${option.node}`
  51. queryNodeStatus()
  52. } else {
  53. errorToast('参数错误');
  54. setTimeout(() => {
  55. toBack()
  56. },1500)
  57. }
  58. })
  59. onShow(() => {
  60. webviewShow.value = true
  61. })
  62. //表格地址
  63. const webSrc = ref('');
  64. const envUrl = getFormApiUrl()
  65. const htmlsrc = `${envUrl}/#/app/table-form?date=${new Date().getTime()}&source=app&type=data-fill`
  66. //渲染完成
  67. onReady(() => {
  68. setWebViewStyle()
  69. })
  70. const setWebViewStyle = async () => {
  71. // #ifdef APP-PLUS
  72. await initWebview()
  73. // #endif
  74. const appSys = await querySelect(instance, 'app-sys')
  75. const appSysHeight = appSys.height
  76. //顶部
  77. const titleBar = await querySelect(instance, 'title-bar')
  78. const titleBarHeight = titleBar?.height ?? 48
  79. // #ifdef H5
  80. webViewStyle.value.top = titleBarHeight + 'px'
  81. // #endif
  82. // #ifdef APP-PLUS
  83. const {statusBarHeight} = uni.getSystemInfoSync()
  84. webViewStyle.value.top = statusBarHeight + 45 + titleBarHeight
  85. // #endif
  86. //底部
  87. const actionBar = await querySelect(instance,'action-bar')
  88. const actionBarHeight = actionBar?.height ?? 80
  89. webViewStyle.value.height = (appSysHeight - titleBarHeight - actionBarHeight - 3) + 'px'
  90. }
  91. //初始化webview
  92. const initWebview = async () => {
  93. return new Promise((resolve) => {
  94. let currentWebview = instance.$scope.$getAppWebview()
  95. //如果是页面初始化调用时,需要延时一下
  96. setTimeout(() => {
  97. wv = currentWebview.children()[0]
  98. wv.setStyle({scalable:true})
  99. // #ifdef APP-PLUS
  100. //ios 禁用缓存,测试生效!!
  101. let cache1 = plus.ios.newObject('NSURLCache');
  102. let cache = plus.ios.invoke(cache1, 'sharedURLCache');
  103. plus.ios.invoke(cache, 'removeAllCachedResponses');
  104. plus.ios.invoke(cache, 'setDiskCapacity:', 0);
  105. plus.ios.invoke(cache, 'setMemoryCapacity:', 0);
  106. //安卓端缓存清理。
  107. plus.cache.clear();
  108. // #endif
  109. resolve(true)
  110. }, 1000);
  111. })
  112. }
  113. const isFormRender = ref(false)
  114. const handleMessage = async (event) => {
  115. let msg = {};
  116. // #ifdef H5
  117. if (event.data && event.data.data && event.data.data.arg) {
  118. msg = event.data.data.arg
  119. }
  120. // #endif
  121. // #ifdef APP-PLUS
  122. msg = event.detail.data[0]
  123. // #endif
  124. if (msg.source === 'web') {
  125. if (msg.type === 'formRender') {
  126. uni.hideLoading();
  127. isFormRender.value = true
  128. }
  129. if (msg.type === 'back') {
  130. toBack() //收到通知,刷新列表去
  131. }
  132. //保存成功
  133. if (msg.type === 'saveSuccess') {
  134. uni.hideLoading();
  135. await getNodeStatus()
  136. await previewClick()
  137. }
  138. //消息提示
  139. if (msg.type === 'msg') {
  140. uni.hideLoading();
  141. const { title, icon } = msg.data
  142. uni.showToast({
  143. title: title,
  144. duration: 2000,
  145. icon: icon,
  146. mask: true
  147. });
  148. }
  149. }
  150. }
  151. const getNodeStatus = async () => {
  152. uni.showLoading({title: '更新数据中...', mask: true});
  153. await queryNodeStatus()
  154. await getWbsContractById()
  155. uni.hideLoading();
  156. }
  157. //获取数据详情
  158. const getWbsContractById = async () => {
  159. const { pKeyId } = pageNode.value
  160. const { data } = await wbsApi.getWbsContractById({
  161. pKeyId: pKeyId,
  162. })
  163. //处理数据
  164. const { isBussShow, isTabPdf, pdfUrl, tabFileType } = getObjValue(data)
  165. pageNode.value.isBussShow = isBussShow
  166. pageNode.value.isTabPdf = isTabPdf
  167. pageNode.value.pdfUrl = pdfUrl
  168. pageNode.value.tabFileType = tabFileType
  169. }
  170. //当前节点状态, 1 未填报,2待上报,3已上报
  171. const queryNodeStatus = async () => {
  172. const {classify, treeId, status} = pageNode.value
  173. let url = `blade-business/informationWriteQuery/${classify === 1?'queryNodeStatus': 'queryNodeStatus-jl'}`;
  174. //查询节点状态
  175. const { error, code, data } = await api.post({
  176. url: url,
  177. params: {
  178. primaryKeyId: treeId ?? '',
  179. classify: classify,
  180. }
  181. })
  182. if (!error && code === 200) {
  183. pageNode.value.status = data ?? status
  184. }
  185. }
  186. const toBack = () => {
  187. webviewShow.value = false
  188. uni.navigateBack()
  189. }
  190. onUnload(()=>{
  191. // #ifdef H5
  192. window.removeEventListener('message', handleMessage);
  193. // #endif
  194. })
  195. //切换显示模式
  196. const editType = ref('form')
  197. const editTypeClick = () => {
  198. if (isFormRender.value === false) {
  199. errorToast('表单未渲染完成,请稍后再试');
  200. return
  201. }
  202. const type = editType.value === 'form' ? 'table' : 'form'
  203. // #ifdef H5
  204. window.frames["exceliframe"].postMessage({
  205. type: 'editTypeClick',
  206. source: 'app',
  207. data: type
  208. }, envUrl);
  209. // #endif
  210. // #ifdef APP-PLUS
  211. wv.evalJS(`editTypeClick('${type}')`)
  212. // #endif
  213. editType.value = type
  214. }
  215. //复制本表
  216. const toCopyClick = async () => {
  217. if (isFormRender.value === false) {
  218. errorToast('表单未渲染完成,请稍后再试');
  219. return
  220. }
  221. const { pkeyId, status } = pageNode.value
  222. if (!pkeyId) {
  223. errorToast('pkeyId为空');
  224. return
  225. } else if (status === '3') {
  226. errorToast('已上报的资料,不允许复制');
  227. return
  228. }
  229. uni.showLoading({title: '复制中...', mask: true});
  230. const { error, code, msg } = await wbsApi.copeBussTab({
  231. pkeyId: pkeyId,
  232. })
  233. uni.hideLoading();
  234. if (!error && code === 200) {
  235. successToast('复制成功');
  236. getNodeStatus().then()
  237. setTimeout(() => {
  238. toBack()
  239. }, 1500)
  240. } else {
  241. errorToast('复制失败:' + msg);
  242. }
  243. }
  244. //隐藏表单
  245. const toHideClick = async () => {
  246. if (isFormRender.value === false) {
  247. errorToast('表单未渲染完成,请稍后再试');
  248. return
  249. }
  250. const { pkeyId, status } = pageNode.value
  251. if (!pkeyId) {
  252. errorToast('pkeyId为空');
  253. return
  254. } else if (status === '3') {
  255. errorToast('已上报的资料,不允许隐藏示');
  256. return
  257. }
  258. uni.showLoading({title: '隐藏中...', mask: true});
  259. const { error, code, msg } = await wbsApi.showBussTab({
  260. pkeyId: pkeyId,
  261. status: 2,
  262. })
  263. uni.hideLoading();
  264. if (!error && code === 200) {
  265. successToast('隐藏成功');
  266. getNodeStatus().then()
  267. setTimeout(() => {
  268. toBack()
  269. }, 1500)
  270. } else {
  271. errorToast('隐藏失败:' + msg);
  272. }
  273. }
  274. //预览表单
  275. const previewClick = async () => {
  276. await getNodeStatus()
  277. if (isFormRender.value === false) {
  278. errorToast('表单未渲染完成,请稍后再试');
  279. return
  280. }
  281. const { pkeyId, isBussShow, isTabPdf, pdfUrl } = pageNode.value
  282. if (!pkeyId) {
  283. errorToast('pkeyId为空');
  284. return
  285. } else if (isBussShow === 2 || isTabPdf === 1 || pdfUrl === '') {
  286. errorToast('当前状态无法预览');
  287. return
  288. }
  289. uni.showLoading({title: '获取PDF中...', mask: true});
  290. const { error, code, data, msg } = await wbsApi.getBussPdfInfo({
  291. pkeyId: pkeyId,
  292. })
  293. uni.hideLoading();
  294. if (!error && code === 200 && data) {
  295. toPdfPreview(data).then()
  296. } else {
  297. errorToast('获取失败:' + msg);
  298. }
  299. }
  300. //保存
  301. const formSaveClick = () => {
  302. if (isFormRender.value === false) {
  303. errorToast('表单未渲染完成,请稍后再试');
  304. return
  305. }
  306. uni.showLoading({title: '保存中...', mask: true})
  307. // #ifdef H5
  308. window.frames["exceliframe"].postMessage({
  309. type: 'formSave',
  310. source: 'app',
  311. data: {}
  312. }, envUrl);
  313. // #endif
  314. // #ifdef APP-PLUS
  315. wv.evalJS(`formSave()`)
  316. // #endif
  317. }
  318. //上传
  319. const toFileUp = async () => {
  320. if (isFormRender.value === false) {
  321. errorToast('表单未渲染完成,请稍后再试');
  322. return
  323. }
  324. await getNodeStatus()
  325. const { pkeyId, status } = pageNode.value
  326. if (!pkeyId) {
  327. errorToast('pkeyId为空');
  328. return
  329. } else if (status === '3') {
  330. errorToast('已上报的资料,不允许上传');
  331. return
  332. }
  333. uni.navigateTo({
  334. url: '/pages/data-fill/fileUp?node=' + encodeURIComponent(JSON.stringify(pageNode.value))
  335. });
  336. }
  337. </script>
  338. <style lang="scss" scoped>
  339. page {
  340. height: 100%;
  341. background: #FAFBFE;
  342. }
  343. .hc-uni-app-table-form {
  344. .title-bar {
  345. position: absolute;
  346. top: 0;
  347. display: flex;
  348. width: 100%;
  349. padding: 15rpx;
  350. background: #E8EEF2;
  351. overflow-x: auto;
  352. white-space: nowrap;
  353. z-index: 9999;
  354. .title-bar-btn {
  355. flex: 1;
  356. color: #6FCC9F;
  357. background-color: #EBF9F4;
  358. height: 58rpx;
  359. font-size: 25rpx;
  360. padding: 0;
  361. margin: 0;
  362. line-height: initial;
  363. border-radius: 6rpx;
  364. display: flex;
  365. justify-content: center;
  366. align-items: center;
  367. &:after {
  368. display: none;
  369. }
  370. }
  371. .title-bar-btn + .title-bar-btn {
  372. margin-left: 16rpx;
  373. }
  374. }
  375. .action-bar {
  376. position: absolute;
  377. bottom: 0;
  378. width: 100%;
  379. display: flex;
  380. background: white;
  381. padding: 28rpx 28rpx calc(var(--window-bottom) + 34rpx);
  382. .action-bar-btn {
  383. flex: 1;
  384. }
  385. }
  386. }
  387. </style>