From 7984e0d8cebf4179f7ee3e2046207edd12398cf6 Mon Sep 17 00:00:00 2001 From: quanyawei <401863037@qq.com> Date: Thu, 09 Nov 2023 14:47:09 +0800 Subject: [PATCH] fix:小程序详情修改 --- uni_modules/uview-ui/libs/luch-request/core/Request.js | 198 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 198 insertions(+), 0 deletions(-) diff --git a/uni_modules/uview-ui/libs/luch-request/core/Request.js b/uni_modules/uview-ui/libs/luch-request/core/Request.js new file mode 100644 index 0000000..cc48566 --- /dev/null +++ b/uni_modules/uview-ui/libs/luch-request/core/Request.js @@ -0,0 +1,198 @@ +/** + * @Class Request + * @description luch-request http������������ + * @version 3.0.7 + * @Author lu-ch + * @Date 2021-09-04 + * @Email webwork.s@qq.com + * ������: https://www.quanzhan.co/luch-request/ + * github: https://github.com/lei-mu/luch-request + * DCloud: http://ext.dcloud.net.cn/plugin?id=392 + * HBuilderX: beat-3.0.4 alpha-3.0.4 + */ + +import dispatchRequest from './dispatchRequest' +import InterceptorManager from './InterceptorManager' +import mergeConfig from './mergeConfig' +import defaults from './defaults' +import { isPlainObject } from '../utils' +import clone from '../utils/clone' + +export default class Request { + /** + * @param {Object} arg - ������������ + * @param {String} arg.baseURL - ��������������� + * @param {Object} arg.header - ������header + * @param {String} arg.method = [GET|POST|PUT|DELETE|CONNECT|HEAD|OPTIONS|TRACE] - ������������������������ + * @param {String} arg.dataType = [json] - ���������������dataType + * @param {String} arg.responseType = [text|arraybuffer] - ���������������responseType������������������������������ + * @param {Object} arg.custom - ������������������������������ + * @param {Number} arg.timeout - ������������������������������������ ms���������60000���H5(HBuilderX 2.9.9+)���APP(HBuilderX 2.9.9+)���������������������2.10.0������������������������ + * @param {Boolean} arg.sslVerify - ��������������������������� ssl ���������������true.���App������������������HBuilderX 2.3.3+��� + * @param {Boolean} arg.withCredentials - ���������������������������������������������������cookies������������false������H5���������HBuilderX 2.6.15+��� + * @param {Boolean} arg.firstIpv4 - ���DNS���������������������ipv4���������false������ App-Android ������ (HBuilderX 2.8.0+) + * @param {Function(statusCode):Boolean} arg.validateStatus - ������������������������������������������statusCode >= 200 && statusCode < 300 + */ + constructor(arg = {}) { + if (!isPlainObject(arg)) { + arg = {} + console.warn('������������������������������������Object') + } + this.config = clone({ ...defaults, ...arg }) + this.interceptors = { + request: new InterceptorManager(), + response: new InterceptorManager() + } + } + + /** + * @Function + * @param {Request~setConfigCallback} f - ������������������������ + */ + setConfig(f) { + this.config = f(this.config) + } + + middleware(config) { + config = mergeConfig(this.config, config) + const chain = [dispatchRequest, undefined] + let promise = Promise.resolve(config) + + this.interceptors.request.forEach((interceptor) => { + chain.unshift(interceptor.fulfilled, interceptor.rejected) + }) + + this.interceptors.response.forEach((interceptor) => { + chain.push(interceptor.fulfilled, interceptor.rejected) + }) + + while (chain.length) { + promise = promise.then(chain.shift(), chain.shift()) + } + + return promise + } + + /** + * @Function + * @param {Object} config - ��������������� + * @prop {String} options.url - ������������ + * @prop {Object} options.data - ������������ + * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - ��������������������� + * @prop {Object} [options.dataType = config.dataType] - ������������ json��������������������������������������� JSON.parse + * @prop {Object} [options.header = config.header] - ������header + * @prop {Object} [options.method = config.method] - ������������ + * @returns {Promise<unknown>} + */ + request(config = {}) { + return this.middleware(config) + } + + get(url, options = {}) { + return this.middleware({ + url, + method: 'GET', + ...options + }) + } + + post(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'POST', + ...options + }) + } + + // #ifndef MP-ALIPAY + put(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'PUT', + ...options + }) + } + + // #endif + + // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU + delete(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'DELETE', + ...options + }) + } + + // #endif + + // #ifdef H5 || MP-WEIXIN + connect(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'CONNECT', + ...options + }) + } + + // #endif + + // #ifdef H5 || MP-WEIXIN || MP-BAIDU + head(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'HEAD', + ...options + }) + } + + // #endif + + // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-BAIDU + options(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'OPTIONS', + ...options + }) + } + + // #endif + + // #ifdef H5 || MP-WEIXIN + trace(url, data, options = {}) { + return this.middleware({ + url, + data, + method: 'TRACE', + ...options + }) + } + + // #endif + + upload(url, config = {}) { + config.url = url + config.method = 'UPLOAD' + return this.middleware(config) + } + + download(url, config = {}) { + config.url = url + config.method = 'DOWNLOAD' + return this.middleware(config) + } +} + +/** + * setConfig������ + * @return {Object} - ������������������config + * @callback Request~setConfigCallback + * @param {Object} config - ������������config + */ -- Gitblit v1.8.0