同域名下,两个网站通过cookie共享登录注册功能大概思路。

  1 // 说明:本文件代码以vue工程化项目为例,其他类型项目请自行微改!!!
  2 // 导入js-cookie依赖
  3 import Cookies from "js-cookie";
  4 // 操作浏览器本地存储
  5 const storage = window.localStorage;
  6 
  7 // 跳转到官网登录,登录成功后会根据redirect参数,跳回之前的地址。
  8 export function gotoLogin() {
  9   if (process.env.NODE_ENV === "development") {
 10     // 开发环境,需要在本地将官网的项目运行起来
 11     window.location.href =
 12       "http://localhost:8089/front/login/passwordlogin?redirect=" +
 13       window.location.href;
 14   } else {
 15     // 其他环境
 16     window.location.href =
 17       window.location.protocol +
 18       "//" +
 19       window.location.host +
 20       "/front/login/passwordlogin?redirect=" +
 21       window.location.href;
 22   }
 23 }
 24 // 跳转到官网注册,注册成功后会根据redirect参数,跳回之前的地址。
 25 export function gotoRegister() {
 26   if (process.env.NODE_ENV === "development") {
 27     // 开发环境,需要在本地将官网的项目运行起来
 28     window.location.href =
 29       "http://localhost:8089/front/register/inputmsisdn?redirect=" +
 30       window.location.href;
 31   } else {
 32     // 其他环境
 33     window.location.href =
 34       window.location.protocol +
 35       "//" +
 36       window.location.host +
 37       "/front/register/inputmsisdn?redirect=" +
 38       window.location.href;
 39   }
 40 }
 41 // 在本项目中获取用户是否登录及登陆后的用户信息
 42 /*
 43  * 权限相关工具函数
 44  */
 45 // !!!!!!!!!!!!!notice
 46 export const USER_AVATAR = "ticket_avatar";
 47 export const USER_TITLENAME = "ticket_titlename";
 48 export const USER_AUTH = "is_auth";
 49 export const COOKIE_AVATAR = "avatar";
 50 export const COOKIE_NICK = "nick";
 51 export const COOKIE_STATUS = "status";
 52 export const COOKIE_PHONE = "phone";
 53 
 54 // 判断用户是否登录 登录返回值为true,没登录返回值为false
 55 export function isLogin() {
 56   console.log("用户状态cookie:", Cookies.get("status"));
 57   if (
 58     Cookies.get("status") &&
 59     Cookies.get("status") === "0" &&
 60     Cookies.get("is_auth")
 61   ) {
 62     return true;
 63   }
 64   return false;
 65 }
 66 // 获取用户信息:头像,昵称,手机号,是否实名等
 67 export function getBasicUserInfo() {
 68   const u = {};
 69   u.avatar = Cookies.get(COOKIE_AVATAR);
 70   u.nick = Cookies.get(COOKIE_NICK);
 71   u.showPhone = Cookies.get(COOKIE_PHONE);
 72   u.isAuth = Cookies.get(USER_AUTH);
 73   return u;
 74 }
 75 
 76 // 退出登录,调用接口,删除cookie
 77 export function doLogout() {
 78   const url =
 79     window.location.protocol +
 80     "//" +
 81     window.location.host +
 82     "/passportservice/passport/logout";
 83   axios
 84     .get(url)
 85     .then((res) => {
 86       unAuth();
 87       console.log("退出登录", res);
 88       this.$router.push({
 89         name: "login",
 90       });
 91     })
 92     .catch((response) => {
 93       console.log(response);
 94     });
 95 }
 96 /*
 97  * 退出登录,删除cookie及storage
 98  *
 99  * */
100 export function unAuth() {
101   const opt = "";
102 
103   storage.removeItem(USER_AVATAR);
104   storage.removeItem(USER_TITLENAME);
105   storage.removeItem(COOKIE_PHONE);
106   storage.removeItem(USER_AUTH);
107   Cookies.remove(COOKIE_AVATAR, opt);
108   Cookies.remove(COOKIE_NICK, opt);
109   Cookies.remove(COOKIE_STATUS, opt);
110   Cookies.remove(COOKIE_PHONE, opt);
111   Cookies.remove(USER_AUTH, opt);
112 }
 1 // 官网注册登录页面,获取url中redirect参数,并在登录/注册成功后跳转回redirect对应的url
 2 
 3 // 1, 获取url中redirect参数的值,保存到浏览器sessionStorage中
 4 mounted() {
 5   const redirectURL = this.$route.query.redirect
 6   if (redirectURL) {
 7     window.sessionStorage.setItem('redirectUrl', redirectURL)
 8   }
 9 }
10 
11 // 2, 登录/注册成功后,判断sessionStorage中是否有redirectUrl,存在的话跳转redirectUrl,不存在则跳转到官网首页
12 const redirectURL = window.sessionStorage.getItem("redirectUrl");
13 if (redirectURL && redirectURL.indexOf("login") === -1) {
14   window.location.href = redirectURL;
15   window.sessionStorage.removeItem("redirectUrl");
16 } else {
17   return this.$router.push("/home");
18 }
原文地址:https://www.cnblogs.com/lml2017/p/15508397.html