editTable.vue 9.3 KB

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