inside.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import {
  2. closeToast,
  3. Icon,
  4. Image,
  5. showLoadingToast,
  6. showToast,
  7. Uploader
  8. } from 'vant';
  9. import { defineComponent, PropType } from 'vue';
  10. import styles from './index.module.less';
  11. import { useCustomFieldValue } from '@vant/use';
  12. import { postMessage } from '@/helpers/native-message';
  13. import umiRequest from 'umi-request';
  14. import iconUploader from '@common/images/icon-upload.png';
  15. // import iconUploadClose from '@common/images/icon-upload-close.png';
  16. import request from '@/helpers/request';
  17. import { getOssUploadUrl } from '@/state';
  18. export default defineComponent({
  19. name: 'col-upload',
  20. props: {
  21. modelValue: {
  22. type: Array,
  23. default: () => []
  24. },
  25. deletable: {
  26. type: Boolean,
  27. default: true
  28. },
  29. maxCount: {
  30. type: Number,
  31. default: 1
  32. },
  33. native: {
  34. // 是否原生上传
  35. type: Boolean,
  36. default: false
  37. },
  38. uploadSize: {
  39. // 上传图片大小
  40. type: Number,
  41. default: 5
  42. },
  43. uploadType: {
  44. type: String as PropType<'IMAGE' | 'VIDEO'>,
  45. default: 'IMAGE'
  46. },
  47. accept: {
  48. type: String,
  49. default: 'image/*'
  50. },
  51. bucket: {
  52. type: String,
  53. default: 'gyt'
  54. },
  55. path: {
  56. type: String,
  57. default: ''
  58. },
  59. uploadIcon: {
  60. type: String,
  61. default: iconUploader
  62. },
  63. size: {
  64. type: String,
  65. default: 'default'
  66. },
  67. disabled: {
  68. type: Boolean,
  69. default: false
  70. },
  71. position: {
  72. type: String as PropType<'outside' | 'inside'>,
  73. default: 'outside'
  74. }
  75. },
  76. emits: ['uploadChange', 'update:modelValue'],
  77. methods: {
  78. nativeUpload() {
  79. if (this.disabled) {
  80. return;
  81. }
  82. const type = this.uploadType === 'VIDEO' ? 'video' : 'img';
  83. let imgCount = 1;
  84. if (this.maxCount > 1) {
  85. imgCount = this.maxCount - this.modelValue.length;
  86. } else {
  87. imgCount = this.maxCount;
  88. }
  89. postMessage(
  90. {
  91. api: 'chooseFile',
  92. content: {
  93. type: type,
  94. max: imgCount,
  95. bucket: this.bucket,
  96. path: this.path
  97. }
  98. },
  99. (res: any) => {
  100. console.log(res, 'fileUrl');
  101. // 判断是否是多选
  102. if (this.maxCount > 1) {
  103. const files = res.fileUrl;
  104. console.log(files, 'files');
  105. this.$emit('update:modelValue', [
  106. ...this.modelValue,
  107. ...files.split(',')
  108. ]);
  109. this.$emit('uploadChange', [
  110. ...this.modelValue,
  111. ...files.split(',')
  112. ]);
  113. } else {
  114. this.$emit('update:modelValue', [res.fileUrl]);
  115. this.$emit('uploadChange', [res.fileUrl]);
  116. }
  117. }
  118. );
  119. },
  120. beforeRead(file: any) {
  121. console.log(file, 'beforeRead');
  122. const isLt2M = file.size / 1024 / 1024 < this.uploadSize;
  123. if (!isLt2M) {
  124. showToast(`上传文件大小不能超过 ${this.uploadSize}MB`);
  125. return false;
  126. }
  127. return true;
  128. },
  129. beforeDelete() {
  130. // this.dataModel.splice(detail.index, 1)
  131. return true;
  132. },
  133. async afterRead(file: any) {
  134. try {
  135. file.status = 'uploading';
  136. file.message = '上传中...';
  137. await this.uploadFile(file.file);
  138. } catch (error) {
  139. closeToast();
  140. }
  141. },
  142. onClose(e: any, item: any) {
  143. const models = this.modelValue;
  144. const index = models.findIndex(model => model == item);
  145. if (index > -1) {
  146. models.splice(index, 1);
  147. this.$emit('update:modelValue', models);
  148. this.$emit('uploadChange');
  149. }
  150. e.stopPropagation();
  151. },
  152. async getFile(file: any) {
  153. try {
  154. await this.uploadFile(file);
  155. } catch {
  156. //
  157. }
  158. },
  159. async uploadFile(file: any) {
  160. // 上传文件
  161. try {
  162. // 获取签名
  163. const signUrl = '/api-web/getUploadSign';
  164. const tempName = file.name || '';
  165. const fileName =
  166. this.path + '/' + (tempName && tempName.replace(/ /gi, '_'));
  167. const key = new Date().getTime() + fileName;
  168. console.log(file);
  169. const res = await request.post(signUrl, {
  170. data: {
  171. filename: fileName,
  172. bucketName: this.bucket,
  173. postData: {
  174. filename: fileName,
  175. acl: 'public-read',
  176. key: key,
  177. unknowValueField: []
  178. }
  179. }
  180. });
  181. showLoadingToast({
  182. message: '加载中...',
  183. forbidClick: true,
  184. loadingType: 'spinner',
  185. duration: 0
  186. });
  187. const obj = {
  188. policy: res.data.policy,
  189. signature: res.data.signature,
  190. key: key,
  191. KSSAccessKeyId: res.data.kssAccessKeyId,
  192. acl: 'public-read',
  193. name: fileName
  194. } as any;
  195. const formData = new FormData();
  196. for (const key in obj) {
  197. formData.append(key, obj[key]);
  198. }
  199. formData.append('file', file, fileName);
  200. await umiRequest(getOssUploadUrl(this.bucket), {
  201. method: 'POST',
  202. data: formData
  203. });
  204. console.log(getOssUploadUrl(this.bucket) + key);
  205. const uploadUrl = getOssUploadUrl(this.bucket) + key;
  206. closeToast();
  207. // 判断是否是多选
  208. if (this.maxCount > 1) {
  209. this.$emit('update:modelValue', [...this.modelValue, uploadUrl]);
  210. this.$emit('uploadChange', [...this.modelValue, uploadUrl]);
  211. } else {
  212. this.$emit('update:modelValue', [uploadUrl]);
  213. this.$emit('uploadChange', [uploadUrl]);
  214. }
  215. } catch (error) {
  216. console.log(error, 'uploadFile');
  217. }
  218. }
  219. },
  220. render() {
  221. useCustomFieldValue(() => this.modelValue);
  222. return (
  223. <>
  224. {this.modelValue.length > 0 &&
  225. this.maxCount > 1 &&
  226. this.modelValue.map((item: any) => (
  227. <div class={['van-uploader', styles.uploader, styles[this.size]]}>
  228. {/* 删除按钮 */}
  229. {this.deletable && !this.disabled && (
  230. <Icon
  231. name="cross"
  232. onClick={(e: any) => this.onClose(e, item)}
  233. class={styles['img-close']}
  234. />
  235. )}
  236. <div class={['van-uploader__upload']}>
  237. {this.uploadType === 'IMAGE' ? (
  238. <Image src={item} class={styles.previewImg} fit="cover" />
  239. ) : (
  240. <video
  241. ref="videoUpload"
  242. style={{ backgroundColor: '#F8F8F8' }}
  243. class={styles.previewImg}
  244. src={item + '#t=1,4'}
  245. />
  246. )}
  247. </div>
  248. </div>
  249. ))}
  250. {this.native ? (
  251. this.maxCount > 1 ? (
  252. // 小于长度才显示
  253. this.modelValue.length < this.maxCount && (
  254. <div
  255. class={['van-uploader', styles.uploader, styles[this.size]]}
  256. onClick={this.nativeUpload}>
  257. <Icon
  258. name={this.uploadIcon}
  259. class={['van-uploader__upload']}
  260. size="32"
  261. />
  262. </div>
  263. )
  264. ) : (
  265. <div
  266. class={['van-uploader', styles.uploader, styles[this.size]]}
  267. onClick={this.nativeUpload}>
  268. {this.modelValue.length > 0 ? (
  269. <div class={['van-uploader__upload']}>
  270. {this.modelValue.map((item: any) => (
  271. <>
  272. {/* 删除按钮 */}
  273. {this.deletable && !this.disabled && (
  274. <Icon
  275. name="cross"
  276. onClick={(e: any) => this.onClose(e, item)}
  277. class={[styles['img-close'], styles.singleImgClose]}
  278. />
  279. )}
  280. {this.uploadType === 'IMAGE' ? (
  281. <Image
  282. fit="cover"
  283. position="center"
  284. class={styles.uploadImg}
  285. src={item}
  286. />
  287. ) : (
  288. <video
  289. ref="videoUpload"
  290. class={styles.uploadImg}
  291. style={{ backgroundColor: '#F8F8F8' }}
  292. src={item + '#t=1,4'}
  293. />
  294. )}
  295. </>
  296. ))}
  297. </div>
  298. ) : (
  299. <Icon
  300. name={this.uploadIcon}
  301. class={['van-uploader__upload']}
  302. size="32"
  303. />
  304. )}
  305. </div>
  306. )
  307. ) : this.maxCount > 1 ? (
  308. // 小于长度才显示
  309. this.modelValue.length < this.maxCount && (
  310. <Uploader
  311. class={['van-uploader', styles.uploader, styles[this.size]]}
  312. afterRead={this.afterRead}
  313. beforeRead={this.beforeRead}
  314. beforeDelete={this.beforeDelete}
  315. uploadIcon={this.uploadIcon}
  316. maxCount={this.maxCount}
  317. disabled={this.disabled}
  318. accept={this.accept}
  319. />
  320. )
  321. ) : (
  322. <Uploader
  323. class={['van-uploader', styles.uploader, styles[this.size]]}
  324. afterRead={this.afterRead}
  325. beforeRead={this.beforeRead}
  326. beforeDelete={this.beforeDelete}
  327. uploadIcon={this.uploadIcon}
  328. accept={this.accept}
  329. disabled={this.disabled}>
  330. {this.modelValue.length > 0 ? (
  331. <div class={['van-uploader__upload']}>
  332. {this.modelValue.map((item: any) => (
  333. <>
  334. {/* 删除按钮 */}
  335. {this.deletable && !this.disabled && (
  336. <Icon
  337. name="cross"
  338. onClick={(e: any) => this.onClose(e, item)}
  339. class={[styles['img-close'], styles.singleImgClose]}
  340. />
  341. )}
  342. {this.uploadType === 'IMAGE' ? (
  343. <Image
  344. fit="cover"
  345. position="center"
  346. class={styles.uploadImg}
  347. src={item}
  348. />
  349. ) : (
  350. <video
  351. ref="videoUpload"
  352. class={styles.uploadImg}
  353. style={{ backgroundColor: '#F8F8F8' }}
  354. src={item + '#t=1,4'}
  355. />
  356. )}
  357. </>
  358. ))}
  359. </div>
  360. ) : (
  361. <Icon
  362. name={this.uploadIcon}
  363. class={['van-uploader__upload']}
  364. size="32"
  365. />
  366. )}
  367. </Uploader>
  368. )}
  369. {this.$slots.default && this.$slots.default()}
  370. </>
  371. );
  372. }
  373. });