Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(settings): rewrite settings itself and related pages #154

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/pages/setting/changePassword/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
border-radius: 0.2rem;
}
}

.warning-text {
color: var(--wjh-color-red-600);
font-size: 0.875rem;
}
112 changes: 53 additions & 59 deletions src/pages/setting/changePassword/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@
<card title="修改密码" class="input-card">
<text>身份证号码</text>
<view>
<input v-model="iid" password placeholder="请输入您的身份证号码">
<input v-model="iid" placeholder="请输入您的身份证号码">
</view>
<text>学号</text>
<view>
<input v-model="stuid" password placeholder="请输入您的学号">
<input v-model="stuid" placeholder="请输入您的学号">
</view>
<text>新密码</text>
<view>
<input v-model="password" password placeholder="请输入您的新密码">
<input
v-model="password"
type="password"
placeholder="请输入您的新密码"
@blur="handleValidateForm"
>
</view>
<text>确认新密码</text>
<view>
<input v-model="passwordAgain" password placeholder="请重复输入您的新密码">
<input
v-model="passwordAgain"
type="password"
placeholder="请重复输入您的新密码"
@blur="handleValidateForm"
>
</view>
<text v-if="warningText" class="warning-text">
{{ warningText }}
</text>
<template #footer>
<w-button block @tap="isShowConfirm = true">
确认修改
Expand All @@ -38,7 +51,7 @@
},
confirm: {
label: '确定',
callback: changePasswordClick
callback: handleSubmit
}
}"
/>
Expand All @@ -52,72 +65,53 @@ import { Card, ThemeConfig, TitleBar, WButton, WModal } from "@/components";
import "./index.scss";
import Taro from "@tarojs/taro";
import { UserService } from "@/services";
import { useRequest } from "@/hooks";
import { helpText } from "@/constants/copywriting";
import { RequestError } from "@/utils";

const iid = ref("");
const stuid = ref("");
const password = ref("");
const passwordAgain = ref("");
const isShowConfirm = ref(false);
const warningText = ref("");

const changePasswordClick = () => {
isShowConfirm.value = false;
if (password.value !== passwordAgain.value) {
Taro.showToast({
icon: "none",
title: "两次密码不一致!"
});
return;
} else if (password.value.length < 6 || password.value.length > 20) {
Taro.showToast({
icon: "none",
title: "密码长度必须在6~20位之间!"
});
return;
function handleValidateForm() {
const hasUnfilled = [iid, stuid, password, passwordAgain].some(field => !field.value);
// 未完成表单情况下,不在 blur 事件中展示 warningText
if (hasUnfilled) return "请完成表单";

let reason = "";
if (password.value.length < 6 || password.value.length > 20) {
reason = "密码长度必须在 6~20 位之间!";
} else if (password.value !== passwordAgain.value) {
reason = "两次密码不一致!";
}
Taro.showLoading({
title: "正在修改中",
mask: true
});
run({
iid: iid.value,
stuid: stuid.value,
password: password.value
});
warningText.value = reason;
return reason;
};

const { run } = useRequest(
UserService.changePassword, {
loadingDelay: 600,
onSuccess: (res) => {
if (res.data.code === 1 && res.data.msg === "OK") {
Taro.showToast({
icon: "success",
title: "修改密码成功"
});
} else if (res.data.code === 200510) {
Taro.showToast({
icon: "none",
title: "该学号或身份证不存在或者不匹配,请重新输入!"
});
} else if (res.data.code === 200511) {
Taro.showToast({
icon: "none",
title: "密码长度必须在6~20位之间!"
});
} else if (res.data.code === 200513) {
Taro.showToast({
icon: "none",
title: "学号格式不正确,请重新输入!"
});
}
},
onError: (e: Error) => {
return `失败\r\n${e.message || "网络错误"}`;
}
async function handleSubmit() {
const failedReason = handleValidateForm();
if (failedReason) {
warningText.value = failedReason;
return;
}

isShowConfirm.value = false;
Taro.showLoading({ title: "正在修改中", mask: true });

try {
await UserService.changePassword({
iid: iid.value,
stuid: stuid.value,
password: password.value
});
Taro.showToast({ icon: "success", title: "修改密码成功" });
} catch (e) {
if (e instanceof RequestError)
Taro.showToast({ icon: "none", title: `修改密码失败: ${e.message}` });
}
);
}

const onCancel = () => {
isShowConfirm.value = false;
Expand Down
10 changes: 1 addition & 9 deletions src/pages/setting/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,12 @@ import Taro from "@tarojs/taro";
import { Card, ThemeConfig, TitleBar, WList, WListItem } from "@/components";
import { settingText } from "@/constants/copywriting";
import { getCopyRight } from "@/utils";
import { ref, watch } from "vue";
import { serviceStore } from "@/store";
import { ref } from "vue";
import "./index.scss";

const isEmpty = ref(true);
const emptyText = settingText.empty;
const copyright = getCopyRight();
const themeMode = ref(serviceStore.theme.themeMode);
const currentTab = ref(themeMode);

watch(() => serviceStore.theme.themeMode, (newValue) => {
currentTab.value = newValue;
themeMode.value = newValue;
});

const nav2ChangePassword = () => {
Taro.navigateTo({ url: "/pages/setting/changePassword/index" });
Expand Down
68 changes: 19 additions & 49 deletions src/pages/setting/logout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<card title="注销" class="input-card">
<text>身份证号码</text>
<view>
<input v-model="iid" password placeholder="请输入您的身份证号码">
<input v-model="iid" type="password" placeholder="请输入您的身份证号码">
</view>
<text>学号</text>
<view>
<input v-model="stuid" password placeholder="请输入您的学号">
<input v-model="stuid" type="password" placeholder="请输入您的学号">
</view>
<template #footer>
<w-button block @tap="isShowConfirm = true">
Expand All @@ -35,7 +35,7 @@
},
confirm: {
label: '确定',
callback: logoutClick
callback: handleLogout
}
}"
/>
Expand All @@ -48,61 +48,31 @@ import { ref } from "vue";
import { Card, ThemeConfig, TitleBar, WButton, WModal } from "@/components";
import "./index.scss";
import Taro from "@tarojs/taro";
import { UserService } from "@/services";
import { useRequest } from "@/hooks";
import { helpText } from "@/constants/copywriting";
import store from "@/store";
import { CookieUtils, RequestError } from "@/utils";
import useUserStore from "@/store/service/user";
import { UserService } from "@/services";

const userStore = useUserStore();
const iid = ref("");
const stuid = ref("");
const isShowConfirm = ref(false);

const logoutClick = () => {
async function handleLogout() {
isShowConfirm.value = false;
Taro.showLoading({
title: "正在注销中",
mask: true
});
run({
iid: iid.value,
stuid: stuid.value
});
};
Taro.showLoading({ title: "正在注销中", mask: true });

const { run } = useRequest(
UserService.logout, {
loadingDelay: 600,
onSuccess: (res) => {
if (res.data.code === 1 && res.data.msg === "OK") {
Taro.showToast({
icon: "success",
title: "注销成功"
});
store.commit("clearSession");
store.commit("clearUserInfo");
setTimeout(nav2Home, 2000);
} else if (res.data.code === 200511) {
Taro.showToast({
icon: "none",
title: "密码长度必须在6~20位之间!"
});
} else if (res.data.code === 200510) {
Taro.showToast({
icon: "none",
title: "该学号或身份证不存在或者不匹配,请重新输入!"
});
} else if (res.data.code === 200513) {
Taro.showToast({
icon: "none",
title: "学号格式不正确,请重新输入!"
});
}
},
onError: (e: Error) => {
return `失败\r\n${e.message || "网络错误"}`;
}
try {
await UserService.logout({ iid: iid.value, stuid: stuid.value });
Taro.showToast({ icon: "success", title: "注销成功" });
CookieUtils.clear();
userStore.clearUserData();
setTimeout(nav2Home, 2000);
} catch (e) {
if (e instanceof RequestError)
Taro.showToast({ icon: "none", title: e.message });
}
);
}

const onCancel = () => {
isShowConfirm.value = false;
Expand Down
Loading