|
@@ -1,37 +1,48 @@
|
1
|
|
-import { ref } from 'vue'
|
2
|
|
-import request, { BASE_URL } from '@/utils/request'
|
3
|
|
-import { Base64 } from 'js-base64'
|
|
1
|
+import { reactive, ref } from 'vue'
|
4
|
2
|
import { message } from 'ant-design-vue';
|
|
3
|
+import * as gitee from '@/service/gitee'
|
5
|
4
|
|
6
|
5
|
export default function useNote() {
|
|
6
|
+ const fileArr = ref([])
|
|
7
|
+ const fileTree = reactive([])
|
7
|
8
|
const note = ref()
|
8
|
9
|
|
|
10
|
+ // 获取目录下所有问题件
|
|
11
|
+ const getFiles = (user, path, repo = 'my-note') => {
|
|
12
|
+ return gitee.getFiles(user, path, repo).then(res => {
|
|
13
|
+ const files = (res || []).filter (x => x.path.indexOf('.gitignore') === -1)
|
|
14
|
+
|
|
15
|
+ if (!path) {
|
|
16
|
+ fileArr.value = files
|
|
17
|
+ fileTree.push(...files)
|
|
18
|
+ } else {
|
|
19
|
+ const dir = fileArr.value.filter(x => x.type === 'dir' && x.name === path)[0]
|
|
20
|
+ dir.children = files
|
|
21
|
+ fileArr.value = fileArr.value.concat(files)
|
|
22
|
+ }
|
|
23
|
+ })
|
|
24
|
+ }
|
|
25
|
+
|
9
|
26
|
const getNote = (user, path = '', repo = 'my-note') => {
|
10
|
|
- const { access_token } = user.token
|
11
|
|
- return request.get(`/api/v5/repos/${user.login}/${repo}/contents/${path}`, { params: { access_token } }).then(res => note.value = res)
|
|
27
|
+ return gitee.getFile(user, path, repo).then(res => note.value = res)
|
12
|
28
|
}
|
13
|
29
|
|
14
|
30
|
const updateNote = (content, user) => {
|
15
|
|
- const { url, sha } = note.value
|
16
|
|
- const { access_token } = user.token
|
17
|
|
- const data = {
|
18
|
|
- access_token,
|
19
|
|
- content: Base64.encode(content),
|
20
|
|
- sha,
|
21
|
|
- message: new Date().toJSON(),
|
22
|
|
- }
|
23
|
|
-
|
24
|
|
- const filePath = url.replace(BASE_URL, '')
|
|
31
|
+ const filePath = note.value.url.replace(BASE_URL, '')
|
|
32
|
+ const fileInfo = { filePath, sha: note.value.sha, content }
|
25
|
33
|
|
26
|
|
- return request.put(filePath, data).then(() => {
|
|
34
|
+ return gitee.updateFile(user, content, fileInfo).then(() => {
|
27
|
35
|
message.success('保存成功');
|
28
|
36
|
// 获取最新的数据, 目的是更新 sha 值
|
29
|
|
- request.get(filePath, { params: { access_token } }).then(res => note.value = res)
|
|
37
|
+ gitee.getFile(user, filePath, 'my-note').then(res => note.value = res)
|
30
|
38
|
})
|
31
|
39
|
}
|
32
|
40
|
|
33
|
41
|
return {
|
|
42
|
+ fileArr,
|
|
43
|
+ fileTree,
|
34
|
44
|
note,
|
|
45
|
+ getFiles,
|
35
|
46
|
getNote,
|
36
|
47
|
updateNote,
|
37
|
48
|
}
|