* @Function * @param {Object} options - 请求配置项 * @prop {String} options.url - 请求路径 * @prop {Object} options.data - 请求参数 * @prop {Object} [options.responseType = config.responseType] [text|arraybuffer] - 响应的数据类型 * @prop {Object} [options.dataType = config.dataType] - 如果设为 json,会尝试对
(options = {})
| 103 | * @returns {Promise<unknown>} |
| 104 | */ |
| 105 | async request(options = {}) { |
| 106 | return new Promise((resolve, reject) => { |
| 107 | options.baseUrl = this.config.baseUrl |
| 108 | options.dataType = options.dataType || this.config.dataType |
| 109 | // #ifndef MP-ALIPAY || APP-PLUS |
| 110 | options.responseType = options.responseType || this.config.responseType |
| 111 | // #endif |
| 112 | // #ifdef MP-ALIPAY || MP-WEIXIN |
| 113 | options.timeout = options.timeout || this.config.timeout |
| 114 | // #endif |
| 115 | // #ifdef H5 |
| 116 | options.withCredentials = isBoolean(options.withCredentials) ? options.withCredentials : this.config.withCredentials |
| 117 | // #endif |
| 118 | options.url = options.url || '' |
| 119 | options.data = options.data || {} |
| 120 | options.params = options.params || {} |
| 121 | options.header = {...this.config.header, ...(options.header || {})} |
| 122 | options.method = options.method || this.config.method |
| 123 | options.custom = {...this.config.custom,...(options.custom || {})} |
| 124 | // #ifdef APP-PLUS |
| 125 | options.sslVerify = options.sslVerify === undefined ? this.config.sslVerify : options.sslVerify |
| 126 | // #endif |
| 127 | options.getTask = options.getTask || this.config.getTask |
| 128 | let next = true |
| 129 | const cancel = (t = 'handle cancel', config = options) => { |
| 130 | const err = { |
| 131 | errMsg: t, |
| 132 | config: config |
| 133 | } |
| 134 | reject(err) |
| 135 | next = false |
| 136 | } |
| 137 | |
| 138 | const handleRe = {...this.requestBeforeFun(options, cancel)} |
| 139 | const _config = {...handleRe} |
| 140 | if (!next) return |
| 141 | const requestTask = uni.request({ |
| 142 | url: buildURL(buildFullPath(_config.baseUrl, _config.url), _config.params), |
| 143 | data: _config.data, |
| 144 | header: _config.header, |
| 145 | method: _config.method, |
| 146 | // #ifdef MP-ALIPAY || MP-WEIXIN |
| 147 | timeout: _config.timeout, |
| 148 | // #endif |
| 149 | dataType: _config.dataType, |
| 150 | // #ifndef MP-ALIPAY || APP-PLUS |
| 151 | responseType: _config.responseType, |
| 152 | // #endif |
| 153 | // #ifdef APP-PLUS |
| 154 | sslVerify: _config.sslVerify, |
| 155 | // #endif |
| 156 | // #ifdef H5 |
| 157 | withCredentials: _config.withCredentials, |
| 158 | // #endif |
| 159 | complete: (response) => { |
| 160 | response.config = handleRe |
| 161 | if (this.validateStatus(response.statusCode)) { // 成功 |
| 162 | response = this.requestComFun(response) |