菜单

监测模块配置说明

下载

逻辑流程

  1. 访问第三方应用
  2. 判断是否有认证数据,一般常用token
    1. 如果有认证数据,则调用第三方接口获取用户信息
      1. 通过第三方用户信息获取nVisual系统用户信息
      2. 获取nVisual用户信息作sso登录
    2. 如果没有认证数据,则跳转至第三方的sso认证页面
      1. 登录成功后跳回第三方应用

nVisual获取用户信息接口

  1. url
    /wapi/v1/getLoginInfo/username/${name}

  2. 回参

    复制代码
    "code": 200,
    "data : {
    	"access_token": "xxxxx",
    	"authority": "xxxxx",
    	"username": "xxxxx",
    	"userId": "xxxxx"
    },
    "message": "success"

nVisual sso登录说明

  1. 参数说明
    userToken: 用户令牌
    username: 用户名
    userId: 用户ID
    authority: 用户权限
  2. 页面跳转URL
    protocol://host/diagram.html?id=24000000000001&userToken=xxxxx&username=xxxxx&userId=xxxxx&authority=xxxxx

代码示例sso.html

复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
</head>
<body>
  <script>
	  // 获取cookie
    function getCookie(name) {
      let cookieArray = document.cookie.split(";"); // 将cookie字符串分割成数组  
      let cookieName = name + "="; // 要查找的cookie名,添加等号  

      for (let i = 0; i < cookieArray.length; i++) {
        let c = cookieArray[i];
        while (c.charAt(0) === " ") {
          c = c.substring(1); // 去除可能的空格  
        }
        if (c.indexOf(cookieName) === 0) { // 如果找到了cookie名  
          return c.substring(cookieName.length, c.length); // 返回cookie的值  
        }
      }
      return ""; // 如果没有找到cookie,返回空字符串  
    }

		// 跳转至第三方sso认证
    const redirectSso = () => {
      const currentUrl = location.href;
      const ssoUrl = 'xxxxx';
      const redirectUrl = `${ssoUrl}?${encodeURIComponent(currentUrl)}`;
      location.href = redirectUrl;
    }

		// 获取第三方用户信息
    const fetchCurrentUser = async () => {
      const currentUserApiUrl = 'xxxxx';
      const response = await fetch(currentUserApiUrl, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'REQUEST_HEADER': 'binary-http-client-header'
        },
        body: {}
      })
      return new Promise((resolve, reject) => {
        if (response.ok) {
          response.json()
            .then(responseData => {
              if (responseData.success) {
                resolve(responseData.data);
              } else {
                reject(null);
              }
            })
            .catch(err => { reject(err) })
        } else {
          reject(response)
        }
      })
    }

    const init = async () => {
      // 获取token 通过localStorage或cookie
      const token = localStorage.getItem('tk');
      // const token = getCookie("token");

      if (token) {
        const user = await fetchCurrentUser();
        if (user) {
          const response = await fetch(`/wapi/v1/getLoginInfo/username/${user.name}`)
            .then((res) => {
              return res.json();
            })
            .then((res) => {
              const userToken = res.data.access_token;
              const username = res.data.username;
              const userId = res.data.userId;
              const authority = res.data.authority;
              location.href = `protocol://host/diagram.html?id=24000000000001&userToken=${userToken}&username=${username}&userId=${userId}&authority=${authority}`;
            })
        } else {
          redirectSso();
        }
      } else {
        redirectSso();
      }
    }

    init();
  </script>
</body>

</html>

nginx相关配置

  1. 第三方

    复制代码
    location /nVisual {
    	proxy_pass sso.html
    }
  2. nVisual

    复制代码
    location /access.html {
    	proxy_pass 第三方认证页面
    }
最近修改: 2024-11-04