index.tsx 7.4 KB

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