Skip to content

Commit 0c1494f

Browse files
committed
fix: tencent cos config #94
1 parent 8353b9f commit 0c1494f

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

blog-cms/src/views/pictureHosting/TxyunManage.vue

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ import SvgIcon from "@/components/SvgIcon";
5454
import {isImgExt} from "@/util/validate";
5555
import {randomUUID} from "@/util/uuid";
5656
import {copy} from "@/util/copy";
57-
import cos from "@/api/cos";
57+
import COS from 'cos-js-sdk-v5';
5858
5959
export default {
6060
name: "TxyunManage",
6161
components: {SvgIcon},
6262
data() {
6363
return {
64+
cos: {},
6465
txyunConfig: {
6566
secretId: '',
6667
secretKey: '',
@@ -111,34 +112,36 @@ export default {
111112
created() {
112113
this.hintShow1 = localStorage.getItem('txyunHintShow1') ? false : true
113114
this.hintShow2 = localStorage.getItem('txyunHintShow2') ? false : true
115+
114116
const txyunConfig = localStorage.getItem('txyunConfig')
115117
if (txyunConfig) {
116118
this.txyunConfig = JSON.parse(txyunConfig)
117119
this.txyunConfig.domain = this.txyunConfig.domain.endsWith('/') ? this.txyunConfig.domain : `${this.txyunConfig.domain}/`
120+
121+
this.cos = new COS({
122+
SecretId: this.txyunConfig.secretId,
123+
SecretKey: this.txyunConfig.secretKey,
124+
})
118125
} else {
119126
this.msgError('请先配置腾讯云')
120127
this.$router.push('/pictureHosting/setting')
121128
}
122-
123129
},
124130
methods: {
125131
//换成懒加载
126132
async getReposContents(arr, path) {
127133
const {txyunConfig} = this
128-
await cos.getBucket({
134+
await this.cos.getBucket({
129135
Bucket: txyunConfig.bucketName, /* 必须 */
130136
Region: txyunConfig.region, /* 存储桶所在地域,必须字段 */
131137
Prefix: path, /* 非必须 */
132138
Delimiter: '/'
133139
}).then(data => {
134-
console.log('查询成功',data);
135140
data.CommonPrefixes.forEach((item) => {
136-
item.name = item.Prefix.replace(path, '').slice(0,-1)
141+
item.name = item.Prefix.replace(path, '').slice(0, -1)
137142
//让所有节点都是非叶子节点
138143
arr.push({value: item.name, label: item.name, leaf: false})
139144
});
140-
}).catch(err => {
141-
console.log('查询失败',err);
142145
})
143146
},
144147
// 获取存储空间下指定目录的文件列表
@@ -150,21 +153,18 @@ export default {
150153
if (path != '') {
151154
path = path.endsWith('/') ? path : `${path}/`
152155
}
153-
cos.getBucket({
156+
this.cos.getBucket({
154157
Bucket: txyunConfig.bucketName, /* 必须 */
155158
Region: txyunConfig.region, /* 存储桶所在地域,必须字段 */
156159
Prefix: path, /* 非必须 */
157160
Delimiter: '/'
158161
}).then(data => {
159-
console.log('查询成功',data);
160162
data.Contents.filter((item) => !item.Key.endsWith('/') && isImgExt(item.Key)).forEach(item => {
161163
item.path = item.Key
162164
item.cdn_url = `${txyunConfig.domain}${item.path}`
163165
item.name = item.Key.replace(path, '')
164166
fileList.push(item)
165167
})
166-
}).catch(err => {
167-
console.log('查询失败',err);
168168
})
169169
this.fileList = fileList
170170
},
@@ -185,22 +185,19 @@ export default {
185185
// 删除文件
186186
delFile(file) {
187187
const {txyunConfig} = this
188-
console.log("删除路径:", file.path)
189188
this.$confirm("此操作将永久删除该文件, 是否删除?", "提示", {
190189
confirmButtonText: '确定',
191190
cancelButtonText: '取消',
192191
type: 'warning',
193192
}).then(() => {
194-
cos.deleteObject({
193+
this.cos.deleteObject({
195194
Bucket: txyunConfig.bucketName, /* 填写自己的bucket,必须字段 */
196195
Region: txyunConfig.region, /* 存储桶所在地域,必须字段 */
197196
Key: file.path, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段 */
198-
}).then(data => {
199-
console.log('删除成功',data);
197+
}).then(() => {
200198
this.msgSuccess('删除成功')
201199
this.search()
202-
}).catch(err => {
203-
console.log('删除失败',err);
200+
}).catch(() => {
204201
this.msgError('删除失败')
205202
})
206203
}).catch(() => {
@@ -229,22 +226,18 @@ export default {
229226
let path = this.realPath;
230227
path = path.startsWith('/') ? path.slice(1) : path
231228
path = path.endsWith('/') ? path : `${path}/`
232-
console.log("上传路径:", path)
233-
cos.uploadFile({
229+
this.cos.uploadFile({
234230
Bucket: txyunConfig.bucketName, /* 必须 */
235231
Region: txyunConfig.region, /* 存储桶所在地域,必须字段 */
236232
Key: `${path}${fileName}`, /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
237233
Body: data.file, // 上传文件对象
238234
SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */
239-
onProgress: function(progressData) {
235+
onProgress: function (progressData) {
240236
console.log(JSON.stringify(progressData));
241237
}
242238
}).then(() => {
243-
console.log('上传成功');
244239
this.msgSuccess('上传成功')
245240
data.onSuccess()
246-
}).catch(err => {
247-
console.log('上传失败', err);
248241
})
249242
},
250243
},
@@ -388,4 +381,4 @@ export default {
388381
.image-container:hover .image-content::before {
389382
height: 80px;
390383
}
391-
</style>
384+
</style>

0 commit comments

Comments
 (0)