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;
      }
    }
  • 相关阅读:
    javascript数组对象
    jquery、javascript实现(get、post两种方式)跨域解决方法
    js检查字符串的包含关系
    element——message消息提示
    js把mysql传过来的时间格式化为:0000-00-00 00:00:00
    beego orm操蛋问题:操作都需要主键
    element——message-box
    VUE.js全局变量的定义
    element el-upload组件获取文件名
    go遍历某个文件夹
  • 原文地址:https://www.cnblogs.com/caoruichun/p/9427432.html
Copyright © 2011-2022 走看看