dev_login.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. const token = route.query.token
  15. let targetUrl = route.query.targetUrl ? route.query.targetUrl : '/home';
  16. // 先从 session 获取保存的角色
  17. let savedRole = null;
  18. if (!token) {
  19. const { data } = await VE_API.system.devLogin({ userId: userId });
  20. store.dispatch(`app/${SET_TOKEN}`, data.token);
  21. store.dispatch(`app/${SET_USER_ROLES}`, data.roles);
  22. const sessionRes = await VE_API.mine.getSessionStore({ key: "cur_role" });
  23. if (sessionRes && sessionRes.data) {
  24. savedRole = sessionRes.data.val;
  25. }
  26. // 优先使用保存的角色,如果保存的角色在 roles 中,则使用保存的角色,否则如果 roles 中包含 2,赋值 2,否则赋值 0
  27. // let curRole = 0;
  28. // if (savedRole !== null && Array.isArray(data.roles) && data.roles.includes(savedRole)) {
  29. // curRole = savedRole;
  30. // }
  31. // store.dispatch(`app/${SET_USER_CUR_ROLE}`, curRole);
  32. store.dispatch(`app/${SET_USER_CUR_ROLE}`, data.roles[data.roles.length - 1]);
  33. localStorage.setItem("localUserInfo", JSON.stringify(data));
  34. }
  35. // 存userId
  36. let pointXy = route.query.point.split(",")
  37. // 刷新后仍保留id和point
  38. localStorage.setItem("MINI_USER_ID", userId)
  39. route.query?.userInfo && localStorage.setItem("localUserInfo", route.query.userInfo);
  40. if(route.query.roles){
  41. const roles = JSON.parse(route.query.roles);
  42. store.dispatch(`app/${SET_USER_ROLES}`, roles);
  43. const sessionRes = await VE_API.mine.getSessionStore({ key: "cur_role" });
  44. if (sessionRes && sessionRes.data) {
  45. savedRole = sessionRes.data.val;
  46. }
  47. // 优先使用保存的角色,如果保存的角色在 roles 中,则使用保存的角色,否则如果 roles 中包含 2,赋值 2,否则赋值 0
  48. // let curRole = 0;
  49. // if (savedRole !== null && Array.isArray(roles) && roles.includes(savedRole)) {
  50. // curRole = savedRole;
  51. // }
  52. // store.dispatch(`app/${SET_USER_CUR_ROLE}`, curRole);
  53. store.dispatch(`app/${SET_USER_CUR_ROLE}`, roles[roles.length - 1]);
  54. }
  55. // 进入首页时请求接口,确定是否为托管农户
  56. await fetchUserType();
  57. await getFarmList(() => {
  58. targetUrl = '/create_farm?type=farmer&expertMiniUserId=81881&isReload=true';
  59. });
  60. localStorage.setItem("MINI_USER_LOCATION", route.query.point)
  61. localStorage.setItem("MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`)
  62. store.commit("home/SET_MINI_USER_LOCATION", route.query.point);
  63. store.commit("home/SET_MINI_USER_ID", userId);
  64. store.commit("home/SET_MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`);
  65. router.push(`${targetUrl}?miniJson=${JSON.stringify(route.query)}`);
  66. })
  67. const getFarmList = async (callback) => {
  68. localStorage.removeItem('selectedFarmId');
  69. localStorage.removeItem('selectedFarmName');
  70. const { data } = await VE_API.farm.userFarmSelectOption();
  71. if(data && data.length > 0) {
  72. const defalutFarm = data[0]
  73. localStorage.setItem('selectedFarmId', defalutFarm.id);
  74. localStorage.setItem('selectedFarmName', defalutFarm.name);
  75. } else {
  76. callback();
  77. }
  78. }
  79. // 获取用户是否为托管农户,并缓存 USER_TYPE
  80. const fetchUserType = async () => {
  81. const { data } = await VE_API.farm
  82. .userFarmSelectOption({ userType: 2 })
  83. if (Array.isArray(data) && data.length > 0) {
  84. localStorage.setItem("USER_TYPE", "2");
  85. } else {
  86. localStorage.setItem("USER_TYPE", "1");
  87. }
  88. };
  89. </script>
  90. <style scoped></style>