Browse Source

创建账单10%

John 8 months ago
parent
commit
abdc5b8256

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

@@ -50,4 +50,11 @@ export const uploadFile = async (sampleFile:File): Promise<void> => {
       'Content-Type': 'multipart/form-data',
     },
   });
+};
+
+
+// 获取账本数据
+export const getBookInfo = async (id: number): Promise<User> => {
+  const response = await http.get<User>(`/api/v1/books/${id}`);
+  return response.data;
 };

+ 4 - 8
frontEndMobile/src/api/base.ts

@@ -1,4 +1,5 @@
-import request from './request'
+
+import http from './http';
 
 export function register(params: {
   account: string
@@ -7,14 +8,9 @@ export function register(params: {
   password: string
 }) {
   console.log(params)
-  return request.post(
+  return http.post(
     '/api/v1/login/register',
     params,
-    {
-      headers: {
-        'Content-Type': 'application/x-www-form-urlencoded'
-      }
-    }
   )
 }
 
@@ -23,5 +19,5 @@ export function login(params: {
   account_type: number
   password: string
 }) {
-  return request.post('/api/v1/login', params)
+  return http.post('/api/v1/login', params)
 }

+ 54 - 19
frontEndMobile/src/api/http.ts

@@ -1,7 +1,11 @@
 // src/services/http.ts
 
-import axios, { AxiosInstance } from 'axios';
-import router from '@/router';
+import axios, { AxiosInstance } from 'axios'
+import router from '@/router'
+
+// 进度条和样式
+import nProgress from 'nprogress' // npm install nprogress
+import 'nprogress/nprogress.css'
 
 // 创建一个 Axios 实例
 const http: AxiosInstance = axios.create({
@@ -9,41 +13,72 @@ const http: AxiosInstance = axios.create({
   baseURL: 'http://127.0.0.1:3000', // 替换为你的 API 基础 URL
   timeout: 10000, // 请求超时时间
   headers: {
-    'Content-Type': 'application/json',
-  },
-});
+    'Content-Type': 'application/json'
+  }
+})
 
 // 请求拦截器
 http.interceptors.request.use(
-  config => {
+  (config) => {
+    // 开始进度条
+    nProgress.start()
     // 在发送请求之前做些什么(例如添加 token)
-    const token = localStorage.getItem('token');
+    const token = localStorage.getItem('token')
     if (token) {
-      config.headers.Authorization = `${token}`;
+      config.headers.Authorization = `${token}`
     }
-    return config;
+    return config
   },
-  error => {
+  (error) => {
     // 处理请求错误
-    return Promise.reject(error);
+    return Promise.reject(error)
   }
-);
+)
 
 // 响应拦截器
 http.interceptors.response.use(
-  response => {
+  (response) => {
+    // 响应成功关闭进度条
+    nProgress.done()
     // 对响应数据做点什么
-    return response.data;
+    return response.data
   },
-  error => {
+  (error) => {
+    // 请求超时处理
+    if (error.message.includes('timeout')) {
+      alert('请求超时')
+      return
+    }
     if (error.status === 400) {
       router.push({
         name: 'login'
       })
     }
-    // 处理响应错误
-    return Promise.reject(error);
+    // 不同错误状态码处理
+    const code = error.response.status
+
+    switch (code) {
+      case 400:
+        console.log('请求错误')
+        break
+      case 401:
+        console.log('未授权')
+        break
+      case 403:
+        console.log('禁止访问')
+        break
+      case 404:
+        console.log('页面消失')
+        break
+      case 500:
+        console.log('服务器内部错误')
+        break
+      case 502:
+        console.log('网关错误')
+        break
+    }
+    return Promise.reject(error)
   }
-);
+)
 
-export default http;
+export default http

+ 2 - 0
frontEndMobile/src/environment/index.js

@@ -0,0 +1,2 @@
+// 环境
+

+ 20 - 8
frontEndMobile/src/views/AddAccountLogPage.vue

@@ -3,7 +3,12 @@
     <van-form @submit="onSubmit">
       <van-cell-group inset>
         <!-- 允许输入数字,调起带符号的纯数字键盘 -->
-        <van-field v-model="totalFee" type="number" label="数字" />
+        <van-field
+          v-model="totalFee"
+          name="totalFee"
+          type="number"
+          label="数字"
+        />
         <van-field name="uploader" label="文件上传">
           <template #input>
             <van-uploader
@@ -25,9 +30,10 @@
           @click="showCalendar = true"
         />
         <van-field
-          v-model="message"
+          v-model="remark"
           rows="2"
           autosize
+          name="remark"
           label="留言"
           type="textarea"
           maxlength="50"
@@ -50,7 +56,7 @@
 </template>
 
 <script setup>
-import { watch, ref, onMounted } from 'vue'
+import { watch, ref, onMounted, toRaw } from 'vue'
 import { useRouter, useRoute } from 'vue-router'
 import dayjs from 'dayjs'
 import { uploadFile } from '@/api/api'
@@ -60,11 +66,9 @@ const route = useRoute()
 
 const files = ref([])
 const totalFee = ref(0)
-const message = ref('')
+const remark = ref('')
 
 const afterRead = async (file) => {
-  console.log(66, file);
-
   // 将文件上传至服务器
   let formData = new FormData()
   formData.append('sampleFile', file.file)
@@ -88,11 +92,19 @@ const onConfirm = (date) => {
 const filesDelete = (file) => {
   files.value = files.value.filter((elm) => elm.url !== file.url)
 }
-const onSubmit = () => {
+const onSubmit = (values) => {
   // 金额
   // 附件
   // 时间
-  console.log(95, newTime.value);
   // 留言
+  // const params = {
+  //   totalFee: toRaw(number),
+  //   files: toRaw(files),
+  //   time: toRaw(newTime),
+  //   remark: toRaw(message)
+  // }
+  // console.log(103, params)
+
+  console.log(102, values)
 }
 </script>

+ 28 - 9
frontEndMobile/src/views/HomePage.vue

@@ -25,7 +25,11 @@
 
   <!-- 时间列表 -->
   <div class="records-box">
-    <RecordInRow :time_record_list="time_record_list" :monthlyStr="monthlyStr" :yearStr=yearStr></RecordInRow>
+    <RecordInRow
+      :time_record_list="time_record_list"
+      :monthlyStr="monthlyStr"
+      :yearStr="yearStr"
+    ></RecordInRow>
   </div>
 
   <!-- 选择时间 -->
@@ -42,9 +46,10 @@
 
 <script setup>
 import { ref, computed, watch, onMounted } from 'vue'
-import {register} from '@/api/base'
+import { register, login } from '@/api/base'
+import { getBookInfo } from '@/api/api'
 import dayjs from 'dayjs'
-import RecordInRow from "@/components/RecordInRow.vue"
+import RecordInRow from '@/components/RecordInRow.vue'
 
 const showSelectTime = ref(false)
 const currentDate = ref([])
@@ -86,7 +91,14 @@ const monthlyStr = computed(() => {
   return `${currentDate.value[1]}`
 })
 
+async function getBookInfoFn() {
+  await getBookInfo(1)
+}
+
 onMounted(() => {
+  getBookInfoFn()
+  // getLogin()
+
   for (let index = 0; index < 40; index++) {
     const elm = {
       name: 'name' + index,
@@ -99,12 +111,21 @@ onMounted(() => {
     time_record_list.value.push(elm)
   }
 })
+async function getRegister() {
+  const res = await register({
+    account: 'x.czvufulcym@qqxhjl.ee',
+    account_type: 2,
+    password: 'admin'
+  })
+  console.log(119, res)
+}
 async function getLogin() {
-  await register({
-      "account": "x.czvufulcym@qqxhjl.ee",
-      "account_type": 2,
-      "password": "admin"
+  const res = await login({
+    account: 'x.czvufulcym@qqxhjl.ee',
+    account_type: 2,
+    password: 'admin'
   })
+  window.localStorage.setItem('token', res.data.token)
 }
 </script>
 
@@ -138,7 +159,5 @@ async function getLogin() {
     }
   }
 }
-
-
 </style>
 >>>>>>> origin/master

+ 0 - 1
node_expores/router/books/index.js

@@ -27,7 +27,6 @@ router.get("/:book_id", async function (req, res) {
   const book_id = req.params.book_id; // 获取 fileId 参数
   const bookInfo = await getBookById(book_id);
   // console.log(29, bookInfo)
-  shanghaiTimeFormat
   bookInfo.create_time = shanghaiTimeFormat(bookInfo.create_time)
   bookInfo.update_time = shanghaiTimeFormat(bookInfo.update_time)
   res.json({

+ 4 - 1
node_expores/router/files/index.js

@@ -3,7 +3,7 @@ import express from "express";
 import path from "path";
 import fs from "node:fs";
 import { files_insert, ishaveFileBymd5, getFileBymd5 } from "#db";
-import {shanghaiTime} from '#utils'
+import {shanghaiTime, dirExists} from '#utils'
 const router = express.Router();
 
 router.use(function timeLog(req, res, next) {
@@ -42,6 +42,8 @@ router.put("/", async function (req, res) {
   }
 
   sampleFile = req.files.sampleFile;
+  console.log(45, sampleFile);
+  
 
   // const fileMd5 = await computeFileMD5(sampleFile);
   const fileMd5 = sampleFile.md5;
@@ -62,6 +64,7 @@ router.put("/", async function (req, res) {
     update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
   };
   uploadPath = "./base_files/" + sampleFile.md5;
+  dirExists("./base_files/")
 
   // 、移动上传文件至指定目录
   sampleFile.mv(uploadPath, async function (err) {

+ 57 - 0
node_expores/utils/files.js

@@ -0,0 +1,57 @@
+import fs from "node:fs";
+import path from "path";
+/**
+ * 读取路径信息
+ * @param {string} path 路径
+ */
+export function getStat(path) {
+  return new Promise((resolve, reject) => {
+    fs.stat(path, (err, stats) => {
+      if (err) {
+        resolve(false);
+      } else {
+        resolve(stats);
+      }
+    })
+  })
+}
+
+/**
+  * 创建路径
+  * @param {string} dir 路径
+  */
+export function mkdir(dir) {
+  return new Promise((resolve, reject) => {
+    fs.mkdir(dir, err => {
+      if (err) {
+        resolve(false);
+      } else {
+        resolve(true);
+      }
+    })
+  })
+}
+
+/**
+  * 路径是否存在,不存在则创建
+  * @param {string} dir 路径
+  */
+export async function dirExists(dir) {
+  let isExists = await getStat(dir);
+  //如果该路径且不是文件,返回true
+  if (isExists && isExists.isDirectory()) {
+    return true;
+  } else if (isExists) {
+    //如果该路径存在但是文件,返回false
+    return false;
+  }
+  //如果该路径不存在,拿到上级路径
+  let tempDir = path.parse(dir).dir;
+  //递归判断,如果上级目录也不存在,则会代码会在此处继续循环执行,直到目录存在
+  let status = await dirExists(tempDir);
+  let mkdirStatus;
+  if (status) {
+    mkdirStatus = await mkdir(dir);
+  }
+  return mkdirStatus;
+}