| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- <template>
- <view class="login-container">
- <view class="login-form">
- <!-- 标题 -->
- <view class="form-header">
- <text class="form-title">手机号登录</text>
- <text class="form-subtitle">输入手机号,获取验证码登录</text>
- </view>
- <!-- 手机号输入 -->
- <view class="input-group">
- <view class="input-wrapper">
- <text class="input-icon">📱</text>
- <input
- class="input-field"
- placeholder="请输入11位手机号"
- type="number"
- minlength="11"
- maxlength="11"
- v-model="loginForm.phone"
- @input="onPhoneInput"
- />
- </view>
- <view v-if="phoneError" class="error-tip">{{ phoneError }}</view>
- </view>
- <!-- 验证码输入 -->
- <view class="input-group">
- <view class="input-wrapper code-wrapper">
- <text class="input-icon">🔢</text>
- <input
- class="input-field code-input"
- placeholder="请输入6位验证码"
- type="number"
- maxlength="6"
- v-model="loginForm.smsCode"
- />
- <button
- class="sms-btn"
- @click="sendSmsCode"
- :disabled="!canSendSms || smsCountdown > 0 || sendingCode"
- >
- <text class="sms-btn-text">
- {{ sendingCode ? '发送中...' : (smsCountdown > 0 ? `${smsCountdown}s` : '获取验证码') }}
- </text>
- </button>
- </view>
- <view v-if="codeError" class="error-tip">{{ codeError }}</view>
- </view>
- <!-- 登录按钮 -->
- <button
- class="login-btn"
- @click="handleLogin()"
- :disabled="!canLogin || isLogging"
- >
- <text class="btn-text">{{ isLogging ? '登录中...' : '登录' }}</text>
- </button>
- <view class="notice">
- <text class="notice-text">请使用SEECODER门户中关联的手机号登录</text>
- <text class="notice-text">若无账号将无法登陆,请尽快点击下方链接前往注册</text>
- <text class="notice-text"></text>
- </view>
-
- <view class="test-login-section">
- <text class="test-login-title">功能展示账号</text>
- <view class="test-login-buttons">
- <button class="test-login-btn" @click="autoLogin('student')">学生测试账号</button>
- <button class="test-login-btn" @click="autoLogin('teacher')">教师测试账号</button>
- </view>
- </view>
-
- <view class="website-link" @click="openWebsite">
- <text class="link-text">SEECODER门户官网(需连NJU VPN)</text>
- <uni-icons type="link" size="16" color="#3498DB" />
- </view>
- </view>
- </view>
- </template>
- <script setup>
- import { request } from '@/utils/request.ts';
- import { ref, computed, defineEmits, onMounted } from 'vue'
- const emit = defineEmits(['loginSuccess'])
- // 响应式数据
- const isLogging = ref(false)
- const sendingCode = ref(false)
- const smsCountdown = ref(0)
- const phoneError = ref('')
- const codeError = ref('')
- const isDev = ref(false)
- const loginForm = ref({
- phone: '',
- smsCode: ''
- })
- // 检测开发环境
- onMounted(() => {
- const systemInfo = uni.getSystemInfoSync()
- isDev.value = systemInfo.platform === 'devtools'
- })
- // 计算属性
- const canSendSms = computed(() => {
- return /^1[3-9]\d{9}$/.test(loginForm.value.phone)
- })
- const canLogin = computed(() => {
- return canSendSms.value && loginForm.value.smsCode.length === 6
- })
- // 手机号输入处理
- const onPhoneInput = (e) => {
- let value = e.detail.value.replace(/\D/g, '')
- if (value.length > 11) {
- value = value.slice(0, 11)
- }
- loginForm.value.phone = value
-
- // 清除错误提示
- phoneError.value = ''
-
- // 实时验证
- if (value.length === 11 && !/^1[3-9]\d{9}$/.test(value)) {
- phoneError.value = '请输入正确的手机号'
- }
- }
- // 发送短信验证码
- const sendSmsCode = async () => {
- if (!canSendSms.value) {
- phoneError.value = '请输入正确的手机号'
- return
- }
-
- sendingCode.value = true
- phoneError.value = ''
-
- try {
- const data = await request({
- method: 'POST',
- url: '/auth/sms-code',
- data: {
- phone: loginForm.value.phone
- }
- });
-
- uni.showToast({
- title: '验证码已发送',
- icon: 'success'
- });
- startCountdown();
-
- } catch (error) {
- // request 封装里已经处理了弹窗,这里可以只在控制台打印错误
- console.error('发送验证码失败:', error);
- phoneError.value = error.message || '发送失败,请重试';
- } finally {
- sendingCode.value = false;
- }
- };
- // 验证码登录
- const handleLogin = async () => {
- if (!canLogin.value) return
-
- isLogging.value = true
- codeError.value = ''
-
- try {
- // 调用登录API
- const resultData = await request({
- method: 'POST',
- url: '/auth/sms-login',
- data: {
- phone: loginForm.value.phone,
- smsCode: loginForm.value.smsCode
- }
- });
- if(resultData && resultData.token && resultData.userInfo){
- if(!resultData.userInfo.officialNumber){
- uni.showToast({ title: '请先前往SEECODER门户注册账号后再登录~', icon: 'none', duration: 3000 });
- return; // 终止登录流程
- }
- }
- //发送code和phone给后端以生成openId
- // wx.login({
- // async success (res) {
- // if (res.code) {
- // await request({
- // method:'POST',
- // url:'/auth/get-openId',
- // data: {
- // code: res.code,
- // phone: loginForm.value.phone,
- // }
- // })
- // } else {
- // console.log('获取code拿openid失败!' + res.errMsg)
- // }
- // }
- // })
- console.log('登录成功');
-
- emit('loginSuccess', resultData);
-
- } catch (error) {
- console.error('登录失败:', error);
- codeError.value = error.message || '登录失败,请重试';
- } finally {
- isLogging.value = false;
- }
- };
- // 倒计时
- const startCountdown = () => {
- smsCountdown.value = 60
- const timer = setInterval(() => {
- smsCountdown.value--
- if (smsCountdown.value <= 0) {
- clearInterval(timer)
- }
- }, 1000)
- }
- const autoLogin = async(role) => {
- let testPhone = '';
- let testCode = '123456';
-
- if(role === 'student'){
- testPhone = '18912345678'
- }else if (role === 'teacher'){
- testPhone = '18987654321'
- }
- loginForm.value.phone = testPhone;
- loginForm.value.smsCode = testCode;
- handleLogin()
-
- }
- const openWebsite = () => {
- const websiteUrl = 'https://p-nju.seec.seecoder.cn/portal'; // 替换为你的官网URL
- // #ifdef MP-WEIXIN
- // 微信小程序中,推荐使用 web-view 组件
- uni.navigateTo({
- url: `/pages/webview/webview?url=${encodeURIComponent(websiteUrl)}`,
- fail: () => {
- uni.showToast({ title: '跳转失败,请检查配置', icon: 'none' });
- }
- });
- // #endif
- // #ifndef MP-WEIXIN
- // App和H5端,可以直接使用 uni.openURL
- uni.openURL({
- url: websiteUrl,
- fail: () => {
- uni.showToast({ title: '无法打开网页', icon: 'none' });
- }
- });
- // #endif
- };
- // 暴露方法
- defineExpose({
- resetForm: () => {
- loginForm.value = { phone: '', smsCode: '' }
- phoneError.value = ''
- codeError.value = ''
- smsCountdown.value = 0
- }
- })
- </script>
- <style scoped>
- .login-container {
- min-height: 100vh;
- background: #ffffff;
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 40rpx;
- background-image: linear-gradient(-225deg, #a8edea 0%, #FFE6FA 100%);
- }
- .login-form {
- width: 100%;
- max-width: 600rpx;
- background: #ffffff;
- border-radius: 24rpx;
- padding: 60rpx 40rpx;
- box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
- }
- /* 表单头部 */
- .form-header {
- text-align: center;
- margin-bottom: 60rpx;
- }
- .form-title {
- display: block;
- font-size: 48rpx;
- font-weight: 600;
- color: #333;
- margin-bottom: 16rpx;
- }
- .form-subtitle {
- display: block;
- font-size: 28rpx;
- color: #666;
- font-weight: 400;
- }
- /* 输入框组 */
- .input-group {
- margin-bottom: 32rpx;
- }
- .input-wrapper {
- display: flex;
- align-items: center;
- background: #f8f9fa;
- border-radius: 16rpx;
- padding: 0 24rpx;
- border: 2rpx solid transparent;
- transition: all 0.3s ease;
- }
- .input-wrapper:focus-within {
- border-color: #98D982;
- background: #ffffff;
- box-shadow: 0 0 0 4rpx rgba(152, 217, 130, 0.15);
- }
- .input-icon {
- font-size: 32rpx;
- margin-right: 20rpx;
- color: #7BC863;
- }
- .input-field {
- flex: 1;
- height: 96rpx;
- font-size: 32rpx;
- color: #333;
- background: transparent;
- border: none;
- }
- .input-field::placeholder {
- color: #999;
- }
- /* 验证码输入特殊样式 */
- .code-wrapper {
- padding-right: 0;
- }
- .code-input {
- margin-right: 20rpx;
- }
- .sms-btn {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 0 24rpx;
- height: 64rpx;
- background: linear-gradient(135deg, #7BC863 0%, #5CB85C 100%);
- border: none;
- border-radius: 12rpx;
- font-size: 24rpx;
- white-space: nowrap;
- transition: all 0.3s ease;
- }
- .sms-btn:disabled {
- background: #e8e8e8;
- }
- .sms-btn:disabled .sms-btn-text {
- color: #999;
- }
- .sms-btn:active:not(:disabled) {
- transform: scale(0.95);
- background: linear-gradient(135deg, #98D982 0%, #7BC863 100%);
- }
- .sms-btn-text {
- color: #ffffff;
- font-size: 24rpx;
- font-weight: 500;
- }
- /* 错误提示 */
- .error-tip {
- font-size: 24rpx;
- color: #e74c3c;
- margin-top: 8rpx;
- margin-left: 56rpx;
- }
- /* 登录按钮 */
- .login-btn {
- display: flex;
- align-items: center;
- justify-content: center;
- width: 100%;
- height: 96rpx;
- border-radius: 16rpx;
- background: linear-gradient(135deg, #7BC863 0%, #5CB85C 100%);
- font-size: 32rpx;
- font-weight: 500;
- border: none;
- box-shadow: 0 4rpx 16rpx rgba(152, 217, 130, 0.25);
- transition: all 0.3s ease;
- margin-bottom: 32rpx;
- }
- .login-btn:active:not(:disabled) {
- transform: translateY(2rpx);
- background: linear-gradient(135deg, #98D982 0%, #7BC863 100%);
- box-shadow: 0 2rpx 8rpx rgba(152, 217, 130, 0.3);
- }
- .login-btn:disabled {
- opacity: 0.5;
- background: #e8e8e8;
- transform: none;
- box-shadow: none;
- }
- .login-btn:disabled .btn-text {
- color: #999;
- }
- .btn-text {
- color: #ffffff;
- font-size: 32rpx;
- font-weight: 500;
- }
- /* 提示信息 */
- .notice {
- margin-top: 60rpx;
- text-align: center;
- margin-bottom: 40rpx;
- }
- .notice-text {
- display: block;
- font-size: 24rpx;
- color: #F0AD4E;
- line-height: 1.6;
- margin-bottom: 8rpx;
- }
- .test-login-section {
- margin-top: 20rpx;
- padding-top: 40rpx;
- border-top: 1rpx solid rgba(255, 255, 255, 0.2);
- text-align: center;
- }
- .test-login-title {
- font-size: 28rpx;
- color: rgba(255, 255, 255, 0.7);
- margin-bottom: 20rpx;
- display: block;
- }
- .test-login-buttons {
- display: flex;
- justify-content: space-around;
- gap: 20rpx;
- }
- .test-login-btn {
- flex: 1;
- background: #7DB4D6;
- color: #fff;
- font-size: 28rpx;
- border: 1rpx solid rgba(255, 255, 255, 0.3);
- border-radius: 20rpx;
- height: 80rpx;
- line-height: 80rpx;
- }
- .website-link {
- display: flex;
- justify-content: center;
- gap: 10rpx;
- margin-top: 40rpx;
- padding: 15rpx 30rpx;
- border-radius: 40rpx;
- background-color: #f0f8ff;
- border: 1rpx solid #3498DB;
- transition: background-color 0.2s;
- cursor: pointer;
- }
- .website-link:active {
- background-color: #e6f2fa;
- }
- .link-text {
- font-size: 28rpx;
- color: #3498DB;
-
- }
- /* 响应式调整 */
- @media screen and (max-width: 350px) {
- .login-form {
- padding: 40rpx 30rpx;
- }
-
- .form-title {
- font-size: 40rpx;
- }
-
- .input-field {
- font-size: 28rpx;
- }
-
- .sms-btn-text {
- font-size: 20rpx;
- }
- }
- </style>
|