john 8 сар өмнө
parent
commit
44a703b84d

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

@@ -61,6 +61,12 @@ export const getAllBook = async (id: number): Promise<User> => {
   return response.data
 }
 
+// 添加账本
+export const addBook = async (params): Promise<User> => {
+  const response = await http.post<User>(`/api/v1/books`, params)
+  return response.data
+}
+
 // 获取账本数据
 export const getBookInfo = async (id: number): Promise<User> => {
   const response = await http.get<User>(`/api/v1/books/${id}`)

+ 1 - 1
frontEndMobile/src/api/http.ts

@@ -43,7 +43,7 @@ http.interceptors.response.use(
     nProgress.done()
     
     // 对响应数据做点什么
-    return response.data
+    return response
   },
   (error) => {
     console.log(47);

+ 0 - 71
frontEndMobile/src/api/request.ts

@@ -1,71 +0,0 @@
-// 引入axios
-import axios from 'axios'
-// 进度条和样式
-import nProgress from 'nprogress' // npm install nprogress
-import 'nprogress/nprogress.css'
-// 实例化axios
-const install = axios.create({
-  // 请求地址的前缀 公共全局的URL
-  baseURL: 'http://127.0.0.1:3000',
-  // 请求时长  --- 超出定义的时长,请求超时
-  timeout: 5000
-})
-// 请求拦截
-install.interceptors.request.use(
-  (config) => {
-    // 开始进度条
-    nProgress.start()
-    // 获取token
-    const token = localStorage.getItem('token')
-    // 请求头携带token
-    config.headers[''] = token
-    return config
-  },
-  (error) => {
-    return Promise.reject(error)
-  }
-)
-// 响应拦截
-install.interceptors.response.use(
-  (response) => {
-    // 响应成功关闭进度条
-    nProgress.done()
-    // 返回响应的数据
-    return response
-  },
-  (error) => {
-    console.log(3737, error)
-    debugger
-    // 请求超时处理
-    if (error.message.includes('timeout')) {
-      alert('请求超时')
-      return
-    }
-    // 不同错误状态码处理
-    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)
-  }
-)
-// 导出封装好的aixos
-export default install

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

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

+ 41 - 0
frontEndMobile/src/views/AddBooksPage.vue

@@ -0,0 +1,41 @@
+<template>
+  <van-form @submit="onSubmit">
+    <van-cell-group inset>
+      <van-field
+        v-model="book_name"
+        name="book_name"
+        label="账本名称"
+        placeholder="账本名称"
+        :rules="[{ required: true, message: '请填写账本名称' }]"
+      />
+    </van-cell-group>
+    <div style="margin: 16px">
+      <van-button round block type="primary" native-type="submit">
+        提交
+      </van-button>
+    </div>
+  </van-form>
+</template>
+
+<script setup>
+import { ref } from 'vue'
+import { useRouter, useRoute } from 'vue-router'
+import { showSuccessToast, showFailToast } from 'vant'
+import { addBook } from '@/api/api'
+const book_name = ref('')
+const router = useRouter()
+
+
+const onSubmit = async params => {
+  const res = await addBook(params)
+  console.log(31313, res);
+  
+  if(res.code === 200) {
+    showSuccessToast('添加成功!')
+    router.back()
+  } else {
+    showFailToast(res.msg)
+  }
+
+}
+</script>

+ 8 - 2
frontEndMobile/src/views/BooksPage.vue

@@ -9,6 +9,8 @@
       <div class="id">{{ item.id }}</div>
       <div class="book_name">{{ item.book_name }}</div>
     </div>
+    <van-button round block type="primary" @click="add"> 新增 </van-button>
+
   </div>
 </template>
 
@@ -25,16 +27,19 @@ const router = useRouter()
 onMounted(async () => {
   const res = await getAllBook()
   console.log(101010, res)
-  books.value = res
+  books.value = res.data
 })
 
 function selectBook (item) {
   window.localStorage.setItem('book', JSON.stringify(item))
   window.localStorage.setItem('bookId', item.id)
-  
+
   // const route = useRoute()
   router.back()
 }
+function add() {
+  router.push('/add_books')
+}
 </script>
 
 <style scoped lang="scss">
@@ -47,6 +52,7 @@ function selectBook (item) {
     background-color: #fff;
     margin-bottom: 24px;
     display: flex;
+    color: black;
     .id {
       padding-right: 12px;
     }

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

@@ -62,7 +62,7 @@ router.post("/", async function (req, res) {
     author_id: userInfo.user_id,
   });
   if (isAddType) {
-    res.status(500).json({
+    res.json({
       code: 500,
       msg: "已存在重复数据",
     });