143 lines
4.1 KiB
JavaScript
143 lines
4.1 KiB
JavaScript
const fs = require('fs');
|
|
const path = 'D:/骅锋/mall/pages/mall/admin/userCenter/index.uvue';
|
|
|
|
let text = fs.readFileSync(path, 'utf-8');
|
|
|
|
const targetScript = `<script setup lang="uts">
|
|
import { reactive, computed, onMounted } from 'vue'
|
|
import { state, logout } from '@/utils/store.uts'
|
|
import { supa } from '@/utils/supabase.uts'
|
|
|
|
const userAccount = computed((): string => state.userProfile.email || 'demo@example.com')
|
|
const avatarUrl = computed((): string => state.userProfile.avatar_url || '/static/logo.png')
|
|
|
|
const formData = reactive({
|
|
name: '',
|
|
oldPassword: '',
|
|
newPassword: '',
|
|
confirmPassword: ''
|
|
})
|
|
|
|
onMounted(() => {
|
|
formData.name = state.userProfile.username || ''
|
|
})
|
|
|
|
const onSubmit = async () => {
|
|
if (formData.name.trim() == '') {
|
|
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
|
|
}
|
|
|
|
uni.showLoading({ title: '验证并提交中...' })
|
|
|
|
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
|
|
}
|
|
|
|
// 2. 更新新密码
|
|
const resUpdate = await supa.auth.updateUser({
|
|
password: formData.newPassword
|
|
})
|
|
|
|
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)
|
|
}
|
|
|
|
uni.hideLoading()
|
|
uni.showToast({ title: '修改成功, 请重新登录', icon: 'success' })
|
|
|
|
// 退出登录
|
|
setTimeout(() => {
|
|
logout()
|
|
uni.removeStorageSync('adminRole')
|
|
uni.removeStorageSync('token')
|
|
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)
|
|
|
|
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' })
|
|
}
|
|
}
|
|
</script>`;
|
|
|
|
text = text.replace(/<script setup lang=["']uts["']>[\s\S]*?<\/script>/, targetScript);
|
|
text = text.replace(/value="demo"/, ':value="userAccount"');
|
|
text = text.replace(/src="\/static\/logo\.png"/, ':src="avatarUrl"');
|
|
|
|
fs.writeFileSync(path, text, 'utf-8');
|
|
console.log('Done'); |