index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="jnpf-file">
  3. <view class="jnpf-file-box" :style="{textAlign:align}">
  4. <u-button size="mini" type="primary" v-if="!detailed &&!disabled " class="jnpf-file-box-line"
  5. @click="chooseFile">{{buttonText}}</u-button>
  6. <u-button size="mini" v-if="disabled" class="jnpf-file-disabled">{{buttonText}}</u-button>
  7. <view v-for='(item,index) in fileList' :key="index" class="jnpf-file-item u-type-primary u-flex u-line-1"
  8. @tap='downLoad(item)'>
  9. <view class="jnpf-file-item-txt u-line-1" v-if="item.fileSize">
  10. {{item.name+'('+`${jnpf.toFileSize(item.fileSize)}`+' )'}}
  11. </view>
  12. <view class="jnpf-file-item-txt u-line-1" v-else>{{item.name}}</view>
  13. <view class="closeBox u-flex-col" @click.stop="delFile(index)" v-if="!detailed && !disabled">
  14. <text class="icon-ym icon-ym-nav-close closeTxt u-flex"></text>
  15. </view>
  16. </view>
  17. <view class="tipText u-p-l-20">
  18. {{tipText}}
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. getDownloadUrl
  26. } from '@/api/common'
  27. import jnpf from '@/utils/jnpf'
  28. const units = {
  29. KB: 1024,
  30. MB: 1024 * 1024,
  31. GB: 1024 * 1024 * 1024,
  32. };
  33. const imgTypeList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
  34. export default {
  35. name: 'jnpf-upload-img',
  36. inheritAttrs: false,
  37. props: {
  38. modelValue: {
  39. type: Array,
  40. default: () => ([])
  41. },
  42. disabled: {
  43. type: Boolean,
  44. default: false
  45. },
  46. limit: {
  47. type: [Number, String],
  48. default: 2
  49. },
  50. fileSize: {
  51. type: Number,
  52. default: 5
  53. },
  54. sizeUnit: {
  55. type: String,
  56. default: 'MB'
  57. },
  58. accept: {
  59. type: String,
  60. default: ''
  61. },
  62. pathType: {
  63. type: String,
  64. default: 'defaultPath'
  65. },
  66. tipText: {
  67. type: String,
  68. default: ''
  69. },
  70. isAccount: {
  71. type: Number,
  72. default: 0
  73. },
  74. folder: {
  75. type: String,
  76. default: ''
  77. },
  78. vModel: {
  79. type: String,
  80. default: ''
  81. },
  82. detailed: {
  83. type: Boolean,
  84. default: false
  85. },
  86. align: {
  87. type: String,
  88. default: 'right'
  89. },
  90. sortRule: {
  91. type: Array,
  92. default: () => ([])
  93. },
  94. timeFormat: {
  95. type: String,
  96. default: ''
  97. },
  98. buttonText: {
  99. type: String,
  100. default: '点击上传'
  101. }
  102. },
  103. data() {
  104. return {
  105. percent: '',
  106. fileList: [],
  107. // 上传接口参数
  108. option: {},
  109. params: {
  110. pathType: this.pathType,
  111. isAccount: this.isAccount,
  112. folder: this.folder,
  113. sortRule: (this.sortRule || []).join(),
  114. timeFormat: this.timeFormat,
  115. },
  116. // 选择文件后是否立即自动上传,true=选择后立即上传
  117. instantly: true,
  118. size: 30,
  119. list: [],
  120. deletable: false,
  121. childId: 'upload' + this.$u.guid(3, false, 2),
  122. lsjUpload: 'lsjUpload' + this.$u.guid(3, false, 2),
  123. width: '140rpx',
  124. height: '70rpx',
  125. }
  126. },
  127. computed: {
  128. baseURL() {
  129. return this.define.baseURL
  130. },
  131. getFormats() {
  132. let formats = this.accept
  133. formats = formats.replace("image/*", 'png,jpg,jpeg,bmp,gif,webp,psd,svg,tiff')
  134. formats = formats.replace("video/*", 'avi,wmv,mpg,mpeg,mov,rm,ram,swf,flv,mp4,wma,rm,rmvb,flv,mpg,mkv')
  135. formats = formats.replace("audio/*", 'mp3,wav,aif,midi,m4a')
  136. return formats
  137. },
  138. },
  139. created() {
  140. const token = uni.getStorageSync('token')
  141. this.option = {
  142. url: this.baseURL + '/api/file/Uploader/annex',
  143. name: 'file',
  144. header: {
  145. 'Authorization': token,
  146. 'uid': '27682',
  147. 'client': 'app',
  148. 'accountid': 'DP',
  149. },
  150. data: this.params
  151. }
  152. },
  153. watch: {
  154. modelValue: {
  155. handler(val) {
  156. this.fileList = JSON.parse(JSON.stringify(val));
  157. },
  158. immediate: true
  159. }
  160. },
  161. methods: {
  162. chooseFile() {
  163. if (this.limit === this.fileList.length) return this.toast(`只允许上传${this.limit}个文件`);
  164. uni.chooseFile({
  165. count: 1, //默认100
  166. success: (res) => {
  167. const tempFilePaths = res.tempFilePaths;
  168. const token = uni.getStorageSync('token')
  169. const file = res.tempFiles[0]
  170. // 限制文件大小
  171. if (file.size > units[this.sizeUnit] * Math.abs(this.fileSize)) {
  172. this.toast(`文件大小超过${this.fileSize}${this.sizeUnit}`)
  173. return;
  174. }
  175. uni.uploadFile({
  176. url: this.baseURL + '/api/file/Uploader/annex',
  177. filePath: tempFilePaths[0],
  178. name: 'file',
  179. formData: {
  180. 'user': 'test'
  181. },
  182. header: {
  183. 'Authorization': token,
  184. 'uid': '27682',
  185. 'client': 'app',
  186. 'accountid': 'DP',
  187. },
  188. success: (response) => {
  189. let item = JSON.parse(response.data)
  190. this.fileList.push({
  191. name: res.tempFiles[0].name,
  192. fileId: item.data.name,
  193. url: item.data.url,
  194. fileExtension: item.data.fileExtension,
  195. fileSize: item.data.fileSize
  196. })
  197. this.$emit('update:modelValue', this.fileList)
  198. this.$emit('change', this.fileList)
  199. }
  200. });
  201. }
  202. });
  203. },
  204. downLoad(item) {
  205. getDownloadUrl('annex', item.fileId).then(res => {
  206. this.downloadFile(res.data.url, item.name);
  207. })
  208. },
  209. downloadFile(url, name) {
  210. uni.downloadFile({
  211. url: `${this.baseURL}${url}&name=${name}`,
  212. success: res => {
  213. if (res.statusCode === 200) {
  214. var filePath = res.tempFilePath;
  215. uni.openDocument({
  216. filePath: filePath,
  217. showMenu: true,
  218. success: (res) => {
  219. uni.showToast({
  220. title: '打开文档成功'
  221. })
  222. }
  223. });
  224. }
  225. }
  226. });
  227. },
  228. // 移除某个文件
  229. delFile(index) {
  230. console.log(index);
  231. uni.showModal({
  232. title: '提示',
  233. content: '是否删除该文件?',
  234. success: res => {
  235. if (res.confirm) {
  236. this.fileList.splice(index, 1)
  237. this.$emit('update:modelValue', this.fileList)
  238. this.$emit('change', this.fileList)
  239. this.fileList.length >= this.fileCount ? this.deletable = true : this.deletable =
  240. false
  241. } else if (res.cancel) {}
  242. }
  243. });
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .jnpf-file {
  250. width: 100%;
  251. .jnpf-file-box {
  252. .jnpf-file-box-line {
  253. height: 70rpx;
  254. }
  255. .tipText {
  256. color: #606266;
  257. word-break: break-all;
  258. line-height: 48rpx;
  259. }
  260. .jnpf-file-item {
  261. justify-content: space-between;
  262. flex-direction: row;
  263. .jnpf-file-item-txt {
  264. width: 230rpx;
  265. flex: auto;
  266. }
  267. .showLeft {
  268. text-align: left;
  269. }
  270. .closeBox {
  271. height: 60rpx;
  272. align-items: flex-end;
  273. justify-content: space-evenly;
  274. flex: 0.2;
  275. .closeTxt {
  276. width: 36rpx;
  277. height: 36rpx;
  278. border-radius: 50%;
  279. background-color: #fa3534;
  280. color: #FFFFFF;
  281. font-size: 20rpx;
  282. align-items: center;
  283. justify-content: center;
  284. line-height: 36rpx;
  285. }
  286. }
  287. }
  288. }
  289. .jnpf-file-disabled {
  290. background-color: #E6E6E6 !important;
  291. border-color: #D9D9D9 !important;
  292. color: #9B9B9B !important;
  293. }
  294. }
  295. </style>