| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <template>
- <view class="register-container">
- <!-- 应用Logo和名称 -->
- <view class="app-header">
- <view class="app-logo">
- <view class="logo-square">
- <view class="book-icon">
- <view class="book-left"></view>
- <view class="book-right"></view>
- <view class="book-line"></view>
- </view>
- </view>
- </view>
- <text class="app-title">云阅读</text>
- <text class="register-title">用户注册</text>
- </view>
-
- <!-- 注册表单 -->
- <view class="form-container">
- <view class="input-group">
- <input
- class="input-field"
- type="text"
- placeholder="请输入用户名"
- v-model="formData.username"
- maxlength="20"
- />
- <view class="input-line"></view>
- </view>
-
- <view class="input-group">
- <input
- class="input-field"
- type="text"
- placeholder="请输入昵称(可选)"
- v-model="formData.nickname"
- maxlength="20"
- />
- <view class="input-line"></view>
- </view>
-
- <view class="input-group">
- <input
- class="input-field password-input"
- :type="showPassword ? 'text' : 'password'"
- placeholder="请输入密码(6-20位)"
- v-model="formData.password"
- maxlength="20"
- />
- <text
- class="toggle-password-btn"
- @click="togglePassword"
- >
- {{ showPassword ? '隐藏' : '显示' }}
- </text>
- <view class="input-line"></view>
- </view>
-
- <view class="input-group">
- <input
- class="input-field password-input"
- :type="showConfirmPassword ? 'text' : 'password'"
- placeholder="请确认密码"
- v-model="formData.confirmPassword"
- maxlength="20"
- />
- <text
- class="toggle-password-btn"
- @click="toggleConfirmPassword"
- >
- {{ showConfirmPassword ? '隐藏' : '显示' }}
- </text>
- <view class="input-line"></view>
- </view>
-
- <view class="input-group">
- <input
- class="input-field"
- type="number"
- placeholder="请输入手机号(可选)"
- v-model="formData.phone"
- maxlength="11"
- />
- <view class="input-line"></view>
- </view>
-
- <view class="input-group">
- <input
- class="input-field"
- type="text"
- placeholder="请输入邮箱(可选)"
- v-model="formData.email"
- maxlength="100"
- />
- <view class="input-line"></view>
- </view>
-
- <button
- class="register-btn"
- @click="handleRegister"
- :disabled="!canRegister"
- >
- {{ isLoading ? '注册中...' : '注册' }}
- </button>
-
- <!-- 返回登录 -->
- <view class="login-link" @click="goToLogin">
- <text class="login-text">已有账号?立即登录</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- import { register } from '../../utils/api.js'
-
- export default {
- data() {
- return {
- formData: {
- username: '',
- nickname: '',
- password: '',
- confirmPassword: '',
- phone: '',
- email: ''
- },
- showPassword: false,
- showConfirmPassword: false,
- isLoading: false
- }
- },
- computed: {
- canRegister() {
- return this.formData.username.trim().length > 0
- && this.formData.password.length >= 6
- && this.formData.password === this.formData.confirmPassword
- && !this.isLoading
- }
- },
- methods: {
- togglePassword() {
- this.showPassword = !this.showPassword
- },
- toggleConfirmPassword() {
- this.showConfirmPassword = !this.showConfirmPassword
- },
- validateForm() {
- // 验证用户名
- if (!this.formData.username || this.formData.username.trim().length === 0) {
- uni.showToast({
- title: '请输入用户名',
- icon: 'none'
- })
- return false
- }
-
- // 验证密码
- if (!this.formData.password || this.formData.password.length < 6) {
- uni.showToast({
- title: '密码长度不能少于6位',
- icon: 'none'
- })
- return false
- }
-
- if (this.formData.password.length > 20) {
- uni.showToast({
- title: '密码长度不能超过20位',
- icon: 'none'
- })
- return false
- }
-
- // 验证确认密码
- if (this.formData.password !== this.formData.confirmPassword) {
- uni.showToast({
- title: '两次输入的密码不一致',
- icon: 'none'
- })
- return false
- }
-
- // 验证手机号(如果填写了)
- if (this.formData.phone && this.formData.phone.trim().length > 0) {
- const phoneReg = /^1[3-9]\d{9}$/
- if (!phoneReg.test(this.formData.phone.trim())) {
- uni.showToast({
- title: '请输入正确的手机号',
- icon: 'none'
- })
- return false
- }
- }
-
- // 验证邮箱(如果填写了)
- if (this.formData.email && this.formData.email.trim().length > 0) {
- const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
- if (!emailReg.test(this.formData.email.trim())) {
- uni.showToast({
- title: '请输入正确的邮箱地址',
- icon: 'none'
- })
- return false
- }
- }
-
- return true
- },
- handleRegister() {
- if (!this.validateForm()) {
- return
- }
-
- // 显示加载中
- this.isLoading = true
- uni.showLoading({
- title: '注册中...',
- mask: true
- })
-
- // 准备注册数据
- const registerData = {
- username: this.formData.username.trim(),
- password: this.formData.password,
- nickname: this.formData.nickname.trim() || this.formData.username.trim(),
- phone: this.formData.phone.trim() || null,
- email: this.formData.email.trim() || null
- }
-
- // 调用后端注册接口
- register(registerData)
- .then((res) => {
- uni.hideLoading()
- this.isLoading = false
-
- if (res.code === 200 && res.data) {
- uni.showToast({
- title: '注册成功',
- icon: 'success'
- })
-
- // 注册成功后,延迟跳转到登录页面,并传递用户名
- setTimeout(() => {
- uni.redirectTo({
- url: `/pages/login/login?username=${encodeURIComponent(registerData.username)}`
- })
- }, 1500)
- } else {
- uni.showToast({
- title: res.message || '注册失败',
- icon: 'none',
- duration: 2000
- })
- }
- })
- .catch((err) => {
- uni.hideLoading()
- this.isLoading = false
-
- console.error('注册失败:', err)
- uni.showToast({
- title: err.message || '注册失败,请检查网络连接',
- icon: 'none',
- duration: 2000
- })
- })
- },
- goToLogin() {
- uni.navigateBack()
- }
- }
- }
- </script>
- <style scoped>
- .register-container {
- width: 100%;
- min-height: 100vh;
- background-color: #FFFFFF;
- display: flex;
- flex-direction: column;
- padding-top: 30px;
- box-sizing: border-box;
- align-items: center;
- padding: 0 60rpx;
- box-sizing: border-box;
- position: relative;
- padding-bottom: 80rpx;
- }
-
- .app-header {
- display: flex;
- flex-direction: column;
- align-items: center;
- margin-top: 60rpx;
- margin-bottom: 60rpx;
- flex-shrink: 0;
- }
-
- .app-logo {
- margin-bottom: 30rpx;
- }
-
- .logo-square {
- width: 120rpx;
- height: 120rpx;
- background-color: #4FC3F7;
- border-radius: 20rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
-
- .book-icon {
- position: relative;
- width: 80rpx;
- height: 60rpx;
- }
-
- .book-left,
- .book-right {
- position: absolute;
- width: 40rpx;
- height: 60rpx;
- background-color: #FFFFFF;
- border-radius: 3rpx 0 0 3rpx;
- }
-
- .book-right {
- right: 0;
- border-radius: 0 3rpx 3rpx 0;
- }
-
- .book-line {
- position: absolute;
- left: 40rpx;
- top: 8rpx;
- width: 2rpx;
- height: 44rpx;
- background-color: #4FC3F7;
- }
-
- .app-title {
- font-size: 40rpx;
- color: #333333;
- font-weight: 600;
- margin-bottom: 20rpx;
- }
-
- .register-title {
- font-size: 32rpx;
- color: #666666;
- font-weight: normal;
- }
-
- .form-container {
- width: 100%;
- flex: 1;
- display: flex;
- flex-direction: column;
- justify-content: flex-start;
- }
-
- .input-group {
- position: relative;
- margin-bottom: 40rpx;
- }
-
- .input-field {
- width: 100%;
- height: 88rpx;
- font-size: 30rpx;
- padding: 0 20rpx;
- color: #333333;
- }
-
- .password-input {
- padding-right: 80rpx;
- }
-
- .toggle-password-btn {
- position: absolute;
- right: 20rpx;
- top: 50%;
- transform: translateY(-50%);
- font-size: 26rpx;
- color: #4FC3F7;
- z-index: 10;
- padding: 8rpx 12rpx;
- }
-
- .input-line {
- position: absolute;
- bottom: 0;
- left: 0;
- right: 0;
- height: 1rpx;
- background-color: #E0E0E0;
- }
-
- .register-btn {
- width: 100%;
- height: 96rpx;
- background-color: #E0E0E0;
- color: #999999;
- font-size: 34rpx;
- border-radius: 48rpx;
- margin-top: 40rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- border: none;
- flex-shrink: 0;
- }
-
- .register-btn:not([disabled]) {
- background-color: #4FC3F7;
- color: #FFFFFF;
- }
-
- .register-btn[disabled] {
- background-color: #E0E0E0;
- color: #999999;
- }
-
- .login-link {
- margin-top: 40rpx;
- text-align: center;
- }
-
- .login-text {
- font-size: 28rpx;
- color: #4FC3F7;
- }
- </style>
|