|
|
@@ -0,0 +1,96 @@
|
|
|
+/**
|
|
|
+ * 微信小程序通过STS直传阿里云OSS
|
|
|
+ *(uniapp版上传,小程序原生需要将uni.XXX替换为wx.XXX)
|
|
|
+ *
|
|
|
+ * @param {filePath} 图片临时地址
|
|
|
+ * @param {option|Object} OSS和STS配置
|
|
|
+ * @param {savePath} oss中的文件目录
|
|
|
+ * @param {AccessKeySecret} 服务端返回的STS中的AccessKeySecret
|
|
|
+ * @param {SecurityToken} 服务端返回的STS中的SecurityToken
|
|
|
+ * @param {AccessKeyId} 服务端返回的STS中的AccessKeyId
|
|
|
+ * @param {bucket} 存储桶
|
|
|
+ * @param {area} 地区
|
|
|
+ * @return {string|boolean} 成功返回文件地址,失败返回false
|
|
|
+ */
|
|
|
+import COS from "./cos-wx-sdk-v5.min"
|
|
|
+
|
|
|
+console.log(COS)
|
|
|
+import tools from "./tools";
|
|
|
+import {getTxySts} from "@/api/common";
|
|
|
+
|
|
|
+// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
|
|
|
+let Bucket = 'wealfavor-1257406827';
|
|
|
+// 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
|
|
|
+// 关于地域的详情见 https://cloud.tencent.com/document/product/436/6224
|
|
|
+let Region = 'ap-beijing';
|
|
|
+
|
|
|
+let cos=null;
|
|
|
+function startCos(){
|
|
|
+ cos = new COS({
|
|
|
+ getAuthorization: function (options, callback) {
|
|
|
+ console.log('---------------获取初始化信息')
|
|
|
+ getTxySts().then((res)=>{
|
|
|
+ if(res.code===0){
|
|
|
+ callback({
|
|
|
+ TmpSecretId: res.data.credentials.tmpSecretId,
|
|
|
+ TmpSecretKey: res.data.credentials.tmpSecretKey,
|
|
|
+ XCosSecurityToken: res.data.credentials.sessionToken,
|
|
|
+ StartTime: res.data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
+ ExpiredTime: res.data.expiredTime, // 时间戳,单位秒,如:1580000900
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * COS文件上传
|
|
|
+ * @param file
|
|
|
+ * @param folder 上传目录
|
|
|
+ * @returns {Promise<unknown>}
|
|
|
+ */
|
|
|
+function txUploadFile(file,folder){
|
|
|
+ if(cos===null){
|
|
|
+ console.log('对象初始化')
|
|
|
+ //初始化对象
|
|
|
+ startCos();
|
|
|
+ }
|
|
|
+ //默认为个人目录
|
|
|
+ folder=folder==='moment'?'moment':'gallery'
|
|
|
+ tools.showLoading()
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ console.log('cos-*-------------------------',cos)
|
|
|
+ console.log(file)
|
|
|
+ console.log(folder+'/'+tools.getDate()+tools.getRandFileName(file))
|
|
|
+ // 分片上传文件
|
|
|
+ cos.postObject({
|
|
|
+ Bucket: Bucket,
|
|
|
+ Region: Region,
|
|
|
+ Key: folder+'/'+tools.getDate()+'/'+tools.getRandFileName(file),
|
|
|
+ FilePath: file,
|
|
|
+ onHashProgress: function (progressData) {
|
|
|
+ console.log('校验中', JSON.stringify(progressData));
|
|
|
+ },
|
|
|
+ onProgress: function (progressData) {
|
|
|
+ console.log('上传中', JSON.stringify(progressData));
|
|
|
+ },
|
|
|
+ }, function (err, data) {
|
|
|
+ tools.hideLoading()
|
|
|
+ console.log(err, data);
|
|
|
+ if(data.Location!==undefined){
|
|
|
+ data.Location='https://'+data.Location;
|
|
|
+ resolve( data)
|
|
|
+ }else {
|
|
|
+ resolve( false)
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ })
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+export default txUploadFile
|