存档点

This commit is contained in:
2026-01-14 11:07:26 +08:00
parent 141ddbb059
commit 39ea6f2fb1
28 changed files with 2738 additions and 418 deletions

View File

@@ -1,38 +1,30 @@
<template>
<view class="login-container">
<view class="login-box">
<view class="title">欢迎登录</view>
<view class="form-item">
<view class="label">手机号</view>
<input
class="input"
type="number"
v-model="phone"
placeholder="请输入手机号"
maxlength="11"
/>
</view>
<view class="form-item">
<view class="label">验证码</view>
<input
class="input"
type="number"
v-model="code"
placeholder="请输入验证码"
maxlength="6"
/>
<view class="logo-section">
<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
<view class="app-name">趣味答题</view>
<view class="app-desc">海量题库轻松学习</view>
</view>
<button
class="login-btn"
class="wx-login-btn"
:class="{ 'disabled': isLoading }"
:disabled="isLoading"
@click="handleLogin"
open-type="getUserInfo"
@getuserinfo="handleWxLogin"
>
{{ isLoading ? '登录中...' : '登录' }}
<text class="wx-icon">👤</text>
<text>{{ isLoading ? '登录中...' : '微信一键登录' }}</text>
</button>
<view class="skip-btn" @click="handleSkip">
<text>暂不登录</text>
</view>
<view class="tips">
登录即表示同意用户协议隐私政策
</view>
</view>
</view>
</template>
@@ -44,82 +36,149 @@ import storage from '../../utils/storage.js'
export default {
data() {
return {
phone: '',
code: '',
isLoading: false
isLoading: false,
redirectUrl: ''
}
},
onLoad(options) {
if (options.redirect) {
this.redirectUrl = decodeURIComponent(options.redirect)
}
},
methods: {
async handleLogin() {
if (!this.phone) {
uni.showToast({
title: '请输入手机号',
icon: 'none'
})
return
}
if (!/^1[3-9]\d{9}$/.test(this.phone)) {
uni.showToast({
title: '手机号格式不正确',
icon: 'none'
})
return
}
if (!this.code) {
uni.showToast({
title: '请输入验证码',
icon: 'none'
})
async handleWxLogin(e) {
if (this.isLoading) {
return
}
this.isLoading = true
try {
const res = await api.login(this.phone, this.code)
uni.showLoading({
title: '登录中...',
mask: true
})
if (res.data && res.data.accessToken) {
storage.setToken(res.data.accessToken)
const userInfo = {
...res.data,
phone: this.phone
}
storage.setUserInfo(userInfo)
try {
const questionRes = await api.getQuestion()
if (questionRes.data) {
storage.setQuestion(questionRes.data)
uni.login({
provider: 'weixin',
success: async (loginRes) => {
console.log('uni.login 成功:', loginRes)
if (!loginRes.code) {
throw new Error('获取微信登录凭证失败')
}
} catch (questionError) {
console.error('获取题目失败:', questionError)
}
uni.showToast({
title: '登录成功',
icon: 'success'
})
setTimeout(() => {
uni.switchTab({
url: '/pages/index/index'
try {
const res = await api.wxLogin(loginRes.code)
console.log('后端登录返回:', res)
if (res.data && res.data.accessToken) {
storage.setToken(res.data.accessToken)
const userInfo = {
...res.data,
nickName: res.data.nickName || '微信用户',
avatarUrl: res.data.avatarUrl || ''
}
storage.setUserInfo(userInfo)
try {
const questionRes = await api.getQuestion()
if (questionRes.data) {
storage.setQuestion(questionRes.data)
}
} catch (questionError) {
console.error('获取题目失败:', questionError)
}
uni.hideLoading()
uni.showToast({
title: '登录成功',
icon: 'success'
})
setTimeout(() => {
this.navigateAfterLogin()
}, 1500)
} else {
throw new Error(res.message || '登录失败')
}
} catch (apiError) {
console.error('登录接口调用失败:', apiError)
uni.hideLoading()
uni.showToast({
title: apiError.message || '登录失败,请重试',
icon: 'none',
duration: 2000
})
}
},
fail: (err) => {
console.error('uni.login 失败:', err)
uni.hideLoading()
uni.showToast({
title: '微信登录失败',
icon: 'none'
})
}, 1500)
} else {
uni.showToast({
title: res.message || '登录失败',
icon: 'none'
})
}
}
})
} catch (error) {
console.error('登录失败:', error)
console.error('登录异常:', error)
uni.hideLoading()
uni.showToast({
title: '登录失败,请重试',
icon: 'none'
})
} finally {
this.isLoading = false
}
},
handleSkip() {
if (this.redirectUrl) {
if (this.isTabBarPage(this.redirectUrl)) {
uni.switchTab({
url: this.redirectUrl
})
} else {
uni.redirectTo({
url: this.redirectUrl
})
}
} else {
uni.switchTab({
url: '/pages/index/index'
})
}
},
navigateAfterLogin() {
if (this.redirectUrl) {
if (this.isTabBarPage(this.redirectUrl)) {
uni.switchTab({
url: this.redirectUrl
})
} else {
uni.redirectTo({
url: this.redirectUrl
})
}
} else {
uni.switchTab({
url: '/pages/index/index'
})
}
},
isTabBarPage(url) {
const tabBarPages = [
'/pages/index/index',
'/pages/gameplay/gameplay',
'/pages/mine/mine'
]
return tabBarPages.some(page => url.includes(page))
}
}
}
@@ -140,55 +199,72 @@ export default {
max-width: 600rpx;
background: #ffffff;
border-radius: 20rpx;
padding: 60rpx 40rpx;
padding: 80rpx 40rpx;
box-shadow: 0 10rpx 40rpx rgba(0, 0, 0, 0.1);
}
.title {
.logo-section {
text-align: center;
margin-bottom: 80rpx;
}
.logo {
width: 160rpx;
height: 160rpx;
margin-bottom: 30rpx;
}
.app-name {
font-size: 48rpx;
font-weight: bold;
color: #333;
text-align: center;
margin-bottom: 60rpx;
}
.form-item {
margin-bottom: 40rpx;
}
.label {
font-size: 28rpx;
color: #666;
margin-bottom: 16rpx;
}
.input {
width: 100%;
height: 88rpx;
border: 2rpx solid #e0e0e0;
border-radius: 12rpx;
padding: 0 24rpx;
font-size: 32rpx;
box-sizing: border-box;
transition: all 0.3s;
.app-desc {
font-size: 28rpx;
color: #999;
}
.input:focus {
border-color: #667eea;
}
.login-btn {
.wx-login-btn {
width: 100%;
height: 88rpx;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
height: 96rpx;
background: linear-gradient(135deg, #07c160 0%, #06ae56 100%);
color: #ffffff;
font-size: 32rpx;
border-radius: 12rpx;
border-radius: 48rpx;
border: none;
margin-top: 20rpx;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 30rpx;
box-shadow: 0 8rpx 24rpx rgba(7, 193, 96, 0.3);
}
.login-btn.disabled {
.wx-login-btn.disabled {
opacity: 0.6;
}
.wx-icon {
font-size: 36rpx;
margin-right: 12rpx;
}
.skip-btn {
text-align: center;
padding: 20rpx 0;
margin-bottom: 40rpx;
}
.skip-btn text {
font-size: 28rpx;
color: #999;
}
.tips {
font-size: 24rpx;
color: #999;
text-align: center;
line-height: 1.6;
}
</style>