login-authorization-v2 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@
16
16
  /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
17
17
 
18
18
  "use strict";
19
- eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"init\": () => (/* binding */ init),\n/* harmony export */ \"getUserInfo\": () => (/* binding */ getUserInfo),\n/* harmony export */ \"getIdToken\": () => (/* binding */ getIdToken),\n/* harmony export */ \"setIdToken\": () => (/* binding */ setIdToken),\n/* harmony export */ \"getRefreshToken\": () => (/* binding */ getRefreshToken),\n/* harmony export */ \"setRefreshToken\": () => (/* binding */ setRefreshToken),\n/* harmony export */ \"clearLoginCookie\": () => (/* binding */ clearLoginCookie),\n/* harmony export */ \"refreshIdTokenTimer\": () => (/* binding */ refreshIdTokenTimer),\n/* harmony export */ \"logout\": () => (/* binding */ logout),\n/* harmony export */ \"setCookie\": () => (/* binding */ setCookie),\n/* harmony export */ \"getCookie\": () => (/* binding */ getCookie),\n/* harmony export */ \"getUrlParam\": () => (/* binding */ getUrlParam)\n/* harmony export */ });\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_0__);\n\r\nlet tokenTimer = null;\r\nlet idTokenFront = null;\r\nlet idTokenBack = null;\r\nlet refreshTokenFront = null;\r\nlet refreshTokenBack = null;\r\nlet userRequestUrl = null;\r\nlet userLogoutUrl = null;\r\nlet tenantId = '';\r\nlet currentSystemType = 'client';\r\n// 获取、设置cookie\r\nfunction getCookie (c_name) {\r\n if (document.cookie.length > 0) {\r\n let c_start = document.cookie.indexOf(c_name + \"=\")\r\n if (c_start != -1) {\r\n c_start = c_start + c_name.length + 1\r\n let c_end = document.cookie.indexOf(\";\", c_start)\r\n if (c_end == -1) c_end = document.cookie.length\r\n return unescape(document.cookie.substring(c_start, c_end))\r\n }\r\n }\r\n return null\r\n}\r\nfunction setCookie (name, value, domain, path = '/', time = 30 * 24 * 60 * 60 * 1000) {\r\n let exp = new Date();\r\n exp.setTime(exp.getTime() + time);\r\n document.cookie = name + \"=\" + escape(value) + \";expires=\" + exp.toGMTString() + \";domain=\" + domain + \";path=\" + path;\r\n}\r\n// 获取url参数中的某个值\r\nfunction getUrlParam (urlStr) {\r\n var query = window.location.href.split('?')[1];\r\n var vars = query ? query.split(\"&\") : [];\r\n for (var i = 0; i < vars.length; i++) {\r\n var pair = vars[i].split(\"=\");\r\n if (pair[0] == urlStr) { return pair[1]; }\r\n }\r\n return null;\r\n}\r\n\r\nif (getCookie('idTokenFront')) {\r\n idTokenFront = getCookie('idTokenFront');\r\n}\r\nif (getCookie('idTokenBack')) {\r\n idTokenBack = getCookie('idTokenBack');\r\n}\r\nif (getCookie('refreshTokenFront')) {\r\n refreshTokenFront = getCookie('refreshTokenFront');\r\n}\r\nif (getCookie('refreshTokenBack')) {\r\n refreshTokenBack = getCookie('refreshTokenBack');\r\n}\r\n// 获取用户信息\r\nfunction getUserInfo () {\r\n if (idTokenBack) {\r\n return JSON.parse(atob(idTokenBack.split('.')[0]))\r\n } else {\r\n return null\r\n }\r\n}\r\n// 获取idToken\r\nfunction getIdToken () {\r\n return (idTokenFront && idTokenBack) ? (idTokenFront + '.' + idTokenBack) : null\r\n}\r\n// 设置idToken\r\nfunction setIdToken (value) {\r\n idTokenFront = value.split('.')[0] + '.' + value.split('.')[1];\r\n idTokenBack = value.split('.')[2];\r\n setCookie('idTokenFront', idTokenFront, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n setCookie('idTokenBack', idTokenBack, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n}\r\n// 获取refreshToken\r\nfunction getRefreshToken () {\r\n return (refreshTokenFront && refreshTokenBack) ? (refreshTokenFront + '.' + refreshTokenBack) : null\r\n}\r\n// 设置refreshToken\r\nfunction setRefreshToken (value) {\r\n refreshTokenFront = value.split('.')[0] + '.' + value.split('.')[1];\r\n refreshTokenBack = value.split('.')[2] + '.' + value.split('.')[3] + '.' + value.split('.')[4];\r\n setCookie('refreshTokenFront', refreshTokenFront, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n setCookie('refreshTokenBack', refreshTokenBack, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n}\r\n// 清空登录相关cookie\r\nfunction clearLoginCookie () {\r\n if (currentSystemType === getCookie('currentSystemType')) {\r\n setCookie('idTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('idTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n } else if (!getCookie('currentSystemType')) {\r\n setCookie('idTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('idTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n }\r\n}\r\n// 定时刷新IdToken\r\nfunction refreshIdTokenTimer (time) {\r\n if (!getIdToken() || !getRefreshToken()) {\r\n return;\r\n }\r\n tokenTimer = setInterval(() => {\r\n axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n url: userRequestUrl + '/user-profile/refresh-token/refresh',\r\n method: 'post',\r\n data: {\r\n refreshToken: getRefreshToken()\r\n }\r\n }).then(res => {\r\n if (res.data.code === 200) {\r\n setIdToken(res.data.content.idToken);\r\n } else if (res.data.code === 500) {\r\n if (userLogoutUrl !== '' || userLogoutUrl !== undefined) {\r\n clearLoginCookie();\r\n window.location.href = userLogoutUrl;\r\n }\r\n }\r\n })\r\n }, time)\r\n}\r\n// 登出\r\nfunction logout () {\r\n return new Promise((resolve, reject) => {\r\n axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n url: userRequestUrl + '/user-profile/logout',\r\n method: 'post',\r\n data: {\r\n refreshToken: getRefreshToken()\r\n }\r\n }).then(res => {\r\n clearInterval(tokenTimer);\r\n clearLoginCookie();\r\n if (userLogoutUrl !== '' || userLogoutUrl !== undefined) {\r\n window.location.href = userLogoutUrl;\r\n }\r\n resolve(true)\r\n })\r\n })\r\n}\r\n// 无权限访问时弹窗显示\r\nfunction createDoalog(){\r\n let html = `<div id=\"confirm-container\" style=\"box-sizing:border-box;position:fixed;width:615px;height:145px;box-shadow: 0 0 2px 2px #eeeeee;border-radius: 5px;z-index: 10000000;display: block;left: 35%;padding:20px 20px;top:10px;font-size: 16px;\">\r\n <div id=\"href\" style=\"color: rgb(32,33,36);\"></div>\r\n <div style=\"margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;\">You do not have access to the system.</div>\r\n <div style=\"display: flex;justify-content: flex-end;\">\r\n <button id=\"cancel\" style=\"padding: 8px 15px;background: #ffffff;color:rgb(26,115,232);border:1px solid #ccc;border-radius:5px;cursor: pointer;font-size:14px;\">Cancel</button>\r\n <button id=\"confirm\" style=\"padding: 8px 15px;background: rgb(26,115,232);color:#ffffff;border:none;border-radius:5px;margin-left: 10px;cursor: pointer;font-size:14px;\">Sign in to another ZERO account</button>\r\n </div>\r\n </div>`\r\n let confirmDialog=document.getElementsByTagName('body')[0];\r\n confirmDialog.innerHTML = '';\r\n confirmDialog.insertAdjacentHTML(\"beforeend\", html);\r\n document.getElementById('href').innerHTML = window.location.hostname;\r\n document.getElementById('confirm').onclick = confirms;\r\n document.getElementById('cancel').onclick = cancels;\r\n}\r\nfunction createDoalogMobile(){\r\n let html = `<div id=\"confirm-container\" style=\"box-sizing:border-box;position:fixed;width:97.6vw;height:145px;box-shadow: 0 0 2px 2px #eeeeee;border-radius: 5px;z-index: 10000000;display: block;left:0px;padding:20px 20px;top:4px;margin:0 4px;font-size: 16px;\">\r\n <div id=\"href\" style=\"color: rgb(32,33,36);\"></div>\r\n <div style=\"margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;\">You do not have access to the system.</div>\r\n <div style=\"display: flex;justify-content: flex-end;\">\r\n <div style=\"display: flex;justify-content: flex-end;\">\r\n <button id=\"cancel\" style=\"padding: 8px 15px;background: #ffffff;color:rgb(26,115,232);border:1px solid #ccc;border-radius:5px;cursor: pointer;font-size:14px;\">Cancel</button>\r\n <button id=\"confirm\" style=\"padding: 8px 15px;background: rgb(26,115,232);color:#ffffff;border:none;border-radius:5px;margin-left: 10px;cursor: pointer;font-size:14px;\">Sign in to another ZERO account</button>\r\n </div>\r\n </div>\r\n </div>`\r\n let confirmDialog=document.getElementsByTagName('body')[0];\r\n confirmDialog.innerHTML = '';\r\n confirmDialog.insertAdjacentHTML(\"beforeend\", html);\r\n document.getElementById('href').innerHTML = window.location.hostname;\r\n document.getElementById('confirm').onclick = confirms;\r\n document.getElementById('cancel').onclick = cancels;\r\n}\r\nfunction confirms() {\r\n clearLoginCookie();\r\n document.getElementById('confirm-container').style.display = 'none';\r\n if (window.location.href.indexOf('hedgehood')!==-1){\r\n window.location.href = currentSystemType === 'staff' ? 'https://login' + window.location.hostname.substring(window.location.hostname.indexOf('.')) + '/login' : 'https://trader' + window.location.hostname.substring(window.location.hostname.indexOf('.'));\r\n } else if (window.location.href.indexOf('ddmarketinghub')!==-1) {\r\n window.location.href = \"http://huangcheng.ddmarketinghub.com:8080\";\r\n } else {\r\n window.location.href = currentSystemType === 'staff' ? 'https://login' + window.location.hostname.substring(window.location.hostname.indexOf('.')) + '/login' : 'https://trader' + window.location.hostname.substring(window.location.hostname.indexOf('.'));\r\n }\r\n}\r\nfunction cancels() {\r\n document.getElementById('confirm-container').style.display = 'none';\r\n window.location.href = 'about:blank';\r\n}\r\nfunction IsPC () {\r\n var userAgentInfo = navigator.userAgent;\r\n var Agents = [\r\n 'Android',\r\n 'iPhone',\r\n 'SymbianOS',\r\n 'Windows Phone',\r\n 'iPad',\r\n 'iPod',\r\n ];\r\n var flag = true;\r\n for (var v = 0; v < Agents.length; v++) {\r\n if (userAgentInfo.indexOf(Agents[v]) > 0) {\r\n flag = false;\r\n break;\r\n }\r\n }\r\n return flag;\r\n}\r\n// 获取可访问系统列表\r\nfunction systemLists (systemName, idToken) {\r\n return new Promise((resolve, reject) => {\r\n axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n url: userRequestUrl + '/session/current/servers',\r\n method: 'get',\r\n headers: {\r\n authorization: 'Bearer ' + idToken,\r\n 'X-tenant-id': tenantId ? tenantId : ''\r\n }\r\n }).then(res => {\r\n if (res.data.code === 200) {\r\n let lists = res.data.content;\r\n if (!systemName) {\r\n IsPC() ? createDoalog() : createDoalogMobile()\r\n } else if (systemName === 'commonLogin') {\r\n resolve(true);\r\n } else if (lists.length === 0) {\r\n IsPC() ? createDoalog() : createDoalogMobile()\r\n } else {\r\n let hasAccess = false;\r\n lists.forEach(item => {\r\n if (systemName.toLowerCase() === item.groupName.toLowerCase()) {\r\n resolve(true);\r\n hasAccess = true;\r\n currentSystemType = item.staffEndpoint ? 'staff' : 'client';\r\n }\r\n })\r\n if (!hasAccess) {\r\n IsPC() ? createDoalog() : createDoalogMobile()\r\n }\r\n }\r\n } else {\r\n resolve(false);\r\n }\r\n }).catch(() => {\r\n resolve(true);\r\n })\r\n })\r\n}\r\n// init初始化\r\nasync function init (requestUrl, loginUrl, systemName, tenant, time = 1000 * 60 * 3) {\r\n tenantId = tenant ? tenant : '';\r\n userLogoutUrl = loginUrl;\r\n return new Promise(async (resolve, reject) => {\r\n if (!getIdToken() || !getRefreshToken()) {\r\n resolve(false);\r\n }\r\n if (requestUrl) {\r\n userRequestUrl = requestUrl;\r\n refreshIdTokenTimer(time);\r\n if (systemName === 'App') {\r\n resolve(true);\r\n } else {\r\n let hasAuth = await systemLists(systemName, getIdToken());\r\n if (hasAuth) {\r\n resolve(true);\r\n } else {\r\n await logout();\r\n resolve(false);\r\n }\r\n }\r\n } else {\r\n alert('Not request url');\r\n reject();\r\n }\r\n })\r\n}\r\n\n\n//# sourceURL=webpack://login-authorization-v2/./index.js?");
19
+ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"init\": () => (/* binding */ init),\n/* harmony export */ \"getUserInfo\": () => (/* binding */ getUserInfo),\n/* harmony export */ \"getIdToken\": () => (/* binding */ getIdToken),\n/* harmony export */ \"setIdToken\": () => (/* binding */ setIdToken),\n/* harmony export */ \"getRefreshToken\": () => (/* binding */ getRefreshToken),\n/* harmony export */ \"setRefreshToken\": () => (/* binding */ setRefreshToken),\n/* harmony export */ \"clearLoginCookie\": () => (/* binding */ clearLoginCookie),\n/* harmony export */ \"refreshIdTokenTimer\": () => (/* binding */ refreshIdTokenTimer),\n/* harmony export */ \"logout\": () => (/* binding */ logout),\n/* harmony export */ \"setCookie\": () => (/* binding */ setCookie),\n/* harmony export */ \"getCookie\": () => (/* binding */ getCookie),\n/* harmony export */ \"getUrlParam\": () => (/* binding */ getUrlParam)\n/* harmony export */ });\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_0__);\n\r\nlet tokenTimer = null;\r\nlet idTokenFront = null;\r\nlet idTokenBack = null;\r\nlet refreshTokenFront = null;\r\nlet refreshTokenBack = null;\r\nlet userRequestUrl = null;\r\nlet userLogoutUrl = null;\r\nlet tenantId = '';\r\nlet currentSystemType = 'client';\r\nlet menuLists = [];\r\n// 获取、设置cookie\r\nfunction getCookie (c_name) {\r\n if (document.cookie.length > 0) {\r\n let c_start = document.cookie.indexOf(c_name + \"=\")\r\n if (c_start != -1) {\r\n c_start = c_start + c_name.length + 1\r\n let c_end = document.cookie.indexOf(\";\", c_start)\r\n if (c_end == -1) c_end = document.cookie.length\r\n return unescape(document.cookie.substring(c_start, c_end))\r\n }\r\n }\r\n return null\r\n}\r\nfunction setCookie (name, value, domain, path = '/', time = 30 * 24 * 60 * 60 * 1000) {\r\n let exp = new Date();\r\n exp.setTime(exp.getTime() + time);\r\n document.cookie = name + \"=\" + escape(value) + \";expires=\" + exp.toGMTString() + \";domain=\" + domain + \";path=\" + path;\r\n}\r\n// 获取url参数中的某个值\r\nfunction getUrlParam (urlStr) {\r\n var query = window.location.href.split('?')[1];\r\n var vars = query ? query.split(\"&\") : [];\r\n for (var i = 0; i < vars.length; i++) {\r\n var pair = vars[i].split(\"=\");\r\n if (pair[0] == urlStr) { return pair[1]; }\r\n }\r\n return null;\r\n}\r\n\r\nif (getCookie('idTokenFront')) {\r\n idTokenFront = getCookie('idTokenFront');\r\n}\r\nif (getCookie('idTokenBack')) {\r\n idTokenBack = getCookie('idTokenBack');\r\n}\r\nif (getCookie('refreshTokenFront')) {\r\n refreshTokenFront = getCookie('refreshTokenFront');\r\n}\r\nif (getCookie('refreshTokenBack')) {\r\n refreshTokenBack = getCookie('refreshTokenBack');\r\n}\r\n// 获取用户信息\r\nfunction getUserInfo () {\r\n if (idTokenBack) {\r\n return JSON.parse(atob(idTokenBack.split('.')[0]))\r\n } else {\r\n return null\r\n }\r\n}\r\n// 获取idToken\r\nfunction getIdToken () {\r\n if (idTokenFront && idTokenBack) {\r\n idTokenFront = getCookie('idTokenFront');\r\n idTokenBack = getCookie('idTokenBack');\r\n }\r\n return (idTokenFront && idTokenBack) ? (idTokenFront + '.' + idTokenBack) : null\r\n}\r\n// 设置idToken\r\nfunction setIdToken (value) {\r\n idTokenFront = value.split('.')[0] + '.' + value.split('.')[1];\r\n idTokenBack = value.split('.')[2];\r\n setCookie('idTokenFront', idTokenFront, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n setCookie('idTokenBack', idTokenBack, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n}\r\n// 获取refreshToken\r\nfunction getRefreshToken () {\r\n return (refreshTokenFront && refreshTokenBack) ? (refreshTokenFront + '.' + refreshTokenBack) : null\r\n}\r\n// 设置refreshToken\r\nfunction setRefreshToken (value) {\r\n refreshTokenFront = value.split('.')[0] + '.' + value.split('.')[1];\r\n refreshTokenBack = value.split('.')[2] + '.' + value.split('.')[3] + '.' + value.split('.')[4];\r\n setCookie('refreshTokenFront', refreshTokenFront, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n setCookie('refreshTokenBack', refreshTokenBack, window.location.hostname.substring(window.location.hostname.indexOf('.')));\r\n}\r\n// 清空登录相关cookie\r\nfunction clearLoginCookie () {\r\n if (currentSystemType === getCookie('currentSystemType')) {\r\n setCookie('idTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('idTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n } else if (!getCookie('currentSystemType')) {\r\n setCookie('idTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('idTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenFront', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n setCookie('refreshTokenBack', null, window.location.hostname.substring(window.location.hostname.indexOf('.')), '/', -1);\r\n }\r\n}\r\n// 定时刷新IdToken\r\nfunction refreshIdTokenTimer (time) {\r\n if (!getIdToken() || !getRefreshToken()) {\r\n return;\r\n }\r\n tokenTimer = setInterval(() => {\r\n axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n url: userRequestUrl + '/user-profile/refresh-token/refresh',\r\n method: 'post',\r\n data: {\r\n refreshToken: getRefreshToken()\r\n }\r\n }).then(res => {\r\n if (res.data.code === 200) {\r\n setIdToken(res.data.content.idToken);\r\n } else if (res.data.code === 500) {\r\n if (userLogoutUrl !== '' || userLogoutUrl !== undefined) {\r\n clearLoginCookie();\r\n window.location.href = userLogoutUrl;\r\n }\r\n }\r\n })\r\n }, time)\r\n}\r\n// 登出\r\nfunction logout () {\r\n return new Promise((resolve, reject) => {\r\n axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n url: userRequestUrl + '/user-profile/logout',\r\n method: 'post',\r\n data: {\r\n refreshToken: getRefreshToken()\r\n }\r\n }).then(res => {\r\n clearInterval(tokenTimer);\r\n clearLoginCookie();\r\n if (userLogoutUrl !== '' || userLogoutUrl !== undefined) {\r\n window.location.href = userLogoutUrl;\r\n }\r\n resolve(true)\r\n })\r\n })\r\n}\r\n// 无权限访问时弹窗显示\r\nfunction createDoalog(){\r\n let html = `<div id=\"confirm-container\" style=\"box-sizing:border-box;position:fixed;width:615px;height:145px;box-shadow: 0 0 2px 2px #eeeeee;border-radius: 5px;z-index: 10000000;display: block;left: 35%;padding:20px 20px;top:10px;font-size: 16px;\">\r\n <div id=\"href\" style=\"color: rgb(32,33,36);\"></div>\r\n <div style=\"margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;\">You do not have access to the system.</div>\r\n <div style=\"display: flex;justify-content: flex-end;\">\r\n <button id=\"cancel\" style=\"padding: 8px 15px;background: #ffffff;color:rgb(26,115,232);border:1px solid #ccc;border-radius:5px;cursor: pointer;font-size:14px;\">Cancel</button>\r\n <button id=\"confirm\" style=\"padding: 8px 15px;background: rgb(26,115,232);color:#ffffff;border:none;border-radius:5px;margin-left: 10px;cursor: pointer;font-size:14px;\">Sign in to another ZERO account</button>\r\n </div>\r\n </div>`\r\n let confirmDialog=document.getElementsByTagName('body')[0];\r\n confirmDialog.innerHTML = '';\r\n confirmDialog.insertAdjacentHTML(\"beforeend\", html);\r\n document.getElementById('href').innerHTML = window.location.hostname;\r\n document.getElementById('confirm').onclick = confirms;\r\n document.getElementById('cancel').onclick = cancels;\r\n}\r\nfunction createDoalogMobile(){\r\n let html = `<div id=\"confirm-container\" style=\"box-sizing:border-box;position:fixed;width:97.6vw;height:145px;box-shadow: 0 0 2px 2px #eeeeee;border-radius: 5px;z-index: 10000000;display: block;left:0px;padding:20px 20px;top:4px;margin:0 4px;font-size: 16px;\">\r\n <div id=\"href\" style=\"color: rgb(32,33,36);\"></div>\r\n <div style=\"margin: 10px 0 20px 0;color: rgb(32,33,36);font-size: 14px;\">You do not have access to the system.</div>\r\n <div style=\"display: flex;justify-content: flex-end;\">\r\n <div style=\"display: flex;justify-content: flex-end;\">\r\n <button id=\"cancel\" style=\"padding: 8px 15px;background: #ffffff;color:rgb(26,115,232);border:1px solid #ccc;border-radius:5px;cursor: pointer;font-size:14px;\">Cancel</button>\r\n <button id=\"confirm\" style=\"padding: 8px 15px;background: rgb(26,115,232);color:#ffffff;border:none;border-radius:5px;margin-left: 10px;cursor: pointer;font-size:14px;\">Sign in to another ZERO account</button>\r\n </div>\r\n </div>\r\n </div>`\r\n let confirmDialog=document.getElementsByTagName('body')[0];\r\n confirmDialog.innerHTML = '';\r\n confirmDialog.insertAdjacentHTML(\"beforeend\", html);\r\n document.getElementById('href').innerHTML = window.location.hostname;\r\n document.getElementById('confirm').onclick = confirms;\r\n document.getElementById('cancel').onclick = cancels;\r\n}\r\nfunction confirms() {\r\n clearLoginCookie();\r\n document.getElementById('confirm-container').style.display = 'none';\r\n window.location.href = userLogoutUrl;\r\n}\r\nfunction cancels() {\r\n document.getElementById('confirm-container').style.display = 'none';\r\n window.location.href = 'about:blank';\r\n}\r\nfunction IsPC () {\r\n var userAgentInfo = navigator.userAgent;\r\n var Agents = [\r\n 'Android',\r\n 'iPhone',\r\n 'SymbianOS',\r\n 'Windows Phone',\r\n 'iPad',\r\n 'iPod',\r\n ];\r\n var flag = true;\r\n for (var v = 0; v < Agents.length; v++) {\r\n if (userAgentInfo.indexOf(Agents[v]) > 0) {\r\n flag = false;\r\n break;\r\n }\r\n }\r\n return flag;\r\n}\r\n// 获取可访问系统列表\r\nfunction systemLists (systemName, idToken) {\r\n return new Promise((resolve, reject) => {\r\n axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n url: userRequestUrl + '/session/current/servers',\r\n method: 'get',\r\n headers: {\r\n authorization: 'Bearer ' + idToken,\r\n 'X-tenant-id': tenantId ? tenantId : ''\r\n }\r\n }).then(res => {\r\n if (res.data.code === 200) {\r\n menuLists = res.data.content;\r\n if (!systemName) {\r\n IsPC() ? createDoalog() : createDoalogMobile()\r\n } else if (systemName === 'commonLogin') {\r\n resolve(true);\r\n } else if (menuLists.length === 0) {\r\n IsPC() ? createDoalog() : createDoalogMobile()\r\n } else {\r\n let hasAccess = false;\r\n menuLists.forEach(item => {\r\n if (systemName.toLowerCase() === item.groupName.toLowerCase()) {\r\n resolve(true);\r\n hasAccess = true;\r\n currentSystemType = item.staffEndpoint ? 'staff' : 'client';\r\n }\r\n })\r\n if (!hasAccess) {\r\n IsPC() ? createDoalog() : createDoalogMobile()\r\n }\r\n }\r\n } else {\r\n resolve(false);\r\n }\r\n }).catch(() => {\r\n resolve(true);\r\n })\r\n })\r\n}\r\n// init初始化\r\nasync function init (requestUrl, loginUrl, systemName, tenant, time = 1000 * 60 * 3) {\r\n tenantId = tenant ? tenant : '';\r\n userLogoutUrl = loginUrl;\r\n return new Promise(async (resolve, reject) => {\r\n if (!getIdToken() || !getRefreshToken()) {\r\n resolve(false);\r\n }\r\n if (requestUrl) {\r\n userRequestUrl = requestUrl;\r\n refreshIdTokenTimer(time);\r\n if (systemName === 'App') {\r\n resolve(true);\r\n } else {\r\n let hasAuth = await systemLists(systemName, getIdToken());\r\n if (hasAuth) {\r\n resolve(true);\r\n } else {\r\n await logout();\r\n resolve(false);\r\n }\r\n }\r\n } else {\r\n alert('Not request url');\r\n reject();\r\n }\r\n })\r\n}\r\n\n\n//# sourceURL=webpack://login-authorization-v2/./index.js?");
20
20
 
21
21
  /***/ }),
22
22
 
package/index.js CHANGED
@@ -8,6 +8,7 @@ let userRequestUrl = null;
8
8
  let userLogoutUrl = null;
9
9
  let tenantId = '';
10
10
  let currentSystemType = 'client';
11
+ let menuLists = [];
11
12
  // 获取、设置cookie
12
13
  function getCookie (c_name) {
13
14
  if (document.cookie.length > 0) {
@@ -59,6 +60,10 @@ function getUserInfo () {
59
60
  }
60
61
  // 获取idToken
61
62
  function getIdToken () {
63
+ if (idTokenFront && idTokenBack) {
64
+ idTokenFront = getCookie('idTokenFront');
65
+ idTokenBack = getCookie('idTokenBack');
66
+ }
62
67
  return (idTokenFront && idTokenBack) ? (idTokenFront + '.' + idTokenBack) : null
63
68
  }
64
69
  // 设置idToken
@@ -174,13 +179,7 @@ function createDoalogMobile(){
174
179
  function confirms() {
175
180
  clearLoginCookie();
176
181
  document.getElementById('confirm-container').style.display = 'none';
177
- if (window.location.href.indexOf('hedgehood')!==-1){
178
- window.location.href = currentSystemType === 'staff' ? 'https://login' + window.location.hostname.substring(window.location.hostname.indexOf('.')) + '/login' : 'https://trader' + window.location.hostname.substring(window.location.hostname.indexOf('.'));
179
- } else if (window.location.href.indexOf('ddmarketinghub')!==-1) {
180
- window.location.href = "http://huangcheng.ddmarketinghub.com:8080";
181
- } else {
182
- window.location.href = currentSystemType === 'staff' ? 'https://login' + window.location.hostname.substring(window.location.hostname.indexOf('.')) + '/login' : 'https://trader' + window.location.hostname.substring(window.location.hostname.indexOf('.'));
183
- }
182
+ window.location.href = userLogoutUrl;
184
183
  }
185
184
  function cancels() {
186
185
  document.getElementById('confirm-container').style.display = 'none';
@@ -217,16 +216,16 @@ function systemLists (systemName, idToken) {
217
216
  }
218
217
  }).then(res => {
219
218
  if (res.data.code === 200) {
220
- let lists = res.data.content;
219
+ menuLists = res.data.content;
221
220
  if (!systemName) {
222
221
  IsPC() ? createDoalog() : createDoalogMobile()
223
222
  } else if (systemName === 'commonLogin') {
224
223
  resolve(true);
225
- } else if (lists.length === 0) {
224
+ } else if (menuLists.length === 0) {
226
225
  IsPC() ? createDoalog() : createDoalogMobile()
227
226
  } else {
228
227
  let hasAccess = false;
229
- lists.forEach(item => {
228
+ menuLists.forEach(item => {
230
229
  if (systemName.toLowerCase() === item.groupName.toLowerCase()) {
231
230
  resolve(true);
232
231
  hasAccess = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "login-authorization-v2",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "login authorization",
5
5
  "main": "index.js",
6
6
  "scripts": {