完成修改密码功能

This commit is contained in:
2026-03-12 20:20:01 +08:00
parent a768597ca2
commit 103c6fe0b6
3 changed files with 279 additions and 83 deletions

View File

@@ -56,9 +56,11 @@
</template>
<script setup lang="uts">
import { reactive, computed, onMounted } from 'vue'
import { reactive, computed, onMounted, ref } from 'vue'
import { state, logout } from '@/utils/store.uts'
import supa from '@/components/supadb/aksupainstance.uts'
import { createTempClient } from '@/components/supadb/aksupa.uts'
import { SUPA_URL, SUPA_KEY } from '@/ak/config.uts'
const userAccount = computed((): string => state.userProfile.email || 'demo@example.com')
const avatarUrl = computed((): string => state.userProfile.avatar_url || '/static/logo.png')
@@ -70,119 +72,123 @@ const formData = reactive({
confirmPassword: ''
})
const isSubmitting = ref(false)
onMounted(() => {
formData.name = state.userProfile.username || ''
})
const onSubmit = async () => {
if (formData.name.trim() == '') {
if (isSubmitting.value) return
const nameTrimmed = formData.name.trim()
if (nameTrimmed == '') {
uni.showToast({ title: '姓名不能为空', icon: 'none' })
return
}
// 修改密码逻辑
if (formData.oldPassword != '' || formData.newPassword != '' || formData.confirmPassword != '') {
if (formData.oldPassword == '') {
uni.showToast({ title: '需要输入原始密码', icon: 'none' })
return
}
if (formData.newPassword == '') {
uni.showToast({ title: '新密码不能为空', icon: 'none' })
return
}
if (formData.newPassword !== formData.confirmPassword) {
uni.showToast({ title: '两次输入的新密码不一致', icon: 'none' })
return
}
if (formData.newPassword === formData.oldPassword) {
uni.showToast({ title: '新密码不能与原始密码相同', icon: 'none' })
return
isSubmitting.value = true
uni.showLoading({ title: '正在处理...', mask: true })
try {
// 1. 如果姓名有变化,更新姓名
if (nameTrimmed !== state.userProfile.username) {
const resName = await supa.from('ak_users').update({
username: nameTrimmed
}).eq('id', state.userProfile.id).execute()
if (resName.error != null) {
throw new Error('更新姓名失败: ' + (resName.error?.message ?? '网络异常'))
}
// 更新本地状态
state.userProfile.username = nameTrimmed
}
uni.showLoading({ title: '验证并提交中...' })
// 2. 处理密码修改
const wantsChangePassword = formData.oldPassword != '' || formData.newPassword != '' || formData.confirmPassword != ''
if (wantsChangePassword) {
if (formData.oldPassword == '') {
throw new Error('需要输入原始密码')
}
if (formData.newPassword == '') {
throw new Error('新密码不能为空')
}
// 关键修复:确保比较前去除可能的多余空格,或至少确保值确实一致
const newPwd = formData.newPassword
const confPwd = formData.confirmPassword
if (newPwd !== confPwd) {
throw new Error('两次输入的新密码不一致')
}
if (newPwd === formData.oldPassword) {
throw new Error('新密码不能与原始密码相同')
}
try {
const email = state.userProfile.email
if (email == '') {
uni.hideLoading()
uni.showToast({ title: '账号缺失,无法验证', icon: 'none' })
return
}
// 1. 验证原密码
const resSignIn = await supa.auth.signInWithPassword({
email: email,
password: formData.oldPassword
})
if (resSignIn.error != null) {
uni.hideLoading()
uni.showToast({ title: '原密码错误', icon: 'none' })
return
throw new Error('账号信息缺失,请重新登录后再试')
}
// 2. 更新新密码
// 使用临时客户端校验密码,不影响当前会话
const tempSupa = createTempClient(SUPA_URL, SUPA_KEY)
try {
await tempSupa.auth.signInWithPassword({
email: email,
password: formData.oldPassword,
options: { persistSession: false }
} as UTSJSONObject)
} catch (e : any) {
const errMsg = e.message || '原始密码校验失败'
if (errMsg.toLowerCase().includes('invalid login credentials')) {
throw new Error('原始密码错误')
}
throw new Error(errMsg)
}
// 密码更新(使用主客户端)
const resUpdate = await supa.auth.updateUser({
password: formData.newPassword
})
} as UTSJSONObject)
if (resUpdate.error != null) {
uni.hideLoading()
uni.showToast({ title: '密码更新失败', icon: 'none' })
return
}
// 3. 同时更新姓名
if (formData.name !== state.userProfile.username) {
await supa.from('ak_users').update({
username: formData.name
}).eq('id', state.userProfile.id)
throw new Error('密码修改失败: ' + (resUpdate.error?.message ?? '网络异常'))
}
uni.hideLoading()
uni.showToast({ title: '修改成功, 请重新登录', icon: 'success' })
// 退出登录
// 退出登录并强制跳转
setTimeout(() => {
logout()
uni.removeStorageSync('adminRole')
// 同时清理管理端特定的角色缓存和状态
try {
const { clearAdminRoleCache } = require('@/layouts/admin/utils/role.uts')
clearAdminRoleCache()
} catch (e) {}
uni.removeStorageSync('adminRole') // 确保清理旧的缓存标识
uni.removeStorageSync('token')
uni.reLaunch({
url: '/pages/user/login'
})
uni.reLaunch({ url: '/pages/user/login' })
}, 1500)
return
} catch (e) {
uni.hideLoading()
uni.showToast({ title: '网络异常', icon: 'none' })
return
}
}
// 仅修改基本信息
if (formData.name !== state.userProfile.username) {
uni.showLoading({ title: '保存中...' })
try {
const res = await supa.from('ak_users').update({
username: formData.name
}).eq('id', state.userProfile.id)
// 如果只是更新了姓名
uni.hideLoading()
uni.showToast({ title: '基本信息已更新', icon: 'success' })
isSubmitting.value = false
if (res.error != null) {
uni.hideLoading()
uni.showToast({ title: '保存失败', icon: 'none' })
return
}
state.userProfile.username = formData.name
uni.hideLoading()
uni.showToast({ title: '保存成功', icon: 'success' })
} catch (e) {
uni.hideLoading()
uni.showToast({ title: '网络异常', icon: 'none' })
}
} else {
uni.showToast({ title: '无修改内容', icon: 'none' })
} catch (err : any) {
uni.hideLoading()
isSubmitting.value = false
const msg = err.message || '操作失败,请重试'
console.error('Submit user update failed:', err)
uni.showToast({
title: msg,
icon: 'none',
duration: 3000
})
}
}
</script>