zoukankan      html  css  js  c++  java
  • angular5.x拦截器 给get post请求添加参数user_token

    export class Interceptor implements HttpInterceptor {
    public myAppListService;
    constructor(private message: NzMessageService, private injector: Injector) {
    }

    public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.myAppListService = this.injector.get(MyAppListService);
    this.myAppListService.selectedUserToken = getQueryString('user_token') || sessionStorage.getItem('user_token');
     
    if (this.myAppListService.selectedUserToken !== 'null') {
    let newParams;
    let newBody;
    if (req.method === 'GET') {
    let userToken = this.myAppListService.selectedUserToken;
    if (userToken) {
    newParams = req.params.set('user_token', userToken);
    const comReq = req.clone({
    params: newParams,
    body: newBody
    });
    return next.handle(comReq).do((res) => {
    this.handleResponse(res);
    });
    }
    } else if (req.method === 'POST' && req.headers.get('content-type').indexOf('application/x-www-form-urlencoded;') > -1) {
    // if (req.body.indexOf('user_token=')) {
    let userToken = this.myAppListService.selectedUserToken;
    if (userToken) {
    if (req.body.match(/(^|&)user_token=([^&]*)(&|$)/)) {
    newBody = req.body.replace(req.body.match(/(^|&)user_token=([^&]*)(&|$)/)[0], '');
    newBody = newBody + '&user_token=' + userToken;
    } else {
    newBody = '&user_token=' + userToken;
    }
    const comReq = req.clone({
    params: newParams,
    body: newBody
    });
    return next.handle(comReq).do((res) => {
    this.handleResponse(res);
    });
    }
    // }
    }

    }
    const comReq = req.clone();
    return next.handle(comReq).do((res) => {
    this.handleResponse(res);
    });
    }
    public handleResponse(res) {
    if (res instanceof HttpResponse) {
    const data = res.body;
    if (data.status && data.status === 2) {
    this.message.error(data.data);
    location.href = '/index.php?r=Login/Ulogin';
    }
    if (data.status && data.status === 1 && data.data === '应用不存在,或者您不是应用管理者') {
    location.href = '/index.php?r=Login/Ulogin';
    return;
    }
    }
    }
    }
  • 相关阅读:
    Selenium之IE浏览器的启动问题及解决
    Selenium之Chrome浏览器的启动问题及解决
    Selenium之IE浏览器的启动
    Selenium之firefox浏览器的启动
    【luogu 3373】【模板】线段树2
    【luogu 3372】【模板】线段树1
    【luogu 1908】逆序对
    【codevs2822】爱在心中
    【bzoj1051】 [HAOI2006]受欢迎的牛
    【luogu 2863】[USACO06JAN]牛的舞会The Cow Prom
  • 原文地址:https://www.cnblogs.com/aisiqi-love/p/10151284.html
Copyright © 2011-2022 走看看