import { logger, promisifyWxApi } from '../utils'; import { ERROR_STRINGS, WX_VALID_METHODS } from '../constants'; /** * 获取使用 wx 发起请求后的 promise 对象 * @param {object} options */ export const getWxPromise = ({ url, data, method: rawMethod, header, isShowLoading = true, showLoadingFn = () => wx.showLoading({ title: '加载中' }), hideLoadingFn = wx.hideLoading.bind(wx), ...rest }) => { const method = rawMethod.toUpperCase(); logger.log(`Req Url: ${url}`); if (data && Object.keys(data).length) { logger.log('Req Data:', data); } if (!WX_VALID_METHODS.includes(method)) { return Promise.reject(Error(ERROR_STRINGS.unknownMethodFn(method))); } if (isShowLoading) { showLoadingFn(); } return promisifyWxApi(wx.request)({ ...rest, url, data, header, method, complete: () => { // 同步隐藏 loading if (isShowLoading) { hideLoadingFn(); } /* istanbul ignore next */ rest.complete?.(); }, }); };