index.tsx 6.7 KB

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