feat: 添加 Vant 组件库支持,优化 toast 消息显示逻辑
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import type { App } from "@capp/eden";
|
||||
import type { WatchSource } from "vue";
|
||||
import { treaty } from "@elysiajs/eden";
|
||||
import { toastController } from "@ionic/vue";
|
||||
import { i18n } from "@/locales";
|
||||
|
||||
const baseURL = import.meta.env.DEV ? window.location.origin : import.meta.env.VITE_API_URL;
|
||||
@@ -63,28 +62,19 @@ export function safeClient<T, E>(
|
||||
|
||||
if (res.error) {
|
||||
if (!options.silent && res.status === 418) {
|
||||
const toast = await toastController.create({
|
||||
message: i18n.global.t((res.error as any).value.code, {
|
||||
...(res.error as any).value.context,
|
||||
}),
|
||||
duration: 3000,
|
||||
position: "bottom",
|
||||
color: "danger",
|
||||
});
|
||||
await toast.present();
|
||||
showToast(i18n.global.t((res.error as any).value.code, {
|
||||
...(res.error as any).value.context,
|
||||
}));
|
||||
}
|
||||
else if (res.status === 401) {
|
||||
localStorage.removeItem("user-token");
|
||||
window.location.reload();
|
||||
setTimeout(() => {
|
||||
showToast("登录状态已过期,2秒后将跳转到登录页面,请重新登录!");
|
||||
localStorage.removeItem("user-token");
|
||||
window.location.reload();
|
||||
}, 2000);
|
||||
}
|
||||
else if (!options.silent) {
|
||||
const toast = await toastController.create({
|
||||
message: (res.error as any).message || i18n.global.t("network_error"),
|
||||
duration: 3000,
|
||||
position: "bottom",
|
||||
color: "danger",
|
||||
});
|
||||
await toast.present();
|
||||
showToast((res.error as any).message || i18n.global.t("network_error"));
|
||||
}
|
||||
|
||||
throw res.error;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<script lang='ts' setup>
|
||||
import { toastController } from "@ionic/vue";
|
||||
import { safeClient } from "@/api";
|
||||
import { authClient } from "@/auth";
|
||||
import { LoginSchema } from "./schema";
|
||||
@@ -15,24 +14,15 @@ const agreed = ref(false);
|
||||
const showPassword = ref(false);
|
||||
const isLoading = ref(false);
|
||||
|
||||
async function showToast(message: string, color: "success" | "danger" | "warning" = "danger") {
|
||||
const toast = await toastController.create({
|
||||
message,
|
||||
duration: 2000,
|
||||
position: "top",
|
||||
color,
|
||||
});
|
||||
await toast.present();
|
||||
}
|
||||
async function handleLogin() {
|
||||
if (!agreed.value) {
|
||||
await showToast("请先阅读并同意服务条款和隐私政策", "warning");
|
||||
showToast("请先阅读并同意服务条款和隐私政策");
|
||||
return;
|
||||
}
|
||||
const result = LoginSchema.safeParse({ ...form.value });
|
||||
if (!result.success) {
|
||||
const first = result.error.issues[0];
|
||||
await showToast(first.message);
|
||||
showToast(first.message);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -43,17 +33,13 @@ async function handleLogin() {
|
||||
password: form.value.password,
|
||||
}));
|
||||
if (!data.value?.token) {
|
||||
toastController.create({
|
||||
message: "登录失败,请检查手机号或密码",
|
||||
duration: 2000,
|
||||
color: "danger",
|
||||
}).then(toast => toast.present());
|
||||
showToast("登录失败,请检查手机号或密码");
|
||||
}
|
||||
else {
|
||||
const userStore = useUserStore();
|
||||
userStore.setToken(data.value.token);
|
||||
await userStore.updateProfile();
|
||||
await showToast("登录成功!", "success");
|
||||
showToast("登录成功!");
|
||||
router.push(route.query.redirect as string || "/");
|
||||
}
|
||||
}
|
||||
@@ -73,6 +59,11 @@ function goToTerms(type: "service" | "privacy") {
|
||||
|
||||
<template>
|
||||
<ion-page>
|
||||
<ion-header class="ion-no-border">
|
||||
<ion-toolbar class="ion-toolbar">
|
||||
<ion-title>登录</ion-title>
|
||||
</ion-toolbar>
|
||||
</ion-header>
|
||||
<ion-content :fullscreen="true" class="login-page">
|
||||
<!-- 渐变背景 -->
|
||||
<div class="gradient-bg" />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script lang='ts' setup>
|
||||
import { faker } from "@faker-js/faker";
|
||||
import { loadingController, toastController } from "@ionic/vue";
|
||||
import { toastController } from "@ionic/vue";
|
||||
import { ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { safeClient } from "@/api";
|
||||
@@ -23,27 +23,17 @@ const showPassword = ref(false);
|
||||
const showConfirmPassword = ref(false);
|
||||
const isLoading = ref(false);
|
||||
|
||||
async function showToast(message: string, color: "success" | "danger" | "warning" = "danger") {
|
||||
const toast = await toastController.create({
|
||||
message,
|
||||
duration: 2000,
|
||||
position: "top",
|
||||
color,
|
||||
});
|
||||
await toast.present();
|
||||
}
|
||||
|
||||
async function handleSignup() {
|
||||
// 检查是否同意协议
|
||||
if (!agreed.value) {
|
||||
await showToast("请先阅读并同意服务条款和隐私政策", "warning");
|
||||
showToast("请先阅读并同意服务条款和隐私政策");
|
||||
return;
|
||||
}
|
||||
|
||||
const result = SignupSchema.safeParse(formData.value);
|
||||
if (!result.success) {
|
||||
const first = result.error.issues[0];
|
||||
await showToast(first.message);
|
||||
showToast(first.message);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -57,7 +47,7 @@ async function handleSignup() {
|
||||
password: formData.value.password,
|
||||
idCard: formData.value.idCard,
|
||||
inviteCode: formData.value.inviteCode || undefined,
|
||||
}));
|
||||
} as any));
|
||||
|
||||
if (!data.value?.token) {
|
||||
toastController.create({
|
||||
@@ -67,7 +57,7 @@ async function handleSignup() {
|
||||
}).then(toast => toast.present());
|
||||
}
|
||||
else {
|
||||
await showToast("注册成功!", "success");
|
||||
showToast("注册成功!");
|
||||
const userStore = useUserStore();
|
||||
userStore.setToken(data.value.token);
|
||||
await userStore.updateProfile();
|
||||
@@ -90,6 +80,11 @@ function goToTerms(type: "service" | "privacy") {
|
||||
|
||||
<template>
|
||||
<ion-page>
|
||||
<ion-header class="ion-no-border">
|
||||
<ion-toolbar class="ion-toolbar">
|
||||
<ion-title>注册</ion-title>
|
||||
</ion-toolbar>
|
||||
</ion-header>
|
||||
<ion-content :fullscreen="true" class="signup-page">
|
||||
<!-- 渐变背景 -->
|
||||
<div class="gradient-bg" />
|
||||
|
||||
Reference in New Issue
Block a user