vue中axios复用封装

    ajax2: function() {
      let that = this;
      return that
        .$http({
          method: "get",
          url:
            "/Home/Verify/index?token=" + localStorage.getItem("manage_token")
        })
        .then(response => {
          //登录成功之后获取用户数据
          if (response.data.verify) {
            return that
              .$http({
                method: "post",
                url: "/Home/Manage/request",
                data: qs.stringify({
                  username: localStorage.getItem("manage_num"),
                  currentPage: that.currentPage + 1,
                  pageSize: 15,
                  status: that.screen_value, //筛选
                  keyword: that.search_value //搜索
                })
              })
              .then(response => {
                return response.data;
              })
              .catch(error => {
                MessageBox("提示", "网络错误" + error.response.status);
              });
          } else {
            //登录过期 => 清除前台存储的登录信息并返回登录页
            let instance = Toast("登录已失效,请重新登录!");
            setTimeout(() => {
              instance.close();
              localStorage.removeItem("manage_token");
              localStorage.removeItem("manage_num");
              that.$router.push({ path: "/pages/Login" });
            }, 1000);
          }
        })
        .catch(error => {
          MessageBox("提示", "网络错误" + error.response.status);
        });
    },

调用

that.ajax2().then(function(res) {
        if (res) {
          for (let i = 0; i < res.length; i++) {
            that.teaching_record.push(res[i]);
          }
          that.currentPage++;
          that.loading = false;
          Indicator.close();
        } else {
          that.loading = true;
          Indicator.close();
          that.bottom = true;
        }
      });

 

posted @ 2018-04-19 15:21  陈小银  阅读(214)  评论(0编辑  收藏  举报