微信小程序开发(1)--数据请求接口封装和保持会话

1.在app.js中进行封装,方便调用。封装后更加便利,改变服务端url不用每个页面都去修改

2.微信小程序的每次请求都会由微信小程序的服务端进行转发,因此对于目标服务端来说每次请求都是新的会话,要保存会话状态就需要每次请求都带上sessionId,返回给目标服务端。因需求是每次打开都需要登录,因此sessionId保存在app.js中,如果下次打开不需要登录可以将sessionId保存在storage中,但是根据服务器的sessionId时效和具体需求而定。

完整代码:

globalData: {
    isLogin: false,
    editorRecord: null,
    sessionId: null,
    locationInfo: null,
    serverUrl: "http://192.168.15.103:8088/zcproduct/rest/v2.0/",
    fileUrl:"http://192.168.15.103:8088/zcproduct/"
  },
  /**
   * 接口公共访问方法
   * @param {string} urlPath 访问路径
   * @param {Object} params 访问参数(json格式)
   * @param {function} success 返回成功
   * @param {function} fail 返回失败
   * @param {function} complete 请求完成(不管成功或失败)回调
   * @param {string} method 请求类型(默认POST)
   */
  requestData: function(urlPath, params, success, fail, complete, method) {
    var url, params, onSuccess, onFail, onComplete, method;
    if (arguments.length > 1) {
      url = this.globalData.serverUrl +
        ((arguments[0] && typeof(arguments[0]) === 'string') ? arguments[0] : '');
      params = (arguments[1] && typeof(arguments[1]) === 'object') ? arguments[1] : {};
      onSuccess = (arguments[2] && typeof(arguments[2]) === 'function') ? arguments[2] : function() {};
      onFail = (arguments[3] && typeof(arguments[3]) === 'function') ? arguments[3] : function() {};
      onComplete = (arguments[4] && typeof(arguments[4]) === 'function') ? arguments[4] : function() {};
      method = (arguments[5] && typeof(arguments[5]) === 'string') ? arguments[5] : "POST";
    } else {
      var option = arguments[0];
      url = this.globalData.serverUrl +
        ((option.url && typeof(option.url) === 'string') ? option.url : '');
      params = (option.data && typeof(option.data) === 'object') ? option.data : {};
      onSuccess = (option.success && typeof(option.success) === 'function') ? option.success : function() {};
      onFail = (option.error && typeof(option.error) === 'function') ? option.error : function() {};
      onComplete = (option.complete && typeof(option.complete) === 'function') ? option.complete : function() {};
      method = (option.method && typeof(option.method) === 'string') ? option.method : "POST";
    }
    var that = this;
    console.log("发起网络请求, 路径:" + url + ", 参数:" + JSON.stringify(params));
    wx.request({
      url: url,
      data: params,
      dataType: 'json',
      method: method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json',
        'Cookie': that.globalData.sessionId ? ('JSESSIONID=' + that.globalData.sessionId) : that.globalData.sessionId
      }, // 设置请求的 header
      success: function(res) {
        console.log('Cookie',that.globalData.sessionId ? ('JSESSIONID=' + that.globalData.sessionId) : that.globalData.sessionId);
        wx.hideLoading();
        onSuccess(res);
      },
      fail: function(res) {
        wx.hideLoading();
        onFail(res);
      },
      complete: function(res) {
        // wx.hideToast();//隐藏提示框
        onComplete(res);
      }
    })
  },

 调用:

var app = getApp();
Page({
upload: function(url,data){
    app.showLoading('正在提交');
    app.requestData({
      url:url,
      data: data,
      success: function (res) {
          console.log(res);
          if (res.data.success) {
            app.showOk('提交成功');
          } else {
            app.showErr('出错', res.data.message);
          }
      },
      error: function (res) {
          console.log(res);
      }
    })
}
})

猜你喜欢

转载自blog.csdn.net/Datura_Elena/article/details/82145442