index.vue 5.0 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) {
  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. const handleRemove = () => {
  76. uni.removeStorageSync('wxLoginCode')
  77. setTimeout(() => {
  78. uni.switchTab({
  79. url: '/pages/index/my'
  80. })
  81. }, 2000)
  82. }
  83. // 已登录的授权给网页
  84. const handleTokenLogin = async () => {
  85. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none', duration: 2000 })
  86. const data = useUserStore.baseInfo
  87. if (!data || !Object.keys(data).length || data.type === undefined || data.type === null) {
  88. console.log('需要完善基本信息')
  89. showAuthModal('selectUserType')
  90. return
  91. }
  92. const necessaryInfoReady = checkPersonBaseInfo(data)
  93. console.log(necessaryInfoReady, '基本信息是否填写完善', data)
  94. if (necessaryInfoReady) {
  95. console.log('信息填写完善-授权网页登录')
  96. closeAuthModal()
  97. if (!uni.getStorageSync('wxLoginCode')) {
  98. return uni.showToast({ title: '请重新扫码进行授权', icon: 'none', duration: 2000 })
  99. }
  100. try {
  101. await weixinLoginAuthorize({ uuid: uni.getStorageSync('wxLoginCode') })
  102. uni.showToast({ title: '授权网页登录成功', duration: 2000, icon: 'none' })
  103. handleRemove()
  104. } catch (error) {
  105. console.log(error, '授权接口error')
  106. handleRemove()
  107. }
  108. }
  109. else showAuthModal('necessaryInfo')
  110. uni.setStorageSync('necessaryInfoReady', necessaryInfoReady ? 'ready' : 'fddeaddc47868b')
  111. }
  112. // 微信登录
  113. const getPhoneNumber = async (e) => {
  114. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none', duration: 2000 })
  115. if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
  116. uni.showToast({ title: '微信登录失败', icon: 'none' })
  117. return
  118. }
  119. wx.login({
  120. success: async (result) => {
  121. const query = {
  122. loginCode: result?.code || '',
  123. phoneCode: e.detail.code,
  124. state: e.detail.encryptedData,
  125. autoRegister: true
  126. }
  127. await useUserStore.handleSmsLogin(query, 0)
  128. handleTokenLogin()
  129. },
  130. fail:(res)=> { console.log("获取登录凭证code失败!", res) }
  131. })
  132. }
  133. </script>
  134. <style scoped lang="scss">
  135. .logo {
  136. display: flex;
  137. align-items: center;
  138. image {
  139. width: 85px;
  140. height: 85px;
  141. border-radius: 6px;
  142. margin: 20vh auto 8vh auto;
  143. z-index: 1;
  144. }
  145. }
  146. </style>