John 8 mēneši atpakaļ
vecāks
revīzija
fb7be23cbe

+ 38 - 30
frontEndMobile/src/api/api.ts

@@ -1,60 +1,68 @@
 // src/services/api.ts
 
-import http from './http';
+import http from './http'
 
 // 定义接口类型
 interface User {
-  id: number;
-  name: string;
-  email: string;
+  id: number
+  name: string
+  email: string
 }
 
 // 登陆
 export const loginUser = async (params): Promise<User[]> => {
-  const response = await http.post<User[]>('/api/v1/login', params);
-  return response.data;
-};
+  const response = await http.post<User[]>('/api/v1/login', params)
+  return response.data
+}
 // 获取用户列表
 export const getUserInfo = async (): Promise<User[]> => {
-  const response = await http.post<User[]>('/api/v1/auth/user_info');
-  return response.data;
-};
+  const response = await http.post<User[]>('/api/v1/auth/user_info')
+  return response.data
+}
 
 // 获取单个用户
 export const fetchUserById = async (id: number): Promise<User> => {
-  const response = await http.get<User>(`/users/${id}`);
-  return response.data;
-};
+  const response = await http.get<User>(`/users/${id}`)
+  return response.data
+}
 
 // 创建新用户
 export const createUser = async (user: Omit<User, 'id'>): Promise<User> => {
-  const response = await http.post<User>('/users', user);
-  return response.data;
-};
+  const response = await http.post<User>('/users', user)
+  return response.data
+}
 
 // 更新用户
-export const updateUser = async (id: number, user: Partial<User>): Promise<User> => {
-  const response = await http.put<User>(`/users/${id}`, user);
-  return response.data;
-};
+export const updateUser = async (
+  id: number,
+  user: Partial<User>
+): Promise<User> => {
+  const response = await http.put<User>(`/users/${id}`, user)
+  return response.data
+}
 
 // 删除用户
 export const deleteUser = async (id: number): Promise<void> => {
-  await http.delete(`/users/${id}`);
-};
+  await http.delete(`/users/${id}`)
+}
 
 // 上传文件
-export const uploadFile = async (sampleFile:File): Promise<void> => {
+export const uploadFile = async (sampleFile: File): Promise<void> => {
   return await http.put(`/api/v1/files/`, sampleFile, {
     headers: {
-      'Content-Type': 'multipart/form-data',
-    },
-  });
-};
+      'Content-Type': 'multipart/form-data'
+    }
+  })
+}
 
+// 获取所有账本
+export const getAllBook = async (id: number): Promise<User> => {
+  const response = await http.get<User>(`/api/v1/books`)
+  return response.data
+}
 
 // 获取账本数据
 export const getBookInfo = async (id: number): Promise<User> => {
-  const response = await http.get<User>(`/api/v1/books/${id}`);
-  return response.data;
-};
+  const response = await http.get<User>(`/api/v1/books/${id}`)
+  return response.data
+}

+ 8 - 0
frontEndMobile/src/router/routes/modules/common.ts

@@ -35,6 +35,14 @@ const routes: RouteRecordRaw[] = [
     },
     component: () => import('@/views/AddAccountLogPage.vue')
   },
+  {
+    path: '/books',
+    name: 'books',
+    meta: {
+      title: '账本'
+    },
+    component: () => import('@/views/BooksPage.vue')
+  },
   {
     path: '/login',
     name: 'login',

+ 36 - 0
frontEndMobile/src/store/common.ts

@@ -0,0 +1,36 @@
+import { getBookInfo } from '@/api/api'
+import { defineStore } from 'pinia'
+import { useRouter, useRoute } from 'vue-router'
+
+// 你可以任意命名 `defineStore()` 的返回值,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。
+// (比如 `useUserStore`,`useCartStore`,`useProductStore`)
+// 第一个参数是你的应用中 Store 的唯一 ID。
+export const useCommonStore = defineStore('common', {
+  // 其他配置...
+  state: () => ({
+    bookInfo: {},
+    token: ''
+  }),
+  actions: {
+    async initBook() {
+      let bookInfo = window.localStorage.getItem('book')
+      let bookId = window.localStorage.getItem('bookId')
+      if (!bookInfo && bookId) {
+        const res = await getBookInfo(1)
+        window.localStorage.setItem('book', JSON.stringify(res))
+        this.bookInfo = res
+        return
+      }
+      if (bookInfo && bookInfo !== '{}' && bookId) {
+        this.bookInfo = JSON.parse(bookInfo)
+        return
+      }
+      const router = useRouter()
+      // const route = useRoute()
+      router.push('/books')
+    },
+    initToken() {
+      // this.count++
+    }
+  }
+})

+ 22 - 0
frontEndMobile/src/views/BooksPage.vue

@@ -0,0 +1,22 @@
+<template>
+  选择book
+  <br />
+  <div>
+    <div v-for="item in books" :key="item.id">
+      {{ item.book_name }}
+    </div>
+  </div>
+</template>
+
+<script setup>
+import { ref, onMounted } from 'vue'
+import { getAllBook } from '@/api/api'
+
+const books = ref([])
+
+onMounted(async () => {
+  const res = await getAllBook()
+  console.log(101010, res)
+  books.value = res
+})
+</script>

+ 11 - 2
frontEndMobile/src/views/HomePage.vue

@@ -51,6 +51,9 @@ import { getBookInfo } from '@/api/api'
 import dayjs from 'dayjs'
 import RecordInRow from '@/components/RecordInRow.vue'
 
+import { useCommonStore } from '@/store/common'
+const commonStore = useCommonStore()
+
 const showSelectTime = ref(false)
 const currentDate = ref([])
 const currentDateAfter = ref([])
@@ -92,11 +95,17 @@ const monthlyStr = computed(() => {
 })
 
 async function getBookInfoFn() {
-  await getBookInfo(1)
+  console.log(window.localStorage.getItem('book'))
+
+  // if() {}
+  // const res = await getBookInfo(1)
+  // console.log(96, res)
+  // window.localStorage.setItem('book', JSON.stringify(res))
 }
 
 onMounted(() => {
-  getBookInfoFn()
+  commonStore.initBook()
+  // getBookInfoFn()
   // getLogin()
 
   for (let index = 0; index < 40; index++) {

+ 13 - 0
node_expores/db/book.js

@@ -25,6 +25,19 @@ export async function books_insert({
   })
 }
 
+// 获取所有账本
+export function getAllBook(author_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(`SELECT * FROM book WHERE author_id = ? AND is_del=0`, [author_id], (err, rows) => {
+      if (err) {
+        resolve(false); // 如果存在记录,则返回 true,否则返回 false
+      } else {
+        resolve(rows); // 如果存在记录,则返回 true,否则返回 false
+      }
+    });
+  });
+}
+
 // 查询账本信息
 export function getBookById(book_id) {
   console.log(30, book_id)

+ 3 - 0
node_expores/db/update.sql

@@ -2,3 +2,6 @@ ALTER TABLE cashbook.book ADD is_del INT DEFAULT 0 NOT NULL;
 
 
 ALTER TABLE cashbook.book CHANGE auther_id author_id varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '账本创建者';
+
+ALTER TABLE cashbook.book ADD is_del varchar(100) DEFAULT '0' NOT NULL;
+ALTER TABLE cashbook.book CHANGE auther_id author_id varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '账本创建者';

+ 20 - 3
node_expores/router/books/index.js

@@ -9,6 +9,7 @@ import {
   delBookById,
   ishaveBookById,
   updateBookName,
+  getAllBook
 } from "#db";
 import {
   shanghaiTime,
@@ -21,19 +22,35 @@ router.use(function timeLog(req, res, next) {
   next();
 });
 
+// 获取所有账本
+router.get("/", async function (req, res) {
+  const { userInfo = {} } = req.body;
+  const bookInfo = await getAllBook(userInfo.user_id);
+  
+  res.json({
+    code: 200,
+    data: bookInfo.map(elm => {
+      return {
+        book_name: elm.book_name,
+        create_time: shanghaiTimeFormat(elm.create_time),
+        id: elm.id,
+        update_time: shanghaiTimeFormat(elm.update_time),
+      }
+    }),
+  });
+});
+
 // 获取账本详情
 router.get("/:book_id", async function (req, res) {
-  // getBookById
   const book_id = req.params.book_id; // 获取 fileId 参数
   const bookInfo = await getBookById(book_id);
-  // console.log(29, bookInfo)
   bookInfo.create_time = shanghaiTimeFormat(bookInfo.create_time)
   bookInfo.update_time = shanghaiTimeFormat(bookInfo.update_time)
+  delete bookInfo.is_del
   res.json({
     code: 200,
     data: bookInfo,
   });
-  // res.send("Books home page");
 });
 
 // 添加账本数据