| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732 |
- <template>
- <view class="login-container">
- <!-- 顶部导航栏 -->
- <view class="nav-bar">
- <text class="nav-title">登录</text>
- </view>
- <!-- Logo 区域 -->
- <view class="logo-section">
- <image class="logo-icon" src="/static/image/public/logo.png" mode="aspectFill"></image>
- <text class="app-name">{{appName}}</text>
- </view>
- <!-- 表单区域 -->
- <view class="form-section">
- <!-- 手机号输入 -->
- <view class="input-item">
- <uni-easyinput v-model="phoneNumber" placeholder="请输入手机号" type="number" maxlength="11"
- :clearable="false" class="phone-input" />
- </view>
- <!-- 验证码输入 -->
- <view class="input-item verify-code-row">
- <uni-easyinput v-model="verifyCode" placeholder="验证码" type="number" maxlength="6" :clearable="false"
- class="code-input" />
- <text class="get-code-btn" :class="{ disabled: countdown > 0 }" @click="getVerifyCode">
- {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
- </text>
- </view>
- <!-- 登录按钮 -->
- <view class="login-btn-wrapper">
- <button class="login-btn" @click="handleLogin" :loading="isLoading">登录</button>
- </view>
- <!-- 协议说明 -->
- <view class="agreement-text">
- <text class="gray-text">登录即代表您已同意</text>
- <text class="link-text" @click="showUserAgreement">《用户协议》</text>
- <text class="gray-text">与</text>
- <text class="link-text" @click="showPrivacyPolicy">《隐私政策》</text>
- </view>
- </view>
- <!-- 其他登录方式 -->
- <view class="other-method-section">
- <view class="divider">
- <view class="divider-line"></view>
- <text class="divider-text">其他方式</text>
- <view class="divider-line"></view>
- </view>
- <!-- 微信一键登录 -->
- <view class="wechat-login-wrapper">
- <!-- #ifdef MP-WEIXIN -->
- <button class="wechat-login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
- <image class="wechat-icon-img" src="/static/image/public/wechat-icon.png" mode="aspectFit" />
- <text class="wechat-text">微信一键登录</text>
- </button>
- <!-- #endif -->
- <!-- #ifndef MP-WEIXIN -->
- <button class="wechat-login-btn" @click="wechatLogin">
- <text class="wechat-icon"></text>
- <text class="wechat-text">微信一键登录</text>
- </button>
- <!-- #endif -->
- </view>
- </view>
- <!-- 用户协议弹窗 -->
- <view class="agreement-popup" v-if="showUserAgreementPopup" @click="closeUserAgreement">
- <view class="popup-mask"></view>
- <view class="popup-content" @click.stop>
- <view class="popup-header">
- <text class="popup-title">用户协议</text>
- <text class="popup-close" @click="closeUserAgreement">✕</text>
- </view>
- <scroll-view scroll-y class="popup-body">
- <view class="agreement-content">
- <yonghuxieyi/>
- </view>
- </scroll-view>
- <view class="popup-footer">
- <button class="agree-btn" @click="closeUserAgreement">我已知晓</button>
- </view>
- </view>
- </view>
- <!-- 隐私政策弹窗 -->
- <view class="agreement-popup" v-if="showPrivacyPolicyPopup" @click="closePrivacyPolicy">
- <view class="popup-mask"></view>
- <view class="popup-content" @click.stop>
- <view class="popup-header">
- <text class="popup-title">隐私政策</text>
- <text class="popup-close" @click="closePrivacyPolicy">✕</text>
- </view>
- <scroll-view scroll-y class="popup-body">
- <view class="agreement-content">
-
- <yonghuzhengce/>
- </view>
- </scroll-view>
- <view class="popup-footer">
- <button class="agree-btn" @click="closePrivacyPolicy">我已知晓</button>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script setup>
- import yonghuxieyi from "./yonghuxieyi.vue"
- import yonghuzhengce from "./yonghuzhengce.vue"
-
- import {
- getCurrentConfig
- } from '@/config/index.js'
- import {
- ref,
- onMounted
- } from 'vue'
- import {
- sendCode,
- codeLogin,
- wxgetLogin,
- getUserInfo,
- wxLogin
- } from '@/api/login.js'
- import {
- saveUserInfo,
- getToken
- } from '@/utils/userCache.js'
- let appName = ref('')
- let version = ref('')
- onMounted(async () => {
- const config = await getCurrentConfig()
- appName.value = config.appName
- version.value = config.appVersion
- })
- // 表单数据
- const phoneNumber = ref('')
- const verifyCode = ref('')
- const countdown = ref(0)
- const isLoading = ref(false)
- let timer = null
- // 弹窗控制
- const showUserAgreementPopup = ref(false)
- const showPrivacyPolicyPopup = ref(false)
- // 获取验证码
- const getVerifyCode = async () => {
- if (countdown.value > 0) return
- if (!phoneNumber.value || phoneNumber.value.length !== 11) {
- uni.showToast({
- title: '请输入正确的手机号',
- icon: 'none'
- })
- return
- }
- try {
- const res = await sendCode({
- phone: phoneNumber.value,
- type: 3
- })
- if (res.code != 200) {
- uni.showToast({
- title: res.msg,
- icon: 'error'
- })
- return
- }
- console.log('验证码发送成功:', res)
- // 开始倒计时
- countdown.value = 60
- timer = setInterval(() => {
- countdown.value--
- if (countdown.value <= 0) {
- clearInterval(timer)
- }
- }, 1000)
- uni.showToast({
- title: '验证码已发送',
- icon: 'success'
- })
- } catch (error) {
- console.error('发送验证码失败:', error)
- }
- }
- // 处理登录
- const handleLogin = async () => {
- if (isLoading.value) return
- if (!phoneNumber.value || phoneNumber.value.length !== 11) {
- uni.showToast({
- title: '请输入正确的手机号',
- icon: 'none'
- })
- return
- }
- if (!verifyCode.value) {
- uni.showToast({
- title: '请输入验证码',
- icon: 'none'
- })
- return
- }
- isLoading.value = true
- try {
- // 第一步:调用登录接口获取 token
- const res = await codeLogin({phonenumber:phoneNumber.value, smsCode:verifyCode.value})
- console.log('登录接口返回:', res)
-
- if (res.code != 200) {
- uni.showToast({
- title: res.msg || '登录失败',
- icon: 'none'
- })
- isLoading.value = false
- return
- }
-
- // 获取 token
- const { token } = res.data || res
-
- if (!token) {
- uni.showToast({
- title: '未获取到 token',
- icon: 'none'
- })
- isLoading.value = false
- return
- }
-
- // 第二步:调用 getUserInfo 获取用户信息
- const userInfoRes = await getUserInfo({})
- console.log('用户信息接口返回:', userInfoRes)
-
- if (userInfoRes.code != 200) {
- uni.showToast({
- title: userInfoRes.msg || '获取用户信息失败',
- icon: 'none'
- })
- isLoading.value = false
- return
- }
-
- // 第三步:保存用户信息和 token
- const userInfo = userInfoRes.data || userInfoRes
- const saveSuccess = saveUserInfo(userInfo, token)
-
- if (saveSuccess) {
- uni.showToast({
- title: '登录成功',
- icon: 'success'
- })
- // 延迟跳转
- setTimeout(() => {
- uni.reLaunch({
- url: '/pages/index/index'
- })
- }, 1500)
- } else {
- uni.showToast({
- title: '保存登录信息失败',
- icon: 'none'
- })
- }
- } catch (error) {
- console.error('登录失败:', error)
- uni.showToast({
- title: '登录失败,请重试',
- icon: 'none'
- })
- } finally {
- isLoading.value = false
- }
- }
- // 微信登录 - 获取手机号
- const getPhoneNumber = async (e) => {
- // #ifdef MP-WEIXIN
- if (e.detail.errMsg !== 'getPhoneNumber:ok') {
- uni.showToast({
- title: '您取消了授权',
- icon: 'none'
- })
- return
- }
- try {
- // 先获取微信登录 code
- const code = await wxLogin()
-
- // 调用 wxgetLogin 接口,传入 code 和手机号信息
- console.log(e,"eeeeeeee");
- const res = await wxgetLogin({
- wxCode: code,
- phoneCode: e.detail.code,
- iv: e.detail.iv,
- type: 1
- })
-
- if (res.code != 200) {
- uni.showToast({
- title: res.msg || '登录失败',
- icon: 'none'
- })
- return
- }
-
- console.log('微信登录成功:', res)
-
- // 获取 token
- const { token } = res.data || res
-
- if (!token) {
- uni.showToast({
- title: '未获取到 token',
- icon: 'none'
- })
- return
- }
-
- // 第二步:调用 getUserInfo 获取用户信息
- const userInfoRes = await getUserInfo({})
- console.log('用户信息接口返回:', userInfoRes)
-
- if (userInfoRes.code != 200) {
- uni.showToast({
- title: userInfoRes.msg || '获取用户信息失败',
- icon: 'none'
- })
- return
- }
-
- // 第三步:保存用户信息和 token
- const userInfo = userInfoRes.data || userInfoRes
- const saveSuccess = saveUserInfo(userInfo, token)
-
- if (saveSuccess) {
- uni.showToast({
- title: '登录成功',
- icon: 'success'
- })
-
- // 延迟跳转到首页
- setTimeout(() => {
- uni.reLaunch({
- url: '/pages/index/index'
- })
- }, .800)
- } else {
- uni.showToast({
- title: '保存登录信息失败',
- icon: 'none'
- })
- }
- } catch (error) {
- console.error('微信登录失败:', error)
- uni.showToast({
- title: '登录失败,请重试',
- icon: 'none'
- })
- }
- // #endif
- }
- // 微信登录(非小程序环境备用)
- const wechatLogin = async () => {
- // #ifndef MP-WEIXIN
- uni.showToast({
- title: '请在微信小程序中使用',
- icon: 'none'
- })
- // #endif
- }
- // 显示用户协议
- const showUserAgreement = () => {
- showUserAgreementPopup.value = true
- }
- // 关闭用户协议
- const closeUserAgreement = () => {
- showUserAgreementPopup.value = false
- }
- // 显示隐私政策
- const showPrivacyPolicy = () => {
- showPrivacyPolicyPopup.value = true
- }
- // 关闭隐私政策
- const closePrivacyPolicy = () => {
- showPrivacyPolicyPopup.value = false
- }
- // 页面卸载时清除定时器
- import {
- onUnmounted
- } from 'vue'
- onUnmounted(() => {
- if (timer) {
- clearInterval(timer)
- }
- })
- </script>
- <style lang="scss" scoped>
- .login-container {
- min-height: 100vh;
- background-color: #ffffff;
- padding: 0 40rpx;
- position: relative;
- }
- // 导航栏
- .nav-bar {
- height: 88rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- .nav-title {
- font-size: 34rpx;
- font-weight: 600;
- color: #000000;
- }
- }
- // Logo 区域
- .logo-section {
- display: flex;
- flex-direction: column;
- align-items: center;
- margin-top: 60rpx;
- margin-bottom: 80rpx;
- .logo-icon {
- width: 160rpx;
- height: 160rpx;
- background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
- border-radius: 32rpx;
- box-shadow: 0 8rpx 24rpx rgba(24, 144, 255, 0.3);
- }
- .app-name {
- font-size: 40rpx;
- font-weight: 600;
- color: #000000;
- margin-top: 32rpx;
- }
- }
- // 表单区域
- .form-section {
- .input-item {
- margin-bottom: 24rpx;
- .phone-input,
- .code-input {
- background-color: #f5f7fa;
- border-radius: 16rpx;
- font-size: 28rpx;
- }
- }
- .verify-code-row {
- display: flex;
- align-items: center;
- .code-input {
- flex: 1;
- margin-right: 20rpx;
- }
- .get-code-btn {
- font-size: 26rpx;
- color: #1890ff;
- white-space: nowrap;
- &.disabled {
- color: #999999;
- }
- }
- }
- .login-btn-wrapper {
- margin-top: 48rpx;
- margin-bottom: 24rpx;
- .login-btn {
- width: 100%;
- height: 88rpx;
- background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
- border-radius: 16rpx;
- border: none;
- font-size: 32rpx;
- font-weight: 500;
- color: #ffffff;
- display: flex;
- align-items: center;
- justify-content: center;
- &::after {
- border: none;
- }
- }
- }
- .agreement-text {
- display: flex;
- flex-wrap: wrap;
- justify-content: center;
- font-size: 22rpx;
- .gray-text {
- color: #999999;
- margin: 0 4rpx;
- }
- .link-text {
- color: #1890ff;
- margin: 0 4rpx;
- }
- }
- }
- // 其他登录方式
- .other-method-section {
- margin-top: 120rpx;
- .divider {
- display: flex;
- align-items: center;
- justify-content: center;
- margin-bottom: 40rpx;
- .divider-line {
- width: 120rpx;
- height: 1rpx;
- background-color: #e5e5e5;
- }
- .divider-text {
- font-size: 24rpx;
- color: #999999;
- margin: 0 24rpx;
- }
- }
- .wechat-login-wrapper {
- .wechat-login-btn {
- width: 100%;
- height: 88rpx;
- background: linear-gradient(90deg, #07c160 0%, #06ad56 100%);
- border-radius: 16rpx;
- border: none;
- font-size: 32rpx;
- font-weight: 500;
- color: #ffffff;
- display: flex;
- align-items: center;
- justify-content: center;
- &::after {
- border: none;
- }
- .wechat-icon-img {
- width: 40rpx;
- height: 40rpx;
- margin-right: 12rpx;
- }
- .wechat-icon {
- margin-right: 12rpx;
- font-size: 36rpx;
- }
- .wechat-text {
- color: #ffffff;
- }
- }
- }
- }
- // 协议弹窗
- .agreement-popup {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- z-index: 1000;
- .popup-mask {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background-color: rgba(0, 0, 0, 0.5);
- }
- .popup-content {
- position: absolute;
- bottom: 0;
- left: 0;
- right: 0;
- background-color: #ffffff;
- border-radius: 32rpx 32rpx 0 0;
- max-height: 80vh;
- display: flex;
- flex-direction: column;
- animation: slideUp 0.3s ease-out;
- .popup-header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 32rpx;
- border-bottom: 1rpx solid #f0f0f0;
- .popup-title {
- font-size: 32rpx;
- font-weight: 600;
- color: #000000;
- }
- .popup-close {
- font-size: 40rpx;
- color: #999999;
- width: 60rpx;
- height: 60rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- }
- .popup-body {
- flex: 1;
- overflow-y: auto;
- padding: 32rpx;
- .agreement-content {
- display: flex;
- flex-direction: column;
- .agreement-title {
- font-size: 36rpx;
- font-weight: 600;
- color: #000000;
- display: block;
- text-align: center;
- margin-bottom: 20rpx;
- }
- .update-time {
- font-size: 24rpx;
- color: #999999;
- display: block;
- text-align: center;
- margin-bottom: 40rpx;
- }
- .section {
- margin-bottom: 32rpx;
- .section-title {
- font-size: 28rpx;
- font-weight: 600;
- color: #000000;
- display: block;
- margin-bottom: 16rpx;
- }
- .section-content {
- font-size: 24rpx;
- color: #333333;
- line-height: 1.8;
- display: block;
- white-space: pre-wrap;
- }
- }
- }
- }
- .popup-footer {
- padding: 24rpx 32rpx 40rpx;
- border-top: 1rpx solid #f0f0f0;
- .agree-btn {
- width: 100%;
- height: 80rpx;
- background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
- border-radius: 16rpx;
- border: none;
- font-size: 30rpx;
- font-weight: 500;
- color: #ffffff;
- display: flex;
- align-items: center;
- justify-content: center;
- &::after {
- border: none;
- }
- }
- }
- }
- }
- @keyframes slideUp {
- from {
- transform: translateY(100%);
- }
- to {
- transform: translateY(0);
- }
- }
- </style>
|