LoginForm.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <template>
  2. <view class="login-container">
  3. <view class="login-form">
  4. <!-- 标题 -->
  5. <view class="form-header">
  6. <text class="form-title">手机号登录</text>
  7. <text class="form-subtitle">输入手机号,获取验证码登录</text>
  8. </view>
  9. <!-- 手机号输入 -->
  10. <view class="input-group">
  11. <view class="input-wrapper">
  12. <text class="input-icon">📱</text>
  13. <input
  14. class="input-field"
  15. placeholder="请输入11位手机号"
  16. type="number"
  17. minlength="11"
  18. maxlength="11"
  19. v-model="loginForm.phone"
  20. @input="onPhoneInput"
  21. />
  22. </view>
  23. <view v-if="phoneError" class="error-tip">{{ phoneError }}</view>
  24. </view>
  25. <!-- 验证码输入 -->
  26. <view class="input-group">
  27. <view class="input-wrapper code-wrapper">
  28. <text class="input-icon">🔢</text>
  29. <input
  30. class="input-field code-input"
  31. placeholder="请输入6位验证码"
  32. type="number"
  33. maxlength="6"
  34. v-model="loginForm.smsCode"
  35. />
  36. <button
  37. class="sms-btn"
  38. @click="sendSmsCode"
  39. :disabled="!canSendSms || smsCountdown > 0 || sendingCode"
  40. >
  41. <text class="sms-btn-text">
  42. {{ sendingCode ? '发送中...' : (smsCountdown > 0 ? `${smsCountdown}s` : '获取验证码') }}
  43. </text>
  44. </button>
  45. </view>
  46. <view v-if="codeError" class="error-tip">{{ codeError }}</view>
  47. </view>
  48. <!-- 登录按钮 -->
  49. <button
  50. class="login-btn"
  51. @click="handleLogin()"
  52. :disabled="!canLogin || isLogging"
  53. >
  54. <text class="btn-text">{{ isLogging ? '登录中...' : '登录' }}</text>
  55. </button>
  56. <view class="notice">
  57. <text class="notice-text">请使用SEECODER门户中关联的手机号登录</text>
  58. <text class="notice-text">若无账号将无法登陆,请尽快点击下方链接前往注册</text>
  59. <text class="notice-text"></text>
  60. </view>
  61. <view class="test-login-section">
  62. <text class="test-login-title">功能展示账号</text>
  63. <view class="test-login-buttons">
  64. <button class="test-login-btn" @click="autoLogin('student')">学生测试账号</button>
  65. <button class="test-login-btn" @click="autoLogin('teacher')">教师测试账号</button>
  66. </view>
  67. </view>
  68. <view class="website-link" @click="openWebsite">
  69. <text class="link-text">SEECODER门户官网(需连NJU VPN)</text>
  70. <uni-icons type="link" size="16" color="#3498DB" />
  71. </view>
  72. </view>
  73. </view>
  74. </template>
  75. <script setup>
  76. import { request } from '@/utils/request.ts';
  77. import { ref, computed, defineEmits, onMounted } from 'vue'
  78. const emit = defineEmits(['loginSuccess'])
  79. // 响应式数据
  80. const isLogging = ref(false)
  81. const sendingCode = ref(false)
  82. const smsCountdown = ref(0)
  83. const phoneError = ref('')
  84. const codeError = ref('')
  85. const isDev = ref(false)
  86. const loginForm = ref({
  87. phone: '',
  88. smsCode: ''
  89. })
  90. // 检测开发环境
  91. onMounted(() => {
  92. const systemInfo = uni.getSystemInfoSync()
  93. isDev.value = systemInfo.platform === 'devtools'
  94. })
  95. // 计算属性
  96. const canSendSms = computed(() => {
  97. return /^1[3-9]\d{9}$/.test(loginForm.value.phone)
  98. })
  99. const canLogin = computed(() => {
  100. return canSendSms.value && loginForm.value.smsCode.length === 6
  101. })
  102. // 手机号输入处理
  103. const onPhoneInput = (e) => {
  104. let value = e.detail.value.replace(/\D/g, '')
  105. if (value.length > 11) {
  106. value = value.slice(0, 11)
  107. }
  108. loginForm.value.phone = value
  109. // 清除错误提示
  110. phoneError.value = ''
  111. // 实时验证
  112. if (value.length === 11 && !/^1[3-9]\d{9}$/.test(value)) {
  113. phoneError.value = '请输入正确的手机号'
  114. }
  115. }
  116. // 发送短信验证码
  117. const sendSmsCode = async () => {
  118. if (!canSendSms.value) {
  119. phoneError.value = '请输入正确的手机号'
  120. return
  121. }
  122. sendingCode.value = true
  123. phoneError.value = ''
  124. try {
  125. const data = await request({
  126. method: 'POST',
  127. url: '/auth/sms-code',
  128. data: {
  129. phone: loginForm.value.phone
  130. }
  131. });
  132. uni.showToast({
  133. title: '验证码已发送',
  134. icon: 'success'
  135. });
  136. startCountdown();
  137. } catch (error) {
  138. // request 封装里已经处理了弹窗,这里可以只在控制台打印错误
  139. console.error('发送验证码失败:', error);
  140. phoneError.value = error.message || '发送失败,请重试';
  141. } finally {
  142. sendingCode.value = false;
  143. }
  144. };
  145. // 验证码登录
  146. const handleLogin = async () => {
  147. if (!canLogin.value) return
  148. isLogging.value = true
  149. codeError.value = ''
  150. try {
  151. // 调用登录API
  152. const resultData = await request({
  153. method: 'POST',
  154. url: '/auth/sms-login',
  155. data: {
  156. phone: loginForm.value.phone,
  157. smsCode: loginForm.value.smsCode
  158. }
  159. });
  160. if(resultData && resultData.token && resultData.userInfo){
  161. if(!resultData.userInfo.officialNumber){
  162. uni.showToast({ title: '请先前往SEECODER门户注册账号后再登录~', icon: 'none', duration: 3000 });
  163. return; // 终止登录流程
  164. }
  165. }
  166. //发送code和phone给后端以生成openId
  167. // wx.login({
  168. // async success (res) {
  169. // if (res.code) {
  170. // await request({
  171. // method:'POST',
  172. // url:'/auth/get-openId',
  173. // data: {
  174. // code: res.code,
  175. // phone: loginForm.value.phone,
  176. // }
  177. // })
  178. // } else {
  179. // console.log('获取code拿openid失败!' + res.errMsg)
  180. // }
  181. // }
  182. // })
  183. console.log('登录成功');
  184. emit('loginSuccess', resultData);
  185. } catch (error) {
  186. console.error('登录失败:', error);
  187. codeError.value = error.message || '登录失败,请重试';
  188. } finally {
  189. isLogging.value = false;
  190. }
  191. };
  192. // 倒计时
  193. const startCountdown = () => {
  194. smsCountdown.value = 60
  195. const timer = setInterval(() => {
  196. smsCountdown.value--
  197. if (smsCountdown.value <= 0) {
  198. clearInterval(timer)
  199. }
  200. }, 1000)
  201. }
  202. const autoLogin = async(role) => {
  203. let testPhone = '';
  204. let testCode = '123456';
  205. if(role === 'student'){
  206. testPhone = '18912345678'
  207. }else if (role === 'teacher'){
  208. testPhone = '18987654321'
  209. }
  210. loginForm.value.phone = testPhone;
  211. loginForm.value.smsCode = testCode;
  212. handleLogin()
  213. }
  214. const openWebsite = () => {
  215. const websiteUrl = 'https://p-nju.seec.seecoder.cn/portal'; // 替换为你的官网URL
  216. // #ifdef MP-WEIXIN
  217. // 微信小程序中,推荐使用 web-view 组件
  218. uni.navigateTo({
  219. url: `/pages/webview/webview?url=${encodeURIComponent(websiteUrl)}`,
  220. fail: () => {
  221. uni.showToast({ title: '跳转失败,请检查配置', icon: 'none' });
  222. }
  223. });
  224. // #endif
  225. // #ifndef MP-WEIXIN
  226. // App和H5端,可以直接使用 uni.openURL
  227. uni.openURL({
  228. url: websiteUrl,
  229. fail: () => {
  230. uni.showToast({ title: '无法打开网页', icon: 'none' });
  231. }
  232. });
  233. // #endif
  234. };
  235. // 暴露方法
  236. defineExpose({
  237. resetForm: () => {
  238. loginForm.value = { phone: '', smsCode: '' }
  239. phoneError.value = ''
  240. codeError.value = ''
  241. smsCountdown.value = 0
  242. }
  243. })
  244. </script>
  245. <style scoped>
  246. .login-container {
  247. min-height: 100vh;
  248. background: #ffffff;
  249. display: flex;
  250. align-items: center;
  251. justify-content: center;
  252. padding: 40rpx;
  253. background-image: linear-gradient(-225deg, #a8edea 0%, #FFE6FA 100%);
  254. }
  255. .login-form {
  256. width: 100%;
  257. max-width: 600rpx;
  258. background: #ffffff;
  259. border-radius: 24rpx;
  260. padding: 60rpx 40rpx;
  261. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  262. }
  263. /* 表单头部 */
  264. .form-header {
  265. text-align: center;
  266. margin-bottom: 60rpx;
  267. }
  268. .form-title {
  269. display: block;
  270. font-size: 48rpx;
  271. font-weight: 600;
  272. color: #333;
  273. margin-bottom: 16rpx;
  274. }
  275. .form-subtitle {
  276. display: block;
  277. font-size: 28rpx;
  278. color: #666;
  279. font-weight: 400;
  280. }
  281. /* 输入框组 */
  282. .input-group {
  283. margin-bottom: 32rpx;
  284. }
  285. .input-wrapper {
  286. display: flex;
  287. align-items: center;
  288. background: #f8f9fa;
  289. border-radius: 16rpx;
  290. padding: 0 24rpx;
  291. border: 2rpx solid transparent;
  292. transition: all 0.3s ease;
  293. }
  294. .input-wrapper:focus-within {
  295. border-color: #98D982;
  296. background: #ffffff;
  297. box-shadow: 0 0 0 4rpx rgba(152, 217, 130, 0.15);
  298. }
  299. .input-icon {
  300. font-size: 32rpx;
  301. margin-right: 20rpx;
  302. color: #7BC863;
  303. }
  304. .input-field {
  305. flex: 1;
  306. height: 96rpx;
  307. font-size: 32rpx;
  308. color: #333;
  309. background: transparent;
  310. border: none;
  311. }
  312. .input-field::placeholder {
  313. color: #999;
  314. }
  315. /* 验证码输入特殊样式 */
  316. .code-wrapper {
  317. padding-right: 0;
  318. }
  319. .code-input {
  320. margin-right: 20rpx;
  321. }
  322. .sms-btn {
  323. display: flex;
  324. align-items: center;
  325. justify-content: center;
  326. padding: 0 24rpx;
  327. height: 64rpx;
  328. background: linear-gradient(135deg, #7BC863 0%, #5CB85C 100%);
  329. border: none;
  330. border-radius: 12rpx;
  331. font-size: 24rpx;
  332. white-space: nowrap;
  333. transition: all 0.3s ease;
  334. }
  335. .sms-btn:disabled {
  336. background: #e8e8e8;
  337. }
  338. .sms-btn:disabled .sms-btn-text {
  339. color: #999;
  340. }
  341. .sms-btn:active:not(:disabled) {
  342. transform: scale(0.95);
  343. background: linear-gradient(135deg, #98D982 0%, #7BC863 100%);
  344. }
  345. .sms-btn-text {
  346. color: #ffffff;
  347. font-size: 24rpx;
  348. font-weight: 500;
  349. }
  350. /* 错误提示 */
  351. .error-tip {
  352. font-size: 24rpx;
  353. color: #e74c3c;
  354. margin-top: 8rpx;
  355. margin-left: 56rpx;
  356. }
  357. /* 登录按钮 */
  358. .login-btn {
  359. display: flex;
  360. align-items: center;
  361. justify-content: center;
  362. width: 100%;
  363. height: 96rpx;
  364. border-radius: 16rpx;
  365. background: linear-gradient(135deg, #7BC863 0%, #5CB85C 100%);
  366. font-size: 32rpx;
  367. font-weight: 500;
  368. border: none;
  369. box-shadow: 0 4rpx 16rpx rgba(152, 217, 130, 0.25);
  370. transition: all 0.3s ease;
  371. margin-bottom: 32rpx;
  372. }
  373. .login-btn:active:not(:disabled) {
  374. transform: translateY(2rpx);
  375. background: linear-gradient(135deg, #98D982 0%, #7BC863 100%);
  376. box-shadow: 0 2rpx 8rpx rgba(152, 217, 130, 0.3);
  377. }
  378. .login-btn:disabled {
  379. opacity: 0.5;
  380. background: #e8e8e8;
  381. transform: none;
  382. box-shadow: none;
  383. }
  384. .login-btn:disabled .btn-text {
  385. color: #999;
  386. }
  387. .btn-text {
  388. color: #ffffff;
  389. font-size: 32rpx;
  390. font-weight: 500;
  391. }
  392. /* 提示信息 */
  393. .notice {
  394. margin-top: 60rpx;
  395. text-align: center;
  396. margin-bottom: 40rpx;
  397. }
  398. .notice-text {
  399. display: block;
  400. font-size: 24rpx;
  401. color: #F0AD4E;
  402. line-height: 1.6;
  403. margin-bottom: 8rpx;
  404. }
  405. .test-login-section {
  406. margin-top: 20rpx;
  407. padding-top: 40rpx;
  408. border-top: 1rpx solid rgba(255, 255, 255, 0.2);
  409. text-align: center;
  410. }
  411. .test-login-title {
  412. font-size: 28rpx;
  413. color: rgba(255, 255, 255, 0.7);
  414. margin-bottom: 20rpx;
  415. display: block;
  416. }
  417. .test-login-buttons {
  418. display: flex;
  419. justify-content: space-around;
  420. gap: 20rpx;
  421. }
  422. .test-login-btn {
  423. flex: 1;
  424. background: #7DB4D6;
  425. color: #fff;
  426. font-size: 28rpx;
  427. border: 1rpx solid rgba(255, 255, 255, 0.3);
  428. border-radius: 20rpx;
  429. height: 80rpx;
  430. line-height: 80rpx;
  431. }
  432. .website-link {
  433. display: flex;
  434. justify-content: center;
  435. gap: 10rpx;
  436. margin-top: 40rpx;
  437. padding: 15rpx 30rpx;
  438. border-radius: 40rpx;
  439. background-color: #f0f8ff;
  440. border: 1rpx solid #3498DB;
  441. transition: background-color 0.2s;
  442. cursor: pointer;
  443. }
  444. .website-link:active {
  445. background-color: #e6f2fa;
  446. }
  447. .link-text {
  448. font-size: 28rpx;
  449. color: #3498DB;
  450. }
  451. /* 响应式调整 */
  452. @media screen and (max-width: 350px) {
  453. .login-form {
  454. padding: 40rpx 30rpx;
  455. }
  456. .form-title {
  457. font-size: 40rpx;
  458. }
  459. .input-field {
  460. font-size: 28rpx;
  461. }
  462. .sms-btn-text {
  463. font-size: 20rpx;
  464. }
  465. }
  466. </style>