Create a new http.js file under utils
import config from './config.js'/** * encapsulation of applets requests */export default { config: { baseUrl: config.webUrl, header: { 'Content-Type': 'application/json; charset=UTF-8'.'Content-Type': 'application/x-www-form-urlencoded' }, data: { openid: wx.getStorageSync('openid')},method: "GET".dataType: "json", }, request(options = {}) { return new Promise((resolve, reject) = > { wx.request({ url: this.config.baseUrl + options.url, data: options.data || this.config.data, header: options.header || this.config.header, method: options.method || this.config.method, dataType: options.dataType || this.config.dataType, success: function (res) { if (res.data.errCode === '0') { resolve(res.data.data) } else { wx.showToast({ title: res.data.message, icon: 'none'})}},fail: function (res) { reject(res) }, }) }) }, get(url, data, options = {}) { options.url = url; options.data = { ... data, ... this.config.data }; options.method ='GET'; return this.request(options); }, post(url, data, options = {}) { options.url = url; /** * and data */options.data = ! url =='/login'? {... data, ... this.config.data } : data options.method ='POST'; return this.request(options); }}Copy the code
Create a new config.js file under utils
export default { webUrl: 'https:/xxxxxxx/xcx'}Copy the code
Reference in the project
Import HTTP from path http.get('url,data). Then ((res)=>{res}) http.post('url,data). Then ((res)=>{res})Copy the code