addFileTransmit.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <template>
  2. <view class="main">
  3. <view class="bg-white default_title">
  4. <u--form :model="inputForm" labelWidth="130px" class="u-form default_title" labelPosition="left"
  5. ref="inputForm">
  6. <u-form-item label="年度" borderBottom prop="yearNum">
  7. <u--input readonly v-model="inputForm.yearNum" placeholder="输入年度" border="none"></u--input>
  8. </u-form-item>
  9. <u-form-item label="卡号" borderBottom prop="cardNum">
  10. <u--input readonly v-model="inputForm.cardNum" placeholder="输入卡号" border="none"></u--input>
  11. </u-form-item>
  12. </u--form>
  13. </view>
  14. <view class="bg-white margin-top main_info">
  15. <u--form :model="inputForm" :rules="rules" labelWidth="130px" class="u-form default_title"
  16. labelPosition="left" ref="inputForm">
  17. <u-form-item required label="来文机关" borderBottom prop="sendingAgency">
  18. <u--input v-model="inputForm.sendingAgency" placeholder="请输入来文机关" border="none"></u--input>
  19. </u-form-item>
  20. <u-form-item label="来文字号" borderBottom prop="docFontSize">
  21. <u--input v-model="inputForm.docFontSize" placeholder="请输入来文字号" border="none"></u--input>
  22. </u-form-item>
  23. <u-form-item label="文件来源" borderBottom prop="fileSource">
  24. <u--input v-model="inputForm.fileSource" placeholder="请输入文件来源" border="none"></u--input>
  25. </u-form-item>
  26. <u-form-item label="成文时间" borderBottom prop="writtenTime">
  27. <jp-datetime-picker :show="iswrite" v-model="inputForm.writtenTime" placeholder="选择成文时间"
  28. mode="date"></jp-datetime-picker>
  29. </u-form-item>
  30. <u-form-item label="收文时间" borderBottom prop="receivingTime">
  31. <jp-datetime-picker :show="isreceive" v-model="inputForm.receivingTime" placeholder="选择收文时间"
  32. mode="date"></jp-datetime-picker>
  33. </u-form-item>
  34. <u-form-item required label="内容摘要" borderBottom prop="contentSummary">
  35. <u--textarea v-model="inputForm.contentSummary" placeholder="请输入内容摘要" border="none"></u--textarea>
  36. </u-form-item>
  37. <u-form-item label="来文附件" prop="attachedDocumentId" labelPosition="top">
  38. <lsj-upload ref="lsjUpload" childId="upload1" width="250rpx" height="200rpx" :option="option"
  39. :debug="false" :instantly="false" @uploadEnd="onuploadEnd" @change="afterRead">
  40. <view class="addfile flex">
  41. <u-icon size="20" bold name="plus"></u-icon>
  42. </view>
  43. </lsj-upload>
  44. <!-- #ifdef H5 -->
  45. <view class="takephoto addfile flex" @click="takePhoto"> <u-icon size="20" bold
  46. name="camera"></u-icon>
  47. </view>
  48. <!-- #endif -->
  49. </u-form-item>
  50. <view class="text-danger" style="font-size: 12px!important;">请确保上传图片内容清晰可见,所有涉密敏感信息不得上传</view>
  51. <view class="other_info">
  52. <view class="other_pdf">
  53. <u-cell-group>
  54. <u-cell v-for="item in fileList" :title="item.name">
  55. <u-icon slot="right-icon" size="28" name="trash-fill"
  56. @click="deleteFile(item)"></u-icon>
  57. </u-cell>
  58. </u-cell-group>
  59. </view>
  60. </view>
  61. <u-upload :fileList="imgList" @delete="deletePic" multiple :maxCount="imgList.length"
  62. :previewFullImage="true"></u-upload>
  63. <view class="submit_btn flex">
  64. <u-button v-if="!loading" type="primary" text="提交新增" @click="formSubmit"></u-button>
  65. <u-button v-if="loading" :loading="loading" type="primary" text="加载中"></u-button>
  66. </view>
  67. </u--form>
  68. </view>
  69. <u-toast ref="uToast"></u-toast>
  70. <u-overlay :show="loading">
  71. <view class="warp">
  72. <view class="rect"><u-button plain loading loadingText="加载中"></u-button></view>
  73. </view>
  74. </u-overlay>
  75. </view>
  76. </template>
  77. <script>
  78. import {
  79. isImageFormat
  80. } from "@/common/util.js"
  81. import BASE_URL from '@/config.js'
  82. import moment from "moment/moment"
  83. import * as $auth from "@/common/auth.js"
  84. import fileService from '@/api/file/fileService.js'
  85. import gwCirculationCard2Service from '@/api/circulation/gwCirculationCard2.js'
  86. export default {
  87. mounted() {
  88. gwCirculationCard2Service.getCardNum().then(data => {
  89. const currentYear = new Date().getFullYear();
  90. this.inputForm.yearNum = currentYear
  91. this.cardNum = data
  92. this.inputForm.cardNum = "[" + currentYear + "]" + data + "号"
  93. this.inputForm.writtenTime = moment().format('YYYY-MM-DD');
  94. this.inputForm.receivingTime = moment().format('YYYY-MM-DD');
  95. })
  96. },
  97. data() {
  98. return {
  99. imgsrc: {},
  100. loading: false,
  101. isreceive: false,
  102. iswrite: false,
  103. fileLists: [],
  104. files: [],
  105. fileList: [],
  106. imgList: [],
  107. cardNum: "",
  108. inputForm: {
  109. id: '',
  110. yearNum: '',
  111. cardNum: '',
  112. sendingAgency: '',
  113. docFontSize: '',
  114. fileSource: '',
  115. writtenTime: '',
  116. receivingTime: '',
  117. contentSummary: '',
  118. attachedDocumentId: '',
  119. remark: '',
  120. state: '1'
  121. },
  122. rules: {
  123. sendingAgency: [{
  124. required: true,
  125. message: '请输入来问机关',
  126. trigger: ['blur', 'change']
  127. }],
  128. contentSummary: [{
  129. required: true,
  130. message: '请输入内容摘要',
  131. trigger: ['blur', 'change']
  132. }]
  133. },
  134. // 上传接口参数
  135. option: {
  136. url: BASE_URL + '/gwfile/upload?uploadPath=filetransmit',
  137. name: 'file',
  138. header: {
  139. "token": $auth.getUserToken()
  140. },
  141. },
  142. }
  143. },
  144. methods: {
  145. showToast(params) {
  146. this.loading = false
  147. this.$refs.uToast.show({
  148. ...params,
  149. complete() {
  150. params.url && uni.redirectTo({
  151. url: params.url
  152. })
  153. }
  154. })
  155. },
  156. // 删除文件
  157. deleteFile(item) {
  158. let that = this
  159. that.fileList = that.fileList.filter(i => !(i.url == item.url))
  160. that.fileLists = that.fileLists.filter(i => !(i.url == item.url))
  161. },
  162. // 删除图片
  163. deletePic(event) {
  164. let that = this
  165. this.imgList = this.imgList.filter(item => item.name != event.file.name)
  166. that.fileLists = that.fileLists.filter(i => !(i.name == event.file.name))
  167. },
  168. // 图片压缩
  169. compressH5(urlData, targetSizeKB, initialQuality = 1.0) {
  170. const maxQuality = 1.0;
  171. const minQuality = 0.0;
  172. const tolerance = 0.01; // 根据需要调整公差
  173. if (!urlData.url) {
  174. this.$set(urlData, "url", urlData.path)
  175. }
  176. let that = this
  177. return new Promise((resolve, reject) => {
  178. let binarySearch = (min, max) => {
  179. const midQuality = (min + max) / 2;
  180. uni.getImageInfo({
  181. src: urlData.url,
  182. success(res) {
  183. const reader = new FileReader();
  184. let img = new Image()
  185. img.src = res.path
  186. img.onload = function() {
  187. const canvas = document.createElement('canvas');
  188. const ctx = canvas.getContext('2d');
  189. canvas.width = img.width;
  190. canvas.height = img.height;
  191. ctx.clearRect(0, 0, canvas.width, canvas.height);
  192. ctx.drawImage(img, 0, 0, canvas.width, canvas
  193. .height);
  194. // 使用异步的 toBlob 方法
  195. canvas.toBlob(async (blob) => {
  196. const fileSizeKB = blob.size /
  197. 1024;
  198. if (Math.abs(fileSizeKB -
  199. targetSizeKB) <
  200. tolerance || max - min <
  201. tolerance) {
  202. // 当前质量足够接近目标大小,使用当前质量解析
  203. reader.readAsDataURL(blob);
  204. reader.onload = () => {
  205. let result = that
  206. .uploadFilePromise(reader
  207. .result)
  208. setTimeout(() => {
  209. resolve(result)
  210. }, 300)
  211. }
  212. } else if (fileSizeKB >
  213. targetSizeKB) {
  214. // 如果文件大小太大,降低质量,继续二分查找
  215. binarySearch(min, midQuality);
  216. } else {
  217. // 如果文件大小太小,增加质量,继续二分查找
  218. binarySearch(midQuality, max);
  219. }
  220. }, urlData.type, midQuality);
  221. };
  222. }
  223. })
  224. }
  225. // 开始二分查找
  226. binarySearch(minQuality, maxQuality);
  227. })
  228. },
  229. // 拍照
  230. takePhoto() {
  231. let that = this
  232. // 调用uniapp的chooseImage API 选择图片
  233. uni.chooseImage({
  234. count: 1, // 默认9, 设置图片的数量
  235. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  236. sourceType: ['camera'], // 可以指定来源是相册还是相机,默认二者都有
  237. success: (chooseImageRes) => {
  238. uni.showLoading({
  239. title: "上传中"
  240. })
  241. const tempFilePaths = chooseImageRes.tempFiles[0];
  242. // 成功选择图片后进行压缩处理
  243. that.compressH5(tempFilePaths, 150).then(result => {
  244. const fileName = result.split(/[/\\=]/).pop();
  245. let item1 = {
  246. name: fileName,
  247. url: BASE_URL + result
  248. }
  249. let item = {
  250. name: fileName,
  251. path: result,
  252. }
  253. that.fileLists.push(item)
  254. that.imgList.push(item1)
  255. uni.hideLoading()
  256. })
  257. },
  258. fail: (error) => {
  259. uni.hideLoading()
  260. }
  261. });
  262. },
  263. // 新增图片
  264. afterRead(files) {
  265. uni.showLoading({
  266. title: "上传中"
  267. })
  268. let lists = [...files.values()]
  269. this.files = files
  270. for (let i = 0; i < lists.length; i++) {
  271. if (lists[i].type == 'success') continue;
  272. this.upload(lists[i].name)
  273. }
  274. },
  275. // APP手动上传
  276. upload(name) {
  277. // name=指定文件名,不指定则上传所有type等于waiting和fail的文件
  278. this.$refs['lsjUpload'].upload(name);
  279. },
  280. onuploadEnd(item) {
  281. console.log(`${item.name}已上传结束,上传状态=${item.type}`);
  282. // 更新当前窗口状态变化的文件
  283. this.files.set(item.name, item);
  284. this.fileLists.push(item)
  285. const fileName = item.name.split(/[/\\=]/).pop();
  286. if (isImageFormat(item.name)) {
  287. let item1 = {
  288. name: fileName,
  289. url: BASE_URL + item.responseText
  290. }
  291. this.imgList.push(item1)
  292. } else {
  293. let a = {
  294. name: fileName,
  295. url: BASE_URL + item.responseText
  296. }
  297. this.fileList.push(a)
  298. }
  299. // 微信小程序Map对象for循环不显示,所以转成普通数组,
  300. // 如果你用不惯Map对象,也可以像这样转普通数组,组件使用Map主要是避免反复文件去重操作
  301. // #ifdef MP-WEIXIN
  302. this.wxFiles = [...this.files.values()];
  303. // #endif
  304. // 强制更新视图
  305. this.$forceUpdate();
  306. // ---可删除--演示判断是否所有文件均已上传成功
  307. let isAll = [...this.files.values()].find(item => item.type !== 'success');
  308. if (!isAll) {
  309. uni.hideLoading()
  310. } else {
  311. console.log(isAll.name + '待上传');
  312. }
  313. },
  314. //
  315. async uploadFilePromise(param) {
  316. return new Promise((resolve, reject) => {
  317. let a = uni.uploadFile({
  318. url: this.BASE_URL + '/gwfile/upload?uploadPath=filetransmit',
  319. filePath: param,
  320. name: 'file',
  321. header: {
  322. "token": $auth.getUserToken()
  323. },
  324. success: (res) => {
  325. setTimeout(() => {
  326. resolve(res.data)
  327. }, 300)
  328. }
  329. })
  330. });
  331. },
  332. // 下载文件
  333. download(param) {
  334. fileService.download(param).then(data)
  335. },
  336. // 表单提交
  337. formSubmit() {
  338. this.loading = true
  339. let auditForm = Object.assign({}, this.inputForm);
  340. let files = []
  341. console.log('this.fileLists', this.fileLists);
  342. this.fileLists.forEach(item => {
  343. files.push(item.path)
  344. })
  345. auditForm.attachedDocumentId = files.join(",");
  346. auditForm.writtenTime = auditForm.writtenTime + " 00:00:00";
  347. auditForm.receivingTime = auditForm.receivingTime + " 00:00:00";
  348. auditForm.cardNum = this.cardNum
  349. this.$refs.inputForm.validate().then(valid => {
  350. if (valid) {
  351. gwCirculationCard2Service.save(auditForm).then((data) => {
  352. let param = {
  353. type: 'success',
  354. message: data,
  355. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
  356. url: "/pages/index/index"
  357. }
  358. this.showToast(param);
  359. }).catch(() => {
  360. let param = {
  361. type: 'error',
  362. message: data,
  363. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
  364. }
  365. this.showToast(param);
  366. })
  367. }
  368. }).catch(e => {
  369. this.loading = false
  370. })
  371. }
  372. }
  373. }
  374. </script>
  375. <style>
  376. .warp {
  377. display: flex;
  378. align-items: center;
  379. justify-content: center;
  380. height: 100%;
  381. }
  382. .default_title {
  383. padding-top: 0px;
  384. }
  385. .main_info {
  386. margin-top: 15px;
  387. }
  388. .u-form-item__body__right__message span {
  389. font-size: 12px !important;
  390. }
  391. .submit_btn {
  392. background-color: #fefefe;
  393. margin-top: 20px;
  394. box-shadow: none;
  395. margin-bottom: 20px;
  396. }
  397. .submit_btn button {
  398. height: 40px;
  399. width: 80%;
  400. border-radius: 50px;
  401. }
  402. .addfile {
  403. width: 80px;
  404. height: 80px;
  405. background-color: #eee;
  406. padding-left: 24%;
  407. margin: 10px;
  408. }
  409. .takephoto {
  410. padding-left: 9%;
  411. }
  412. .other_info {
  413. width: 100%;
  414. }
  415. </style>