请求接口的封装

  1. get方法
    1. /*方法说明
        *@method get
        *@param { path:接口路径, 
                  arg:参数,
                  callback: 成功回调的函数, 
                  err:失败回调的函数
                }
        *@return {
                  callback():成功函数 ,
                  err(): 失败的函数
                 } 
        */
        get(path: any, arg: object, callback: Function, err: Function) {
          axios.get(url.return.host + path,  {
              headers: {
              "Authorization":localStorage.getItem("token"), 
              }, 
              params:arg     
            })
            .then(function (response) {
                return callback(response); 
            })
            .catch(function (error) {
              // console.log(error.status_code)
                return err(error);
           }); 
        },
  2. post方法
    1. /*方法说明
        *@method post
        *@param { path:接口路径, 
                  arg:参数,
                  head: 请求头
                  callback: 成功回调的函数, 
                  err:失败回调的函数
                }
        *@return {
                  callback():成功函数 ,
                  err(): 失败的函数
                 } 
        */
        post(path: any, head: any, arg: any, callback: Function, err: Function){
          axios({
            method: "post",
            url: url.return.host + path,
            headers: head,
            data: arg,
          })
          .then(function (response) {
            return callback(response);
          })
          .catch(function (error) {
            return err(error);
          }); 
        },
  3. patch方法    
  4. /*方法说明
      *@method pat
      *@param { path:接口路径, 
                head: 请求头
                arg:参数,
                callback: 成功回调的函数, 
                err:失败回调的函数
              }
      *@return {
                callback():成功函数 ,
                err(): 失败的函数
               } 
      */
      patch(path: any, head: any, arg: any, callback: Function, err: Function){
        axios({
          method: "patch",
          url: url.return.host + path,
          headers: head,
          data: arg,
        })
        .then(function (response) {
          return callback(response);
        })
        .catch(function (error) {
          return err(error);
        }); 
      }



猜你喜欢

转载自www.cnblogs.com/zwh520/p/10272350.html