addFileTransmit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <view>
  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" labelWidth="130px" class="u-form default_title" labelPosition="left"
  16. ref="inputForm">
  17. <u-form-item 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" @click="openDate(1)">
  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" @click="openDate(2)">
  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 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-form-item>
  43. <view class="other_info">
  44. <view class="other_pdf">
  45. <u-cell-group>
  46. <u-cell v-for="item in fileList" :title="item.name">
  47. <u-icon slot="right-icon" size="28" name="trash-fill"
  48. @click="deleteFile(item)"></u-icon>
  49. </u-cell>
  50. </u-cell-group>
  51. </view>
  52. </view>
  53. <u-upload :fileList="imgList" @delete="deletePic" multiple :maxCount="imgList.length"
  54. :previewFullImage="true"></u-upload>
  55. <u--text size="10" type="error" text="请确保上传图片内容清晰可见,所有涉密敏感信息不得上传"></u--text>
  56. <view class="submit_btn flex">
  57. <u-button v-if="!loading" type="primary" text="提交新增" @click="formSubmit"></u-button>
  58. <u-button v-if="loading" :loading="loading" type="primary" text="加载中"></u-button>
  59. </view>
  60. </u--form>
  61. </view>
  62. <u-toast ref="uToast"></u-toast>
  63. </view>
  64. </template>
  65. <script>
  66. import {
  67. isImageFormat
  68. } from "@/common/util.js"
  69. import BASE_URL from '@/config.js'
  70. import moment from "moment/moment"
  71. import * as $auth from "@/common/auth.js"
  72. import fileService from '@/api/file/fileService.js'
  73. import gwCirculationCard2Service from '@/api/circulation/gwCirculationCard2.js'
  74. export default {
  75. mounted() {
  76. gwCirculationCard2Service.getCardNum().then(data =>{
  77. const currentYear = new Date().getFullYear();
  78. this.inputForm.yearNum = currentYear
  79. this.inputForm.cardNum = "["+currentYear +"]"+data+"号"
  80. this.inputForm.writtenTime = moment().format('YYYY-MM-DD');
  81. this.inputForm.receivingTime = moment().format('YYYY-MM-DD');
  82. })
  83. },
  84. data() {
  85. return {
  86. imgsrc: {},
  87. loading: false,
  88. isreceive: false,
  89. iswrite: false,
  90. fileLists: [],
  91. fileList: [],
  92. imgList: [],
  93. inputForm: {
  94. id: '',
  95. yearNum: '',
  96. cardNum: '',
  97. sendingAgency: '',
  98. docFontSize: '',
  99. fileSource: '',
  100. writtenTime: '',
  101. receivingTime: '',
  102. contentSummary: '',
  103. attachedDocumentId: '',
  104. remark: '',
  105. state: '1'
  106. },
  107. }
  108. },
  109. methods: {
  110. // 打开日期
  111. openDate(type) {
  112. console.log("c====", type);
  113. if (type == 1) {
  114. this.iswrite = !this.iswrite
  115. } else if (type == 2) {
  116. this.isreceive = !this.isreceive
  117. }
  118. },
  119. showToast(params) {
  120. this.$refs.uToast.show({
  121. ...params,
  122. complete() {
  123. params.url && uni.redirectTo({
  124. url: params.url
  125. })
  126. }
  127. })
  128. },
  129. // 删除文件
  130. deleteFile(item) {
  131. let that = this
  132. uni.showModal({
  133. title: '提示',
  134. content: '确认删除该文件',
  135. success: function(res) {
  136. if (res.confirm) {
  137. that.fileLists = that.fileLists.filter(i => !(i.url == item.url))
  138. that.fileList = that.fileList.filter(i => !(i.url == item.url))
  139. } else if (res.cancel) {
  140. console.log('用户点击取消');
  141. }
  142. }
  143. });
  144. },
  145. // 删除图片
  146. deletePic(event) {
  147. this[`imgList${event.name}`].splice(event.index, 1)
  148. this[`fileLists${event.name}`].splice(event.index, 1)
  149. },
  150. // 图片压缩
  151. compressH5(urlData) {
  152. let that = this
  153. return new Promise((resolve, reject) => {
  154. let a = uni.getImageInfo({
  155. src: urlData.url,
  156. success(res) {
  157. let originWidth = res.width; //图片原始宽
  158. let originHeight = res.height; //图片原始高
  159. let maxWidth = 800;
  160. let maxHeight = 800;
  161. let img = new Image()
  162. img.src = res.path
  163. let canvas = document.createElement('canvas');
  164. let ctx = canvas.getContext('2d')
  165. // 目标尺寸
  166. let targetWidth = originWidth;
  167. let targetHeight = originHeight;
  168. canvas.width = targetWidth
  169. canvas.height = targetHeight
  170. // 图片压缩
  171. ctx.drawImage(img, 0, 0, targetWidth, targetHeight)
  172. // canvas对图片进行缩放 0.3是我定义的图片质量,
  173. let base64 = canvas.toDataURL(urlData.type, 0.3);
  174. // 将base64转换为file流,
  175. let file = that.convertBase64UrlToFile({
  176. url: base64,
  177. type: urlData.type,
  178. name: urlData.name
  179. })
  180. urlData.size = file.size
  181. let result = that.uploadFilePromise(urlData)
  182. setTimeout(() => {
  183. resolve(result)
  184. }, 1000)
  185. },
  186. fail() {
  187. uni.showModal({
  188. title: '提示',
  189. content: '图片压缩失败',
  190. showCancel: false
  191. });
  192. }
  193. })
  194. })
  195. },
  196. convertBase64UrlToFile(base64) {
  197. let urlData = base64.url
  198. let type = base64.type
  199. let contentName = base64.name
  200. let bytes = null
  201. if (urlData.split(',').length > 1) { //是否带前缀
  202. bytes = window.atob(urlData.split(',')[1]) // 去掉url的头,并转换为byte
  203. } else {
  204. bytes = window.atob(urlData)
  205. }
  206. // 处理异常,将ascii码小于0的转换为大于0
  207. let ab = new ArrayBuffer(bytes.length)
  208. let ia = new Uint8Array(ab)
  209. for (let i = 0; i < bytes.length; i++) {
  210. ia[i] = bytes.charCodeAt(i)
  211. }
  212. let result = new Blob([ab], {
  213. type: type,
  214. })
  215. let result1 = new File([result], contentName, {
  216. type: type
  217. })
  218. result1.url = window.URL.createObjectURL(result)
  219. return result1
  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) && lists[i].size > (200 * 1024)) {
  239. result = await this.compressH5(lists[i]);
  240. }else {
  241. result = await this.uploadFilePromise(lists[i])
  242. }
  243. let item = this.fileLists[fileListLen]
  244. this.fileLists.splice(fileListLen, 1, Object.assign(item, {
  245. status: 'success',
  246. message: '',
  247. url: result
  248. }))
  249. fileListLen++
  250. if (isImageFormat(item.url)) {
  251. let item1 = {
  252. url: BASE_URL + result
  253. }
  254. this.imgList.push(item1)
  255. } else {
  256. const fileName = item.url.split(/[/\\=]/).pop();
  257. let a = {
  258. name: fileName,
  259. url: item.url
  260. }
  261. this.fileList.push(a)
  262. }
  263. }
  264. uni.hideLoading()
  265. },
  266. async uploadFilePromise(param) {
  267. return new Promise((resolve, reject) => {
  268. let a = uni.uploadFile({
  269. url: this.BASE_URL + '/gwfile/upload?uploadPath=filtransmit',
  270. filePath: param.url,
  271. name: 'file',
  272. header: {
  273. "token": $auth.getUserToken()
  274. },
  275. formData: param,
  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.$refs['inputForm'].validate((valid) => {
  299. // if (valid) {
  300. gwCirculationCard2Service.save(this.inputForm).then((data) => {
  301. let param = {
  302. type: 'success',
  303. message: data,
  304. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
  305. url: "/pages/index/index"
  306. }
  307. this.showToast(param);
  308. }).catch(() => {
  309. let param = {
  310. type: 'error',
  311. message: data,
  312. iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png',
  313. }
  314. this.showToast(param);
  315. })
  316. // }
  317. // })
  318. }
  319. }
  320. }
  321. </script>
  322. <style>
  323. .default_title {
  324. padding-top: 0px;
  325. }
  326. .main_info {
  327. margin-top: 15px;
  328. /* height: 80vh; */
  329. }
  330. .submit_btn {
  331. background-color: #fefefe;
  332. margin-top: 20px;
  333. box-shadow: none;
  334. margin-bottom: 20px;
  335. }
  336. .submit_btn button {
  337. height: 40px;
  338. width: 80%;
  339. border-radius: 30px;
  340. }
  341. .addfile {
  342. width: 80px;
  343. height: 80px;
  344. background-color: #eee;
  345. padding-left: 30%;
  346. /* padding-left: 18%; */
  347. margin: 10px;
  348. }
  349. .other_info {
  350. width: 100%;
  351. }
  352. </style>