当前位置: 首页 > Web前端 > vue.js

SPA项目页面添加token失效弹框

时间:2023-03-31 21:55:47 vue.js

.dialog{z-index:6;宽度:75%;背景:rgba(255,255,255,1);位置:固定;顶部:50%;变换:翻译Y(-50%);左:12%;字体大小:32px;border-radius:25px;}.dialog-context{填充:70px38px52px;文本对齐:居中;颜色:rgba(74、74、74、1);行高:42px;border-bottom:2pxsolid#DDDDDD;}.dialog-botton{display:flex;证明内容:空格之间;弹性方向:行;文本对齐:居中;颜色:#3e78c4;}.dialog-cancel{border-right:2pxsolid#DDDDDD;颜色:rgba(128,128,128,1);}.dialog-bottonspan{flex:1;padding:30px0;}在hybridapp内嵌的h5页面,token过期后,提示需要重新登录。点击确定,跳转到应用的登录页面。这是因为任何ajax请求在收到token失效错误码时都会显示“您的账号登录已过期或您的密码已更改,请重新登录”。弹框http.jshttp.interceptors.response.use(response=>{returnresponse;},error=>{if(error.response){/*后端同意,当错误码返回3002时,表示前端的令牌已过期*/if(error.response.status==500&&error.response.data.error==3002){//console.log("tokenexpired");sessionStorage.setItem("relogin","true");//弹出请重新登录的弹框Vue.prototype.$dialog({content:"您的账号登录已过期,或密码已更改,请重新登录",justAlert:true,sureBtnText:"OK"}).then(res=>{if(res){console.log("AcallbackontheappsideexecutedafterclickingOK");}})}}returnPromise.reject(error)//返回接口返回的错误信息});Dialog.vue提示组件.dialog{z-index:6;宽度:75%;背景:rgba(255,255,255,1);位置:固定;顶部:50%;变换:翻译Y(-50%);左:12%;字体大小:32px;border-radius:25px;}.dialog-context{填充:70px38px52px;文本对齐:居中;颜色:rgba(74、74、74、1);行高:42px;border-bottom:2pxsolid#DDDDDD;}.dialog-botton{display:flex;证明内容:空格之间;弹性方向:行;文本对齐:居中;颜色:#3e78c4;}.dialog-cancel{border-right:2pxsolid#DDDDDD;颜色:rgba(128,128,128,1);}.dialog-bottonspan{flex:1;padding:30px0;}Dialog.jsimportDialogViewfrom'../../components/Dialog.vue'//importVuefrom'vue'letDialog={};Dialog.install=function(Vue,config){constDialogViewConstructor=Vue.extend(DialogView)//生成一个该子类的实例constinstance=newDialogViewConstructor();Vue.prototype.$dialog=(config)=>{//这里返回一个Promise//在项目中使用时,XX.$dialog(...).then(res=>{})链式调用方式~returnnewPromise((resolve,reject)=>{instance.config=config;console.log(instance.config);instance.isShow=true;instance.sure=()=>{instance.isShow=false;resolve(true)}instance.cancel=()=>{instance.isShow=false;resolve(false)}})}//将此实例挂载到我创建的div上//并将此div添加到全局挂载点内部实例。$mount()document.body.appendChild(instance.$el)}导出默认对话框;最后记得参考//introducetokeninmain.js提示框插件importDialogfrom"./modules/plugins/Dialog.js";Vue.use(Dialog);

猜你喜欢