Wechat widget - - encapsulate widget get&&post requests into global functions

Keywords: JSON

First copy the code in app.js

  /**
   * methods:  Request mode
   * url: Request address
   * data:  The parameters to be passed
   * callback:  Request success callback function
   * errFun:  Callback function for request failure
   */
  appRequest(methods, url, data, callback, errFun) {
    wx.request({
      url: url,
      method: methods,
      header: {
        'content-type': methods == 'GET' ? 'application/json' : 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      data: data,
      success: function (res) {
        callback(res.data);
      },
      fail: function (err) {
        errFun(err);
      }
    })
  },

First is the get request

const app = new getApp();

// page/a/a.js
Page({

  /**
   * Initial data of pages
   */
  data: {

  },

  /**
   * Life Cycle Function -- Listening for Page Loading
   */
  onLoad: function (options) {
    //Lazy people's writing URL s are too long and have too many references.
    app.appRequest('get', 'https://www.apiopen.top/satinApi?type=1&page=1', {}, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });

    //A slightly elegant way of writing, in fact, is one more line of code, but much more beautiful, and easy to maintain.
    let url = 'https://www.apiopen.top/satinApi?type=1&page=1';
    app.appRequest('get', url, {}, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });
  }
})

When a get request is made, the data reference is written as {}.

Then the post request

const app = new getApp();

// page/b/b.js
Page({

  /**
   * Initial data of pages
   */
  data: {

  },

  /**
   * Life Cycle Function -- Listening for Page Loading
   */
  onLoad: function (options) {
    //Lazy people's writing URL s are too long and have too many references.
    app.appRequest('post', 'https://www.apiopen.top/satinApi', { type:1, page:1 }, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });

    //A slightly elegant way of writing, in fact, is one more line of code, but much more beautiful, and easy to maintain.
    let url = 'https://www.apiopen.top/satinApi';
    let data = {
        type: 1,
        page: 1
    }
    app.appRequest('post', url, data, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });
  }
})

Of course, if every URL is on the page, it will increase the maintenance cost, so all interfaces should be placed in a file, I choose to put it in app.js. Here's how I write it.

//Add global attributes and a function to app.js

//Global request URL handler
 globalRequestUrl(domainName, site) {
   return this.globalData[domainName] + this.globalData[site]
 },

//Global Url for all requests
 globalData: {
   domainNameA: 'https://www.apiopen.top',//Request domain name A
   domainNameB: 'https://www.apiopen.top',//Request domain name B
   siteA: '/satinApi'
 }

Use the global request URL handler to use get requests in page C

const app = new getApp();

// page/c/c.js
Page({

  /**
   * Initial data of pages
   */
  data: {

  },

  /**
   * Life Cycle Function -- Listening for Page Loading
   */
  onLoad: function (options) {
    //Lazy people's writing URL s are too long and have too many references.
    app.appRequest('get', app.globalRequestUrl('domainNameA','siteA'), {}, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });

    //A slightly elegant way of writing, in fact, is one more line of code, but much more beautiful, and easy to maintain.
    let url = app.globalRequestUrl('domainNameA','siteA');
    app.appRequest('get', url, {}, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });
  }
})

Use the global request URL handler to use post requests in page D

const app = new getApp();

// page/d/d.js
Page({

  /**
   * Initial data of pages
   */
  data: {

  },

  /**
   * Life Cycle Function -- Listening for Page Loading
   */
  onLoad: function (options) {
    //Lazy people's writing URL s are too long and have too many references.
    app.appRequest('post', app.globalRequestUrl('domainNameA','siteA'), { type:1, page:1 }, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });

    //A slightly elegant way of writing, in fact, is one more line of code, but much more beautiful, and easy to maintain.
    let url = app.globalRequestUrl('domainNameA','siteA');
    let data = {
        type: 1,
        page: 1
    }
    app.appRequest('post', url, data, (res) => {
      console.log(res)
    }, (err) => {
      console.log('Request error message:  ' + err.errMsg);
    });
  }
})
//Legend has it that a line of code completes the request here, hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha
app.appRequest('get', app.globalRequestUrl('domainNameA', 'siteA'), {}, (res) => {console.log(res)});

Above is the whole content of this article, I hope to help you.

Posted by amrigo on Mon, 04 Feb 2019 08:12:16 -0800