Create an HTTP folder and create two JS files, request and API
Create request.js to encapsulate the request request of the applet
const app = getApp(); Const host = app.globaldata. URL // get Request to convert json object to string (formatParams) Const formatParams = (data) => {let arr = [] for (let name in data) { arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name])) } return arr.join('&') } Const httpService = (url, params, method, loading = true) => {let header = {"content-type": "application/x-www-form-urlencoded" } let defaultData = { token:app.globalData.token, source:'wxxcx' } return new Promise((resolve, reject) => { if (method === 'post') { wx.request({ url: host + url, data: Object.assign(defaultData, params), method: method, header: header, timeout: 15000, complete: (res) => { resolve(res) } }) } else if (method === 'get') { wx.request({ url: host + url + '? ' + formatParams(Object.assign(defaultData, params)), method: method, header: header, timeout: 15000, complete: (res) => { resolve(res) } }) } }) } module.exports = { httpService }Copy the code
Create api.js for unified management of API interfaces
Const {httpService} = require('.. / HTTP /request') const app = getApp() // job related interface /** * Get job list */ const getPositionList = (params) => {return httpService('/position/jobs', params, } /** * const getCompanyList = (params) => {return httpService('/position/company2', params, } /** * const getBanner = (params) => {return httpService('/position/fairbanner', params, } /** * const getCvDetails = (params) => {return httpService('/ CV /detail', params, / const deliveryCv = (params) => {return httpService('/ CV /deliver', params, } /** * const getJobDetails = (params) => {return httpService('/position/jobdetail', params, /** * const collectJob = (params) => {return httpService('/position/ collectJob ',params,'get')} /** * */ const getFormresource = (params) => {return httpService('/position/ formResource ',params,'get')} /** * */ const getcompanyJob = (params) => {return httpService('/position/jobfeed',params,'post')} /** * */ const getCompanyInfo = (params) => {return httpService('/position/ morePosition ',params,'post')} // Login to the relevant interface / / const wxLogin = (params) => {return httpService('/login/xcxlogin', params, } /** * const wxgroupConfirm = (params) => {return httpService('/position/groupconfirm', } /** * const getCode = (params) => {return httpService('/mobile/phonecode', params, // const getMyMetting = (params) => {return httpService('/user/usermeeting', params, Module.exports = {getPositionList, getBanner, getCompanyList, getCvDetails, wxLogin, wxGroupConfirm, getCode, deliveryCv, getJobDetails, collectjob, getFormresource, getcompanyJob, getCompanyInfo, getMyMetting }Copy the code
Page using
Import {exported interface object} from ‘relative path of encapsulated API file’
case
import {
getPositionList,
getBanner,
getCompanyList
} from ".. /.. /.. /http/api"
// The usage modeGetPositionList ({arguments}).then(res= >{successful callback}). Catch (err= >{failed callback})Copy the code
This API management mode is not friendly to functions with a large number of apis, and will continue to be optimized in the future