index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { Icon, Image, Toast, Uploader } from 'vant'
  2. import { defineComponent } from 'vue'
  3. import styles from './index.module.less'
  4. import ColCropper from '../col-cropper'
  5. import { useCustomFieldValue } from '@vant/use'
  6. import { postMessage } from '@/helpers/native-message'
  7. import umiRequest from 'umi-request'
  8. import iconUploader from '@common/images/icon_uploader.png'
  9. import iconDelete from '@common/images/icon-delete.png'
  10. import request from '@/helpers/request'
  11. import { getOssUploadUrl, state } from '@/state'
  12. import { getUploadSign, onOnlyFileUpload } from '@/helpers/oss-file-upload'
  13. export default defineComponent({
  14. name: 'col-upload',
  15. props: {
  16. modelValue: String,
  17. tips: {
  18. type: String,
  19. default: '点击上传'
  20. },
  21. deletable: {
  22. type: Boolean,
  23. default: true
  24. },
  25. native: {
  26. // 是否原生上传
  27. type: Boolean,
  28. default: false
  29. },
  30. cropper: {
  31. // 是否进行裁切
  32. type: Boolean,
  33. default: false
  34. },
  35. options: {
  36. // 裁切需要参数
  37. type: Object,
  38. default: {}
  39. },
  40. uploadSize: {
  41. // 上传图片大小
  42. type: Number,
  43. default: 5
  44. },
  45. onUploadChange: {
  46. type: Function,
  47. default: (url: string) => {}
  48. },
  49. bucket: {
  50. type: String,
  51. default: 'daya'
  52. },
  53. disabled: {
  54. type: Boolean,
  55. default: false
  56. },
  57. hasDeviceAuth: {
  58. type: Boolean,
  59. default: true
  60. }
  61. },
  62. methods: {
  63. nativeUpload() {
  64. if (this.disabled) return
  65. postMessage(
  66. {
  67. api: 'chooseFile',
  68. content: { type: 'img', max: 1, bucket: this.bucket }
  69. },
  70. (res: any) => {
  71. console.log(res, 'fileUrl')
  72. this.$emit('update:modelValue', res.fileUrl)
  73. }
  74. )
  75. },
  76. beforeRead(file: any) {
  77. console.log(file, 'beforeRead')
  78. const isLt2M = file.size / 1024 / 1024 < this.uploadSize
  79. if (!isLt2M) {
  80. Toast(`上传文件大小不能超过 ${this.uploadSize}MB`)
  81. return false
  82. }
  83. return true
  84. },
  85. beforeDelete(file: any, detail: { index: any }) {
  86. // this.dataModel.splice(detail.index, 1)
  87. return true
  88. },
  89. async afterRead(file: any, detail: any) {
  90. try {
  91. file.status = 'uploading'
  92. file.message = '上传中...'
  93. await this.uploadFile(file.file)
  94. } catch (error) {
  95. //
  96. console.log(error, '2323')
  97. Toast.clear()
  98. }
  99. },
  100. onClose(e: any) {
  101. this.$emit('update:modelValue', null)
  102. this.onUploadChange()
  103. e.stopPropagation()
  104. },
  105. async getFile(file: any) {
  106. try {
  107. await this.uploadFile(file)
  108. } catch {
  109. //
  110. }
  111. },
  112. async uploadFile(file: any) {
  113. // 上传文件
  114. try {
  115. // 获取签名
  116. // const signUrl =
  117. // state.platformType === 'TEACHER'
  118. // ? '/api-teacher/getUploadSign'
  119. // : '/api-student/getUploadSign'
  120. const tempName = file.name || ''
  121. const fileName = tempName && tempName.replace(/ /gi, '_')
  122. const key = new Date().getTime() + fileName
  123. console.log(file)
  124. // const res = await request.post(signUrl, {
  125. // data: {
  126. // filename: fileName,
  127. // bucketName: this.bucket,
  128. // postData: {
  129. // filename: fileName,
  130. // acl: 'public-read',
  131. // key: key
  132. // }
  133. // }
  134. // })
  135. const { data } = await getUploadSign({
  136. filename: key,
  137. bucketName: this.bucket,
  138. postData: {
  139. filename: key,
  140. acl: 'public-read',
  141. key: key
  142. }
  143. })
  144. Toast.loading({
  145. message: '加载中...',
  146. forbidClick: true,
  147. loadingType: 'spinner',
  148. duration: 0
  149. })
  150. const obj = {
  151. policy: data.policy,
  152. signature: data.signature,
  153. key: key,
  154. KSSAccessKeyId: data.kssAccessKeyId,
  155. acl: 'public-read',
  156. name: key,
  157. file: file
  158. }
  159. const uploadUrl = await onOnlyFileUpload(
  160. getOssUploadUrl(this.bucket),
  161. obj
  162. )
  163. Toast.clear()
  164. this.$emit('update:modelValue', uploadUrl)
  165. this.onUploadChange(uploadUrl)
  166. } catch (error) {
  167. console.log(error, 'uploadFile')
  168. }
  169. }
  170. },
  171. render() {
  172. useCustomFieldValue(() => this.modelValue)
  173. return (
  174. <div class={styles['uploader-section']}>
  175. {this.modelValue && !this.disabled && this.deletable ? (
  176. <Icon
  177. name={iconDelete}
  178. onClick={this.onClose}
  179. class={styles['img-close']}
  180. />
  181. ) : null}
  182. {this.cropper && !this.native ? (
  183. <div class={styles['col-uploader']}>
  184. {this.modelValue ? (
  185. <Image
  186. fit="cover"
  187. position="center"
  188. class={styles.uploadImg}
  189. src={this.modelValue}
  190. />
  191. ) : (
  192. <div class={styles.uploader}>
  193. <Icon name={iconUploader} size="32" />
  194. {this.tips && <p class={styles.uploaderText}>{this.tips}</p>}
  195. </div>
  196. )}
  197. {
  198. this.hasDeviceAuth &&
  199. <ColCropper
  200. disabled={this.disabled }
  201. option={this.options}
  202. getFile={this.getFile}
  203. />
  204. }
  205. </div>
  206. ) : this.native ? (
  207. <div
  208. style={{
  209. display: 'flex',
  210. alignItems: 'center',
  211. justifyContent: 'center',
  212. height: '100%'
  213. }}
  214. onClick={this.nativeUpload}
  215. >
  216. {this.modelValue ? (
  217. <Image
  218. fit="cover"
  219. position="center"
  220. class={styles.uploadImg}
  221. src={this.modelValue}
  222. />
  223. ) : (
  224. <div class={styles.uploader}>
  225. <Icon name={iconUploader} size="32" />
  226. {this.tips && <p class={styles.uploaderText}>{this.tips}</p>}
  227. </div>
  228. )}
  229. </div>
  230. ) : (
  231. <Uploader
  232. afterRead={this.afterRead}
  233. beforeRead={this.beforeRead}
  234. beforeDelete={this.beforeDelete}
  235. disabled={this.disabled}
  236. v-slots={{
  237. default: () =>
  238. this.modelValue ? (
  239. <Image
  240. fit="cover"
  241. position="center"
  242. class={styles.uploadImg}
  243. src={this.modelValue}
  244. />
  245. ) : (
  246. <div class={styles.uploader}>
  247. <Icon name={iconUploader} size="32" />
  248. {this.tips && (
  249. <p class={styles.uploaderText}>{this.tips}</p>
  250. )}
  251. </div>
  252. )
  253. }}
  254. />
  255. )}
  256. </div>
  257. )
  258. }
  259. })