This commit is contained in:
@@ -6,6 +6,12 @@
|
||||
<view class="subtitle">请输入手机号获取验证码完成注册</view>
|
||||
</view>
|
||||
|
||||
<!-- 用户信息展示(来自微信授权) -->
|
||||
<view v-if="authInfo.nickname" class="auth-info">
|
||||
<image class="auth-avatar" :src="authInfo.avatar || '/static/default-avatar.png'" mode="aspectFill" />
|
||||
<text class="auth-nickname">{{ authInfo.nickname }}</text>
|
||||
</view>
|
||||
|
||||
<!-- 表单区域 -->
|
||||
<view class="form-area">
|
||||
<!-- 手机号 -->
|
||||
@@ -62,8 +68,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import config from '@/config/env.js';
|
||||
import { post } from '@/utils/request.js';
|
||||
import { gatewayPost } from '@/utils/request.js';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@@ -71,7 +76,10 @@ export default {
|
||||
phone: '',
|
||||
code: '',
|
||||
countdown: 0,
|
||||
timer: null
|
||||
timer: null,
|
||||
tempToken: '',
|
||||
authInfo: { nickname: '', avatar: '' },
|
||||
deviceId: '',
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
@@ -79,8 +87,22 @@ export default {
|
||||
return this.phone.length === 11 && this.code.length >= 4;
|
||||
}
|
||||
},
|
||||
onLoad(options) {
|
||||
this.deviceId = options.device_id || '';
|
||||
|
||||
// 从缓存读取授权信息(loading 页写入)
|
||||
this.tempToken = uni.getStorageSync('temp_token') || '';
|
||||
const cachedAuthInfo = uni.getStorageSync('auth_info');
|
||||
if (cachedAuthInfo) {
|
||||
this.authInfo = cachedAuthInfo;
|
||||
}
|
||||
|
||||
// 如果没有 temp_token,提示用户重新授权
|
||||
if (!this.tempToken) {
|
||||
uni.showToast({ title: '授权信息已过期,请重新登录', icon: 'none' });
|
||||
}
|
||||
},
|
||||
beforeDestroy() {
|
||||
// 清除定时器
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
@@ -91,15 +113,21 @@ export default {
|
||||
async handleGetCode() {
|
||||
if (this.countdown > 0) return;
|
||||
|
||||
// 验证手机号
|
||||
if (!/^1[3-9]\d{9}$/.test(this.phone)) {
|
||||
uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.tempToken) {
|
||||
uni.showToast({ title: '授权信息已过期,请重新登录', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await post('/auth/send-sms-code', {
|
||||
phone: this.phone
|
||||
const res = await gatewayPost('/api/v1/user/sms/send', {
|
||||
phone: this.phone,
|
||||
temp_token: this.tempToken,
|
||||
purpose: 'register',
|
||||
});
|
||||
|
||||
const ret = res.data || {};
|
||||
@@ -114,7 +142,6 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
// 开始倒计时
|
||||
startCountdown() {
|
||||
this.countdown = 60;
|
||||
this.timer = setInterval(() => {
|
||||
@@ -130,51 +157,49 @@ export default {
|
||||
async handleRegister() {
|
||||
if (!this.canSubmit) return;
|
||||
|
||||
// 验证手机号
|
||||
if (!/^1[3-9]\d{9}$/.test(this.phone)) {
|
||||
uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证验证码
|
||||
if (this.code.length < 4) {
|
||||
uni.showToast({ title: '请输入验证码', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.tempToken) {
|
||||
uni.showToast({ title: '授权信息已过期,请重新登录', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
uni.showLoading({ title: '注册中...' });
|
||||
|
||||
try {
|
||||
const res = await post('/auth/register', {
|
||||
const res = await gatewayPost('/api/v1/user/register', {
|
||||
temp_token: this.tempToken,
|
||||
phone: this.phone,
|
||||
code: this.code
|
||||
code: this.code,
|
||||
});
|
||||
|
||||
const ret = res.data || {};
|
||||
if (ret.code === 0) {
|
||||
// 保存 token
|
||||
const token = ret.data && ret.data.token;
|
||||
if (token) {
|
||||
uni.setStorageSync('token', token);
|
||||
}
|
||||
if (ret.code === 0 && ret.data) {
|
||||
// 保存 token 和用户信息
|
||||
uni.setStorageSync('token', ret.data.token);
|
||||
uni.setStorageSync('user_info', ret.data.user_info);
|
||||
|
||||
// 保存用户信息
|
||||
const userInfo = ret.data && ret.data.userInfo;
|
||||
if (userInfo) {
|
||||
uni.setStorageSync('userInfo', userInfo);
|
||||
}
|
||||
// 清除临时数据
|
||||
uni.removeStorageSync('temp_token');
|
||||
uni.removeStorageSync('auth_info');
|
||||
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: '注册成功', icon: 'success' });
|
||||
|
||||
// 返回上一页或跳转首页
|
||||
// 跳转首页
|
||||
setTimeout(() => {
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length > 1) {
|
||||
uni.navigateBack();
|
||||
} else {
|
||||
uni.redirectTo({ url: '/pages/index/index' });
|
||||
}
|
||||
const url = this.deviceId
|
||||
? `/pages/index/index?device_id=${this.deviceId}`
|
||||
: '/pages/index/index';
|
||||
uni.redirectTo({ url });
|
||||
}, 1500);
|
||||
} else {
|
||||
uni.hideLoading();
|
||||
@@ -186,7 +211,6 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
// 显示协议
|
||||
showAgreement(type) {
|
||||
const title = type === 'user' ? '用户协议' : '隐私政策';
|
||||
uni.showToast({ title: title + '开发中', icon: 'none' });
|
||||
@@ -210,7 +234,7 @@ page {
|
||||
/* 顶部标题 */
|
||||
.header {
|
||||
padding-top: 180rpx;
|
||||
margin-bottom: 80rpx;
|
||||
margin-bottom: 40rpx;
|
||||
}
|
||||
|
||||
.title {
|
||||
@@ -225,6 +249,29 @@ page {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
/* 微信授权信息 */
|
||||
.auth-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 24rpx 0;
|
||||
margin-bottom: 40rpx;
|
||||
border-bottom: 1rpx solid #f0f0f0;
|
||||
}
|
||||
|
||||
.auth-avatar {
|
||||
width: 80rpx;
|
||||
height: 80rpx;
|
||||
border-radius: 50%;
|
||||
margin-right: 20rpx;
|
||||
background-color: #e0e0e0;
|
||||
}
|
||||
|
||||
.auth-nickname {
|
||||
font-size: 30rpx;
|
||||
color: #333;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
/* 表单区域 */
|
||||
.form-area {
|
||||
width: 100%;
|
||||
|
||||
Reference in New Issue
Block a user