addFileTransmit.vue 11 KB

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