zoukankan      html  css  js  c++  java
  • axios项目二次封装

    import axios from 'axios'
    import qs from 'qs'
    
    const baseURL = "http://10.4.108.117:8081/"
    
    exports.install = function (Vue, options) {
      Vue.prototype.get = function (url, data, success) {
        axios({
          url: baseURL + url,
          params: data,
          headers: {
            "token": localStorage.getItem("token")
          }
        }).then((respose) => {
          if (respose.status == 200) {
            var resultMap = respose.data;
            if (resultMap) {
              if (!resultMap.code) {
                success(resultMap);
              } else {
                if (resultMap.code == 200) {
                  success(resultMap.data);
                } else {
                  if (resultMap.msg) {
                    let errorMsg = resultMap.msg.replace(/d+/, "").replace("|", "");
                    this.$Message.error(errorMsg);
                    let _this = this;
                    if (errorMsg == "帐号未登录") {
                      setTimeout(function () {
                        // 退出登录
                        _this.$store.commit('logout', this);
                        _this.$store.commit('clearOpenedSubmenu');
                        _this.$router.push({
                          name: 'login'
                        });
                      }, 300);
                    }
                  }
                }
              }
            }
          } else {
            this.$Message.error(respose.statusText);
          }
        }).catch((error) => {
        })
      };
      Vue.prototype.post = function (url, data, success) {
        axios.post(baseURL + url, qs.stringify(data), {
          headers: {
            "token": localStorage.getItem("token")
          }
        }).then((respose) => {
          if (respose.status == 200) {
            var resultMap = respose.data;
            if (resultMap) {
              if (resultMap.code == 200) {
                success(resultMap.data)
              } else {
                if (resultMap.msg) {
                  let errorMsg = resultMap.msg.replace(/d+/, "").replace("|", "");
                  this.$Message.error(errorMsg);
                  let _this;
                  if (errorMsg == "帐号未登录") {
                    setTimeout(function () {
                      // 退出登录
                      _this.$store.commit('logout', this);
                      _this.$store.commit('clearOpenedSubmenu');
                      _this.$router.push({
                        name: 'login'
                      });
                    }, 300);
                  }
                }
              }
            }
          } else {
            this.$Message.error(respose.statusText);
          }
        }).catch((error) => {
          console.log(error)
        })
      };
      Vue.prototype.postHandle = function (url, data, success) {
        axios.post(baseURL + url, qs.stringify(data)).then((respose) => {
          if (success) {
            success(respose)
          }
        }).catch((error) => {
          console.log(error)
        })
      };
      Vue.prototype.getBaseUrl = function () {
        return baseURL;
      }
    
      Vue.prototype.getUrlParameter = function (obj) {
        let _paramter = "?token=" + localStorage.getItem("token");
        if (obj) {
          Object.keys(obj).forEach(function (key) {
            if (typeof obj[key] == "number" || obj[key]) {
              _paramter = _paramter + "&" + key + "=" + obj[key];
            }
          });
        }
        return _paramter;
      }
    }
  • 相关阅读:
    这年头学爬虫还就得会点 scrapy 框架
    【全栈之路】JAVA基础课程十_JAVA虚拟机(20190706v1.1)
    牛客练习赛61
    ERD图
    深入理解Java虚拟机-如何利用VisualVM对高并发项目进行性能分析
    阿里研究员吴翰清:世界需要什么样的智能系统?
    《深入理解 C# (第2版)》
    HtmlAgility 抓取网页上的数据
    abp(net core)+easyui+efcore实现仓储管理系统——入库管理之三存储过程(三十九)
    如何Tomcat完美访问web项目,无需配置“项目名”
  • 原文地址:https://www.cnblogs.com/caoruichun/p/9427432.html
Copyright © 2011-2022 走看看