txOssSts.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * 微信小程序通过STS直传阿里云OSS
  3. *(uniapp版上传,小程序原生需要将uni.XXX替换为wx.XXX)
  4. *
  5. * @param {filePath} 图片临时地址
  6. * @param {option|Object} OSS和STS配置
  7. * @param {savePath} oss中的文件目录
  8. * @param {AccessKeySecret} 服务端返回的STS中的AccessKeySecret
  9. * @param {SecurityToken} 服务端返回的STS中的SecurityToken
  10. * @param {AccessKeyId} 服务端返回的STS中的AccessKeyId
  11. * @param {bucket} 存储桶
  12. * @param {area} 地区
  13. * @return {string|boolean} 成功返回文件地址,失败返回false
  14. */
  15. import COS from "./cos-wx-sdk-v5.min"
  16. console.log(COS)
  17. import tools from "./tools";
  18. import {getTxySts} from "@/api/common";
  19. // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
  20. let Bucket = 'wealfavor-1257406827';
  21. // 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
  22. // 关于地域的详情见 https://cloud.tencent.com/document/product/436/6224
  23. let Region = 'ap-beijing';
  24. let cos=null;
  25. function startCos(){
  26. cos = new COS({
  27. getAuthorization: function (options, callback) {
  28. console.log('---------------获取初始化信息')
  29. getTxySts().then((res)=>{
  30. if(res.code===1){
  31. callback({
  32. TmpSecretId: res.data.tmpSecretId,
  33. TmpSecretKey: res.data.tmpSecretKey,
  34. XCosSecurityToken: res.data.sessionToken,
  35. StartTime: res.data.StartTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
  36. ExpiredTime: res.data.ExpiredTime, // 时间戳,单位秒,如:1580000900
  37. })
  38. }
  39. })
  40. }
  41. });
  42. }
  43. /**
  44. * COS文件上传
  45. * @param file
  46. * @returns {Promise<unknown>}
  47. */
  48. function txUploadFile(file){
  49. if(cos===null){
  50. console.log('对象初始化')
  51. //初始化对象
  52. startCos();
  53. }
  54. tools.showLoading()
  55. return new Promise((resolve, reject) => {
  56. // 分片上传文件
  57. cos.postObject({
  58. Bucket: Bucket,
  59. Region: Region,
  60. Key: '/app-serve/'+tools.getDate('-')+'/'+tools.getRandFileName(file),
  61. FilePath: file,
  62. onHashProgress: function (progressData) {
  63. console.log('校验中', JSON.stringify(progressData));
  64. },
  65. onProgress: function (progressData) {
  66. console.log('上传中', JSON.stringify(progressData));
  67. },
  68. }, function (err, data) {
  69. tools.hideLoading()
  70. console.log(err)
  71. console.log(data)
  72. if(data.Location!==undefined){
  73. let suffixArr = ['.pdf', '.doc', '.docx', '.xlsx', '.xls','.mp4']
  74. let suffix = data.Location.substring(data.Location.lastIndexOf("."));
  75. let suffixKey = suffixArr.indexOf(suffix)
  76. if (suffixKey < 0){
  77. data.Location='https://'+data.Location+'?imageMogr2/quality/50';
  78. }else {
  79. data.Location='https://'+data.Location;
  80. }
  81. resolve( data)
  82. }else {
  83. resolve( false)
  84. }
  85. });
  86. })
  87. }
  88. export default txUploadFile