index.tsx 4.8 KB

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