index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import {
  2. ElButton,
  3. ElIcon,
  4. ElImage,
  5. ElLoading,
  6. ElMessage,
  7. ElUpload
  8. } from 'element-plus'
  9. import { defineComponent, PropType } from 'vue'
  10. import { Document } from '@element-plus/icons-vue'
  11. import styles from './index.module.less'
  12. import iconVideo from './images/icon_video.png'
  13. import request from '@/helpers/request'
  14. export default defineComponent({
  15. name: 'col-upload-video',
  16. props: {
  17. modelValue: {
  18. type: String,
  19. default: ''
  20. },
  21. disabled: {
  22. type: Boolean,
  23. default: false
  24. },
  25. bucket: {
  26. type: String,
  27. default: 'daya'
  28. },
  29. multiple: {
  30. // 是否支持多文件上传
  31. type: Boolean,
  32. default: false
  33. },
  34. limit: {
  35. type: Number,
  36. default: 1
  37. },
  38. size: {
  39. type: Number,
  40. default: 800 // 默认5M
  41. },
  42. accept: {
  43. type: String,
  44. // ,.mov,.avi,.flv,.wmv,.mpg,.mpeg,.mpe,.mp3,.wav,.wma,.aac,.m4a,.m4r,.m4v,.3gp,.3g2,.mkv,.webm,.mov,.qt,.mxf,.asf,.asx,.rm,.ram,.rmvb
  45. default: '.mp4'
  46. },
  47. tips: {
  48. type: String,
  49. default: '请上传视频'
  50. },
  51. extraTips: {
  52. type: String,
  53. default: '视频最大不能超过800MB'
  54. },
  55. multipleModel: {
  56. type: Function,
  57. default: (data: any) => {}
  58. }
  59. },
  60. data() {
  61. return {
  62. // ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/' + this.bucket,
  63. ossUploadUrl: `https://${this.bucket}.ks3-cn-beijing.ksyuncs.com/`,
  64. dataObj: {
  65. policy: '',
  66. signature: '',
  67. key: '',
  68. KSSAccessKeyId: '',
  69. acl: 'public-read',
  70. name: ''
  71. },
  72. fileList: [] as any,
  73. tempUrls: {} as any,
  74. responseList: [] as any, // 请求成功之后的数据
  75. btnLoading: false,
  76. loading: null as any
  77. }
  78. },
  79. methods: {
  80. handleSuccess(response: any, uploadFile: any, uploadFiles: any) {
  81. this.loading?.close()
  82. // 多文件上传,每个文件上传成功后,将文件的url添加到fileList
  83. console.log(this.fileList, 'fileList')
  84. console.log(response, uploadFile, uploadFiles, 'response')
  85. if (this.multiple) {
  86. if (uploadFile.status === 'success') {
  87. this.responseList.push(this.tempUrls[uploadFile.uid])
  88. }
  89. // 说明已经上传完成
  90. // console.log(uploadFiles, 'uploadFiles')
  91. // console.log(this.responseList, 'responseList')
  92. if (uploadFiles.length === this.responseList.length) {
  93. this.btnLoading = false
  94. this.multipleModel(this.responseList)
  95. this.responseList = [] as any
  96. this.fileList = [] as any
  97. }
  98. } else {
  99. const url = this.ossUploadUrl + this.dataObj.key
  100. this.$emit('update:modelValue', url)
  101. }
  102. },
  103. handleRemove() {
  104. console.log('remove')
  105. },
  106. handleChange() {
  107. console.log('handleChange')
  108. // this.responseList = []
  109. // this.tempUrls = []
  110. },
  111. handleProgress(e) {
  112. console.log('handleProgress', e)
  113. },
  114. handleError() {
  115. this.btnLoading = false
  116. this.loading?.close()
  117. },
  118. async beforeUpload(file: any) {
  119. // beforeUpload
  120. console.log(file)
  121. // let fileType = true
  122. // if (props.rules.type && props.rules.type.length > 0) {
  123. // const fileExtension = file.name.split('.').pop().toUpperCase()
  124. // console.log(
  125. // props.rules.type,
  126. // fileExtension,
  127. // props.rules.type.indexOf(fileExtension) != -1
  128. // )
  129. // if (props.rules.type.indexOf(fileExtension) != -1) {
  130. // fileType = true
  131. // } else {
  132. // fileType = false
  133. // ElMessage.error('请上传正确的文件!')
  134. // return false
  135. // }
  136. // }
  137. let isLt2M = true
  138. if (this.size) {
  139. isLt2M = file.size / 1024 / 1024 < this.size
  140. if (!isLt2M) {
  141. ElMessage.error(`文件大小不能超过${this.size}M!`)
  142. return false
  143. }
  144. }
  145. if (this.multiple) {
  146. this.btnLoading = true
  147. } else {
  148. this.loading = ElLoading.service({
  149. target: this.$refs.uploadDom as HTMLElement,
  150. lock: true,
  151. fullscreen: false,
  152. text: '上传中...',
  153. background: 'rgba(0, 0, 0, 0.7)'
  154. })
  155. }
  156. try {
  157. let fileName = file.name.replaceAll(' ', '_')
  158. let key = new Date().getTime() + fileName
  159. let obj = {
  160. filename: fileName,
  161. bucketName: this.bucket,
  162. postData: {
  163. filename: fileName,
  164. acl: 'public-read',
  165. key: key,
  166. unknowValueField: []
  167. }
  168. }
  169. const { data } = await request.post('/api-website/getUploadSign', {
  170. data: obj
  171. })
  172. this.dataObj = {
  173. policy: data.policy,
  174. signature: data.signature,
  175. key: key,
  176. KSSAccessKeyId: data.kssAccessKeyId,
  177. acl: 'public-read',
  178. name: fileName
  179. }
  180. this.tempUrls[file.uid] = this.ossUploadUrl + '/' + this.dataObj.key
  181. } catch (e) {
  182. this.btnLoading = false
  183. this.loading?.close()
  184. }
  185. },
  186. fileName(name = '') {
  187. return name.split('/').pop()
  188. },
  189. handleExceed(e: any) {
  190. if (e.length > this.limit) {
  191. ElMessage.error(`一次性最多只能上传${this.limit}个文件`)
  192. return false
  193. }
  194. }
  195. },
  196. render() {
  197. return (
  198. <div class={[styles.colUpload, 'w-full']}>
  199. <ElUpload
  200. disabled={this.disabled || this.btnLoading}
  201. action={this.ossUploadUrl}
  202. data={this.dataObj}
  203. onSuccess={this.handleSuccess}
  204. onRemove={this.handleRemove}
  205. onChange={this.handleChange}
  206. onProgress={this.handleProgress}
  207. onError={this.handleError}
  208. fileList={this.fileList}
  209. showFileList={false}
  210. accept={this.accept}
  211. beforeUpload={this.beforeUpload}
  212. onExceed={this.handleExceed}
  213. ref="uploadRef"
  214. multiple={this.multiple}
  215. limit={this.limit}
  216. class={[
  217. this.multiple && styles.fileUpload,
  218. this.disabled && styles.disabled
  219. ]}
  220. style={{ lineHeight: '0' }}
  221. >
  222. <div
  223. ref="uploadDom"
  224. class={[styles.uploadClass, 'w-full']}
  225. style={{ height: this.multiple ? '40px' : '85px' }}
  226. >
  227. {this.modelValue ? (
  228. <video
  229. ref="videoUpload"
  230. crossorigin="anonymous"
  231. class={styles.uploadSection}
  232. src={this.modelValue}
  233. // poster={iconUploadPoster}
  234. />
  235. ) : this.multiple ? (
  236. <ElButton size="large" type="primary" loading={this.btnLoading}>
  237. {this.btnLoading ? '上传中...' : '点击上传'}
  238. </ElButton>
  239. ) : (
  240. <div
  241. class={[
  242. styles.uploadSection,
  243. 'flex items-center flex-col justify-center'
  244. ]}
  245. >
  246. <img src={iconVideo} class="w-8 h-7 mb-3" />
  247. <p>{this.tips}</p>
  248. </div>
  249. )}
  250. </div>
  251. </ElUpload>
  252. {!this.multiple && (
  253. <p class="text-3 text-[#999999] leading-6 pt-1">{this.extraTips}</p>
  254. )}
  255. </div>
  256. )
  257. }
  258. })