editTable.vue 10 KB

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