addFileTransmit.vue 13 KB

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