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;
      }
    }
  • 相关阅读:
    Oracle 推出 ODAC for Entity Framework 和 LINQ to Entities Beta版
    Entity Framework Feature CTP 5系列文章
    MonoDroid相关资源
    MSDN杂志上的Windows Phone相关文章
    微软学Android Market推出 Web Windows Phone Marketplace
    使用 Visual Studio Agent 2010 进行负载压力测试的安装指南
    MonoMac 1.0正式发布
    Shawn Wildermuth的《Architecting WP7 》系列文章
    使用.NET Mobile API即51Degrees.mobi检测UserAgent
    MongoDB 客户端 MongoVue
  • 原文地址:https://www.cnblogs.com/caoruichun/p/9427432.html
Copyright © 2011-2022 走看看