index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: 50 // 默认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: '视频最大不能超过50MB'
  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. dataObj: {
  64. policy: '',
  65. signature: '',
  66. key: '',
  67. KSSAccessKeyId: '',
  68. acl: 'public-read',
  69. name: ''
  70. },
  71. fileList: [] as any,
  72. tempUrls: {} as any,
  73. responseList: [] as any, // 请求成功之后的数据
  74. btnLoading: false,
  75. loading: null as any
  76. }
  77. },
  78. methods: {
  79. handleSuccess(response: any, uploadFile: any, uploadFiles: any) {
  80. this.loading?.close()
  81. // 多文件上传,每个文件上传成功后,将文件的url添加到fileList
  82. console.log(this.fileList, 'fileList')
  83. console.log(response, uploadFile, uploadFiles, 'response')
  84. if (this.multiple) {
  85. if (uploadFile.status === 'success') {
  86. this.responseList.push(this.tempUrls[uploadFile.uid])
  87. }
  88. // 说明已经上传完成
  89. if (uploadFiles.length === this.responseList.length) {
  90. this.btnLoading = false
  91. this.multipleModel(this.responseList)
  92. }
  93. } else {
  94. let url = this.ossUploadUrl + '/' + this.dataObj.key
  95. this.$emit('update:modelValue', url)
  96. }
  97. },
  98. handleRemove() {
  99. console.log('remove')
  100. },
  101. handleChange() {
  102. console.log('handleChange')
  103. },
  104. handleProgress(e) {
  105. console.log('handleProgress', e)
  106. },
  107. handleError() {
  108. this.btnLoading = false
  109. this.loading?.close()
  110. },
  111. async beforeUpload(file: any) {
  112. // beforeUpload
  113. console.log(file)
  114. // let fileType = true
  115. // if (props.rules.type && props.rules.type.length > 0) {
  116. // const fileExtension = file.name.split('.').pop().toUpperCase()
  117. // console.log(
  118. // props.rules.type,
  119. // fileExtension,
  120. // props.rules.type.indexOf(fileExtension) != -1
  121. // )
  122. // if (props.rules.type.indexOf(fileExtension) != -1) {
  123. // fileType = true
  124. // } else {
  125. // fileType = false
  126. // ElMessage.error('请上传正确的文件!')
  127. // return false
  128. // }
  129. // }
  130. let isLt2M = true
  131. if (this.size) {
  132. isLt2M = file.size / 1024 / 1024 < this.size
  133. if (!isLt2M) {
  134. ElMessage.error(`文件大小不能超过${this.size}M!`)
  135. return false
  136. }
  137. }
  138. if (this.multiple) {
  139. this.btnLoading = true
  140. } else {
  141. this.loading = ElLoading.service({
  142. target: this.$refs.uploadDom as HTMLElement,
  143. lock: true,
  144. fullscreen: false,
  145. text: '上传中...',
  146. background: 'rgba(0, 0, 0, 0.7)'
  147. })
  148. }
  149. try {
  150. let fileName = file.name.replaceAll(' ', '_')
  151. let key = new Date().getTime() + fileName
  152. let obj = {
  153. filename: fileName,
  154. bucketName: this.bucket,
  155. postData: {
  156. filename: fileName,
  157. acl: 'public-read',
  158. key: key,
  159. unknowValueField: []
  160. }
  161. }
  162. const { data } = await request.post('/api-website/getUploadSign', {
  163. data: obj
  164. })
  165. this.dataObj = {
  166. policy: data.policy,
  167. signature: data.signature,
  168. key: key,
  169. KSSAccessKeyId: data.kssAccessKeyId,
  170. acl: 'public-read',
  171. name: fileName
  172. }
  173. this.tempUrls[file.uid] = this.ossUploadUrl + '/' + this.dataObj.key
  174. } catch (e) {
  175. this.btnLoading = false
  176. this.loading?.close()
  177. }
  178. },
  179. fileName(name = '') {
  180. return name.split('/').pop()
  181. },
  182. handleExceed(e: any) {
  183. if (e.length > this.limit) {
  184. ElMessage.error(`一次性最多只能上传${this.limit}个文件`)
  185. return false
  186. }
  187. }
  188. },
  189. render() {
  190. return (
  191. <div class={[styles.colUpload, 'w-full']}>
  192. <ElUpload
  193. disabled={this.disabled}
  194. action={this.ossUploadUrl}
  195. data={this.dataObj}
  196. onSuccess={this.handleSuccess}
  197. onRemove={this.handleRemove}
  198. onChange={this.handleChange}
  199. onProgress={this.handleProgress}
  200. onError={this.handleError}
  201. fileList={this.fileList}
  202. showFileList={false}
  203. accept={this.accept}
  204. beforeUpload={this.beforeUpload}
  205. onExceed={this.handleExceed}
  206. ref="uploadRef"
  207. multiple={this.multiple}
  208. limit={this.limit}
  209. class={[
  210. this.multiple && styles.fileUpload,
  211. this.disabled && styles.disabled
  212. ]}
  213. >
  214. <div
  215. ref="uploadDom"
  216. class={[styles.uploadClass, 'w-full']}
  217. style={{ height: this.multiple ? '40px' : '106px' }}
  218. >
  219. {this.modelValue ? (
  220. <video
  221. ref="videoUpload"
  222. crossorigin="anonymous"
  223. class={styles.uploadSection}
  224. src={this.modelValue}
  225. // poster={iconUploadPoster}
  226. />
  227. ) : this.multiple ? (
  228. <ElButton size="large" type="primary" loading={this.btnLoading}>
  229. {this.btnLoading ? '上传中...' : '点击上传'}
  230. </ElButton>
  231. ) : (
  232. <div
  233. class={[
  234. styles.uploadSection,
  235. 'flex items-center flex-col justify-center'
  236. ]}
  237. >
  238. <img src={iconVideo} class="w-8 h-7 mb-3" />
  239. <p>{this.tips}</p>
  240. </div>
  241. )}
  242. </div>
  243. </ElUpload>
  244. {!this.multiple && (
  245. <p class="text-3 text-[#999999] leading-6 pt-1">{this.extraTips}</p>
  246. )}
  247. </div>
  248. )
  249. }
  250. })