editTable.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 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. resolve(true)
  97. }, 1000);
  98. })
  99. }
  100. const isFormRender = ref(false)
  101. const handleMessage = (event) => {
  102. let msg = {};
  103. // #ifdef H5
  104. if (event.data && event.data.data && event.data.data.arg) {
  105. msg = event.data.data.arg
  106. }
  107. // #endif
  108. // #ifdef APP-PLUS
  109. msg = event.detail.data[0]
  110. // #endif
  111. if (msg.source === 'web') {
  112. if (msg.type === 'formRender') {
  113. uni.hideLoading();
  114. isFormRender.value = true
  115. }
  116. if (msg.type === 'back') {
  117. toBack() //收到通知,刷新列表去
  118. }
  119. //保存成功
  120. if (msg.type === 'saveSuccess') {
  121. uni.hideLoading();
  122. previewClick()
  123. }
  124. //消息提示
  125. if (msg.type === 'msg') {
  126. uni.hideLoading();
  127. const { title, icon } = msg.data
  128. uni.showToast({
  129. title: title,
  130. duration: 2000,
  131. icon: icon,
  132. mask: true
  133. });
  134. }
  135. }
  136. }
  137. const toBack = () => {
  138. webviewShow.value = false
  139. uni.navigateBack()
  140. }
  141. onUnload(()=>{
  142. // #ifdef H5
  143. window.removeEventListener('message', handleMessage);
  144. // #endif
  145. })
  146. //切换显示模式
  147. const editType = ref('form')
  148. const editTypeClick = () => {
  149. if (isFormRender.value === false) {
  150. errorToast('表单未渲染完成,请稍后再试');
  151. return
  152. }
  153. const type = editType.value === 'form' ? 'table' : 'form'
  154. // #ifdef H5
  155. window.frames["exceliframe"].postMessage({
  156. type: 'editTypeClick',
  157. source: 'app',
  158. data: type
  159. }, envUrl);
  160. // #endif
  161. // #ifdef APP-PLUS
  162. wv.evalJS(`editTypeClick('${type}')`)
  163. // #endif
  164. editType.value = type
  165. }
  166. //复制本表
  167. const toCopyClick = async () => {
  168. if (isFormRender.value === false) {
  169. errorToast('表单未渲染完成,请稍后再试');
  170. return
  171. }
  172. const { pkeyId, status } = pageNode.value
  173. if (!pkeyId) {
  174. errorToast('pkeyId为空');
  175. return
  176. } else if (status === '3') {
  177. errorToast('已上报的资料,不允许复制');
  178. return
  179. }
  180. uni.showLoading({title: '复制中...', mask: true});
  181. const { error, code, msg } = await wbsApi.copeBussTab({
  182. pkeyId: pkeyId,
  183. })
  184. uni.hideLoading();
  185. if (!error && code === 200) {
  186. successToast('复制成功');
  187. setTimeout(() => {
  188. toBack()
  189. }, 1500)
  190. } else {
  191. errorToast('复制失败:' + msg);
  192. }
  193. }
  194. //隐藏表单
  195. const toHideClick = async () => {
  196. if (isFormRender.value === false) {
  197. errorToast('表单未渲染完成,请稍后再试');
  198. return
  199. }
  200. const { pkeyId, status } = pageNode.value
  201. if (!pkeyId) {
  202. errorToast('pkeyId为空');
  203. return
  204. } else if (status === '3') {
  205. errorToast('已上报的资料,不允许隐藏示');
  206. return
  207. }
  208. uni.showLoading({title: '隐藏中...', mask: true});
  209. const { error, code, msg } = await wbsApi.showBussTab({
  210. pkeyId: pkeyId,
  211. status: 2,
  212. })
  213. uni.hideLoading();
  214. if (!error && code === 200) {
  215. successToast('隐藏成功');
  216. setTimeout(() => {
  217. toBack()
  218. }, 1500)
  219. } else {
  220. errorToast('隐藏失败:' + msg);
  221. }
  222. }
  223. //预览表单
  224. const previewClick = async () => {
  225. if (isFormRender.value === false) {
  226. errorToast('表单未渲染完成,请稍后再试');
  227. return
  228. }
  229. const { pkeyId, isBussShow, isTabPdf, pdfUrl } = pageNode.value
  230. if (!pkeyId) {
  231. errorToast('pkeyId为空');
  232. return
  233. } else if (isBussShow === 2 || isTabPdf === 1 || pdfUrl === '') {
  234. errorToast('当前状态无法预览');
  235. return
  236. }
  237. uni.showLoading({title: '获取PDF中...', mask: true});
  238. const { error, code, data, msg } = await wbsApi.getBussPdfInfo({
  239. pkeyId: pkeyId,
  240. })
  241. uni.hideLoading();
  242. if (!error && code === 200 && data) {
  243. toPdfPreview(data).then()
  244. } else {
  245. errorToast('获取失败:' + msg);
  246. }
  247. }
  248. //保存
  249. const formSaveClick = () => {
  250. if (isFormRender.value === false) {
  251. errorToast('表单未渲染完成,请稍后再试');
  252. return
  253. }
  254. uni.showLoading({title: '保存中...', mask: true})
  255. // #ifdef H5
  256. window.frames["exceliframe"].postMessage({
  257. type: 'formSave',
  258. source: 'app',
  259. data: {}
  260. }, envUrl);
  261. // #endif
  262. // #ifdef APP-PLUS
  263. wv.evalJS(`formSave()`)
  264. // #endif
  265. }
  266. //上传
  267. const toFileUp = () => {
  268. if (isFormRender.value === false) {
  269. errorToast('表单未渲染完成,请稍后再试');
  270. return
  271. }
  272. const { pkeyId, status } = pageNode.value
  273. if (!pkeyId) {
  274. errorToast('pkeyId为空');
  275. return
  276. } else if (status === '3') {
  277. errorToast('已上报的资料,不允许上传');
  278. return
  279. }
  280. uni.navigateTo({
  281. url: '/pages/data-fill/fileUp?node=' + encodeURIComponent(JSON.stringify(pageNode.value))
  282. });
  283. }
  284. </script>
  285. <style lang="scss" scoped>
  286. page {
  287. height: 100%;
  288. background: #FAFBFE;
  289. }
  290. .hc-uni-app-table-form {
  291. .title-bar {
  292. position: absolute;
  293. top: 0;
  294. display: flex;
  295. width: 100%;
  296. padding: 15rpx;
  297. background: #E8EEF2;
  298. overflow-x: auto;
  299. white-space: nowrap;
  300. z-index: 9999;
  301. .title-bar-btn {
  302. flex: 1;
  303. color: #6FCC9F;
  304. background-color: #EBF9F4;
  305. height: 58rpx;
  306. font-size: 25rpx;
  307. padding: 0;
  308. margin: 0;
  309. line-height: initial;
  310. border-radius: 6rpx;
  311. display: flex;
  312. justify-content: center;
  313. align-items: center;
  314. &:after {
  315. display: none;
  316. }
  317. }
  318. .title-bar-btn + .title-bar-btn {
  319. margin-left: 16rpx;
  320. }
  321. }
  322. .action-bar {
  323. position: absolute;
  324. bottom: 0;
  325. width: 100%;
  326. display: flex;
  327. background: white;
  328. padding: 28rpx 28rpx calc(var(--window-bottom) + 34rpx);
  329. .action-bar-btn {
  330. flex: 1;
  331. }
  332. }
  333. }
  334. </style>