-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1243 from XiaoMi/develop
3.0.0
- Loading branch information
Showing
718 changed files
with
59,672 additions
and
40,533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es2021: true | ||
}, | ||
extends: ['plugin:react/recommended', 'standard', 'prettier'], | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true | ||
}, | ||
ecmaVersion: 12, | ||
sourceType: 'module' | ||
}, | ||
plugins: ['react', 'prettier'], | ||
rules: { | ||
'prettier/prettier': ['error', { singleQuote: true, semi: false, printWidth: 120, trailingComma: 'none' }], | ||
'react/prop-types': 0, | ||
'react/no-children-prop': 0, | ||
'react/display-name': 0 | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import axios from 'axios' | ||
|
||
const callBackInter = new Map() | ||
|
||
const axiosInstance = axios.create({ | ||
type: 'basics', | ||
url: '', | ||
responseType: 'json' | ||
}) | ||
|
||
axiosInstance.interceptors.request.use( | ||
(config) => { | ||
if (callBackInter.has('beforeRequest')) { | ||
return callBackInter.get('beforeRequest')(config) | ||
} | ||
return config | ||
}, | ||
(error) => { | ||
if (callBackInter.has('errorRequest')) { | ||
return callBackInter.get('errorRequest')(error) | ||
} | ||
callBackInter.has('errorCallback') && callBackInter.get('errorCallback')(error) | ||
return error | ||
} | ||
) | ||
|
||
axiosInstance.interceptors.response.use( | ||
(response) => { | ||
if (callBackInter.has('beforeResponse')) { | ||
return callBackInter.get('beforeResponse')(response) | ||
} | ||
return response | ||
}, | ||
(error) => { | ||
if (callBackInter.has('errorResponse')) { | ||
return callBackInter.get('errorResponse')(error) | ||
} | ||
callBackInter.has('errorCallback') && callBackInter.get('errorCallback')(error) | ||
return error | ||
} | ||
) | ||
|
||
const axiosIns = (options) => { | ||
const { beforeResponse, errorResponse, beforeRequest, errorRequest, data, errorCallback } = options | ||
beforeRequest && callBackInter.set('beforeRequest', beforeRequest) | ||
errorResponse && callBackInter.set('errorResponse', errorResponse) | ||
beforeResponse && callBackInter.set('beforeResponse', beforeResponse) | ||
errorRequest && callBackInter.set('errorRequest', errorRequest) | ||
errorCallback && callBackInter.set('errorCallback', errorCallback) | ||
|
||
return axiosInstance({ ...options }) | ||
} | ||
export { axios } | ||
export default axiosIns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import axiosIns from './axios' | ||
|
||
const download = (options, host) => { | ||
const { filename = '未命名' } = options | ||
const url = host ? host + options.url : options.url | ||
|
||
// 设置类型,防止出现乱码 | ||
Object.assign(options, { responseType: 'blob' }) | ||
axiosIns({ ...options, url }).then( | ||
(res) => { | ||
const { downloadSuccess } = options | ||
const blob = new window.Blob([res.data]) | ||
const downloadElement = document.createElement('a') | ||
const href = window.URL.createObjectURL(blob) // 创建下载的链接 | ||
downloadElement.href = href | ||
downloadElement.download = filename // 下载后文件名 | ||
document.body.appendChild(downloadElement) | ||
downloadElement.click() // 点击下载 | ||
document.body.removeChild(downloadElement) // 下载完成移除元素 | ||
window.URL.revokeObjectURL(href) // 释放blob对象 | ||
downloadSuccess && downloadSuccess(res) | ||
}, | ||
(error) => { | ||
const { downloadFail } = options | ||
downloadFail && downloadFail(error) | ||
} | ||
) | ||
} | ||
export default download |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import jsonp from './jsonp' | ||
import download from './download' | ||
import upload from './upload' | ||
import axiosIns, { axios } from './axios' | ||
|
||
/** | ||
* 请求方法 | ||
* @param options | ||
* @param baseUrl | ||
*/ | ||
|
||
const InternalRequest = (options, host) => { | ||
const { type = 'basics' } = options | ||
const url = host ? host + options.url : options.url | ||
if (type === 'jsonp' || type === 'download') { | ||
return type === 'jsonp' ? jsonp : download | ||
} | ||
return axiosIns( | ||
type === 'upload' | ||
? { | ||
url, | ||
method: 'post', | ||
...upload(options).options | ||
} | ||
: { | ||
url, | ||
type: 'basics', | ||
...options | ||
} | ||
) | ||
} | ||
const HiRequest = (options, host) => { | ||
return InternalRequest(options, host) | ||
} | ||
// 请求语法糖: reguest.get HiRequest.post …… | ||
const METHODS = ['get', 'post', 'delete', 'put', 'patch', 'head', 'options'] | ||
METHODS.forEach((method) => { | ||
HiRequest[method] = (url, options) => HiRequest({ ...options, method, url }) | ||
}) | ||
// 取消请求 | ||
const CANCEL = ['CancelToken', 'Cancel', 'isCancel'] | ||
CANCEL.forEach((type) => { | ||
HiRequest[type] = axios[type] | ||
}) | ||
// add jsonp | ||
HiRequest.jsonp = jsonp | ||
// download | ||
HiRequest.download = download | ||
// upload | ||
HiRequest.upload = (options, host) => { | ||
options.type = 'upload' | ||
return HiRequest(options, host) | ||
} | ||
|
||
// Expose all/spread | ||
HiRequest.all = (promises) => { | ||
return Promise.all(promises) | ||
} | ||
HiRequest.spread = (callback) => { | ||
return (arr) => { | ||
return callback.apply(null, arr) | ||
} | ||
} | ||
|
||
export default HiRequest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const defaultJsonpOptions = { | ||
timeout: 5000, | ||
jsonpCallback: 'callback' | ||
} | ||
|
||
const generateCallbackFunction = () => { | ||
return `jsonp_${Date.now()}_${Math.ceil(Math.random() * 100000)}` | ||
} | ||
const clearFunction = (functionName) => { | ||
try { | ||
delete window[functionName] | ||
} catch (e) { | ||
window[functionName] = undefined | ||
} | ||
} | ||
|
||
const removeScript = (scriptId) => { | ||
const script = document.getElementById(scriptId) | ||
if (script) { | ||
document.getElementsByTagName('head')[0].removeChild(script) | ||
} | ||
} | ||
const jsonp = (_url, options = defaultJsonpOptions) => { | ||
const { timeout, jsonpCallback } = options | ||
let url = _url | ||
let timeoutId | ||
return new Promise((resolve, reject) => { | ||
const callbackFunction = | ||
options && options.jsonpCallbackFunction ? options.jsonpCallbackFunction : generateCallbackFunction() | ||
|
||
const scriptId = `${jsonpCallback}_${callbackFunction}` | ||
|
||
window[callbackFunction] = (response) => { | ||
resolve({ | ||
ok: true, | ||
// keep consistent with fetch API | ||
json: () => Promise.resolve(response) | ||
}) | ||
|
||
if (timeoutId) clearTimeout(timeoutId) | ||
|
||
removeScript(scriptId) | ||
|
||
clearFunction(callbackFunction) | ||
} | ||
|
||
url += url.indexOf('?') === -1 ? '?' : '&' | ||
|
||
const jsonpScript = document.createElement('script') | ||
jsonpScript.setAttribute('src', `${url}${jsonpCallback}=${callbackFunction}`) | ||
if (options && options.charset) { | ||
jsonpScript.setAttribute('charset', options.charset) | ||
} | ||
jsonpScript.id = scriptId | ||
document.getElementsByTagName('head')[0].appendChild(jsonpScript) | ||
|
||
timeoutId = setTimeout(() => { | ||
reject(new Error(`JSONP request to ${_url} timed out`)) | ||
|
||
clearFunction(callbackFunction) | ||
removeScript(scriptId) | ||
window[callbackFunction] = () => { | ||
clearFunction(callbackFunction) | ||
} | ||
}, timeout) | ||
|
||
// Caught if got 404/500 | ||
jsonpScript.onerror = () => { | ||
reject(new Error(`JSONP request to ${_url} failed`)) | ||
|
||
clearFunction(callbackFunction) | ||
removeScript(scriptId) | ||
if (timeoutId) clearTimeout(timeoutId) | ||
} | ||
}) | ||
} | ||
export default jsonp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const upload = (options) => { | ||
const { file, name = 'file', params = {}, headers, data } = options | ||
const formFile = new window.FormData() | ||
if (file) { | ||
formFile.append(name, file) | ||
} | ||
// 设置除file外需要带入的参数 | ||
if (params) { | ||
Object.keys(params).forEach((key) => { | ||
formFile.append(key, params[key]) | ||
}) | ||
} | ||
return { | ||
options: Object.assign({ | ||
...options, | ||
data: data || formFile, | ||
headers: { ...headers, 'Content-Type': 'multipart/form-data' } | ||
}) | ||
} | ||
} | ||
export default upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './depreactedPropsCompat' | ||
export { default as SwitchVersion } from './SwitchVersion' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ export function warningOnce (condition, format, args) { | |
warned[format] = !condition | ||
} | ||
} | ||
|
Oops, something went wrong.