GitBucket
4.6.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull Requests
Labels
Milestones
Wiki
08335
/
hivui-platform-template
hivui平台项目模板
Browse code
token
master
1 parent
501f1d0
commit
88e84505e4c3468c2aac3820f83702069327f101
20278
authored
on 30 Aug 2021
Showing
3 changed files
project/hivuiLogin/utils/request.js
project/hivuiMain/components/tabpanel/panel.vue
project/hivuiMain/utils/request.js
Ignore Space
Show notes
View
project/hivuiLogin/utils/request.js
import {baseURL,projectName} from '../config' // import { MessageBox, Message } from 'element-ui' import store from '../store' import { getToken,removeToken } from '../utils/auth' import axios from 'axios' // create an axios instance const service = axios.create({ // withCredentials: true, //加了这个 要明确那个地址跨域才行 baseURL: baseURL, // api的base_url timeout: 5000 // request timeout }) // request interceptor service.interceptors.request.use( config => { // do something before request is sent if (getToken()) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['token'] = getToken() config.headers['Authorization'] = "Bearer " + getToken() } if (config.url.indexOf('?')==-1) config.url = config.url+"?pn="+projectName; else config.url = config.url+"&pn="+projectName; return config }, error => { // Do something with request error Promise.reject(error) } ) // respone interceptor service.interceptors.response.use( //response => response, /** * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 (response) => { const res = response.data; if (res.code !== 0) { return Promise.reject('error'); } else { return res; } }, */ response => { const res = response.data if (res.type == "application/octet-stream") return res; // if the custom code is not 20000, it is judged as an error. if(response.status+"" == "401" || res.status+"" =="401"){ removeToken(); if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else if (!(response.status+"" == "200" || res.status+"" =="200")) { debugger;console.error(res.msg || res.message || 'Error'); /*Message({ message: res.msg || res.message || 'Error', type: 'error', duration: 5 * 1000 })*/ // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login /*MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { confirmButtonText: 'Re-Login', cancelButtonText: 'Cancel', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) })*/ if(confirm('You have been logged out, you can cancel to stay on this page, or log in again')){ store.dispatch('user/resetToken').then(() => { location.reload() }) } } //return Promise.reject(new Error(res.message || 'Error')) return Promise.reject(res) } else { if(res.status+"" =="200"){ res.dataPack = res.dataPack || res.data return res }else{ debugger;console.error(res.msg || res.message || 'Error'); alert(res.msg || res.message || 'Error'); } } }, error => { if(error.response.status+"" == "401" || error.response.data.status+"" =="401"){ removeToken(); if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else{ Message({ message: error.msg||error.message, type: 'error', }) } console.log('err' + error) // for debug console.error(error.msg||error.message); return Promise.reject(error) }) export default service
import {baseURL,projectName} from '../config' // import { MessageBox, Message } from 'element-ui' import store from '../store' import { getToken } from '../utils/auth' import axios from 'axios' // create an axios instance const service = axios.create({ // withCredentials: true, //加了这个 要明确那个地址跨域才行 baseURL: baseURL, // api的base_url timeout: 5000 // request timeout }) // request interceptor service.interceptors.request.use( config => { // do something before request is sent if (getToken()) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['token'] = getToken() config.headers['Authorization'] = "Bearer " + getToken() } if (config.url.indexOf('?')==-1) config.url = config.url+"?pn="+projectName; else config.url = config.url+"&pn="+projectName; return config }, error => { // Do something with request error Promise.reject(error) } ) // respone interceptor service.interceptors.response.use( //response => response, /** * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 (response) => { const res = response.data; if (res.code !== 0) { return Promise.reject('error'); } else { return res; } }, */ response => { const res = response.data if (res.type == "application/octet-stream") return res; // if the custom code is not 20000, it is judged as an error. if(response.status+"" == "401" || res.status+"" =="401"){ if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else if (!(response.status+"" == "200" || res.status+"" =="200")) { debugger;console.error(res.msg || res.message || 'Error'); /*Message({ message: res.msg || res.message || 'Error', type: 'error', duration: 5 * 1000 })*/ // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login /*MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { confirmButtonText: 'Re-Login', cancelButtonText: 'Cancel', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) })*/ if(confirm('You have been logged out, you can cancel to stay on this page, or log in again')){ store.dispatch('user/resetToken').then(() => { location.reload() }) } } //return Promise.reject(new Error(res.message || 'Error')) return Promise.reject(res) } else { if(res.status+"" =="200"){ res.dataPack = res.dataPack || res.data return res }else{ debugger;console.error(res.msg || res.message || 'Error'); alert(res.msg || res.message || 'Error'); } } }, error => { if(error.response.status+"" == "401" || error.response.data.status+"" =="401"){ if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else{ Message({ message: error.msg||error.message, type: 'error', }) } console.log('err' + error) // for debug console.error(error.msg||error.message); return Promise.reject(error) }) export default service
Ignore Space
Show notes
View
project/hivuiMain/components/tabpanel/panel.vue
<template> <div class="pl-tabs-panel" :title="title" v-show="active" > <div class="pl-loading pl-loading-fixed" v-show="url&&!loaded" ref="dom-loader"> <span></span> <span></span> <span></span> <span></span> <span></span> </div> <iframe v-if="hasUrl" @load="handleIframeLoad" :name="'iframe'+name" ref="iframe"></iframe> <slot v-if="!hasUrl"></slot> </div> </template> <script> export default { name:'PTabpanel', props:{ close:[String,Number,Boolean], itemData:Object, title:{ type:String, default:"" }, name:{ type:[String,Number], default:"" }, url:{ type:[String], default:"" }, index:[String,Number] }, data(){ return { scrollTop: 0, loaded: false, hasUrl:!(this.url==null||this.url=='') } }, computed:{ active(){ const active = this.$parent.currentName === this.name; return active; } }, watch:{ active(val){ let ifm = this.$refs["iframe"]; if(val){ this.setIframeSrc(); if (ifm.contentDocument) { this.$nextTick(()=>{ //保持原有的滚动位置 ifm.contentDocument.documentElement.scrollTop=this.scrollTop; }); } }else{ if (ifm.contentDocument) { //保持原有的滚动位置 this.scrollTop = ifm.contentDocument.documentElement.scrollTop; } } } }, updated() { this.$parent.$emit('tab-nav-update'); }, mounted(){ if(this.active){ // console.log('___________ mounted active ! setIframeSrc') this.setIframeSrc(); } }, beforeDestroy() { // this.offIframeScroll(); }, methods:{ setIframeSrc(){ let ifm = this.$refs['iframe']; if(!this.hasUrl||this.loaded==true){ if (ifm&&ifm.contentDocument) { ifm.contentWindow.document.body.scrollTop = this.scrollTop; } return; } if(ifm&&this.url.indexOf("about:blank")<0){ this.loaded = false; ifm.setAttribute('src',this.url); }else{ this.loaded = true; } }, reload(){ let ifm = this.$refs['iframe']; this.loaded=false; // console.log(' reload this page :',ifm.src,ifm.contentWindow.window.location.href); ifm.contentWindow.location.reload(); }, handleIframeLoad(evt){ // if(this.active==false){ // //如果没有被激活,就不加载 // return; // } let ifm = this.$refs['iframe']; if(this.title==""&& ifm.contentWindow.document.title!=""){ this.$emit("update:title", ifm.contentWindow.document.title); } if(ifm.src!=''&&ifm.src!=="about:blank"){ this.loaded=true; }else{ if(ifm.contentWindow.document.body.innerHTML==""){ this.loaded=false; }else{ this.loaded=true; } } if (ifm.contentDocument) { ifm.contentWindow.window.addEventListener('beforeunload',()=>{ this.loaded=false;//右击刷新 时进行加载状态 }); } this.loaded&&this.$emit('onIframeLoad',this,evt); }, } } </script>
<template> <div class="pl-tabs-panel" :title="title" v-show="active" > <div class="pl-loading pl-loading-fixed" v-show="url&&!loaded" ref="dom-loader"> <span></span> <span></span> <span></span> <span></span> <span></span> </div> <iframe v-if="hasUrl" @load="handleIframeLoad" :name="'iframe'+name" ref="iframe"></iframe> <slot v-if="!hasUrl"></slot> </div> </template> <script> export default { name:'PTabpanel', props:{ close:[String,Number,Boolean], itemData:Object, title:{ type:String, default:"" }, name:{ type:[String,Number], default:"" }, url:{ type:[String], default:"" }, index:[String,Number] }, data(){ return { scrollTop: 0, loaded: false, hasUrl:!(this.url==null||this.url=='') } }, computed:{ active(){ const active = this.$parent.currentName === this.name; return active; } }, watch:{ active(val){ let ifm = this.$refs["iframe"]; if(val){ this.setIframeSrc(); if (ifm.contentDocument) { this.$nextTick(()=>{ //保持原有的滚动位置 ifm.contentDocument.documentElement.scrollTop=this.scrollTop; }); } }else{ if (ifm.contentDocument) { //保持原有的滚动位置 this.scrollTop = ifm.contentDocument.documentElement.scrollTop; } } } }, updated() { this.$parent.$emit('tab-nav-update'); }, mounted(){ if(this.active){ // console.log('___________ mounted active ! setIframeSrc') this.setIframeSrc(); } }, beforeDestroy() { // this.offIframeScroll(); }, methods:{ setIframeSrc(){ let ifm = this.$refs['iframe']; if(!this.hasUrl||this.loaded==true){ if (ifm.contentDocument) { ifm.contentWindow.document.body.scrollTop = this.scrollTop; } return; } if(ifm&&this.url.indexOf("about:blank")<0){ this.loaded = false; ifm.setAttribute('src',this.url); }else{ this.loaded = true; } }, reload(){ let ifm = this.$refs['iframe']; this.loaded=false; // console.log(' reload this page :',ifm.src,ifm.contentWindow.window.location.href); ifm.contentWindow.location.reload(); }, handleIframeLoad(evt){ // if(this.active==false){ // //如果没有被激活,就不加载 // return; // } let ifm = this.$refs['iframe']; if(this.title==""&& ifm.contentWindow.document.title!=""){ this.$emit("update:title", ifm.contentWindow.document.title); } if(ifm.src!=''&&ifm.src!=="about:blank"){ this.loaded=true; }else{ if(ifm.contentWindow.document.body.innerHTML==""){ this.loaded=false; }else{ this.loaded=true; } } if (ifm.contentDocument) { ifm.contentWindow.window.addEventListener('beforeunload',()=>{ this.loaded=false;//右击刷新 时进行加载状态 }); } this.loaded&&this.$emit('onIframeLoad',this,evt); }, } } </script>
Ignore Space
Show notes
View
project/hivuiMain/utils/request.js
import {baseURL,projectName} from '@main/config' // import { MessageBox, Message } from 'element-ui' import Message from '@main/components/vue-m-message' import store from '@main/store' import { getToken,removeToken } from '@main/utils/auth' import axios from 'axios' // create an axios instance const service = axios.create({ // withCredentials: true, //加了这个 要明确那个地址跨域才行 baseURL: baseURL, // api的base_url timeout: 5000 // request timeout }) // request interceptor service.interceptors.request.use( config => { // do something before request is sent if (store.getters.token) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['token'] = getToken() config.headers['Authorization'] = "Bearer " + getToken() }else{ //没有token,跳转登录页 if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; }else{ location="/"+projectName+"/hivuiLogin/index.html#/"; } } if (config.url.indexOf('?')==-1) config.url = config.url+"?pn="+projectName; else config.url = config.url+"&pn="+projectName; return config }, error => { // Do something with request error Promise.reject(error) } ) // respone interceptor service.interceptors.response.use( //response => response, /** * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 (response) => { const res = response.data; if (res.code !== 0) { return Promise.reject('error'); } else { return res; } }, */ response => { const res = response.data if (res.type == "application/octet-stream") return res; // if the custom code is not 20000, it is judged as an error. if(response.status+"" == "401" || res.status+"" =="401"){ removeToken(); if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else if (!(response.status+"" == "200" || res.status+"" =="200")) { Message.error(res.msg || res.message || 'Error') /*Message({ message: res.msg || res.message || 'Error', type: 'error', duration: 5 * 1000 })*/ // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login /*MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { confirmButtonText: 'Re-Login', cancelButtonText: 'Cancel', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) })*/ if(confirm('You have been logged out, you can cancel to stay on this page, or log in again')){ store.dispatch('user/resetToken').then(() => { location.reload() }) } } //return Promise.reject(new Error(res.message || 'Error')) return Promise.reject(res) } else { if(res.status+"" =="200"){ res.dataPack = res.dataPack || res.data return res }else{ debugger; console.error(res.msg || res.message || 'Error'); Message.error(res.msg || res.message || 'Error') } } }, error => { if(error.response.status+"" == "401" || error.response.data.status+"" =="401"){ removeToken(); if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else{ Message.error(error.msg||error.message) } console.log('err' + error) // for debug return Promise.reject(error) }) export default service
import {baseURL,projectName} from '@main/config' // import { MessageBox, Message } from 'element-ui' import Message from '@main/components/vue-m-message' import store from '@main/store' import { getToken } from '@main/utils/auth' import axios from 'axios' // create an axios instance const service = axios.create({ // withCredentials: true, //加了这个 要明确那个地址跨域才行 baseURL: baseURL, // api的base_url timeout: 5000 // request timeout }) // request interceptor service.interceptors.request.use( config => { // do something before request is sent if (store.getters.token) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['token'] = getToken() config.headers['Authorization'] = "Bearer " + getToken() }else{ //没有token,跳转登录页 if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; }else{ location="/"+projectName+"/hivuiLogin/index.html#/"; } } if (config.url.indexOf('?')==-1) config.url = config.url+"?pn="+projectName; else config.url = config.url+"&pn="+projectName; return config }, error => { // Do something with request error Promise.reject(error) } ) // respone interceptor service.interceptors.response.use( //response => response, /** * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 (response) => { const res = response.data; if (res.code !== 0) { return Promise.reject('error'); } else { return res; } }, */ response => { const res = response.data if (res.type == "application/octet-stream") return res; // if the custom code is not 20000, it is judged as an error. if(response.status+"" == "401" || res.status+"" =="401"){ if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else if (!(response.status+"" == "200" || res.status+"" =="200")) { Message.error(res.msg || res.message || 'Error') /*Message({ message: res.msg || res.message || 'Error', type: 'error', duration: 5 * 1000 })*/ // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login /*MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { confirmButtonText: 'Re-Login', cancelButtonText: 'Cancel', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) })*/ if(confirm('You have been logged out, you can cancel to stay on this page, or log in again')){ store.dispatch('user/resetToken').then(() => { location.reload() }) } } //return Promise.reject(new Error(res.message || 'Error')) return Promise.reject(res) } else { if(res.status+"" =="200"){ res.dataPack = res.dataPack || res.data return res }else{ debugger; console.error(res.msg || res.message || 'Error'); Message.error(res.msg || res.message || 'Error') } } }, error => { if(error.response.status+"" == "401" || error.response.data.status+"" =="401"){ if(window.HIVUI_SETTING.loginUrl){ location=window.HIVUI_SETTING.loginUrl; } }else{ Message.error(error.msg||error.message) } console.log('err' + error) // for debug return Promise.reject(error) }) export default service
Show line notes below