feat: change password

This commit is contained in:
KUN1007 2023-10-12 18:05:31 +08:00
parent b2eed3c920
commit f7b64e0a14
7 changed files with 157 additions and 26 deletions

View file

@ -53,7 +53,18 @@ export async function getUserResetEmailCodeApi(
export async function updateUserEmailApi( export async function updateUserEmailApi(
request: User.UserUpdateEmailRequestData request: User.UserUpdateEmailRequestData
): Promise<User.UserUpdateEmailResponseData> { ): Promise<User.UserUpdateEmailResponseData> {
const url = `/user/:uid/email` const url = `/user/${request.uid}/email`
const response = fetchPut<User.UserUpdateEmailResponseData>(url, request)
return response
}
// 更新密码
export async function updateUserPasswordApi(
request: User.UserUpdatePasswordRequestData
): Promise<User.UserUpdatePasswordResponseData> {
const url = `/user/${request.uid}/password`
const response = fetchPut<User.UserUpdateEmailResponseData>(url, request) const response = fetchPut<User.UserUpdateEmailResponseData>(url, request)

View file

@ -30,6 +30,13 @@ export interface UserUpdateEmailRequestData {
code: string code: string
} }
// 用户更新密码
export interface UserUpdatePasswordRequestData {
uid: number
oldPassword: string
newPassword: string
}
export type UserInfoResponseData = KUNGalgameResponseData<UserInfo> export type UserInfoResponseData = KUNGalgameResponseData<UserInfo>
export type UserUpdateBioResponseData = KUNGalgameResponseData<{}> export type UserUpdateBioResponseData = KUNGalgameResponseData<{}>
@ -41,3 +48,5 @@ export type UserGetUserEmailResponseData = KUNGalgameResponseData<{
export type UserGetEmailRestCodeResponseData = KUNGalgameResponseData<{}> export type UserGetEmailRestCodeResponseData = KUNGalgameResponseData<{}>
export type UserUpdateEmailResponseData = KUNGalgameResponseData<{}> export type UserUpdateEmailResponseData = KUNGalgameResponseData<{}>
export type UserUpdatePasswordResponseData = KUNGalgameResponseData<{}>

View file

@ -108,5 +108,8 @@ onActivated(() => {
.disabled { .disabled {
cursor: not-allowed; cursor: not-allowed;
color: var(--kungalgame-font-color-0); color: var(--kungalgame-font-color-0);
input {
cursor: not-allowed;
}
} }
</style> </style>

View file

@ -24,6 +24,8 @@ import type {
UserGetEmailRestCodeResponseData, UserGetEmailRestCodeResponseData,
UserUpdateEmailRequestData, UserUpdateEmailRequestData,
UserUpdateEmailResponseData, UserUpdateEmailResponseData,
UserUpdatePasswordRequestData,
UserUpdatePasswordResponseData,
} from '@/api' } from '@/api'
import { import {
@ -32,6 +34,7 @@ import {
getUserEmailApi, getUserEmailApi,
getUserResetEmailCodeApi, getUserResetEmailCodeApi,
updateUserEmailApi, updateUserEmailApi,
updateUserPasswordApi,
} from '@/api' } from '@/api'
// kungalgame store 类型 // kungalgame store 类型
@ -161,5 +164,18 @@ export const useKUNGalgameUserStore = defineStore({
} }
return updateUserEmailApi(requestData) return updateUserEmailApi(requestData)
}, },
// 更新密码
async updatePassword(
oldPassword: string,
newPassword: string
): Promise<UserUpdatePasswordResponseData> {
const requestData: UserUpdatePasswordRequestData = {
uid: this.uid,
oldPassword: oldPassword,
newPassword: newPassword,
}
return updateUserPasswordApi(requestData)
},
}, },
}) })

View file

@ -1,9 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref } from 'vue' import { onMounted, reactive, ref } from 'vue'
import type { UserInfo } from '@/api' import type { UserInfo } from '@/api'
import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer' import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer'
import Message from '@/components/alert/Message' import Message from '@/components/alert/Message'
import { checkSendCode, checkResetEmail } from '../utils/check' import {
checkSendCode,
checkResetEmail,
checkChangePassword,
} from '../utils/check'
const props = defineProps<{ const props = defineProps<{
user: UserInfo user: UserInfo
@ -11,22 +15,32 @@ const props = defineProps<{
// //
const email = ref('') const email = ref('')
//
const newEmail = ref('')
//
const code = ref('')
// //
const hasSentCodeEmail = ref('') const hasSentCodeEmail = ref('')
// 5
const input = reactive({
newEmail: '',
code: '',
oldPassword: '',
newPassword: '',
repeatPassword: '',
})
// //
const handleSendCode = async () => { const handleSendCode = async () => {
// //
if (!checkSendCode(newEmail.value)) { if (!checkSendCode(input.newEmail)) {
return return
} }
hasSentCodeEmail.value = newEmail.value hasSentCodeEmail.value = input.newEmail
const res = await useKUNGalgameUserStore().getResetEmailCode(newEmail.value) Message(
'The email verification code is being sent ~~~',
'邮箱验证码正在发送 ~~~',
'info'
)
const res = await useKUNGalgameUserStore().getResetEmailCode(input.newEmail)
if (res.code === 200) { if (res.code === 200) {
Message( Message(
@ -46,13 +60,15 @@ const handleSendCode = async () => {
// //
const handleResetEmail = async () => { const handleResetEmail = async () => {
// //
if (!checkResetEmail(hasSentCodeEmail.value, newEmail.value, code.value)) { if (!checkResetEmail(hasSentCodeEmail.value, input.newEmail, input.code)) {
return return
} }
input.newEmail = ''
input.code = ''
const res = await useKUNGalgameUserStore().updateEmail( const res = await useKUNGalgameUserStore().updateEmail(
hasSentCodeEmail.value, hasSentCodeEmail.value,
code.value input.code
) )
if (res.code === 200) { if (res.code === 200) {
@ -62,6 +78,31 @@ const handleResetEmail = async () => {
} }
} }
//
const handleChangePassword = async () => {
//
if (
!checkChangePassword(
input.oldPassword,
input.newPassword,
input.repeatPassword
)
) {
return
}
const res = await useKUNGalgameUserStore().updatePassword(
input.oldPassword,
input.newPassword
)
if (res.code === 200) {
Message('Password change successfully!', '密码更改成功', 'success')
} else {
Message('Password change failed!', '密码更改失败', 'error')
}
}
onMounted(async () => { onMounted(async () => {
const response = await useKUNGalgameUserStore().getEmail() const response = await useKUNGalgameUserStore().getEmail()
@ -81,14 +122,18 @@ onMounted(async () => {
<div class="title">更改邮箱:</div> <div class="title">更改邮箱:</div>
<div class="current-email">您当前的邮箱是: {{ email }}</div> <div class="current-email">您当前的邮箱是: {{ email }}</div>
<div class="input"> <div class="input">
<span>请输入您的新邮箱: </span><input v-model="newEmail" type="text" /> <span>请输入您的新邮箱: </span>
<input v-model="input.newEmail" type="text" />
</div> </div>
<div class="input"> <div class="input">
<span>请输入您的验证码: </span><input v-model="code" type="text" /> <span>请输入您的验证码: </span>
<input v-model="input.code" type="text" />
</div> </div>
<div class="btn"> <div class="btn">
<button @click="handleSendCode">发送验证码</button> <button @click="handleSendCode" v-if="!hasSentCodeEmail">
发送验证码
</button>
<button @click="handleResetEmail">确定更改邮箱</button> <button @click="handleResetEmail">确定更改邮箱</button>
</div> </div>
</div> </div>
@ -96,17 +141,20 @@ onMounted(async () => {
<div class="password"> <div class="password">
<div class="title">更改密码:</div> <div class="title">更改密码:</div>
<div class="input"> <div class="input">
<span>请输入您的旧密码: </span><input type="password" /> <span>请输入您的旧密码: </span
><input v-model="input.oldPassword" type="password" />
</div> </div>
<div class="input"> <div class="input">
<span>请输入您的新密码: </span><input type="password" /> <span>请输入您的新密码: </span
><input v-model="input.newPassword" type="password" />
</div> </div>
<div class="input"> <div class="input">
<span>请再次输入新密码: </span><input type="password" /> <span>请再次输入新密码: </span
><input v-model="input.repeatPassword" type="password" />
</div> </div>
<div class="btn"> <div class="btn">
<button>确定更改密码</button> <button @click="handleChangePassword">确定更改密码</button>
</div> </div>
</div> </div>
</div> </div>

View file

@ -2,7 +2,11 @@
* *
*/ */
import Message from '@/components/alert/Message' import Message from '@/components/alert/Message'
import { isValidEmail, isValidMailConfirmCode } from '@/utils/validate' import {
isValidEmail,
isValidMailConfirmCode,
isValidPassword,
} from '@/utils/validate'
// 检查发送邮箱验证码 // 检查发送邮箱验证码
export const checkSendCode = (email: string) => { export const checkSendCode = (email: string) => {
@ -31,8 +35,8 @@ export const checkResetEmail = (
) => { ) => {
if (!hasSentCodeEmail) { if (!hasSentCodeEmail) {
Message( Message(
'Please send the email verification code first', 'Please send the email verification code first!',
'请先发送邮箱验证码', '请先发送邮箱验证码',
'warn' 'warn'
) )
return false return false
@ -41,8 +45,8 @@ export const checkResetEmail = (
// 这里只是临时验证一下,验证逻辑在后端 // 这里只是临时验证一下,验证逻辑在后端
if (hasSentCodeEmail !== newEmail) { if (hasSentCodeEmail !== newEmail) {
Message( Message(
'The email to which the verification code has been sent is different from the one you want to change.', 'The email to which the verification code has been sent is different from the one you want to change.!',
'已经发送验证码的邮箱与要更改的邮箱不相等', '已经发送验证码的邮箱与要更改的邮箱不相等',
'warn' 'warn'
) )
return false return false
@ -50,8 +54,8 @@ export const checkResetEmail = (
if (!code) { if (!code) {
Message( Message(
'Email verification code cannot be empty', 'Email verification code cannot be empty!',
'邮箱验证码不可为空', '邮箱验证码不可为空',
'warn' 'warn'
) )
} }
@ -66,3 +70,43 @@ export const checkResetEmail = (
} }
return true return true
} }
// 检查更改密码
export const checkChangePassword = (
oldPassword: string,
newPassword: string,
repeatPassword: string
) => {
if (!oldPassword) {
Message('Old password cannot be empty!', '旧密码不可为空!', 'warn')
return false
}
if (!newPassword) {
Message('New password cannot be empty!', '新密码不可为空!', 'warn')
return false
}
if (!repeatPassword) {
Message('Please input new password again!', '请再次输入新密码!', 'warn')
return false
}
if (newPassword !== repeatPassword) {
Message(
'The two password entries do not match',
'两次输入密码不一致',
'warn'
)
return false
}
if (!isValidPassword(oldPassword) || !isValidPassword(newPassword)) {
Message(
'Invalid password format.\nPassword must be 6 to 17 characters long and must include at least one letter and one number.\nIt can optionally include special characters such as \\w!@#$%^&()-+=',
'非法的密码格式,密码的长度为 6 到 17 位,必须包含至少一个英文字符和一个数字,可以选择性的包含 \\w!@#$%^&*()-+= 等特殊字符',
'warn',
5000
)
return false
}
return true
}