In order to extract methods from the page, the applets request is rewrapped.

  • Api.js (Common Request)

    Export const pubUrl = "https://xxx.com"// This is the public part of the data interface I want to request const HTTP = (options) =>{return new Promise((resolve,reject))  => { wx.request({ url: pubUrl + options.url, method: options.method || 'get', data: options.data || {}, header: options.header || { // 'content-type': 'application/x-www-form-urlencoded', 'content-type': 'application/json', Cookie: wx.getStorageSync("sessionid") }, success: (res => { if (res.data.code === 0) { resolve(res.data) } else if (res.data.code === 401) { wx.showToast({ title: res.data.msg, icon: 'none', complete: () => { wx.navigateTo({ url: '/pages/login/index' }) } }) } else { resolve(res.data) } }), fail: reject }) }) } module.exports = { http }Copy the code

Create a service folder in the root directory, create a JS file according to the module, and put the interface request of the corresponding module into it, for example:

  • Home.js (interface js file, directory: service > home.js)

    Import HTTP from './api.js' // open timelist const timelist = () => {return HTTP ({method: 'get', url: '/merchant/openTime/' }) } module.exports = { timelist }Copy the code

Use: home > index.js(the page js file)

import { timelist } from '.. /home' Page({ onLoad() { this.getData() }, async getData() { const {code, data, msg} = await timelist() } })Copy the code