editTable.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. return Promise.resolve(true)
  157. }
  158. //获取数据详情
  159. const getWbsContractById = async () => {
  160. const { pkeyId } = pageNode.value
  161. const { data } = await wbsApi.getWbsContractById({
  162. pKeyId: pkeyId,
  163. })
  164. //处理数据
  165. const { isBussShow, isTabPdf, pdfUrl, tabFileType } = getObjValue(data)
  166. pageNode.value.isBussShow = isBussShow
  167. pageNode.value.isTabPdf = isTabPdf
  168. pageNode.value.pdfUrl = pdfUrl
  169. pageNode.value.tabFileType = tabFileType
  170. }
  171. //当前节点状态, 1 未填报,2待上报,3已上报
  172. const queryNodeStatus = async () => {
  173. const {classify, treeId, status} = pageNode.value
  174. let url = `blade-business/informationWriteQuery/${classify === 1?'queryNodeStatus': 'queryNodeStatus-jl'}`;
  175. //查询节点状态
  176. const { error, code, data } = await api.post({
  177. url: url,
  178. params: {
  179. primaryKeyId: treeId ?? '',
  180. classify: classify,
  181. }
  182. })
  183. if (!error && code === 200) {
  184. pageNode.value.status = data ?? status
  185. }
  186. }
  187. const toBack = () => {
  188. webviewShow.value = false
  189. uni.navigateBack()
  190. }
  191. onUnload(()=>{
  192. // #ifdef H5
  193. window.removeEventListener('message', handleMessage);
  194. // #endif
  195. })
  196. //切换显示模式
  197. const editType = ref('form')
  198. const editTypeClick = () => {
  199. if (isFormRender.value === false) {
  200. errorToast('表单未渲染完成,请稍后再试');
  201. return
  202. }
  203. const type = editType.value === 'form' ? 'table' : 'form'
  204. // #ifdef H5
  205. window.frames["exceliframe"].postMessage({
  206. type: 'editTypeClick',
  207. source: 'app',
  208. data: type
  209. }, envUrl);
  210. // #endif
  211. // #ifdef APP-PLUS
  212. wv.evalJS(`editTypeClick('${type}')`)
  213. // #endif
  214. editType.value = type
  215. }
  216. //复制本表
  217. const toCopyClick = async () => {
  218. if (isFormRender.value === false) {
  219. errorToast('表单未渲染完成,请稍后再试');
  220. return
  221. }
  222. const { pkeyId, status } = pageNode.value
  223. if (!pkeyId) {
  224. errorToast('pkeyId为空');
  225. return
  226. } else if (status === '3') {
  227. errorToast('已上报的资料,不允许复制');
  228. return
  229. }
  230. uni.showLoading({title: '复制中...', mask: true});
  231. const { error, code, msg } = await wbsApi.copeBussTab({
  232. pkeyId: pkeyId,
  233. })
  234. uni.hideLoading();
  235. if (!error && code === 200) {
  236. successToast('复制成功');
  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. setTimeout(() => {
  267. toBack()
  268. }, 1500)
  269. } else {
  270. errorToast('隐藏失败:' + msg);
  271. }
  272. }
  273. //预览表单
  274. const previewClick = async () => {
  275. if (isFormRender.value === false) {
  276. errorToast('表单未渲染完成,请稍后再试');
  277. return
  278. }
  279. await getNodeStatus()
  280. const { pkeyId, isBussShow, isTabPdf, pdfUrl } = pageNode.value
  281. if (!pkeyId) {
  282. errorToast('pkeyId为空');
  283. return
  284. } else if (isBussShow === 2 || isTabPdf === 1 || pdfUrl === '') {
  285. errorToast('当前状态无法预览');
  286. return
  287. }
  288. uni.showLoading({title: '获取PDF中...', mask: true});
  289. const { error, code, data, msg } = await wbsApi.getBussPdfInfo({
  290. pkeyId: pkeyId,
  291. })
  292. uni.hideLoading();
  293. if (!error && code === 200 && data) {
  294. toPdfPreview(data).then()
  295. } else {
  296. errorToast('获取失败:' + msg);
  297. }
  298. }
  299. //保存
  300. const formSaveClick = () => {
  301. if (isFormRender.value === false) {
  302. errorToast('表单未渲染完成,请稍后再试');
  303. return
  304. }
  305. uni.showLoading({title: '保存中...', mask: true})
  306. // #ifdef H5
  307. window.frames["exceliframe"].postMessage({
  308. type: 'formSave',
  309. source: 'app',
  310. data: {}
  311. }, envUrl);
  312. // #endif
  313. // #ifdef APP-PLUS
  314. wv.evalJS(`formSave()`)
  315. // #endif
  316. }
  317. //上传
  318. const toFileUp = async () => {
  319. if (isFormRender.value === false) {
  320. errorToast('表单未渲染完成,请稍后再试');
  321. return
  322. }
  323. await getNodeStatus()
  324. const { pkeyId, status } = pageNode.value
  325. if (!pkeyId) {
  326. errorToast('pkeyId为空');
  327. return
  328. } else if (status === '3') {
  329. errorToast('已上报的资料,不允许上传');
  330. return
  331. }
  332. uni.navigateTo({
  333. url: '/pages/data-fill/fileUp?node=' + encodeURIComponent(JSON.stringify(pageNode.value))
  334. });
  335. }
  336. </script>
  337. <style lang="scss" scoped>
  338. page {
  339. height: 100%;
  340. background: #FAFBFE;
  341. }
  342. .hc-uni-app-table-form {
  343. .title-bar {
  344. position: absolute;
  345. top: 0;
  346. display: flex;
  347. width: 100%;
  348. padding: 15rpx;
  349. background: #E8EEF2;
  350. overflow-x: auto;
  351. white-space: nowrap;
  352. z-index: 9999;
  353. .title-bar-btn {
  354. flex: 1;
  355. color: #6FCC9F;
  356. background-color: #EBF9F4;
  357. height: 58rpx;
  358. font-size: 25rpx;
  359. padding: 0;
  360. margin: 0;
  361. line-height: initial;
  362. border-radius: 6rpx;
  363. display: flex;
  364. justify-content: center;
  365. align-items: center;
  366. &:after {
  367. display: none;
  368. }
  369. }
  370. .title-bar-btn + .title-bar-btn {
  371. margin-left: 16rpx;
  372. }
  373. }
  374. .action-bar {
  375. position: absolute;
  376. bottom: 0;
  377. width: 100%;
  378. display: flex;
  379. background: white;
  380. padding: 28rpx 28rpx calc(var(--window-bottom) + 34rpx);
  381. .action-bar-btn {
  382. flex: 1;
  383. }
  384. }
  385. }
  386. </style>