• 小程序封装post请求,以及get请求


    /**
       * get请求
       */
      _get: function(url, data, success, fail, complete, check_login) {
        wx.showNavigationBarLoading();
        let App = this;
        // 构造请求参数
        data = data || {};
        data.wxapp_id = App.siteInfo.uniacid;

        // if (typeof check_login === 'undefined')
        //   check_login = true;

        // 构造get请求
        let request = function() {
          data.token = wx.getStorageSync('token');
          wx.request({
            url: App.globalData.api_root + url,
            header: {
              'content-type': 'application/json'
            },
            data: data,
            success: function(res) {
              if (res.statusCode !== 200 || typeof res.data !== 'object') {
                console.log(res);
                App.showError('网络请求出错');
                return false;
              }
              if (res.data.code === -1) {
                // 登录态失效, 重新登录
                wx.hideNavigationBarLoading();
                App.doLogin();
              } else if (res.data.code === 0) {
                App.showError(res.data.msg);
                return false;
              } else {
                success && success(res.data);
              }
            },
            fail: function(res) {
              // console.log(res);
              App.showError(res.errMsg, function() {
                fail && fail(res);
              });
            },
            complete: function(res) {
              wx.hideNavigationBarLoading();
              complete && complete(res);
            },
          });
        };
        // 判断是否需要验证登录
        check_login ? App.doLogin(request) : request();
      },
    /**
       * post提交
       */
      _post_form: function(url, data, success, fail, complete) {
        wx.showNavigationBarLoading();
        let App = this;
        // let api_root=App.api_root
        // console.log(App.globalData.api_root)
        // data.wxapp_id = App.siteInfo.uniacid;
        // data.token = wx.getStorageSync('token');
        wx.request({
          url: App.globalData.api_root+url,//App.api_root + 
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          method: 'POST',
          data: data,
          success: function(res) {
            if (res.statusCode !== 200 || typeof res.data !== 'object') {
              App.showError('网络请求出错');
              return false;
            }
            if (res.data.code === -1) {
              // 登录态失效, 重新登录
              App.doLogin(function() {
                App._post_form(url, data, success, fail);
              });
              return false;
            } else if (res.data.code === 0) {
              App.showError(res.data.msg, function() {
                fail && fail(res);
              });
              return false;
            }
            success && success(res.data);
          },
          fail: function(res) {
            // console.log(res);
            App.showError(res.errMsg, function() {
              fail && fail(res);
            });
          },
          complete: function(res) {
            wx.hideLoading();
            wx.hideNavigationBarLoading();
            complete && complete(res);
          }
        });
      },
    正道的光终将来临,当太阳升起的时候,光芒总会普照大地温暖人间。些许的阴霾也终会有被阳光洒满的一天
  • 相关阅读:
    day17 内置方法、数学模块、randrange随机模块、序列化模块pickle
    线性模型L2正则化——岭回归
    KMP算法
    KNN算法:KNN-classifier和KNN-regressor
    机器学习开篇——编译器的选择
    STL好坑
    树状数组学习笔记
    无题
    最小树形图:朱刘算法
    2019ICPC徐州站题解
  • 原文地址:https://www.cnblogs.com/sjruxe/p/13383949.html
Copyright © 2020-2023  润新知