Files
medical-mall/pages/user/register.uvue
2026-01-22 08:54:47 +08:00

506 lines
13 KiB
Plaintext

<template>
<view class="page">
<scroll-view class="scroll" scroll-y="true" show-scrollbar="false">
<view class="top-actions">
<button class="lang-btn" @click="toggleLanguage">
{{ currentLocale === 'zh-CN' ? 'EN' : '中' }}
</button>
</view>
<view class="hero">
<view class="brand-row">
<view class="brand-mark"></view>
<view class="brand-text">
<text class="brand-name">Mall</text>
<text class="brand-slogan">注册后默认成为消费者账号</text>
</view>
</view>
<view class="title-block">
<text class="page-title">{{ $t('user.register.title') }}</text>
<text class="page-subtitle">{{ $t('user.register.subtitle') }}</text>
</view>
</view>
<view class="card">
<form @submit="onSubmit">
<view class="field" :class="{ 'field-error': emailError }">
<text class="label">{{ $t('user.register.email') }}</text>
<view class="input-wrap">
<input class="input" name="email" type="text" v-model="email" :placeholder="$t('user.register.email_placeholder')" @blur="validateEmail" />
</view>
<text v-if="emailError" class="support error">{{ emailError }}</text>
</view>
<view class="field" :class="{ 'field-error': passwordError }">
<text class="label">{{ $t('user.register.password') }}</text>
<view class="input-wrap">
<input class="input" name="password" :type="showPassword ? 'text' : 'password'" v-model="password" :placeholder="$t('user.register.password_placeholder')" @blur="validatePassword" />
<view class="suffix" @click="togglePasswordVisibility">
<text class="suffix-text">{{ showPassword ? '隐藏' : '显示' }}</text>
</view>
</view>
<text v-if="passwordError" class="support error">{{ passwordError }}</text>
</view>
<view class="field" :class="{ 'field-error': confirmPasswordError }">
<text class="label">{{ $t('user.register.confirm_password') }}</text>
<view class="input-wrap">
<input class="input" name="confirmPassword" :type="showConfirmPassword ? 'text' : 'password'" v-model="confirmPassword" :placeholder="$t('user.register.confirm_password_placeholder')" @blur="validateConfirmPassword" />
<view class="suffix" @click="toggleConfirmPasswordVisibility">
<text class="suffix-text">{{ showConfirmPassword ? '隐藏' : '显示' }}</text>
</view>
</view>
<text v-if="confirmPasswordError" class="support error">{{ confirmPasswordError }}</text>
</view>
<view class="field" :class="{ 'field-error': usernameError }">
<text class="label">{{ $t('user.register.username') }}</text>
<view class="input-wrap">
<input class="input" name="username" type="text" v-model="username" :placeholder="$t('user.register.username_placeholder')" @blur="validateUsername" />
</view>
<text v-if="usernameError" class="support error">{{ usernameError }}</text>
</view>
<view class="terms" :class="{ 'field-error': termsError }">
<view class="terms-row" @click="agreeTerms = !agreeTerms">
<checkbox :checked="agreeTerms" color="#FF4D4F" />
<view class="terms-text">
<text>{{ $t('user.register.agree_terms_part1') }} </text>
<text class="terms-link" @click="showTerms">{{ $t('user.register.terms_link') }}</text>
<text>{{ $t('user.register.agree_terms_part2') }}</text>
</view>
</view>
<text v-if="termsError" class="support error">{{ termsError }}</text>
</view>
<button form-type="submit" class="primary" :disabled="isLoading" :loading="isLoading">
{{ $t('user.register.button') }}
</button>
<text v-if="generalError" class="support error center">{{ generalError }}</text>
</form>
<view class="footer">
<text class="footer-text">{{ $t('user.register.already_account') }}</text>
<text class="footer-link" @click="navigateToLogin">{{ $t('user.register.login') }}</text>
</view>
</view>
<view class="trust">
<text class="trust-text">注册即代表你同意用户协议与隐私政策</text>
</view>
</scroll-view>
</view>
</template>
<script lang="uts">
import type { AkReqOptions, AkReqResponse, AkReqError } from '@/uni_modules/ak-req/index.uts';
import supa from '@/components/supadb/aksupainstance.uts';
import { switchLocale, getCurrentLocale } from '@/utils/utils.uts';
export default {
data() {
return {
email: "",
password: "",
confirmPassword: "",
username: "",
emailError: "",
passwordError: "",
confirmPasswordError: "",
usernameError: "",
termsError: "",
generalError: "",
isLoading: false,
showPassword: false,
showConfirmPassword: false,
agreeTerms: false,
currentLocale: getCurrentLocale()
};
},
methods: {
toggleLanguage() {
const newLocale = this.currentLocale === 'zh-CN' ? 'en-US' : 'zh-CN';
switchLocale(newLocale);
this.currentLocale = newLocale;
uni.showToast({
title: this.$t('user.register.language_switched'),
icon: 'success'
});
},
onSubmit(e: UniFormSubmitEvent) {
this.handleRegister();
},
togglePasswordVisibility() {
this.showPassword = !this.showPassword;
},
toggleConfirmPasswordVisibility() {
this.showConfirmPassword = !this.showConfirmPassword; },
validateEmail() {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (this.email.trim() === "") {
this.emailError = this.$t('user.register.email_required');
return false;
} else if (!emailRegex.test(this.email)) {
this.emailError = this.$t('user.register.email_invalid');
return false;
} else {
this.emailError = '';
return true;
}
},
validatePassword() {
if (this.password.trim() === "") {
this.passwordError = this.$t('user.register.password_required');
return false;
} else if (this.password.length < 6) {
this.passwordError = this.$t('user.register.password_too_short');
return false;
} else {
this.passwordError = '';
this.validateConfirmPassword(); // Re-validate confirm password when password changes
return true;
}
},
validateConfirmPassword() {
if (this.confirmPassword.trim() === "") {
this.confirmPasswordError = this.$t('user.register.confirm_password_required');
return false;
} else if (this.confirmPassword !== this.password) {
this.confirmPasswordError = this.$t('user.register.passwords_not_match');
return false;
} else {
this.confirmPasswordError = '';
return true;
}
},
validateUsername() {
if (this.username.trim() !== "" && this.username.length < 3) {
this.usernameError = this.$t('user.register.username_too_short');
return false;
} else {
this.usernameError = '';
return true;
}
},
validateTerms() {
if (!this.agreeTerms) {
this.termsError = this.$t('user.register.terms_required');
return false;
} else {
this.termsError = '';
return true;
}
},
validateForm() {
const emailValid = this.validateEmail();
const passwordValid = this.validatePassword();
const confirmPasswordValid = this.validateConfirmPassword();
const usernameValid = this.validateUsername();
const termsValid = this.validateTerms();
return emailValid && passwordValid && confirmPasswordValid && usernameValid && termsValid; },
randomPasswordHash(password: String): String {
const salt = Math.random().toString(36).substring(2, 10);
const base = password + salt + Date.now();
return btoa(base);
},
async handleRegister() {
this.generalError = '';
if (!this.validateForm()) {
return;
}
this.isLoading = true;
try {
console.log('register: attempting to create account for', this.email);
// Call signup method from supa
const result = await supa.signUp(
this.email, this.password
);
if (result!=null && result.user!=null) {
// Show success message
uni.showToast({
title: this.$t('user.register.success'),
icon: 'success'
});
const user=result.get("user") as UTSJSONObject;
const auth_id =user.get("id")
// Create user profile in ak_users table
const userData = {
auth_id: auth_id,
// auth_id:1,
email: this.email,
username: this.username ?? this.email.split('@')[0],
password_hash: this.randomPasswordHash(this.password)
// preferred_language: this.$locale
} as UTSJSONObject;
try {
await supa.insert('ak_users', userData );
} catch (profileErr) {
console.error('Failed to create user profile:', profileErr);
// Continue anyway, as auth account was created
}
// Redirect to login page after registration
setTimeout(() => {
uni.redirectTo({
url: '/pages/user/login'
});
}, 1500);
} else {
throw new Error(this.$t('user.register.failed'));
}
} catch (err) {
const errObj = err as UniError; // or UtsError
if (typeof errObj.message === 'string') {
this.generalError = errObj.message;
} else {
this.generalError = this.$t('user.register.unknown_error');
}
}
this.isLoading = false;
},
navigateToLogin() {
uni.navigateTo({
url: '/pages/user/login'
});
},
showTerms() {
uni.navigateTo({
url: '/pages/user/terms'
});
}
}
}
</script>
<style>
.page {
min-height: 100vh;
background: #f7f8fa;
}
.scroll {
min-height: 100vh;
background: radial-gradient(900rpx 700rpx at 20% 10%, rgba(255, 77, 79, 0.16), rgba(255, 77, 79, 0) 60%),
linear-gradient(180deg, #ffffff 0%, #f7f8fa 45%, #f7f8fa 100%);
}
.top-actions {
position: relative;
padding: 24rpx 24rpx 0;
display: flex;
justify-content: flex-end;
}
.lang-btn {
width: 76rpx;
height: 76rpx;
border-radius: 38rpx;
background: rgba(255, 255, 255, 0.9);
border: 1rpx solid rgba(0, 0, 0, 0.06);
color: #111;
font-size: 26rpx;
line-height: 76rpx;
text-align: center;
box-shadow: 0 10rpx 24rpx rgba(0, 0, 0, 0.06);
}
.hero {
padding: 26rpx 24rpx 14rpx;
}
.brand-row {
display: flex;
align-items: center;
}
.brand-mark {
width: 72rpx;
height: 72rpx;
border-radius: 20rpx;
background: linear-gradient(135deg, #ff4d4f 0%, #ff8a65 100%);
box-shadow: 0 12rpx 26rpx rgba(255, 77, 79, 0.28);
}
.brand-text {
margin-left: 18rpx;
display: flex;
flex-direction: column;
}
.brand-name {
font-size: 40rpx;
font-weight: 700;
color: #111;
letter-spacing: 1rpx;
}
.brand-slogan {
margin-top: 6rpx;
font-size: 24rpx;
color: rgba(0, 0, 0, 0.55);
}
.title-block {
margin-top: 26rpx;
}
.page-title {
font-size: 46rpx;
font-weight: 700;
color: #111;
}
.page-subtitle {
margin-top: 10rpx;
font-size: 26rpx;
color: rgba(0, 0, 0, 0.55);
}
.card {
margin: 18rpx 24rpx 0;
padding: 26rpx 22rpx;
background: rgba(255, 255, 255, 0.96);
border-radius: 20rpx;
border: 1rpx solid rgba(0, 0, 0, 0.06);
box-shadow: 0 16rpx 36rpx rgba(0, 0, 0, 0.06);
}
.field {
margin-bottom: 18rpx;
}
.label {
font-size: 24rpx;
color: rgba(0, 0, 0, 0.78);
margin-bottom: 10rpx;
display: block;
}
.input-wrap {
position: relative;
display: flex;
align-items: center;
background: #f6f7f9;
border: 2rpx solid rgba(0, 0, 0, 0.06);
border-radius: 14rpx;
padding: 0 14rpx;
}
.field-error .input-wrap {
border-color: rgba(255, 77, 79, 0.55);
background: rgba(255, 77, 79, 0.06);
}
.input {
flex: 1;
height: 84rpx;
font-size: 28rpx;
color: #111;
padding: 0 6rpx;
}
.suffix {
height: 84rpx;
padding: 0 10rpx;
display: flex;
align-items: center;
justify-content: center;
}
.suffix-text {
font-size: 24rpx;
color: rgba(0, 0, 0, 0.55);
}
.support {
margin-top: 10rpx;
font-size: 22rpx;
color: rgba(0, 0, 0, 0.52);
}
.error {
color: #ff4d4f;
}
.center {
text-align: center;
width: 100%;
display: block;
}
.terms {
margin: 12rpx 0 18rpx;
}
.terms-row {
display: flex;
align-items: flex-start;
}
.terms-text {
margin-left: 10rpx;
font-size: 24rpx;
color: rgba(0, 0, 0, 0.55);
flex: 1;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.terms-link {
color: #ff4d4f;
font-weight: 600;
}
.primary {
width: 100%;
height: 92rpx;
border-radius: 18rpx;
background: linear-gradient(135deg, #ff4d4f 0%, #ff7a45 100%);
color: #fff;
font-size: 30rpx;
font-weight: 600;
box-shadow: 0 16rpx 32rpx rgba(255, 77, 79, 0.24);
}
.primary:disabled {
background: #d9d9d9;
box-shadow: none;
}
.footer {
margin-top: 22rpx;
display: flex;
align-items: center;
justify-content: center;
}
.footer-text {
font-size: 24rpx;
color: rgba(0, 0, 0, 0.55);
}
.footer-link {
margin-left: 8rpx;
font-size: 24rpx;
color: #ff4d4f;
font-weight: 600;
}
.trust {
padding: 18rpx 24rpx 36rpx;
}
.trust-text {
font-size: 22rpx;
color: rgba(0, 0, 0, 0.4);
text-align: center;
}
</style>