scan-login.vue 4.4 KB

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