Bladeren bron

记录数据

John 8 maanden geleden
bovenliggende
commit
e4431d803b

+ 13 - 0
DB/cashbook.sql

@@ -65,3 +65,16 @@ CREATE TABLE `files` (
   `is_del` int NOT NULL DEFAULT '0',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+
+-- cashbook.record_files definition
+CREATE TABLE `record_files` (
+  `id` int NOT NULL AUTO_INCREMENT,
+  `file_id` varchar(300) NOT NULL,
+  `record_id` varchar(300) NOT NULL,
+  `book_id` varchar(300) NOT NULL,
+  `author_id` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
+  `create_time` timestamp NOT NULL,
+  `update_time` timestamp NOT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

+ 5 - 0
frontEndMobile/src/api/api.ts

@@ -66,3 +66,8 @@ export const getBookInfo = async (id: number): Promise<User> => {
   const response = await http.get<User>(`/api/v1/books/${id}`)
   return response.data
 }
+// 添加数据
+export const addRecord = async (params: any): Promise<User> => {
+  const response = await http.post<User>(`/api/v1/record`, params)
+  return response.data
+}

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

@@ -12,6 +12,11 @@ export const useCommonStore = defineStore('common', {
     token: '',
     isLogin: false
   }),
+  getters: {
+    getBookInfo() {
+      return this.bookInfo
+    }
+  },
   actions: {
     async initBook() {
       let bookInfo = window.localStorage.getItem('book')

+ 12 - 4
frontEndMobile/src/views/AddAccountLogPage.vue

@@ -59,7 +59,9 @@
 import { watch, ref, onMounted, toRaw } from 'vue'
 import { useRouter, useRoute } from 'vue-router'
 import dayjs from 'dayjs'
-import { uploadFile } from '@/api/api'
+import { uploadFile, addRecord } from '@/api/api'
+import { useCommonStore } from '@/store/common'
+const commonStore = useCommonStore()
 
 const router = useRouter()
 const route = useRoute()
@@ -92,7 +94,7 @@ const onConfirm = (date) => {
 const filesDelete = (file) => {
   files.value = files.value.filter((elm) => elm.url !== file.url)
 }
-const onSubmit = (values) => {
+const onSubmit = async (values) => {
   // 金额
   // 附件
   // 时间
@@ -104,7 +106,13 @@ const onSubmit = (values) => {
   //   remark: toRaw(message)
   // }
   // console.log(103, params)
-
-  console.log(102, values)
+  await addRecord({
+    book_id: commonStore.bookInfo.id,
+    time: values.calendar,
+    total_fee: values.totalFee,
+    types: 'consectetur',
+    remark: values.remark,
+    files: files.value.map((elm) => elm.file_id)
+  })
 }
 </script>

+ 41 - 0
node_expores/db/files.js

@@ -65,3 +65,44 @@ export function delFileBymd5(md5Str) {
     });
   });
 }
+
+// 映射数据与文件数据
+export function addFileByRecordId({
+  file_id = '',
+  record_id = '',
+  book_id = '',
+  author_id = '',
+  create_time = '',
+  update_time = '',
+}) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+        INSERT INTO record_files (file_id, record_id, book_id, author_id, create_time, update_time)
+        VALUES (?, ?, ?, ?, ?, ?)
+      `;
+      const values = [file_id, record_id, book_id, author_id, create_time, update_time];
+      // 直接接收 execute 返回的内容
+      const result = await connection.execute(sql, values);
+      // console.log("Record inserted successfully:", result);
+      return resolve(result);
+    } catch (err) {
+      // console.error("Error inserting data:", err);
+      // throw err;
+      return resolve(false);
+    }
+  })
+}
+
+// 根据id获取files
+export function getFileByRecordId(record_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(`SELECT * FROM record_files WHERE record_id = ?`, [record_id], (err, rows) => {
+      if (err) {
+        resolve(false); // 如果存在记录,则返回 true,否则返回 false
+      } else {
+        resolve(rows); // 如果存在记录,则返回 true,否则返回 false
+      }
+    });
+  });
+}

+ 22 - 3
node_expores/db/record.js

@@ -7,16 +7,17 @@ export async function record_insert({
   author_id = '',
   total_fee = '',
   remark = '',
+  time = '',
   create_time = "",
   update_time = "",
 }) {
   return new Promise(async (resolve, reject) => {
     try {
       const sql = `
-        INSERT INTO record (book_id, type_id, author_id, total_fee, remark, create_time, update_time)
-        VALUES (?, ?, ?, ?, ?, ?, ?)
+        INSERT INTO record (book_id, type_id, author_id, total_fee, remark, create_time, update_time, time)
+        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
       `;
-      const values = [book_id, type_id, author_id, total_fee, remark, create_time, update_time];
+      const values = [book_id, type_id, author_id, total_fee, remark, create_time, update_time, time];
       // 直接接收 execute 返回的内容
       connection.execute(sql, values, (result, fields) => {
         return resolve(fields.insertId);
@@ -109,3 +110,21 @@ export function getRecordInfoById(record_id) {
     );
   });
 }
+
+// 根据日期查询记录
+export function getRecordsInfoByTime(time, book_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(
+      `SELECT * FROM record WHERE time = ? AND book_id = ?`,
+      [time, book_id],
+      (err, rows) => {
+        if (err) {
+          // reject(err);
+          resolve(false); // 如果存在记录,则返回 true,否则返回 false
+        } else {
+          resolve(rows); // 如果存在记录,则返回 true,否则返回 false
+        }
+      },
+    );
+  });
+}

+ 6 - 6
node_expores/db/type.js

@@ -1,11 +1,11 @@
 import connection from "./base.js";
 
 // 用户是否已经注册
-export function isType({ book_id = "", user_id = "", type = "" }) {
+export function isType({ book_id = "", author_id = "", type = "" }) {
   return new Promise((resolve, reject) => {
     connection.query(
-      `SELECT * FROM types WHERE book_id = ? AND user_id = ? AND name = ?`,
-      [book_id, user_id, type],
+      `SELECT * FROM types WHERE book_id = ? AND author_id = ? AND name = ?`,
+      [book_id, author_id, type],
       (err, rows) => {
         if (err) {
           // reject(err);
@@ -20,7 +20,7 @@ export function isType({ book_id = "", user_id = "", type = "" }) {
 
 export async function type_insert({
   book_id = "",
-  user_id = "",
+  author_id = "",
   type = "",
   create_time = "",
   update_time = "",
@@ -28,11 +28,11 @@ export async function type_insert({
   return new Promise(async (resolve, reject) => {
     try {
       const sql = `
-        INSERT INTO types (name, book_id, user_id, create_time, update_time)
+        INSERT INTO types (name, book_id, author_id, create_time, update_time)
         VALUES (?, ?, ?, ?, ?)
       `;
       const values = [
-        type, book_id, user_id, create_time, update_time
+        type, book_id, author_id, create_time, update_time
       ];
       // 直接接收 execute 返回的内容
       const result = await connection.execute(sql, values);

+ 1 - 0
node_expores/db/update.sql

@@ -10,3 +10,4 @@ ALTER TABLE cashbook.book CHANGE auther_id author_id varchar(300) CHARACTER SET
 ALTER TABLE cashbook.record_files CHANGE user_id author_id varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL;
 ALTER TABLE cashbook.types CHANGE user_id author_id varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL;
 
+ALTER TABLE cashbook.record ADD `time` TIMESTAMP NOT NULL;

+ 7 - 7
node_expores/environment/index.js

@@ -1,12 +1,12 @@
 function dbInfo() {
   // 根据需要更新db的数据配置
-  return {
-    host: "localhost",
-    port: 3306,
-    user: "root",
-    password: "12345678",
-    database: "cashbook",
-  };
+  // return {
+  //   host: "localhost",
+  //   port: 3306,
+  //   user: "root",
+  //   password: "12345678",
+  //   database: "cashbook",
+  // };
   return {
     host: "192.168.2.101",
     port: 6806,

+ 60 - 5
node_expores/router/record/index.js

@@ -5,9 +5,13 @@ import {
   record_insert,
   isType,
   type_insert,
-  getRecordInfoById
+  getRecordInfoById,
+  addFileByRecordId,
+  getFileByRecordId,
+  getRecordsInfoByTime
 } from "#db";
-import {shanghaiTime} from '#utils'
+import {shanghaiTime, shanghaiTimeFormat} from '#utils'
+import dayjs from "dayjs";
 
 // middleware that is specific to this router
 router.use(function timeLog(req, res, next) {
@@ -21,6 +25,7 @@ router.post("/", async function (req, res) {
     book_id = "",
     total_fee = 0,
     type = "",
+    time = "",
     remark = "",
     files = [],
     userInfo = {},
@@ -28,21 +33,21 @@ router.post("/", async function (req, res) {
   // type 是否存在重复项,有就返回id,没有就新增 再返回id
   const isAddType = await isType({
     book_id,
-    user_id: userInfo.user_id,
+    author_id: userInfo.user_id,
     type,
   });
   let typeInfo = {};
   if (!isAddType) {
     await type_insert({
       book_id,
-      user_id: userInfo.user_id,
+      author_id: userInfo.user_id,
       type,
       create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
       update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
     });
     typeInfo = await isType({
       book_id,
-      user_id: userInfo.user_id,
+      author_id: userInfo.user_id,
       type,
     });
   } else {
@@ -63,10 +68,25 @@ router.post("/", async function (req, res) {
     author_id: userInfo.user_id,
     total_fee,
     remark,
+    time: shanghaiTimeFormat(time),
     create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
     update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
   });
 
+  console.log(7171, userInfo);
+  
+
+  const filesRes = await Promise.all(files.map((file_id) => addFileByRecordId({
+    file_id,
+    record_id: insertId,
+    book_id,
+    author_id: userInfo.user_id,
+    create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
+    update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
+  })))
+
+  console.log(82, filesRes);
+
   res.json({
     code: 200,
     data: {
@@ -75,6 +95,23 @@ router.post("/", async function (req, res) {
   });
 });
 
+// define the home page route
+router.get("/:record_id", async function (req, res) {
+  const record_id = req.params.record_id; // 获取 fileId 参数
+  const recordInfo = await getRecordInfoById(record_id)
+  const files = await getFileByRecordId(record_id)
+  res.json({
+    code: 200,
+    data: {
+      ...recordInfo,
+       "time": shanghaiTimeFormat(recordInfo.time, 'YYYY-MM-DD'),
+       "create_time": shanghaiTimeFormat(recordInfo.create_time),
+       "update_time": shanghaiTimeFormat(recordInfo.update_time),
+       files: files.map(elm =>`http://127.0.0.1:3000/api/v1/files/${elm.file_id}`)
+    }
+  });
+});
+
 // define the home page route
 router.get("/:record_id", async function (req, res) {
   const record_id = req.params.record_id; // 获取 fileId 参数
@@ -90,4 +127,22 @@ router.get("/about", function (req, res) {
   res.send("About record");
 });
 
+// 根据日期获取数据
+router.get("/:book_id/:time", async function (req, res) {
+  console.log(13423);
+  const time = req.params.time; // 获取 fileId 参数
+  const book_id = req.params.book_id; // 获取 fileId 参数
+  const recordsInfoRes = await getRecordsInfoByTime(time, book_id);
+  console.log(136, recordsInfoRes);
+
+  res.json({
+    code: 200,
+    data: recordsInfoRes.map(elm => ({
+      ...elm,
+      "time": shanghaiTimeFormat(elm.time, 'YYYY-MM-DD'),
+      "create_time": shanghaiTimeFormat(elm.create_time),
+      "update_time": shanghaiTimeFormat(elm.update_time),
+    }))
+  });
+});
 export default router;

+ 3 - 0
node_expores/utils/authorization.js

@@ -19,12 +19,15 @@ export function generateToken(payload) {
 
 // 验证token
 export function verifyToken(req, res, next) {
+  console.log(22, req.headers.authorization);
   const token = req.headers.authorization.split(" ")[1];
   jwt.verify(token, secretKey, function (err, decoded) {
     if (err) {
       console.log("verify error", err);
       return res.json({ code: "404", msg: "token无效" });
     }
+    console.log(29292, decoded);
+    
     req.body.userInfo = decoded
     next();
   });

+ 2 - 2
node_expores/utils/dayTime.js

@@ -13,10 +13,10 @@ export function shanghaiTime () {
   return shanghaiTime
 }
 
-export function shanghaiTimeFormat (time) {
+export function shanghaiTimeFormat (time, formatStr = 'YYYY-MM-DD HH:mm:ss') {
   // 获取当前时间
   const now = dayjs(time);
   // 设置为上海时区
   const shanghaiTime = now.tz('Asia/Shanghai');
-  return shanghaiTime.format("YYYY-MM-DD HH:mm:ss")
+  return shanghaiTime.format(formatStr)
 }