index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <view class="jnpf-file">
  3. <view class="jnpf-file-box" :style="{textAlign:align}">
  4. <view v-if="!detailed &&!disabled " class="jnpf-file-box-line">
  5. <CommentLsjUpload :ref="lsjUpload" :childId="childId" :width="width" :height="height" :option="option"
  6. :size="fileSize" :formats="getFormats" :instantly="instantly" @uploadEnd="onuploadEnd"
  7. :lsjUpload="lsjUpload" v-if="!disabled"
  8. :currentCount="currentCount">
  9. <view class="icon-ym icon-ym-comment-file" size="mini"></view>
  10. </CommentLsjUpload>
  11. <view class="icon-ym icon-ym-comment-file" size="mini" v-else @click="onCountOver"></view>
  12. </view>
  13. <view class="icon-ym icon-ym-comment-file" size="mini" v-if="disabled" @click="onCountOver"></view>
  14. <view class="tipText u-p-l-20">
  15. {{tipText}}
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import CommentLsjUpload from './lsj-upload/lsj-upload.vue'
  22. import {
  23. getDownloadUrl
  24. } from '@/api/common'
  25. const imgTypeList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
  26. export default {
  27. components: {
  28. CommentLsjUpload
  29. },
  30. name: 'jnpf-upload-img',
  31. inheritAttrs: false,
  32. props: {
  33. modelValue: {
  34. type: Array,
  35. default: () => ([])
  36. },
  37. limit: {
  38. type: [Number, String],
  39. default: 9
  40. },
  41. fileSize: {
  42. type: Number,
  43. default: 10
  44. },
  45. sizeUnit: {
  46. type: String,
  47. default: 'MB'
  48. },
  49. accept: {
  50. type: String,
  51. default: ''
  52. },
  53. pathType: {
  54. type: String,
  55. default: 'defaultPath'
  56. },
  57. tipText: {
  58. type: String,
  59. default: ''
  60. },
  61. isAccount: {
  62. type: Number,
  63. default: 0
  64. },
  65. folder: {
  66. type: String,
  67. default: ''
  68. },
  69. vModel: {
  70. type: String,
  71. default: ''
  72. },
  73. detailed: {
  74. type: Boolean,
  75. default: false
  76. },
  77. align: {
  78. type: String,
  79. default: 'right'
  80. },
  81. currentCount: {
  82. type: Number,
  83. default: 0
  84. }
  85. },
  86. data() {
  87. return {
  88. percent: '',
  89. fileList: [],
  90. // 上传接口参数
  91. option: {},
  92. params: {
  93. pathType: this.pathType,
  94. isAccount: this.isAccount,
  95. folder: this.folder
  96. },
  97. // 选择文件后是否立即自动上传,true=选择后立即上传
  98. instantly: true,
  99. size: 30,
  100. list: [],
  101. deletable: false,
  102. childId: 'upload' + this.$u.guid(3, false, 2),
  103. lsjUpload: 'lsjUpload' + this.$u.guid(3, false, 2),
  104. width: '48rpx',
  105. height: '48rpx',
  106. disabled: false
  107. }
  108. },
  109. computed: {
  110. baseURL() {
  111. return this.define.baseURL
  112. },
  113. comUploadUrl() {
  114. return this.define.comUploadUrl
  115. },
  116. getFormats() {
  117. let formats = this.accept
  118. formats = formats.replace("image/*", 'png,jpg,jpeg,bmp,gif,webp,psd,svg,tiff')
  119. formats = formats.replace("video/*", 'avi,wmv,mpg,mpeg,mov,rm,ram,swf,flv,mp4,wma,rm,rmvb,flv,mpg,mkv')
  120. formats = formats.replace("audio/*", 'mp3,wav,aif,midi,m4a')
  121. return formats
  122. },
  123. },
  124. created() {
  125. const token = uni.getStorageSync('token')
  126. this.option = {
  127. url: this.baseURL + '/api/file/Uploader/annex',
  128. name: 'file',
  129. header: {
  130. 'Authorization': token,
  131. 'uid': '27682',
  132. 'client': 'app',
  133. 'accountid': 'DP',
  134. },
  135. data: this.params
  136. }
  137. },
  138. watch: {
  139. modelValue: {
  140. handler(val) {
  141. this.fileList = JSON.parse(JSON.stringify(val));
  142. },
  143. immediate: true
  144. }
  145. },
  146. methods: {
  147. onCountOver() {
  148. uni.showToast({
  149. title: `最多可以上传${this.limit}个文件`,
  150. icon: 'none'
  151. });
  152. return;
  153. },
  154. // 某文件上传结束回调(成功失败都回调)
  155. onuploadEnd(item) {
  156. if (this.currentCount >= this.limit) {
  157. // this.disabled = true;
  158. return this.$u.toast('最多可以上传' + this.limit + '个文件')
  159. }
  160. if (item['responseText']) {
  161. let response = JSON.parse(item.responseText)
  162. let count = this.fileList.length
  163. if (count >= this.limit) {
  164. // this.disabled = true;
  165. return this.$u.toast('最多可以上传' + this.limit + '个文件')
  166. }
  167. if (response.code != 200) return this.$u.toast(response.msg)
  168. this.fileList.push({
  169. name: item.name,
  170. fileId: response.data.name,
  171. url: response.data.url,
  172. fileExtension: response.data.fileExtension,
  173. fileSize: response.data.fileSize
  174. })
  175. this.$emit('update:modelValue', this.fileList)
  176. this.$emit('change', this.fileList)
  177. }
  178. this.$forceUpdate();
  179. },
  180. downLoad(item) {
  181. if (item.fileExtension && imgTypeList.includes(item.fileExtension)) return this.previewImage(item)
  182. // #ifdef MP
  183. this.previewFile(item)
  184. // #endif
  185. // #ifndef MP
  186. getDownloadUrl('annex', item.fileId).then(res => {
  187. const fileUrl = this.baseURL + res.data.url + '&name=' + item.name;
  188. // #ifdef H5
  189. window.location.href = fileUrl;
  190. // #endif
  191. // #ifdef APP-PLUS
  192. this.downloadFile(res.data.url);
  193. // #endif
  194. })
  195. // #endif
  196. },
  197. // 移除某个文件
  198. delFile(files) {
  199. this.fileList = files
  200. // if(this.fileList.length >= this.limit) {
  201. // this.disabled = true
  202. // }else{
  203. // this.disabled = false
  204. // }
  205. },
  206. previewFile(item) {
  207. let fileTypes = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx']
  208. let url = item.url
  209. let fileType = url.split('.')[1]
  210. if (fileTypes.includes(fileType)) {
  211. uni.downloadFile({
  212. url: this.baseURL + url,
  213. success: (res) => {
  214. var filePath = res.tempFilePath;
  215. uni.openDocument({
  216. filePath: encodeURI(filePath),
  217. showMenu: true,
  218. fileType: fileType,
  219. success: (res) => {
  220. console.log('打开文档成功');
  221. },
  222. fail(err) {
  223. console.log('小程序', err);
  224. }
  225. });
  226. }
  227. });
  228. } else {
  229. this.$u.toast(
  230. '该文件类型无法打开'
  231. )
  232. }
  233. },
  234. previewImage(item) {
  235. if (!item.url) return
  236. const url = this.baseURL + item.url
  237. uni.previewImage({
  238. urls: [url],
  239. current: url,
  240. success: () => {},
  241. fail: () => {
  242. uni.showToast({
  243. title: '预览图片失败',
  244. icon: 'none'
  245. });
  246. }
  247. });
  248. },
  249. downloadFile(url) {
  250. uni.downloadFile({
  251. url: this.baseURL + url,
  252. success: res => {
  253. if (res.statusCode === 200) {
  254. uni.saveFile({
  255. tempFilePath: res.tempFilePath,
  256. success: red => {
  257. uni.showToast({
  258. icon: 'none',
  259. mask: true,
  260. title: '文件已保存:' + red.savedFilePath, //保存路径
  261. duration: 3000,
  262. });
  263. setTimeout(() => {
  264. uni.openDocument({
  265. filePath: red.savedFilePath,
  266. success: ress => {},
  267. fail(err) {}
  268. });
  269. }, 500)
  270. }
  271. });
  272. }
  273. }
  274. });
  275. },
  276. }
  277. }
  278. </script>
  279. <style lang="scss" scoped>
  280. .icon-ym {
  281. font-size: 48rpx;
  282. }
  283. .jnpf-file {
  284. width: 100%;
  285. .jnpf-file-box {
  286. .jnpf-file-box-line {
  287. height: 70rpx;
  288. align-items: center;
  289. display: flex;
  290. }
  291. .tipText {
  292. color: #606266;
  293. word-break: break-all;
  294. line-height: 48rpx;
  295. }
  296. .jnpf-file-item {
  297. justify-content: space-between;
  298. flex-direction: row;
  299. .jnpf-file-item-txt {
  300. width: 230rpx;
  301. flex: auto;
  302. }
  303. .showLeft {
  304. text-align: left;
  305. }
  306. .closeBox {
  307. height: 60rpx;
  308. align-items: flex-end;
  309. justify-content: space-evenly;
  310. flex: 0.2;
  311. .closeTxt {
  312. width: 36rpx;
  313. height: 36rpx;
  314. border-radius: 50%;
  315. background-color: #fa3534;
  316. color: #FFFFFF;
  317. font-size: 20rpx;
  318. align-items: center;
  319. justify-content: center;
  320. line-height: 36rpx;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. </style>