Commit e5c4b2f231b86c44bde9d564ca4959b7e8ad8e42

Authored by ww
1 parent adff909a

perf: add getToken usage webview can not get token

... ... @@ -2,7 +2,7 @@ const GLOBAL_STORAGE_KEY = (() => {
2 2 const isDEV = location.href.includes('dev=1')
3 3 const DEVELOPMENT = 'DEVELOPMENT'
4 4 const PRODUCTION = 'PRODUCTION'
5   - return `UNDEFINED__${ isDEV ? DEVELOPMENT : PRODUCTION }__2.7.1__COMMON__LOCAL__KEY__`
  5 + return `UNDEFINED__${isDEV ? DEVELOPMENT : PRODUCTION}__2.7.1__COMMON__LOCAL__KEY__`
6 6 })()
7 7
8 8 const GLOBAL_TOKEN = (() => {
... ... @@ -12,11 +12,14 @@ const GLOBAL_TOKEN = (() => {
12 12 * @type {{JWT_TOKEN: {value: string}}}
13 13 */
14 14 const common = ls.get(GLOBAL_STORAGE_KEY)
15   - return common.JWT_TOKEN.value
  15 + return {
  16 + token: common && common.JWT_TOKEN && common.JWT_TOKEN.value,
  17 + refreshToken: common && common.JWT_TOKEN && common.JWT_TOKEN.value,
  18 + }
16 19 })()
17 20
18   -const GLOBAL_WS_URL = (() => {
  21 +const GLOBAL_WS_URL = () => {
19 22 const { host, href } = location
20 23 const reg = /^https/
21   - return `${ reg.test(href) ? 'wss' : 'ws' }://${ host }/api/ws/plugins/telemetry?token=${ GLOBAL_TOKEN }`
22   -})()
  24 + return `${reg.test(href) ? 'wss' : 'ws'}://${host}/api/ws/plugins/telemetry?token=${GLOBAL_TOKEN.token}`
  25 +}
... ...
... ... @@ -599,7 +599,6 @@ App.main = function(callback, createUi)
599 599 defHttp = createAxios({
600 600 timeout:10*1000,
601 601 baseURL:'/api',
602   - token: GLOBAL_TOKEN
603 602 })
604 603 // Logs uncaught errors
605 604 window.onerror = function(message, url, linenumber, colno, err)
... ... @@ -4991,33 +4990,69 @@ App.prototype.loadFile = function (id, sameWindow, file, success, force) {
4991 4990 if (urlParams['openInSameWin'] == '1' || navigator.standalone) {
4992 4991 sameWindow = true;
4993 4992 }
4994   - //调用API,获取保存的内容
4995   - defHttp.get('/yt/configuration/center/getConfigurationInfo/'+Editor.configurationId)
4996   - .then(function (response) {
4997   - Editor.configurationName = response.configurationName + ".drawio";
4998   - if(response.configurationContentList.length > 0){
4999   - response.configurationContentList.forEach((item)=>{
5000   - Editor.configurationContentId = item.id;
5001   - if(item.content){
5002   - Editor.defaultContent = item.content;
5003   - }else{
5004   - //默认空白内容
5005   - Editor.defaultContent = EditorUi.prototype.emptyDiagramXml;
5006   - }
5007   - })
5008   - }
5009   - if (id == null || id.length == 0) {
5010   - fn();
5011   - } else if (currentFile != null && !sameWindow) {
5012   - this.showDialog(new PopupDialog(this, this.getUrl() + '#' + id,
5013   - null, fn).container, 320, 140, true, true);
5014   - } else {
5015   - fn();
  4993 +
  4994 + function getToken() {
  4995 + const urlParams = (function () {
  4996 + var result = new Object();
  4997 + var params = window.location.search.slice(1).split('&');
  4998 +
  4999 + for (var i = 0; i < params.length; i++) {
  5000 + var idx = params[i].indexOf('=');
  5001 +
  5002 + if (idx > 0) {
  5003 + result[params[i].substring(0, idx)] = params[i].substring(idx + 1);
  5004 + }
5016 5005 }
5017   - })
5018   - .catch(function (error) {
5019   - console.log(error);
5020   - });
  5006 +
  5007 + return result;
  5008 + })();
  5009 +
  5010 + if (urlParams.userId) {
  5011 + axios.get(`/api/yt/third/login/id/${urlParams.userId}`)
  5012 + .then(res => {
  5013 + const {token, refreshToken} = res.data || {}
  5014 + GLOBAL_TOKEN.token = token
  5015 + GLOBAL_TOKEN.refreshToken = refreshToken
  5016 + getSaveContent()
  5017 + })
  5018 + } else {
  5019 + getSaveContent()
  5020 + }
  5021 + }
  5022 +
  5023 + getToken()
  5024 +
  5025 + //调用API,获取保存的内容
  5026 + function getSaveContent() {
  5027 + defHttp.get('/yt/configuration/center/getConfigurationInfo/'+Editor.configurationId)
  5028 + .then(function (response) {
  5029 + Editor.configurationName = response.configurationName + ".drawio";
  5030 + if(response.configurationContentList.length > 0){
  5031 + response.configurationContentList.forEach((item)=>{
  5032 + Editor.configurationContentId = item.id;
  5033 + if(item.content){
  5034 + Editor.defaultContent = item.content;
  5035 + }else{
  5036 + //默认空白内容
  5037 + Editor.defaultContent = EditorUi.prototype.emptyDiagramXml;
  5038 + }
  5039 + })
  5040 + }
  5041 + if (id == null || id.length == 0) {
  5042 + fn();
  5043 + } else if (currentFile != null && !sameWindow) {
  5044 + this.showDialog(new PopupDialog(this, this.getUrl() + '#' + id,
  5045 + null, fn).container, 320, 140, true, true);
  5046 + } else {
  5047 + fn();
  5048 + }
  5049 + })
  5050 + .catch(function (error) {
  5051 + console.log(error);
  5052 + });
  5053 + }
  5054 +
  5055 +
5021 5056 this.hideDialog();
5022 5057 var fn2 = mxUtils.bind(this, function () {
5023 5058 //加载数据开始
... ...
... ... @@ -12341,7 +12341,7 @@ class DispatchCenter {
12341 12341 * @description 建立socket连接
12342 12342 */
12343 12343 connectSocket() {
12344   - this.socket = Ws.getInstance({ url: GLOBAL_WS_URL, onmessageCallback: this.socketOnmessage })
  12344 + this.socket = Ws.getInstance({ url: GLOBAL_WS_URL(), onmessageCallback: this.socketOnmessage })
12345 12345 }
12346 12346
12347 12347 /**
... ...
... ... @@ -10,9 +10,15 @@ function createAxios(options) {
10 10 timeout: options.timeout,
11 11 headers: {
12 12 "content-type": "application/json; charset=UTF-8",
13   - "X-Authorization": "Bearer " + options.token,
  13 + "X-Authorization": "Bearer " + GLOBAL_TOKEN.token,
14 14 },
15 15 });
  16 +
  17 + instance.interceptors.request.use((config) => {
  18 + config.headers["X-Authorization"] = "Bearer " + GLOBAL_TOKEN.token
  19 + return config
  20 + })
  21 +
16 22 /**
17 23 * 数据返回拦截-响应拦截器
18 24 */
... ...