在utils下新建http.js文件
import config from './config.js'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; options.data = !url == '/login' ? { ...data, ...this.config.data } : data options.method = 'POST'; return this.request(options); }}
在utils下新建config.js文件
export default { webUrl: 'https:/xxxxxxx/xcx'}
在项目中引用
import http from 路径
http.get('url,data).then((res)=>{
res
})
http.post('url,data).then((res)=>{
res
})