From 9cf46e022c1363c58a4fb0e65283cfb381929212 Mon Sep 17 00:00:00 2001 From: yg <3280565352@qq.com> Date: Fri, 29 Nov 2024 09:35:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=A8=E5=85=89=E7=9A=84axios=E7=9A=84?= =?UTF-8?q?=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(tabs)/index.tsx | 9 +++++---- app/Api/api.js | 26 ++++++++++++++++++++++++++ app/YgFile/Academic.tsx | 3 ++- app/YgFile/Anser.tsx | 1 + app/YgFile/DescAcademic.tsx | 3 ++- app/YgFile/Descist.tsx | 9 +++++---- app/YgFile/Meeting.tsx | 3 ++- app/YgFile/Oncologist.tsx | 3 ++- app/YgFile/activity.tsx | 4 ++-- 9 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 app/Api/api.js diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 3316951..5e720ce 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -14,6 +14,7 @@ import Swiper from "react-native-swiper"; import axios from "axios"; import { router } from "expo-router"; import Constants from "expo-constants"; +import Api from '../Api/api' export default function Index() { const [carousels, setcarousel] = useState([]); // 轮播图数据 const [loading, setLoading] = useState(true); // 加载状态 @@ -73,7 +74,7 @@ export default function Index() { // 获取轮播图数据 const fetchCarouselImages = async () => { try { - const data = await axios.get("http://192.168.17.111:3000/yg/carousel"); + const data = await Api.get("http://192.168.17.111:3000/yg/carousel"); setcarousel(data.data.data); setError((prev) => ({ ...prev, carousel: false })); } catch (error) { @@ -84,7 +85,7 @@ export default function Index() { // 获取名师讲堂数据 const getMing = async () => { try { - const data = await axios.get("http://192.168.17.111:3000/yg/ming"); + const data = await Api.get("/yg/ming"); setming(data.data.data.slice(0, 2)); setError((prev) => ({ ...prev, ming: false })); } catch (error) { @@ -95,7 +96,7 @@ export default function Index() { // 获取精品课程数据 const getCourse = async () => { try { - const data = await axios.get("http://192.168.17.111:3000/yg/course"); + const data = await Api.get("/yg/course"); setcourse(data.data.data.slice(0, 4)); setError((prev) => ({ ...prev, course: false })); } catch (error) { @@ -106,7 +107,7 @@ export default function Index() { // 获取肿瘤专家数据 const getDoctor = async () => { try { - const data = await axios.get("http://192.168.17.111:3000/yg/expert"); + const data = await Api.get("/yg/expert"); setdoctor(data.data.data.slice(0, 4)); setError((prev) => ({ ...prev, doctor: false })); } catch (error) { diff --git a/app/Api/api.js b/app/Api/api.js new file mode 100644 index 0000000..b512a05 --- /dev/null +++ b/app/Api/api.js @@ -0,0 +1,26 @@ +import axios from "axios"; + +var axiosApi = axios.create({ + baseURL: "http://192.168.17.111:3000/", + timeout: 1000, +}); +// 请求拦截器 +axiosApi.interceptors.request.use( + function (config) { + return config; + }, + function (error) { + return Promise.reject(error); + } +); +// 响应拦截器 +axiosApi.interceptors.response.use( + function (response) { + return response; + }, + function (error) { + return Promise.reject(error); + } +); + +export default axiosApi; diff --git a/app/YgFile/Academic.tsx b/app/YgFile/Academic.tsx index 57d4274..aa9aee0 100644 --- a/app/YgFile/Academic.tsx +++ b/app/YgFile/Academic.tsx @@ -14,6 +14,7 @@ import Ionicons from "@expo/vector-icons/Ionicons"; import { Tabs } from "@ant-design/react-native"; import { router } from "expo-router"; import axios from "axios"; +import Api from '../Api/api' export default function Academic() { const [state, setState] = useState(false); //控制搜索框的显示 const [text, onChangeText] = useState(""); @@ -27,7 +28,7 @@ export default function Academic() { try { const { data: { data }, - } = await axios.get("http://192.168.17.111:3000/yg/expert"); + } = await Api.get("/yg/expert"); setlist(data); } catch (error) { console.log(error); diff --git a/app/YgFile/Anser.tsx b/app/YgFile/Anser.tsx index 4c30be1..ee1f635 100644 --- a/app/YgFile/Anser.tsx +++ b/app/YgFile/Anser.tsx @@ -2,6 +2,7 @@ import { StyleSheet, Text, View, Button, Dimensions } from "react-native"; import React, { useState } from "react"; import { Radio } from "@ant-design/react-native"; import { router } from "expo-router"; +import Api from '../Api/api' export default function Anser() { const RadioItem = Radio.RadioItem; const radi = [ diff --git a/app/YgFile/DescAcademic.tsx b/app/YgFile/DescAcademic.tsx index 1c15a25..fd9cb55 100644 --- a/app/YgFile/DescAcademic.tsx +++ b/app/YgFile/DescAcademic.tsx @@ -9,6 +9,7 @@ import { import React, { useEffect, useState } from "react"; import { useRoute } from "@react-navigation/native"; import axios from "axios"; +import Api from '../Api/api' export default function DescAcademic() { let route = useRoute(); let [List, setlist] = useState([]); @@ -19,7 +20,7 @@ export default function DescAcademic() { const getexpoer = async () => { let { data: { data }, - } = await axios.get("http://192.168.17.111:3000/yg/getexpert", { + } = await Api.get("/yg/getexpert", { params: { id }, }); setlist(data); diff --git a/app/YgFile/Descist.tsx b/app/YgFile/Descist.tsx index 0dd0540..f9735e8 100644 --- a/app/YgFile/Descist.tsx +++ b/app/YgFile/Descist.tsx @@ -22,6 +22,7 @@ import * as Print from "expo-print"; // 导入打印模块 import * as FileSystem from "expo-file-system"; import { shareAsync } from "expo-sharing"; import { store } from "expo-router/build/global-state/router-store"; +import Api from '../Api/api' export default function Descist() { const route = useRoute(); let { id } = route.params; @@ -51,7 +52,7 @@ export default function Descist() { try { let { data: { data }, - } = await axios.post(`http://192.168.17.111:3000/yg/getcontent`, { id }); + } = await Api.post(`/yg/getcontent`, { id }); setlist(data); } catch (error) { console.log(error); @@ -62,7 +63,7 @@ export default function Descist() { try { let { data: { data }, - } = await axios.get(`http://192.168.17.111:3000/yg/getcomment`); + } = await Api.get(`/yg/getcomment`); setContextList(data); } catch (error) { console.log(error); @@ -176,8 +177,8 @@ export default function Descist() { xiangtitle: comment, }; console.log(obj); - await axios - .post(`http://192.168.17.111:3000/yg/sendcomment`, obj) + await Api + .post(`/yg/sendcomment`, obj) .then((res) => { if (res.data.code === 200) { getcomment(); diff --git a/app/YgFile/Meeting.tsx b/app/YgFile/Meeting.tsx index d8ca852..73c21c9 100644 --- a/app/YgFile/Meeting.tsx +++ b/app/YgFile/Meeting.tsx @@ -13,6 +13,7 @@ import Ionicons from "@expo/vector-icons/Ionicons"; import { Tabs } from "@ant-design/react-native"; import axios from "axios"; import { router } from "expo-router"; +import Api from '../Api/api' export default function Meeting() { const tabs = [{ title: "经典病例" }, { title: "读片会" }]; const [text, onChangeText] = useState(""); @@ -34,7 +35,7 @@ export default function Meeting() { try { const { data: { data }, - } = await axios.get("http://192.168.17.111:3000/yg/share"); + } = await Api.get("/yg/share"); setcase(data); } catch (error) { console.log(error); diff --git a/app/YgFile/Oncologist.tsx b/app/YgFile/Oncologist.tsx index 331c266..f1b396d 100644 --- a/app/YgFile/Oncologist.tsx +++ b/app/YgFile/Oncologist.tsx @@ -12,6 +12,7 @@ import React, { useEffect, useState } from "react"; import Ionicons from "@expo/vector-icons/Ionicons"; import { Tabs } from "@ant-design/react-native"; import axios from "axios"; +import Api from '../Api/api' import { router } from "expo-router"; export default function Oncologist() { useEffect(() => { @@ -25,7 +26,7 @@ export default function Oncologist() { try { let { data: { data }, - } = await axios.get("http://192.168.17.111:3000/yg/getleanring"); + } = await Api.get("/yg/getleanring"); setlearning(data); } catch (error) { console.log(error); diff --git a/app/YgFile/activity.tsx b/app/YgFile/activity.tsx index eeba20c..d58739c 100644 --- a/app/YgFile/activity.tsx +++ b/app/YgFile/activity.tsx @@ -13,7 +13,7 @@ import React, { useEffect, useState } from "react"; import Ionicons from "@expo/vector-icons/Ionicons"; import axios from "axios"; import { router } from "expo-router"; - +import Api from '../Api/api' export default function activity() { const [state, setState] = useState(false); //控制搜索框的显示 const [text, onChangeText] = useState(""); //搜索框的值 @@ -21,7 +21,7 @@ export default function activity() { let getacitiy = async () => { let { data: { data }, - } = await axios.get("http://192.168.17.111:3000/yg/activity"); + } = await Api.get("/yg/activity"); let data1 = data.sort((a: any, b: any) => { if ( a.activitystatus === "活动报名中" && -- Gitee