login.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!-- 微信公众号的登录回调页 -->
  2. <template>
  3. <!-- 空登陆页 -->
  4. <view />
  5. </template>
  6. <script setup>
  7. import sheep from '@/sheep';
  8. import {
  9. onLoad
  10. } from '@dcloudio/uni-app';
  11. import {
  12. closeAuthModal,
  13. showAuthModal
  14. } from '@/sheep/hooks/useModal';
  15. onLoad(async (options) => {
  16. // #ifdef H5
  17. // 将 search 参数赋值到 options 中,方便下面解析
  18. new URLSearchParams(location.search).forEach((value, key) => {
  19. options[key] = value;
  20. });
  21. const event = options.event;
  22. const code = options.code;
  23. const state = options.state;
  24. if (event === 'login') { // 场景一:登录
  25. const res = await sheep.$platform.useProvider().login(code, state);
  26. if (!res) {
  27. console.log("返回第一次登录了", res)
  28. showAuthModal("officialAccountFirstLogin")
  29. }
  30. } else if (event === 'bind') { // 场景二:绑定
  31. sheep.$platform.useProvider().bind(code, state);
  32. }
  33. // 检测 H5 登录回调
  34. let returnUrl = uni.getStorageSync('returnUrl');
  35. if (returnUrl) {
  36. uni.removeStorage('returnUrl');
  37. location.replace(returnUrl);
  38. } else {
  39. uni.switchTab({
  40. url: '/',
  41. });
  42. }
  43. // #endif
  44. });
  45. </script>