湘妃到家前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

1 month ago
  1. import OSS from "ali-oss"
  2. import { v4 as uuidv4 } from 'uuid';
  3. import store from "../store/store";
  4. function uploadFileToOSS(file) {
  5. uni.showLoading({
  6. title: '图片上传中...'
  7. });
  8. return new Promise((resolve,reject) => {
  9. // 创建OSS实例
  10. const client = new OSS({
  11. // region: 'oss-cn-shenzhen',
  12. // accessKeyId: 'LTAI5tMan18fjJPUtr3Aim2W',
  13. // accessKeySecret: 'lhALqqgYijc115wY8c1KfTYkbSnq5I',
  14. // bucket: 'mangoimageapplet',
  15. // endpoint:'oss-cn-shenzhen.aliyuncs.com'
  16. //桶的地址
  17. // region: 'oss-cn-guangzhou',
  18. // //id
  19. // accessKeyId:'LTAI5tNycA46YTwm383dRvMV',
  20. // //密钥
  21. // accessKeySecret:'tAdbYQCmdur6jbZ8hjvgB7T1Z52mIG',
  22. // //桶的名字
  23. // bucket: 'zhuoqiu-image',
  24. // endpoint:'oss-cn-guangzhou.aliyuncs.com',
  25. /**
  26. * 自己的
  27. */
  28. //桶的地址
  29. // region: 'oss-cn-guangzhou',
  30. // //id
  31. // accessKeyId: 'LTAI5tQSs47izVy8DLVdwUU9',
  32. // //密钥
  33. // accessKeySecret: 'qHI7C3PaXYZySr84HTToviC71AYlFq',
  34. // //桶的名字
  35. // bucket: 'hanhaiimage',
  36. // endpoint: 'oss-cn-shenzhen.aliyuncs.com',
  37. region : store.state.configList.region,
  38. accessKeyId : store.state.configList.accessKeyId,
  39. accessKeySecret : store.state.configList.accessKeySecret,
  40. bucket : store.state.configList.bucket,
  41. endpoint : store.state.configList.endpoint,
  42. });
  43. // 设置文件名和文件目录
  44. const suffix = '.' + file.name.split('.').pop();
  45. const fileName = uuidv4() + suffix; // 注意:文件名需要是唯一的
  46. // 使用put接口上传文件
  47. client.multipartUpload(fileName, file, {
  48. headers: {
  49. 'Content-Disposition': 'inline',
  50. 'Content-Type': file.type
  51. }
  52. }).then(res => {
  53. uni.hideLoading();
  54. resolve(this.configList.staticDomain + res.name);
  55. }).catch(err => {
  56. uni.hideLoading();
  57. reject(err)
  58. })
  59. })
  60. }
  61. export default uploadFileToOSS