index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { ElIcon, ElImage, ElLoading, ElMessage, ElUpload } from 'element-plus'
  2. import { defineComponent, PropType } from 'vue'
  3. import { Document } from '@element-plus/icons-vue'
  4. import styles from './index.module.less'
  5. import iconUpload from './images/icon_upload.png'
  6. import request from '@/helpers/request'
  7. export default defineComponent({
  8. name: 'col-upload',
  9. props: {
  10. modelValue: {
  11. type: String,
  12. default: ''
  13. },
  14. uploadType: {
  15. type: String as PropType<'image' | 'file'>,
  16. default: 'image'
  17. },
  18. disabled: {
  19. type: Boolean,
  20. default: false
  21. },
  22. bucket: {
  23. type: String,
  24. default: 'daya'
  25. },
  26. size: {
  27. type: Number,
  28. default: 5 // 默认5M
  29. },
  30. accept: {
  31. type: String,
  32. default: '.png,.jpg,.jpeg'
  33. },
  34. tips: {
  35. type: String,
  36. default: '请上传图片'
  37. },
  38. extraTips: {
  39. type: String,
  40. default: '图片最大不能超过5MB'
  41. },
  42. onChange: {
  43. type: Function,
  44. default: () => {}
  45. }
  46. },
  47. data() {
  48. return {
  49. ossUploadUrl: 'https://ks3-cn-beijing.ksyuncs.com/' + this.bucket,
  50. dataObj: {
  51. policy: '',
  52. signature: '',
  53. key: '',
  54. KSSAccessKeyId: '',
  55. acl: 'public-read',
  56. name: ''
  57. },
  58. fileList: [] as any,
  59. loading: null as any
  60. }
  61. },
  62. methods: {
  63. handleSuccess() {
  64. this.loading?.close()
  65. let url = this.ossUploadUrl + '/' + this.dataObj.key
  66. console.log(url)
  67. this.$emit('update:modelValue', url)
  68. this.onChange(url)
  69. },
  70. handleRemove() {
  71. console.log('remove')
  72. },
  73. handleChange() {
  74. console.log('handleChange')
  75. },
  76. handleProgress() {
  77. console.log('handleProgress')
  78. },
  79. handleError() {
  80. this.loading?.close()
  81. },
  82. async beforeUpload(file: any) {
  83. // beforeUpload
  84. console.log(file)
  85. // let fileType = true
  86. // if (props.rules.type && props.rules.type.length > 0) {
  87. // const fileExtension = file.name.split('.').pop().toUpperCase()
  88. // console.log(
  89. // props.rules.type,
  90. // fileExtension,
  91. // props.rules.type.indexOf(fileExtension) != -1
  92. // )
  93. // if (props.rules.type.indexOf(fileExtension) != -1) {
  94. // fileType = true
  95. // } else {
  96. // fileType = false
  97. // ElMessage.error('请上传正确的文件!')
  98. // return false
  99. // }
  100. // }
  101. let isLt2M = true
  102. if (this.size) {
  103. isLt2M = file.size / 1024 / 1024 < this.size
  104. if (!isLt2M) {
  105. ElMessage.error(`文件大小不能超过${this.size}M!`)
  106. return false
  107. }
  108. }
  109. this.loading = ElLoading.service({
  110. target: this.$refs.uploadDom as HTMLElement,
  111. lock: true,
  112. fullscreen: false,
  113. text: '上传中...',
  114. background: 'rgba(0, 0, 0, 0.7)'
  115. })
  116. console.log(this.loading)
  117. try {
  118. let fileName = file.name.replaceAll(' ', '_')
  119. let key = new Date().getTime() + fileName
  120. let obj = {
  121. filename: fileName,
  122. bucketName: this.bucket,
  123. postData: {
  124. filename: fileName,
  125. acl: 'public-read',
  126. key: key,
  127. unknowValueField: []
  128. }
  129. }
  130. const { data } = await request.post('/api-website/getUploadSign', {
  131. data: obj
  132. })
  133. this.dataObj = {
  134. policy: data.policy,
  135. signature: data.signature,
  136. key: key,
  137. KSSAccessKeyId: data.kssAccessKeyId,
  138. acl: 'public-read',
  139. name: fileName
  140. }
  141. } catch (e) {
  142. this.loading.close()
  143. }
  144. },
  145. fileName(name = '') {
  146. return name.split('/').pop()
  147. },
  148. handleExceed() {}
  149. },
  150. render() {
  151. return (
  152. <div class={[styles.colUpload, 'w-full']}>
  153. <ElUpload
  154. disabled={this.disabled}
  155. action={this.ossUploadUrl}
  156. data={this.dataObj}
  157. onSuccess={this.handleSuccess}
  158. onRemove={this.handleRemove}
  159. onChange={this.handleChange}
  160. onProgress={this.handleProgress}
  161. onError={this.handleError}
  162. fileList={this.fileList}
  163. showFileList={false}
  164. accept={this.accept}
  165. beforeUpload={this.beforeUpload}
  166. onExceed={this.handleExceed}
  167. ref="uploadRef"
  168. class={this.uploadType === 'file' ? styles.fileUpload : ''}
  169. >
  170. <div
  171. ref="uploadDom"
  172. class={[styles.uploadClass, 'w-full']}
  173. style={{ height: this.uploadType === 'image' ? '85px' : '48px' }}
  174. >
  175. {this.modelValue ? (
  176. this.uploadType === 'image' ? (
  177. <ElImage
  178. src={this.modelValue}
  179. fit="cover"
  180. class={styles.uploadSection}
  181. />
  182. ) : (
  183. <div class={styles.uploadFile}>
  184. <ElIcon>
  185. <Document />
  186. </ElIcon>
  187. <span class="whitespace-nowrap overflow-hidden text-ellipsis">
  188. {this.fileName(this.modelValue)}
  189. </span>
  190. </div>
  191. )
  192. ) : this.uploadType === 'image' ? (
  193. <div
  194. class={[
  195. styles.uploadSection,
  196. 'flex items-center flex-col justify-center'
  197. ]}
  198. >
  199. <img src={iconUpload} class="w-8 h-7 mb-3" />
  200. <p>{this.tips}</p>
  201. </div>
  202. ) : (
  203. <div class={styles.uploadFile}>
  204. <ElIcon>
  205. <Document />
  206. </ElIcon>
  207. 上传文件
  208. </div>
  209. )}
  210. </div>
  211. </ElUpload>
  212. <p class="text-3 text-[#999999] leading-6 pt-1">{this.extraTips}</p>
  213. </div>
  214. )
  215. }
  216. })