• vue封装接口


    1、新建一个api文件夹,再在里面新建一个api 文件,在文件里封装接口
    import axios from "axios";
    axios.defaults.withCredentials = true;

    import Vue from "vue";
    import qs from "qs";
    // const appserver = "http://192.168.1.116:8888/";               可能需要更换的接口
    // const appserver = "http://10.34.106.119:8013/";               可能需要更换的接口
    const appserver = "http://192.168.1.97:8013/";                        现在需要用的接口
    export function appServer() {
      return appserver;                                                                 //后面导入导出等直接需要用地址的要用
    }

    //get请求
    export function commonGet(urls, params, callback) {
      let appUrl = appserver + urls;
      axios
        .get(appUrl, {
          params
        })
        .then(res => {
          callback(res.data);
        })
        .catch(res => {
          try {
            if (res.response.status == 401) {
              Vue.prototype.$message({
                message: "登录已过期,请重新登录!",
                type: "error"
              });
              Session.remove("token");
              Session.remove("wsUrl");
              Vue.prototype.$router.push({
                path: "/login"
              });
            } else if (res.response.status == 504) {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            } else if (res.response.status == 404) {
              Vue.prototype.$message({
                message: "接口异常!",
                type: "error"
              });
            } else if (res.response.status == 500) {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            } else {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            }
          } catch (e) {}
        });
    }

    //post请求
    export function commonPost(urls, params, callback) {
      let appUrl = appserver + urls;
      axios
        .post(appUrl, JSON.stringify(params), {
          headers: {
            "Content-Type": "application/json;charset=utf-8"
          }
        })
        .then(res => {
          callback(res.data);
        })
        .catch(res => {
          try {
            if (res.response.status == 401) {
              Vue.prototype.$message({
                message: "登录已过期,请重新登录!",
                type: "error"
              });
              Session.remove("token");
              Session.remove("wsUrl");
              Vue.prototype.$router.push({
                path: "/login"
              });
            } else if (res.response.status == 504) {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            } else if (res.response.status == 404) {
              Vue.prototype.$message({
                message: "接口异常!",
                type: "error"
              });
            } else if (res.response.status == 500) {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            } else {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            }
          } catch (e) {}
        });
    }
    //post请求
    export function commonPost2(urls, params, callback) {
      let appUrl = appserver + urls;
      axios
        .post(appUrl, qs.stringify(params))
        .then(res => {
          callback(res.data);
        })
        .catch(res => {
          try {
            if (res.response.status == 401) {
              Vue.prototype.$message({
                message: "登录已过期,请重新登录!",
                type: "error"
              });
              Session.remove("token");
              Session.remove("wsUrl");
              Vue.prototype.$router.push({
                path: "/login"
              });
            } else if (res.response.status == 504) {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            } else if (res.response.status == 404) {
              Vue.prototype.$message({
                message: "接口异常!",
                type: "error"
              });
            } else if (res.response.status == 500) {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            } else {
              Vue.prototype.$message({
                message: "服务器异常!",
                type: "error"
              });
            }
          } catch (e) {}
        });
    }
    看到我的div了吗?在你那你就完蛋了
  • 相关阅读:
    redis
    配置ssh无密码登陆Linux
    nginx rewrite规则笔记
    git自动部署到服务器
    从电影《Her》来看AI时代下,未来的七夕我们将会如何度过?
    人工智能+智能制造,会产生什么火花?
    大数据时代,市场对企业级云存储的需求更加迫切
    别太神化AI,也别太小看智能医疗
    自动驾驶江湖,将有一场恶战
    区块链 | 详解以太坊的工作原理
  • 原文地址:https://www.cnblogs.com/web-shu/p/12014842.html
Copyright © 2020-2023  润新知