scan-login.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="page">
  3. <view class="card">
  4. <view class="icon">{{ icon }}</view>
  5. <text class="title">{{ title }}</text>
  6. <text class="desc">{{ desc }}</text>
  7. <button
  8. v-if="status === 'needlogin'"
  9. class="btn btn-primary"
  10. @tap="goLogin"
  11. >微信一键登录</button>
  12. <button
  13. v-if="status === 'error'"
  14. class="btn btn-primary"
  15. @tap="tryScan"
  16. >重试</button>
  17. <button
  18. v-if="status === 'success'"
  19. class="btn btn-primary"
  20. @tap="goHome"
  21. >进入小程序首页</button>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import { wechatWebScan } from '@/utils/api.js'
  27. export default {
  28. data() {
  29. return {
  30. state: '',
  31. status: 'loading', // loading / success / needlogin / error
  32. message: ''
  33. }
  34. },
  35. computed: {
  36. icon() {
  37. if (this.status === 'success') return '✅'
  38. if (this.status === 'error') return '❌'
  39. if (this.status === 'needlogin') return '🔑'
  40. return '⏳'
  41. },
  42. title() {
  43. if (this.status === 'success') return '登录成功'
  44. if (this.status === 'error') return '登录失败'
  45. if (this.status === 'needlogin') return '需要先登录'
  46. return '正在确认登录…'
  47. },
  48. desc() {
  49. if (this.status === 'success') return '已与电脑端同步登录,请返回电脑端客户端继续'
  50. if (this.status === 'error') return this.message || '二维码无效或已过期,请在电脑端重新获取'
  51. if (this.status === 'needlogin') return '请先登录小程序,登录后将自动完成电脑端登录'
  52. return '请稍候'
  53. }
  54. },
  55. onLoad(query) {
  56. this._onLoadFired = true
  57. query = query || {}
  58. var state = query.state || ''
  59. if (!state && query.scene) {
  60. // 小程序码 scene 为 URL 编码串
  61. try {
  62. state = decodeURIComponent(query.scene)
  63. } catch (e) {
  64. state = query.scene
  65. }
  66. }
  67. this.state = state || ''
  68. this.tryScan()
  69. },
  70. onShow() {
  71. // 首次进入:onLoad 已处理;从登录页返回时重新尝试
  72. if (this._onLoadFired) {
  73. this._onLoadFired = false
  74. return
  75. }
  76. if (this.state && this.status !== 'success') {
  77. this.tryScan()
  78. }
  79. },
  80. methods: {
  81. tryScan() {
  82. if (!this.state) {
  83. this.status = 'error'
  84. this.message = '二维码缺少参数,请在电脑端重新获取'
  85. return
  86. }
  87. var token = uni.getStorageSync('token')
  88. if (!token) {
  89. this.status = 'needlogin'
  90. return
  91. }
  92. this.status = 'loading'
  93. var self = this
  94. wechatWebScan(this.state).then(function (res) {
  95. if (res && res.code === 200) {
  96. self.status = 'success'
  97. } else {
  98. self.status = 'error'
  99. self.message = (res && res.message) || '登录失败'
  100. }
  101. }).catch(function (e) {
  102. self.status = 'error'
  103. self.message = (e && e.message) || '网络异常,请重试'
  104. })
  105. },
  106. goLogin() {
  107. uni.navigateTo({ url: '/pages/login/login' })
  108. },
  109. goHome() {
  110. uni.switchTab({ url: '/pages/index/index' })
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped>
  116. .page {
  117. padding: 40rpx;
  118. min-height: 100vh;
  119. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  120. display: flex;
  121. align-items: flex-start;
  122. justify-content: center;
  123. }
  124. .card {
  125. width: 100%;
  126. max-width: 600rpx;
  127. margin-top: 16vh;
  128. background: #FFFFFF;
  129. border-radius: 24rpx;
  130. padding: 60rpx 40rpx;
  131. display: flex;
  132. flex-direction: column;
  133. align-items: center;
  134. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
  135. }
  136. .icon {
  137. font-size: 96rpx;
  138. margin-bottom: 24rpx;
  139. }
  140. .title {
  141. font-size: 36rpx;
  142. font-weight: 700;
  143. color: #1E293B;
  144. margin-bottom: 16rpx;
  145. }
  146. .desc {
  147. font-size: 28rpx;
  148. color: #64748B;
  149. text-align: center;
  150. line-height: 1.6;
  151. margin-bottom: 40rpx;
  152. }
  153. .btn {
  154. width: 100%;
  155. padding: 28rpx;
  156. border-radius: 16rpx;
  157. font-size: 30rpx;
  158. font-weight: 600;
  159. text-align: center;
  160. border: none;
  161. }
  162. .btn-primary {
  163. background: linear-gradient(135deg, #F97316, #EA580C);
  164. color: #FFFFFF;
  165. }
  166. </style>