consumer-app/pages/login/login.vue

730 lines
20 KiB
Vue
Raw Normal View History

2025-12-19 12:27:55 +00:00
<template>
<view class="login-page">
2026-03-09 03:37:41 +00:00
<view class="header-fixed-wrapper" :style="{ height: headerHeight + 'px' }">
<NavHeader title="登录" />
</view>
<view class="main-wrap" :style="{ paddingTop: headerHeight + 'px' }">
2025-12-19 12:27:55 +00:00
<view class="login-content">
<!-- Logo区域 -->
2026-01-13 04:12:48 +00:00
<view class="logo-section">
<image class="logo" src="/static/home/logo.png" mode="aspectFit"></image>
</view>
2025-12-19 12:27:55 +00:00
<!-- 登录表单 -->
<view class="login-form">
2026-01-13 04:12:48 +00:00
<!-- <view class="form-item">
2025-12-19 12:27:55 +00:00
<view class="input-wrapper">
<text class="input-label">手机号</text>
<input
class="input-field"
type="number"
v-model="formData.mobile"
placeholder="请输入手机号"
maxlength="11"
/>
</view>
2026-01-13 04:12:48 +00:00
</view> -->
2025-12-19 12:27:55 +00:00
2026-01-13 04:12:48 +00:00
<!-- <view class="form-item">
2025-12-19 12:27:55 +00:00
<view class="input-wrapper">
<text class="input-label">密码</text>
<input
class="input-field"
:type="showPassword ? 'text' : 'password'"
v-model="formData.password"
placeholder="请输入密码"
/>
<view class="password-toggle" @click="togglePassword">
<text class="toggle-icon">{{ showPassword ? '👁️' : '👁️‍🗨️' }}</text>
</view>
</view>
2026-01-13 04:12:48 +00:00
</view> -->
2025-12-19 12:27:55 +00:00
<!-- 登录按钮 -->
2026-01-13 04:12:48 +00:00
<!-- <button
2025-12-19 12:27:55 +00:00
class="login-btn"
:class="{ disabled: !canLogin }"
:disabled="!canLogin || loading"
@click="handleLogin"
>
{{ loading ? '登录中...' : '登录' }}
2026-01-13 04:12:48 +00:00
</button> -->
2025-12-19 12:27:55 +00:00
<!-- 分割线 -->
2026-01-13 04:12:48 +00:00
<!-- <view class="divider">
2025-12-19 12:27:55 +00:00
<view class="divider-line"></view>
<text class="divider-text"></text>
<view class="divider-line"></view>
2026-01-13 04:12:48 +00:00
</view> -->
2025-12-19 12:27:55 +00:00
<!-- 小程序一键授权登录 -->
<!-- #ifdef MP-WEIXIN -->
<button
class="wechat-login-btn"
open-type="getPhoneNumber"
@getphonenumber="handleGetPhoneNumber"
2026-03-02 07:32:41 +00:00
:disabled="loading || !agreedToTerms"
2025-12-19 12:27:55 +00:00
>
<text>一键授权手机号快速登录</text>
</button>
<!-- #endif -->
<!-- #ifndef MP-WEIXIN -->
<view class="wechat-login-tip">
<text>小程序一键授权登录仅支持微信小程序环境</text>
</view>
<!-- #endif -->
2026-03-02 07:32:41 +00:00
<!-- 协议同意区域 -->
<view class="agreement-section">
<view class="agreement-checkbox" @click="toggleAgreement">
<view class="checkbox-icon" :class="{ checked: agreedToTerms }">
<text v-if="agreedToTerms" class="checkmark"></text>
</view>
<text class="agreement-text">
我已阅读并同意
<text class="link-text" @click.stop="goToUserAgreement">用户服务协议</text>
<text class="link-text" @click.stop="goToPrivacyPolicy">隐私政策</text>
</text>
</view>
</view>
2025-12-19 12:27:55 +00:00
</view>
</view>
2026-03-09 03:37:41 +00:00
</view>
2025-12-19 12:27:55 +00:00
</view>
</template>
<script>
import { login, loginByPhone } from '@/api/auth.js'
import { getUserInfo } from '@/api/profile.js'
export default {
data() {
return {
statusBarHeight: 0,
showBack: false,
loading: false,
showPassword: false,
2026-03-02 07:32:41 +00:00
agreedToTerms: false, // 是否同意协议
2025-12-19 12:27:55 +00:00
formData: {
mobile: '',
password: ''
2026-01-13 04:12:48 +00:00
},
// 登录相关参数
state: '', // state 参数
inviteCode: '' // 邀请码
2025-12-19 12:27:55 +00:00
}
},
computed: {
2026-03-09 03:37:41 +00:00
headerHeight() {
return this.statusBarHeight + 44
},
2025-12-19 12:27:55 +00:00
// 判断是否可以登录
canLogin() {
return (
this.formData.mobile.length === 11 &&
this.formData.password.length > 0
)
}
},
onLoad(options) {
// 检查是否已登录
const token = uni.getStorageSync('token')
if (token) {
// 已登录,跳转到首页
uni.switchTab({
url: '/pages/index/index',
fail: () => {
uni.reLaunch({
url: '/pages/index/index'
})
}
})
return
}
2026-03-09 03:37:41 +00:00
// 保存页面参数state、inviteCode扫码进入时邀请码在 scene 中)
2026-01-13 04:12:48 +00:00
if (options.state) {
this.state = options.state
}
if (options.inviteCode) {
this.inviteCode = options.inviteCode
}
2026-03-09 03:37:41 +00:00
if (options.scene && /^\d+-\d+$/.test(String(options.scene))) {
this.inviteCode = String(options.scene)
uni.setStorageSync('inviteCode', this.inviteCode)
}
2026-01-13 04:12:48 +00:00
2025-12-19 12:27:55 +00:00
// 判断是否显示返回按钮(从其他页面跳转过来时显示)
const pages = getCurrentPages()
if (pages.length > 1) {
this.showBack = true
2026-01-14 10:55:42 +00:00
// 从其他页面跳转过来,说明可能有弹窗需要关闭
// 延迟一下,确保页面已经加载完成,弹窗会自动关闭
setTimeout(() => {
// 尝试隐藏可能存在的弹窗(虽然 uni 没有直接关闭 showModal 的 API
// 但页面跳转后,弹窗应该会自动关闭
}, 100)
2025-12-19 12:27:55 +00:00
}
this.getSystemInfo()
},
methods: {
// 获取系统信息
getSystemInfo() {
const systemInfo = uni.getSystemInfoSync()
this.statusBarHeight = systemInfo.statusBarHeight || 0
},
// 返回上一页
handleBack() {
uni.navigateBack()
},
// 切换密码显示/隐藏
togglePassword() {
this.showPassword = !this.showPassword
},
// 账户密码登录
async handleLogin() {
if (!this.canLogin) {
uni.showToast({
title: '请填写完整的登录信息',
icon: 'none'
})
return
}
// 验证手机号格式
const mobileReg = /^1[3-9]\d{9}$/
if (!mobileReg.test(this.formData.mobile)) {
uni.showToast({
title: '请输入正确的手机号',
icon: 'none'
})
return
}
this.loading = true
try {
const res = await login({
mobile: this.formData.mobile,
password: this.formData.password
})
// 登录成功保存token兼容不同的响应格式
2026-01-14 10:55:42 +00:00
console.log('[登录] 登录接口返回数据:', res)
2025-12-19 12:27:55 +00:00
const token = res?.accessToken || res?.token || res?.data?.accessToken || res?.data?.token
if (token) {
uni.setStorageSync('token', token)
2026-01-14 10:55:42 +00:00
console.log('[登录] Token 已保存,长度:', token.length)
// 验证保存是否成功
const savedToken = uni.getStorageSync('token')
console.log('[登录] 验证保存的 token:', savedToken ? '成功' : '失败')
} else {
console.error('[登录] 未找到 token返回数据:', res)
2025-12-19 12:27:55 +00:00
}
// 保存refreshToken用于刷新accessToken
const refreshToken = res?.refreshToken || res?.data?.refreshToken
if (refreshToken) {
uni.setStorageSync('refreshToken', refreshToken)
}
// 保存token过期时间
const expiresTime = res?.expiresTime || res?.data?.expiresTime
if (expiresTime) {
uni.setStorageSync('tokenExpiresTime', expiresTime)
}
// 保存用户ID
const userId = res?.userId || res?.data?.userId
if (userId) {
uni.setStorageSync('userId', userId)
}
// 保存用户信息(如果有)
const userInfo = res?.userInfo || res?.data?.userInfo || res?.user || res?.data?.user
if (userInfo) {
uni.setStorageSync('userInfo', userInfo)
}
// 登录成功后,调用个人信息接口获取完整用户信息
try {
const userInfoRes = await getUserInfo()
if (userInfoRes && userInfoRes.data) {
// 根据接口返回的数据结构,保存 res.data
uni.setStorageSync('userInfo', userInfoRes.data)
} else if (userInfoRes) {
// 兼容直接返回数据的情况
uni.setStorageSync('userInfo', userInfoRes)
}
} catch (error) {
console.error('获取用户信息失败:', error)
// 获取用户信息失败不影响登录流程,继续执行
}
uni.showToast({
title: '登录成功',
icon: 'success'
})
2026-01-14 10:55:42 +00:00
// 延迟跳转,让用户看到成功提示,统一跳转到首页
2025-12-19 12:27:55 +00:00
setTimeout(() => {
2026-01-14 10:55:42 +00:00
uni.switchTab({
url: '/pages/index/index',
fail: () => {
// 如果 switchTab 失败(可能不在 tabBar 页面),使用 reLaunch
uni.reLaunch({
url: '/pages/index/index'
})
}
})
2025-12-19 12:27:55 +00:00
}, 1500)
} catch (error) {
console.error('登录失败:', error)
// 错误信息已在request中统一处理这里不需要再次提示
} finally {
this.loading = false
}
},
2026-03-02 07:32:41 +00:00
// 切换协议同意状态
toggleAgreement() {
this.agreedToTerms = !this.agreedToTerms
},
// 跳转到用户协议
goToUserAgreement() {
uni.navigateTo({
url: '/pages/profileSub/userAgreement'
})
},
// 跳转到隐私政策
goToPrivacyPolicy() {
uni.navigateTo({
url: '/pages/profileSub/privacyPolicy'
})
},
2025-12-19 12:27:55 +00:00
// 小程序一键授权手机号登录
async handleGetPhoneNumber(e) {
console.log('获取手机号授权结果:', e)
2026-03-02 07:32:41 +00:00
// 检查是否同意协议
if (!this.agreedToTerms) {
uni.showToast({
title: '请先阅读并同意用户协议和隐私政策',
icon: 'none',
duration: 2000
})
return
}
2025-12-19 12:27:55 +00:00
if (e.detail.errMsg === 'getPhoneNumber:ok') {
this.loading = true
try {
2026-01-13 04:12:48 +00:00
// 1. 获取微信临时登录code
2025-12-19 12:27:55 +00:00
const loginRes = await new Promise((resolve, reject) => {
uni.login({
provider: 'weixin',
success: resolve,
fail: reject
})
})
2026-01-13 04:12:48 +00:00
if (loginRes.errMsg !== 'login:ok') {
uni.showToast({
title: '获取登录凭证失败',
icon: 'none'
})
this.loading = false
return
}
2025-12-19 12:27:55 +00:00
2026-01-13 04:12:48 +00:00
// 2. 调用登录接口
// 根据接口文档:/app-api/member/auth/weixin-mini-app-login
// 参数phoneCode手机code、loginCode登录code、state必填回调用的随机值、inviteCode可选
// 生成随机 state 值(用于回调)
const state = this.generateState()
const loginParams = {
phoneCode: e.detail.code, // 手机 code, 通过 wx.getPhoneNumber 获得
loginCode: loginRes.code, // 登录 code, 通过 wx.login 获得
state: state, // state 是必填项,用于回调的随机值
}
// 如果有邀请码(例如从页面参数或存储中获取)
const inviteCode = this.getInviteCode()
if (inviteCode) {
loginParams.inviteCode = inviteCode
}
const res = await loginByPhone(loginParams)
// 登录成功保存token
// 根据接口返回:{ code: 0, data: { accessToken, refreshToken, expiresTime, userId, ... } }
// request 函数在 code === 0 时返回 res.data.data || res.data所以 res 就是 data 对象
2026-01-14 10:55:42 +00:00
console.log('[登录] 登录接口返回数据:', res)
2026-01-13 04:12:48 +00:00
const token = res?.accessToken || res?.data?.accessToken || res?.token || res?.data?.token
2025-12-19 12:27:55 +00:00
if (token) {
uni.setStorageSync('token', token)
2026-01-14 10:55:42 +00:00
console.log('[登录] Token 已保存,长度:', token.length)
// 验证保存是否成功
const savedToken = uni.getStorageSync('token')
console.log('[登录] 验证保存的 token:', savedToken ? '成功' : '失败')
2026-01-13 04:12:48 +00:00
} else {
2026-01-14 10:55:42 +00:00
console.error('[登录] 未找到 token返回数据:', res)
2025-12-19 12:27:55 +00:00
}
// 保存refreshToken用于刷新accessToken
const refreshToken = res?.refreshToken || res?.data?.refreshToken
if (refreshToken) {
uni.setStorageSync('refreshToken', refreshToken)
2026-01-13 04:12:48 +00:00
console.log('RefreshToken 已保存:', refreshToken)
2025-12-19 12:27:55 +00:00
}
// 保存token过期时间
const expiresTime = res?.expiresTime || res?.data?.expiresTime
if (expiresTime) {
uni.setStorageSync('tokenExpiresTime', expiresTime)
}
// 保存用户ID
const userId = res?.userId || res?.data?.userId
if (userId) {
uni.setStorageSync('userId', userId)
}
2026-01-13 04:12:48 +00:00
// 保存 openid如果有
const openid = res?.openid || res?.data?.openid
if (openid) {
uni.setStorageSync('openid', openid)
2025-12-19 12:27:55 +00:00
}
// 登录成功后,调用个人信息接口获取完整用户信息
try {
const userInfoRes = await getUserInfo()
2026-01-13 04:12:48 +00:00
if (userInfoRes && userInfoRes.data) {
// 根据接口返回的数据结构,保存 res.data
uni.setStorageSync('userInfo', userInfoRes.data)
} else if (userInfoRes) {
// 兼容直接返回数据的情况
uni.setStorageSync('userInfo', userInfoRes)
}
2025-12-19 12:27:55 +00:00
} catch (error) {
console.error('获取用户信息失败:', error)
2026-01-13 04:12:48 +00:00
// 获取用户信息失败不影响登录流程,继续执行
2025-12-19 12:27:55 +00:00
}
uni.showToast({
title: '登录成功',
icon: 'success'
})
2026-01-14 10:55:42 +00:00
// 延迟跳转,让用户看到成功提示,统一跳转到首页
2025-12-19 12:27:55 +00:00
setTimeout(() => {
2026-01-14 10:55:42 +00:00
uni.switchTab({
url: '/pages/index/index',
fail: () => {
// 如果 switchTab 失败(可能不在 tabBar 页面),使用 reLaunch
uni.reLaunch({
url: '/pages/index/index'
})
}
})
2025-12-19 12:27:55 +00:00
}, 1500)
} catch (error) {
console.error('一键登录失败:', error)
// 错误信息已在request中统一处理
} finally {
this.loading = false
}
} else {
uni.showToast({
title: '授权失败,请重试',
icon: 'none'
})
}
2026-01-13 04:12:48 +00:00
},
// 生成随机 state 值(用于回调)
generateState() {
// 生成 UUID 格式的随机字符串类似9b2ffbc1-7425-4155-9894-9d5c08541d62
// 格式xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
const generateUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0
const v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
const state = generateUUID()
// 保存 state 到本地存储,以便后续回调时使用
uni.setStorageSync('loginState', state)
return state
},
// 获取邀请码
getInviteCode() {
// 优先从页面参数获取
if (this.inviteCode) {
return this.inviteCode
}
// 也可以从本地存储获取
const storedInviteCode = uni.getStorageSync('inviteCode')
if (storedInviteCode) {
return storedInviteCode
}
// 也可以从全局变量获取
const app = getApp()
if (app && app.globalData && app.globalData.inviteCode) {
return app.globalData.inviteCode
}
return ''
2025-12-19 12:27:55 +00:00
}
}
}
</script>
<style lang="scss" scoped>
.login-page {
min-height: 100vh;
background: linear-gradient(180deg, #e2e8f1 0%, #ffffff 100%);
}
2026-03-09 03:37:41 +00:00
.header-fixed-wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 100;
background: linear-gradient(180deg, #e2e8f1 0%, #ffffff 100%);
}
.main-wrap {
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
}
2025-12-19 12:27:55 +00:00
.status-bar {
background-color: transparent;
}
.header {
display: flex;
align-items: center;
justify-content: center;
position: relative;
padding: 20rpx 0;
background-color: transparent;
.back-btn {
position: absolute;
left: 30rpx;
width: 60rpx;
height: 60rpx;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
.back-icon {
font-size: 60rpx;
color: #333333;
line-height: 1;
}
}
.header-title {
font-size: 36rpx;
font-weight: 500;
color: #1a1819;
}
}
.login-content {
padding: 60rpx 60rpx 0;
}
.logo-section {
display: flex;
justify-content: center;
2026-01-13 04:12:48 +00:00
align-items: center;
margin-bottom: 120rpx;
margin-top: 80rpx;
2025-12-19 12:27:55 +00:00
.logo {
2026-01-13 04:12:48 +00:00
width: 306rpx;
height: 72rpx;
2025-12-19 12:27:55 +00:00
}
}
.login-form {
.form-item {
margin-bottom: 40rpx;
.input-wrapper {
position: relative;
background-color: #ffffff;
border-radius: 16rpx;
padding: 30rpx 24rpx;
display: flex;
align-items: center;
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
.input-label {
width: 120rpx;
font-size: 28rpx;
color: #333333;
font-weight: 500;
flex-shrink: 0;
}
.input-field {
flex: 1;
font-size: 28rpx;
color: #1a1819;
}
.password-toggle {
width: 60rpx;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
.toggle-icon {
font-size: 32rpx;
}
}
}
}
.login-btn {
width: 100%;
height: 88rpx;
background: linear-gradient(135deg, #004294 0%, #0066cc 100%);
border-radius: 44rpx;
color: #ffffff;
font-size: 32rpx;
font-weight: 500;
border: none;
margin-top: 40rpx;
display: flex;
align-items: center;
justify-content: center;
&::after {
border: none;
}
&.disabled {
background: #cccccc;
color: #999999;
}
}
.divider {
display: flex;
align-items: center;
margin: 60rpx 0 40rpx;
padding: 0 20rpx;
.divider-line {
flex: 1;
height: 1rpx;
background-color: #e0e0e0;
}
.divider-text {
margin: 0 20rpx;
font-size: 24rpx;
color: #999999;
}
}
.wechat-login-btn {
width: 100%;
height: 88rpx;
background-color: #07c160;
border-radius: 44rpx;
color: #ffffff;
font-size: 28rpx;
border: none;
display: flex;
align-items: center;
justify-content: center;
gap: 12rpx;
&::after {
border: none;
}
.wechat-icon {
font-size: 36rpx;
}
}
.wechat-login-tip {
text-align: center;
padding: 40rpx 0;
font-size: 24rpx;
color: #999999;
}
2026-03-02 07:32:41 +00:00
.agreement-section {
margin-top: 40rpx;
padding: 0 20rpx;
.agreement-checkbox {
display: flex;
align-items: flex-start;
gap: 12rpx;
.checkbox-icon {
width: 32rpx;
height: 32rpx;
border: 1rpx solid #cccccc;
border-radius: 4rpx;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
margin-top: 4rpx;
&.checked {
background-color: #004294;
border-color: #004294;
.checkmark {
color: #ffffff;
font-size: 24rpx;
font-weight: bold;
}
}
}
.agreement-text {
font-size: 24rpx;
color: #666666;
line-height: 1.6;
flex: 1;
.link-text {
color: #004294;
text-decoration: underline;
}
}
}
}
2025-12-19 12:27:55 +00:00
}
</style>