index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view>
  3. <Navbar title="扫码登录网页" :textLeft="false" />
  4. <layout-page>
  5. <view :style="{'padding-top': navbarHeight + 'px'}">
  6. <view class="commonBackground"></view>
  7. <view class="defaultBgc" :style="{'height': `calc(100vh - ${navbarHeight}px)`}">
  8. <view class="logo">
  9. <image src="https://minio.menduner.com/dev/19cce88e87fb8e895fbbe4418a937d610b2c20b8f87c6705569a096aca0a91b0.png"></image>
  10. </view>
  11. <button v-if="useUserStore.isLogin" class="recomm-button MiSans-Medium" @click="handleTokenLogin">一键登录</button>
  12. <button v-else class="recomm-button MiSans-Medium" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键登录</button>
  13. <view class="agreement-box ss-flex ss-row-center">
  14. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  15. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  16. <span class="MiSans-Normal">我已阅读并遵守</span>
  17. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('user')">
  18. 《用户协议》
  19. </view>
  20. <view class="agreement-text MiSans-Normal">和</view>
  21. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('privacy')">
  22. 《隐私协议》
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </layout-page>
  29. </view>
  30. </template>
  31. <script setup>
  32. import { ref, onMounted } from 'vue'
  33. import layoutPage from '@/layout'
  34. import { userStore } from '@/store/user'
  35. import { onLoad } from '@dcloudio/uni-app'
  36. import { weixinLoginAuthorize } from '@/api/qrcodeLogin.js'
  37. import { checkPersonBaseInfo } from '@/utils/check'
  38. import { showAuthModal, closeAuthModal } from '@/hooks/useModal'
  39. import Navbar from '@/components/Navbar'
  40. const useUserStore = userStore()
  41. const protocol = ref(false)
  42. onLoad((options) => {
  43. if (!useUserStore.isLogin || !uni.getStorageSync('token') || !useUserStore.refreshToken) {
  44. console.log('没登录时不需要弹窗');
  45. closeAuthModal()
  46. }
  47. console.log(options?.scene, '===========options')
  48. if (options?.scene) {
  49. uni.setStorageSync('wxLoginCode', options.scene)
  50. }
  51. })
  52. const navbarHeight = ref(0)
  53. onMounted(async () => {
  54. try {
  55. const res = await new Promise((resolve, reject) => {
  56. uni.getStorage({
  57. key: 'navbarHeight',
  58. success: (result) => resolve(result),
  59. fail: (err) => reject(err)
  60. })
  61. })
  62. navbarHeight.value = res.data
  63. console.log(navbarHeight.value, '扫码授权-导航栏高度')
  64. } catch (error) {
  65. console.error('读取本地缓存出错:', error)
  66. }
  67. })
  68. // 查看协议详情
  69. const handleToDetail = (type) => {
  70. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  71. uni.navigateTo({
  72. url
  73. })
  74. }
  75. // 已登录的授权给网页
  76. const handleTokenLogin = async () => {
  77. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none', duration: 2000 })
  78. const data = useUserStore.baseInfo
  79. if (!data || !Object.keys(data).length || data.type === undefined || data.type === null) {
  80. console.log('需要完善基本信息')
  81. showAuthModal('selectUserType')
  82. return
  83. }
  84. const necessaryInfoReady = checkPersonBaseInfo(data)
  85. console.log(necessaryInfoReady, '基本信息是否填写完善', data)
  86. if (necessaryInfoReady) {
  87. console.log('信息填写完善-授权网页登录')
  88. closeAuthModal()
  89. if (!uni.getStorageSync('wxLoginCode')) {
  90. return uni.showToast({ title: '请重新扫码进行授权', icon: 'none', duration: 2000 })
  91. }
  92. try {
  93. await weixinLoginAuthorize({ uuid: uni.getStorageSync('wxLoginCode') })
  94. uni.showToast({ title: '授权网页登录成功', duration: 2000, icon: 'none' })
  95. uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'student')
  96. setTimeout(() => {
  97. uni.switchTab({
  98. url: '/pages/index/my'
  99. })
  100. }, 2000)
  101. } catch (error) {
  102. console.log(error, '授权接口error')
  103. } finally {
  104. uni.removeStorageSync('wxLoginCode')
  105. }
  106. }
  107. else showAuthModal('necessaryInfo')
  108. // uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'student')
  109. }
  110. // 微信登录
  111. const getPhoneNumber = async (e) => {
  112. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none', duration: 2000 })
  113. if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
  114. uni.showToast({ title: '微信登录失败', icon: 'none' })
  115. return
  116. }
  117. wx.login({
  118. success: async (result) => {
  119. const query = {
  120. loginCode: result?.code || '',
  121. phoneCode: e.detail.code,
  122. state: e.detail.encryptedData,
  123. autoRegister: true
  124. }
  125. await useUserStore.handleSmsLogin(query, 0)
  126. handleTokenLogin()
  127. },
  128. fail:(res)=> { console.log("获取登录凭证code失败!", res) }
  129. })
  130. }
  131. </script>
  132. <style scoped lang="scss">
  133. .logo {
  134. display: flex;
  135. align-items: center;
  136. image {
  137. width: 85px;
  138. height: 85px;
  139. border-radius: 6px;
  140. margin: 20vh auto 8vh auto;
  141. z-index: 1;
  142. }
  143. }
  144. </style>