login.vue 1.1 KB

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