Free to aggregate data interfaces, for example, by Class class inherits way for small programs to achieve the project of the interface call

Small micro-channel program data source, through the interface. But the interface how to adjust, how to get the data? Everyone has a different method, the following aggregate data in order to free interface as an example.

Configuration Interface config.js

Request interface needs to be aggregated data key as a parameter.

const config = {
    api_base_url: "http://apis.juhe.cn/goodbook",
    key: "93bdf89de207034fa6c7544f88b99c76"
};

export {
    config
}

The method of packaging wx.request

ES6 have class methods can be used directly, using HTTP as the class name, method as the class of the new request, call wx.request in this method.

class HTTP{
  
  //request
  request(params) {
    let that = this;

    if (!params.method) {
      params.method = 'GET';
    }
    wx.request({
      url: config.api_base_url + params.url + "?key=" + config.key,
      data: params.data,
      method: params.method,
      header: {
        'content-type': 'application/json'
      },
      success:(res)=>{
        let code = res.statusCode.toString();
          let error_code = res.error_code;

        IF (code.startsWith ( "2" )) { 
          wx.showToast ({ 
            title: res.data.reason, 
          }); 

          // value acquired by the callback function will return 
          params.success (res.data); 
        } the else {
             the this ._show_message (ERROR_CODE); 
        } 
      }, 
      Fail: (ERR) => { 
          the console.log ( "ERR" + ERR) 
      } 
    }); 
  } 

    // displays error message 
    _show_message (ERROR_CODE) {
         IF (! ERROR_CODE) { 
            ERROR_CODE ==. 1 
        }
        wx.showToast({
            title: tips[error_code],
            icon:'error',
            duration:2500
        })
    }
}

export {
    HTTP
}

This method is useful to request the basic interface config files and error information request success or failure prompt needs to be introduced into the config, and defines the interface error message.

Applet file reference path must be a relative path, using an absolute path may lead path does not point correctly.

{config} from Import "../config" ; 

const Tips = {
     1: "Sorry, wrong", // default error 
    205001: "Picture category is empty" ,
     205,002: "Book category ID can not be empty ' ,
     205 003: "I can not find the results" ,
     10001: "Bad request KEY" ,
     10002: "the KEY no request rights" ,
     10003: "KEY expired" ,
     10004: "wrong OPENID" ,
     10005: "application is not audit timeout, please submit certification " ,
     10007:" unknown request source " ,
     10008:" prohibited IP " ,
     10009:" prohibited KEY " ,
     10011:" current IP requests exceeds the limit " ,
    10012: "The request exceeds the number of restrictions" ,
     10013: "Test KEY request limit exceeded" ,
    10014: "internal system exception" ,
     10020: "Interface Maintenance" ,
     10021: "Interface disabled" 
}

The method of using an external request, this class must be instantiated:

const http = new Class();
const request = http.request();

Module as using a different method of processing data traffic

Different services, different interfaces, call interface parameters and the data returned is different. Methods such as, catalog this business, contains a query catalog, catalog details, catalog directory, etc., can not use the same request processing, but also try not to call interface directly in Pages, you may with safety problems caused, it is necessary to process the data They were written in a different business. Everyone is different wording may be different, but in practical operation should have such ideas.

HTTP Class inheritance by calling direct request method requests data.

Example, the query catalog:

ClassCatalogModel the extends the HTTP {class 

    getCatalog (sCallback) { 
      the this .request ({ 
        URL: "/ Catalog" , 
        Success: (RES) => {
           // the data to the data acquired by the callback method 
          sCallback (RES) 
        } 
      }) 
    } 
} 

{Export 
    ClassCatalogModel 
}
sCallback as a callback method after the promise of success, the acquired data is returned. All data applet requests mostly asynchronous request, the use of callback callback data will be more reasonable.

Trigger Module method after the page loads

Data are typically loaded and executed in the onLoad method. It should be noted, update the data need to use setData method, it is recommended to define data objects in setData name.

{} from ClassCatalogModel Import '../../models/classCatalog.js' ; 
the let catalogModel = new new ClassCatalogModel (); 
Page ({ 

  / * * 
   * page initial data 
   * / 
  Data: { 
    the CatalogData: null 
  }, 

  / * * 
   * life cycle function - listen for page loads 
   * / 
  onLoad: function (Options) { 
    catalogModel.getCatalog ((RES) => {
         IF ! (RES = null ! || RES = '' ) {
             the this .setData ({ 
                the CatalogData : res.result 
            }); 
            console.log ( the this.data.catalogData); 
        } 
    }) 
  } 
})

 

Class of each class using convenient method class may be inherited, than ES5 achieved by modifying the prototype inheritance chain, and a lot easier to clear.

 

Guess you like

Origin www.cnblogs.com/baiyygynui/p/11514382.html