wechat-binding-authorize.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <script setup>
  2. import { getCurrentInstance, ref } from 'vue'
  3. import { onLoad, onReady, onUnload } from '@dcloudio/uni-app'
  4. import { isWechatAuthorizeUrl } from '@/api/wechat-notices'
  5. const instance = getCurrentInstance()
  6. const src = ref('')
  7. const errorText = ref('')
  8. let ready = false, pendingUrl = '', channel
  9. const receive = ({ authorizeUrl } = {}) => {
  10. if (!isWechatAuthorizeUrl(authorizeUrl)) { errorText.value = '授权地址无效,请返回重新绑定'; return }
  11. pendingUrl = authorizeUrl
  12. if (ready) src.value = pendingUrl
  13. }
  14. onLoad(() => {
  15. channel = instance?.proxy?.getOpenerEventChannel?.()
  16. if (!channel) { errorText.value = '请从关注服务号页面发起绑定'; return }
  17. channel.on('wechatBindingAuthorize', receive)
  18. })
  19. onReady(() => {
  20. ready = true
  21. if (pendingUrl) src.value = pendingUrl
  22. })
  23. onUnload(() => { channel?.off('wechatBindingAuthorize', receive) })
  24. const onError = () => { errorText.value = '微信授权页面打开失败,请返回后重试或联系管理员检查域名配置' }
  25. </script>
  26. <template>
  27. <view class="binding-authorization">
  28. <view v-if="errorText" class="binding-authorization__error">{{ errorText }}</view>
  29. <web-view v-else-if="src" :src="src" @error="onError" />
  30. <view v-else>正在打开微信授权…</view>
  31. </view>
  32. </template>
  33. <style scoped>
  34. .binding-authorization { padding: 40rpx 32rpx; color: #6b7b74; font-size: 28rpx; line-height: 1.7; }
  35. .binding-authorization__error { color: #b45309; }
  36. </style>