张延森 3 лет назад
Родитель
Сommit
4cea1c41ba

+ 6
- 0
src/layouts/NoteLayout.vue Просмотреть файл

@@ -17,9 +17,15 @@
17 17
 </template>
18 18
 
19 19
 <script setup>
20
+// import { watch } from 'vue';
21
+// import { useModel } from '@zjxpcyc/vue-tiny-store'
20 22
 import Header from './components/Header.vue'
21 23
 import SiderBar from './components/SiderBar.vue';
22 24
 
25
+// const { getRepo, getBranches, user : userRef } = useModel('repo')
26
+// const { getTree } = useModel('note')
27
+// const { user } = useModel('user')
28
+
23 29
 const siderWidth = '300px';
24 30
 const menuStyle = { width: siderWidth, height: '100%' };
25 31
 

+ 1
- 1
src/layouts/components/SiderBar.vue Просмотреть файл

@@ -38,7 +38,7 @@ const props = defineProps({
38 38
 
39 39
 const { user } = useModel('user')
40 40
 const { curMenu, menus, getMenus } = useModel('menu')
41
-const { getNote } = useModel('note')
41
+const { getNote, getFiles } = useModel('note')
42 42
 const { newFile, newFolder } = useModel('repo')
43 43
 
44 44
 if (!menus || !menus.length) {

+ 2
- 2
src/pages/login/index.vue Просмотреть файл

@@ -8,10 +8,10 @@
8 8
         <a-form-item has-feedback label="密码" name="password">
9 9
           <a-input type="password" v-model:value="formData.password"></a-input>
10 10
         </a-form-item>
11
-        <a-form-item has-feedback label="client_id" name="client_id">
11
+        <a-form-item has-feedback label="Client ID" name="client_id">
12 12
           <a-input v-model:value="formData.client_id"></a-input>
13 13
         </a-form-item>
14
-        <a-form-item has-feedback label="client_secret" name="client_secret">
14
+        <a-form-item has-feedback label="Client Secret" name="client_secret">
15 15
           <a-input v-model:value="formData.client_secret"></a-input>
16 16
         </a-form-item>
17 17
         <a-form-item :wrapper-col="{ span: 14, offset: 4 }">

+ 10
- 7
src/service/gitee.js Просмотреть файл

@@ -44,6 +44,12 @@ export const newRepo = (name = 'my-note') => {
44 44
   return request.post('/api/v5/user/repos', data)
45 45
 }
46 46
 
47
+// 获取分支信息
48
+export const getBranches = (user, repo = 'my-note') => {
49
+  const { access_token } = getToken() || {}
50
+  return request.get(`/api/v5/repos/${user.login}/${repo}/branches`, { params: { access_token } })
51
+}
52
+
47 53
 // 创建分支
48 54
 export const newBranch = (user, branch_name = 'master', repo = 'my-note') => {
49 55
   const { access_token } = getToken() || {}
@@ -56,9 +62,8 @@ export const newBranch = (user, branch_name = 'master', repo = 'my-note') => {
56 62
 }
57 63
 
58 64
 // 获取文件树
59
-export const getTree = (user, repo = 'my-note') => {
65
+export const getTree = (user, sha, repo = 'my-note') => {
60 66
   const { access_token } = getToken() || {}
61
-  const sha = '4f26aeafdb2367620a393c973eddbe8f8b846ebd' // master
62 67
   return request.get(`/api/v5/repos/${user.login}/${repo}/git/trees/${sha}`, { params: { access_token, recursive: 1 } })
63 68
 }
64 69
 
@@ -92,9 +97,9 @@ export const newFolder = (user, path, repo = 'my-note') => {
92 97
 }
93 98
 
94 99
 // 更新文件
95
-export const updateFile = (user, content, fileInfo) => {
100
+export const updateFile = (user, fileInfo) => {
96 101
   const { access_token } = getToken() || {}
97
-  const { url, sha } = fileInfo
102
+  const { path, sha, content } = fileInfo
98 103
   const data = {
99 104
     access_token,
100 105
     content: Base64.encode(content),
@@ -102,7 +107,5 @@ export const updateFile = (user, content, fileInfo) => {
102 107
     message: new Date().toJSON(),
103 108
   }
104 109
 
105
-  const filePath = url.replace(BASE_URL, '')
106
-
107
-  return request.put(filePath, data)
110
+  return request.put(path, data)
108 111
 }

+ 2
- 4
src/store/model/menu.js Просмотреть файл

@@ -5,10 +5,8 @@ export default function useMenu() {
5 5
   const menus = reactive([])
6 6
   const curMenu = ref()
7 7
 
8
-  const getMenus = (user, path = '', repo = 'my-note') => {
9
-    gitee.getTree(user, repo).then(console.log)
10
-
11
-    return gitee.getFiles(user, path, repo).then(res => {
8
+  const getMenus = (user, repo = 'my-note') => {
9
+    return gitee.getFiles(user, '', repo).then(res => {
12 10
       menus.push(...res)
13 11
     })
14 12
   }

+ 28
- 17
src/store/model/note.js Просмотреть файл

@@ -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
   }

+ 13
- 0
src/store/model/repo.js Просмотреть файл

@@ -3,15 +3,26 @@ import * as gitee from '@/service/gitee'
3 3
 
4 4
 export default function useRepo() {
5 5
   const repo = reactive({})
6
+  const branches = ref([])
6 7
   const user = ref()
7 8
 
9
+  // 获取仓库信息
8 10
   const getRepo = (user, repo = 'my-note') => {
9 11
     return gitee.getRepo(user, repo).then(res => Object.assign(repo, res))
10 12
   }
11 13
 
14
+  // 新增仓库
12 15
   const newRepo = (user, name = 'my-note') => {
13 16
     return gitee.newRepo(data).then(res => Object.assign(repo, res))
14 17
   }
18
+  
19
+  // 获取所有分支
20
+  const getBranches = (user, repo = 'my-note') => {
21
+    return gitee.getBranches(user, repo).then(res => {
22
+      branches.value = res
23
+      return res;
24
+    })
25
+  }
15 26
 
16 27
   const newBranch = (user, branch_name = 'master', repo = 'my-note') => {
17 28
     return gitee.newBranch(user, branch_name, repo).then(res => Object.assign(repo, res))
@@ -29,8 +40,10 @@ export default function useRepo() {
29 40
 
30 41
   return {
31 42
     repo,
43
+    user,
32 44
     getRepo,
33 45
     newRepo,
46
+    getBranches,
34 47
     newBranch,
35 48
     newFile,
36 49
     newFolder,