From 4ab0a5ca811d425a53149307e95e48bb09cd854a Mon Sep 17 00:00:00 2001 From: xingyy <64720302+Concur-max@users.noreply.github.com> Date: Thu, 16 Jan 2025 16:18:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E9=87=8D=E6=9E=84=20HTTP?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=B9=B6=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 http.ts 重命名为 http.js - 添加全局错误处理逻辑 - 更新 API 调用和错误处理方式 - 优化登录页面的验证码发送逻辑 - 调整消息组件的显示位置 --- app/api/auth/index.js | 2 +- app/api/{http.ts => http.js} | 18 +++++++++++------- app/components/x-message/index.vue | 2 +- app/pages/login/index.vue | 29 +++++++++++++++++++---------- 4 files changed, 32 insertions(+), 19 deletions(-) rename app/api/{http.ts => http.js} (73%) diff --git a/app/api/auth/index.js b/app/api/auth/index.js index 09325fa..b974d76 100644 --- a/app/api/auth/index.js +++ b/app/api/auth/index.js @@ -1,4 +1,4 @@ -import { getHttp } from '@/api/http.ts' +import { getHttp } from '~/api/http.js' export async function senCode(data) { const http = getHttp() diff --git a/app/api/http.ts b/app/api/http.js similarity index 73% rename from app/api/http.ts rename to app/api/http.js index 60f2356..d9ec74b 100644 --- a/app/api/http.ts +++ b/app/api/http.js @@ -1,19 +1,17 @@ -import type { $Fetch } from 'ofetch' import { useRuntimeConfig } from '#app' import { ofetch } from 'ofetch' +import {message} from '@/components/x-message/useMessage.js' +let httpStatusErrorHandler -type HttpStatusErrorHandler = (message: string, statusCode: number) => void -let httpStatusErrorHandler: HttpStatusErrorHandler - -let http: $Fetch +let http export function setupHttp() { if (http) return http const config = useRuntimeConfig() - const baseURL = config.public.NUXT_PUBLIC_API_BASE as string + const baseURL = config.public.NUXT_PUBLIC_API_BASE http = ofetch.create({ baseURL, @@ -26,7 +24,13 @@ export function setupHttp() { ...(token && { Authorization: token }), } }, + async onResponse({ response }) { + if (response._data.status===1){ + message.warning(response._data.msg) + } + }, async onResponseError({ response }) { + console.log('error错误') const { message } = response._data if (Array.isArray(message)) { message.forEach((item) => { @@ -43,7 +47,7 @@ export function setupHttp() { }) } -export function injectHttpStatusErrorHandler(handler: HttpStatusErrorHandler) { +export function injectHttpStatusErrorHandler(handler) { httpStatusErrorHandler = handler } diff --git a/app/components/x-message/index.vue b/app/components/x-message/index.vue index 6d9194e..cd38213 100644 --- a/app/components/x-message/index.vue +++ b/app/components/x-message/index.vue @@ -28,7 +28,7 @@ defineExpose({ showMessage }) v-if="visible" :text="messageText" :type="messageType" - class="fixed top-20px left-1/2 -translate-x-1/2 z-9999" + class="fixed top-50px left-1/2 -translate-x-1/2 z-9999" /> diff --git a/app/pages/login/index.vue b/app/pages/login/index.vue index cbfbd96..0198c90 100644 --- a/app/pages/login/index.vue +++ b/app/pages/login/index.vue @@ -11,10 +11,14 @@ definePageMeta({ title: '登录', i18n: 'login.title', }) +const loadingRef=ref({ + loading1:false +}) +const isExist=ref(false)//帐号是否存在 true存在 +const isReal=ref(false) //isReal 是否实名过 function goToPage() { router.push('/countryRegion'); } - const phoneNum = ref('') const code = ref('') const pane = ref(0) @@ -63,13 +67,19 @@ watch(locale, () => { }) const vanSwipeRef=ref(null) const getCode =async () => { -/* const res=await senCode({ + loadingRef.value.loading1=true + const res=await senCode({ telNum:phoneNum.value, zone:selectedZone.value - })*/ - pane.value = 1 - vanSwipeRef.value?.swipeTo(pane.value) - showKeyboard.value=true + }) + loadingRef.value.loading1=false + if (res.status===0){ + + pane.value = 1 + vanSwipeRef.value?.swipeTo(pane.value) + showKeyboard.value=true + } + } const goBack = () => { code.value = '' @@ -88,7 +98,7 @@ const goLogin = () => { -
+
@@ -108,7 +118,7 @@ const goLogin = () => {
- {{ $t('login.getCode') + {{ $t('login.getCode') }} {{ $t('login.getCode') }} @@ -116,13 +126,12 @@ const goLogin = () => {
-
+
{{ $t('login.hasSendTo') }}
+{{ selectedZone }} {{ phoneNum }}
-
{{ $t('login.reSend') }}