index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view class="login-page">
  3. <view class="login-bg">
  4. <view class="login-content">
  5. <view class="logo-area">
  6. <view class="logo-icon">🤖</view>
  7. <text class="logo-title">爱伴·AI之旅</text>
  8. <text class="logo-subtitle">L0 家立方-AI管家成长营</text>
  9. </view>
  10. <view class="login-btn-area">
  11. <button
  12. class="login-btn"
  13. open-type="getPhoneNumber"
  14. @getphonenumber="handleGetPhoneNumber"
  15. :loading="loading"
  16. :disabled="loading"
  17. >
  18. 微信一键登录
  19. </button>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import { wechatPhoneLogin } from '@/utils/api.js'
  27. export default {
  28. data() {
  29. return {
  30. loading: false
  31. }
  32. },
  33. methods: {
  34. // 微信手机号一键授权登录:getPhoneNumber 返回 phoneCode,同时用 uni.login 取 login code
  35. handleGetPhoneNumber(e) {
  36. if (this.loading) return
  37. if (!e.detail || !e.detail.code) {
  38. uni.showToast({ title: '授权失败,请重试', icon: 'none' })
  39. return
  40. }
  41. // 手机号授权成功,实时获取 login code(微信 code 一次性有效,必须点击时取)
  42. var self = this
  43. self.loading = true
  44. uni.login({
  45. provider: 'weixin',
  46. success: function(res) {
  47. if (!res.code) {
  48. uni.showToast({ title: '登录失败,请重试', icon: 'none' })
  49. self.loading = false
  50. return
  51. }
  52. wechatPhoneLogin({ code: res.code, phoneCode: e.detail.code }).then(function(resp) {
  53. var data = resp.data
  54. self.$store.dispatch('login', data)
  55. uni.showToast({ title: '登录成功', icon: 'success' })
  56. setTimeout(function() {
  57. if (!data.alumniVerify || data.alumniVerify === 'pending') {
  58. uni.redirectTo({ url: '/pages/verify/index' })
  59. } else if (!data.classId) {
  60. uni.switchTab({ url: '/pages/course/index' })
  61. } else {
  62. uni.switchTab({ url: '/pages/index/index' })
  63. }
  64. }, 800)
  65. }).catch(function(err) {
  66. console.error('登录失败', err)
  67. uni.showToast({ title: (err && err.message) || '登录失败', icon: 'none' })
  68. }).finally(function() {
  69. self.loading = false
  70. })
  71. },
  72. fail: function() {
  73. uni.showToast({ title: '登录失败,请重试', icon: 'none' })
  74. self.loading = false
  75. }
  76. })
  77. }
  78. }
  79. }
  80. </script>
  81. <style scoped>
  82. .login-page {
  83. min-height: 100vh;
  84. background: linear-gradient(135deg, #F97316, #EA580C);
  85. display: flex;
  86. flex-direction: column;
  87. }
  88. .login-bg {
  89. flex: 1;
  90. display: flex;
  91. flex-direction: column;
  92. justify-content: center;
  93. align-items: center;
  94. }
  95. .login-content {
  96. display: flex;
  97. flex-direction: column;
  98. align-items: center;
  99. padding: 0 60rpx;
  100. }
  101. .logo-area {
  102. display: flex;
  103. flex-direction: column;
  104. align-items: center;
  105. margin-bottom: 120rpx;
  106. }
  107. .logo-icon {
  108. font-size: 120rpx;
  109. margin-bottom: 30rpx;
  110. }
  111. .logo-title {
  112. font-size: 48rpx;
  113. font-weight: 700;
  114. color: #FFFFFF;
  115. margin-bottom: 16rpx;
  116. }
  117. .logo-subtitle {
  118. font-size: 28rpx;
  119. color: rgba(255,255,255,0.85);
  120. }
  121. .login-btn-area {
  122. width: 100%;
  123. }
  124. .login-btn {
  125. width: 100%;
  126. height: 96rpx;
  127. line-height: 96rpx;
  128. background: #FFFFFF;
  129. color: #F97316;
  130. font-size: 32rpx;
  131. font-weight: 600;
  132. border-radius: 48rpx;
  133. border: none;
  134. }
  135. .login-btn:active {
  136. opacity: 0.85;
  137. }
  138. </style>