小程序封装请求

1.根目录新建一个js,例如:config.js



```

/**

* 小程序配置文件

*/

var host = 'http://39.97.242.26:8001';  //测试

var config = {

  service: {

    host,

    login: `${host}/login`,  //获取客户列表

  }

};

module.exports = config;

```

2.新建一个js用来封装请求(位置随便),例如:util.js




把需要的导出


```

//请求数据

function getRequest(url, params, callback) {

  this.showBusy('加载中...');

  wx.request({

    url: url,

    data: params,

    method: 'POST',

    header: { 'content-type': 'application/x-www-form-urlencoded' },

    success: function (result) {

      if (result.data.code == '10000') {

        wx.hideToast();

        var result = result.data;

        callback(result.data);

      } else {

        wx.hideToast();

        wx.showModal({

          title: '提示',

          content: '加载失败,请重新刷新',

          showCancel: false

        });

        return false;

      }

    },

    fail(error) {

      wx.hideToast();

      wx.showModal({

        title: '提示',

        content: '网络异常,请重新刷新',

        showCancel: false

      });

      return false;

    }

  })

}

//请求数据(异步)

function getRequestPromise(url, params) {

  this.showBusy('请求中...');

  var token = wx.getStorageSync('weapp_session_login_data');

  return new Promise(function (resolve, reject) {

    wx.request({

      url: url,

      data: params,

      method: 'POST',

      header: {

        'content-type': 'application/json',

        'token': token.token          //---------------------token验证已取消

      },

      success: function (result) {

        if (result.data.code == '10000') {

          wx.hideToast();

          var results = result.data;

          resolve(results.data);

        } else if (result.data.code == '50001'){  //未登录

          wx.hideToast();

          wx.showModal({

            title: '提示',

            content: '您的登录已失效,请重新登录',

            showCancel: false,

            success(res) {

              if (res.confirm) {

                try {

                  wx.clearStorageSync();

                  wx.reLaunch({

                    url: '/pages/login/index', //绝对路径

                  });

                } catch (e) {

                  return false;

                }

              }

            }

          });

          return false;

        } else {

          wx.hideToast();

          wx.showModal({

            title: '提示',

            content: '请求失败,请重新尝试',

            showCancel: false

          });

          return false;

        }

      },

      fail(error) {

        wx.hideToast();

        wx.showModal({

          title: '提示',

          content: '网络异常,请重新尝试',

          showCancel: false

        });

        return false;

      }

    })

  })

}

module.exports = {

  getRequest: getRequest,

  getRequestPromise: getRequestPromise,

}

```

3.在需要请求的页面中的js中引入两个文件


请求接口

```

getGrade: function (e) {

    var that = this;

    var params = {

      type: 'grade'

    };

    util.getRequestPromise(config.service.findListByType, params)

    .then(data => {

      let result = data ? data : [];

      //console.log(result);

      that.setData({

        gradeData: result

      });

    });

  },

```

你可能感兴趣的:(小程序封装请求)