This article is mainly for some beginners, there is a bad place to write, but also ask everyone to forgive!

Create two new JS files in the utils folder: api.js and Requtil.js

api.js

This file mainly API interface, nonsense not to say directly on the code

const request = require('requtil.js')
/*Apis put all Apis in here */
const Apis = { 
   /* User related */
  'login': '/devicecenter/auth/weChtLoin'.'bindUser': '/devicecenter/user/userBindinOpenId'.'genQrCode': '/devicecenter/user/getUserRcode'./* Device related */
  'getDeviceList': '/minipro/group/getDl'.// Get the device list
  'getDeviceAdd': '/minipro/group/addDl'.// Add devices
  'getDeviceDtl': '/minipro/group/delDl'.// Delete the device
}
/* Define the request method */
const user = {
  login: function(data) {
    request.get(Apis.login, data)
  },
  getSecret: function(data) {
    request.get(Apis.getSecret, data)
  },
}
module.exports = { ... user }Copy the code

requtil.js

Separate wx. Request requests from wechat

const globalsetting = require('globalsetting.js')
const baseURL = globalsetting.server
const util = require('util.js')

const ignoreUrls = [
  '/auth/weChatLogin'.'/user/userBindingOpenId'.'/user/getSecret'.'/user/getOpenId'
]
var token = ' '

function post(url, args) {
  args = _prev(url, 'POST', args)
  wx.request(args)
}
function get(url, args) {
  args = _prev(url, 'GET', args)
  wx.request(args)
}
function put(url, args) {
  args = _prev(url, 'PUT', args)
  wx.request(args)
}
function _delete(url, args) {
  args = _prev(url, 'DELETE', args)
  wx.request(args)
}
function _prev(url, method, args) {
  // console.log('123',args)
  args = args || {}
  args.url = url
  if(args.urlparam) 
    args.url += '/' + args.urlparam
  var params = parseParams(args)
  params.method = method
  params.success = success(params.success)
  params.fail = fail(params.fail)
  setToken(params)
  return params
}
// Handle whether the interface needs to add the header.token method
function setToken(params) {
  if(! ignoreUrls.some(url= > params.url.match(new RegExp(url)))) {
    if(! params.header) params.header = {token: getToken() }
    else 
      params.header.token = getToken()
  } else {
    // console.log('ignore: ', params.url)}}// handle interface parameter methods
function parseParams(args) {
  var params = Object.assign(args)
  if(! (params.url.startsWith('https://') || params.url.startsWith('http://')))
    params.url = baseURL + params.url
  if(params.param) {
    if (params.url.indexOf('? ') > -1 && params.url.indexOf('? ') != params.url.length - 1) {
      params.url += '&' 
    } else if(params.url.indexOf('? ') == params.url.length -1) {
      // There is no operation
    } else {
      params.url += '? '
    }
    var buf = ' '
    for(var name in params.param) {
      let val = params.param[name];
      buf += name + '=' + encodeURI(typeof val == 'object' ? JSON.stringify(val) : val) + '&'
    }
    params.url += buf
  }
  return params
}
// The interface returns a success method
function success(callback) {
  return function(rs) {
    var status = rs.statusCode
    if (status == 405) {
      util.errorMsg('Request failed 405: \n Server return failed')}else if(status == 404) {
      util.errorMsg('Request failed 404: \n Interface not found')}if(callback) callback(rs.data)
  }
}

function fail(callback) {
  return function(rs) {
    console.log(rs)
    if(callback) callback(rs)
  }
}
// Get the token returned by the interface request
function _setToken(tk) {
  token = tk
  wx.setStorageSync('token', token)
}
Copy the code

How to call the page

It’s in the global app.js

import api from './utils/apis.js';
App({
	api: api,
})
Copy the code

The index page

Get the API from getApp(), define a function that uses the Promise method to get the data, and then call the getChatRecord method on getDevList to assign the data

const APP = getApp()
getDevList(e){
  this.getChatRecord().then(res= > {
    wx.hideLoading({
      success: (res) = >{}});if(res.id == '1') {
      utils.errorMsg(res.message);
    }else {
      console.log(res)
    }
  })
}
// Device list request interface
getChatRecord (params = {}) {
  return new Promise((resolve, reject) = > {
    APP.api.getDeviceList({
      success: res= > {
        resolve(res)
      }
    })
  })
},
Copy the code

Later, I will make a demo and put it on Github, so that you can see it more intuitively