微信封装wx.request
import config from '../config'; export default { get: (url, params, success, error) => { wx.request({ url: config.host + url, header: { 'Content-type': 'application/json', 'Authorization': config.getAuthorization() }, method: 'GET', data: params, success: (res) => { if (success) { success(res.data); } }, error: (err) => { if (error) { error(err); } } }) }, post: (url, data, success, error) => { wx.request({ url: config.host + url, header: { 'Content-type': 'application/json', 'Authorization': config.getAuthorization() }, method: 'POST', data: data, success: (res) => { if (success) { success(res.data); } }, error: (err) => { if (error) { error(err); } } }) }, getAsync(url, params = {}, options = {}) { const defaultOptions = { url: config.host + url, data: params, method: 'GET', timeout: 5000, header: { 'Content-type': 'application/json', 'Authorization': config.getAuthorization() }, }; return new Promise((resolve, reject) => { wx.request({ ...defaultOptions, ...options, success: (res) => { // 这里可以根据业务需求处理成功的逻辑 resolve(res.data); }, fail: (err) => { // 这里可以根据业务需求处理失败的逻辑 reject(err); } }); }); }, postAsync(url, data = {}, options = {}) { const defaultOptions = { url: config.host + url, data: data, method: 'POST', timeout: 5000, header: { 'Content-type': 'application/json', 'Authorization': config.getAuthorization() }, }; return new Promise((resolve, reject) => { wx.request({ ...defaultOptions, ...options, success: (res) => { // 这里可以根据业务需求处理成功的逻辑 resolve(res.data); }, fail: (err) => { // 这里可以根据业务需求处理失败的逻辑 reject(err); } }); }); }, }