index.vue 1.41 KB
<!--
 * @Date: 2022-09-19 14:11:06
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-01-13 00:18:41
 * @FilePath: /xyxBooking-weapp/src/pages/auth/index.vue
 * @Description: 授权页
-->
<template>
  <view class="auth-page">
    <view class="loading">
      <view>正在授权登录...</view>
    </view>
  </view>
</template>

<script setup>
import Taro, { useDidShow } from '@tarojs/taro'
import { silentAuth, returnToOriginalPage } from '@/utils/authRedirect'

let last_try_at = 0
let has_shown_fail_modal = false
let has_failed = false

useDidShow(() => {
  if (has_failed) {
    return
  }
  const now = Date.now()
  if (now - last_try_at < 1200) {
    return
  }
  last_try_at = now

  /**
   * 尝试静默授权
   * - 授权成功后回跳到来源页
   * - 授权失败则跳转至授权页面
   */
  silentAuth()
    .then(() => returnToOriginalPage())
    .catch(async error => {
      has_failed = true
      if (has_shown_fail_modal) {
        return
      }
      has_shown_fail_modal = true
      await Taro.showModal({
        title: '提示',
        content: error?.message || '授权失败,请稍后再尝试',
        showCancel: false,
        confirmText: '我知道了'
      })
    })
})
</script>

<style lang="less">
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  .loading {
    text-align: center;
    color: #999;
  }
}
</style>