dev_login.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <div></div>
  3. </template>
  4. <script setup>
  5. import { useRoute, useRouter } from "vue-router";
  6. import { useStore } from "vuex";
  7. import { SET_TOKEN,SET_USER_ROLES,SET_USER_CUR_ROLE } from "@/store/modules/app/type";
  8. import { onMounted } from "vue";
  9. const router = useRouter();
  10. const route = useRoute();
  11. const store = useStore();
  12. let userId = route.query.userId;
  13. onMounted(async () => {
  14. await getFarmList();
  15. const token = route.query.token
  16. const targetUrl = route.query.targetUrl ? route.query.targetUrl : '/home';
  17. if (!token) {
  18. const { data } = await VE_API.system.devLogin({ userId: userId });
  19. store.dispatch(`app/${SET_TOKEN}`, data.token);
  20. store.dispatch(`app/${SET_USER_ROLES}`, data.roles);
  21. // 如果 roles 中包含 2,赋值 2,否则赋值 0
  22. const curRole = Array.isArray(data.roles) && data.roles.includes(2) ? 2 : 0;
  23. store.dispatch(`app/${SET_USER_CUR_ROLE}`, curRole);
  24. localStorage.setItem("localUserInfo", JSON.stringify(data));
  25. }
  26. // 存userId
  27. let pointXy = route.query.point.split(",")
  28. // 刷新后仍保留id和point
  29. localStorage.setItem("MINI_USER_ID", userId)
  30. route.query?.userInfo && localStorage.setItem("localUserInfo", route.query.userInfo);
  31. if(route.query.roles){
  32. const roles = JSON.parse(route.query.roles);
  33. store.dispatch(`app/${SET_USER_ROLES}`, roles);
  34. // 如果 roles 中包含 2,赋值 2,否则赋值 0
  35. const curRole = Array.isArray(roles) && roles.includes(2) ? 2 : 0;
  36. store.dispatch(`app/${SET_USER_CUR_ROLE}`, curRole);
  37. }
  38. localStorage.setItem("MINI_USER_LOCATION", route.query.point)
  39. localStorage.setItem("MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`)
  40. store.commit("home/SET_MINI_USER_LOCATION", route.query.point);
  41. store.commit("home/SET_MINI_USER_ID", userId);
  42. store.commit("home/SET_MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`);
  43. router.push(`${targetUrl}?miniJson=${JSON.stringify(route.query)}`);
  44. })
  45. const getFarmList = async () => {
  46. const { data } = await VE_API.farm.userFarmSelectOption();
  47. if(data && data.length > 0) {
  48. localStorage.setItem('isGarden', true);
  49. }
  50. }
  51. </script>
  52. <style scoped></style>