Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DiDi
DoraemonKit
提交
432fee9f
D
DoraemonKit
项目概览
DiDi
/
DoraemonKit
10 个月 前同步成功
通知
166
Star
19623
Fork
3062
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DoraemonKit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
432fee9f
编写于
9月 06, 2021
作者:
C
caoenze
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bugfix(Web): 修复api-mock拦截接口导致跨域错误
上级
84adf606
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
61 addition
and
27 deletion
+61
-27
Web/packages/utils/src/network.js
Web/packages/utils/src/network.js
+13
-3
Web/packages/web/src/plugins/api-mock/index.js
Web/packages/web/src/plugins/api-mock/index.js
+22
-2
Web/playground/index.html
Web/playground/index.html
+26
-22
未找到文件。
Web/packages/utils/src/network.js
浏览文件 @
432fee9f
...
...
@@ -31,11 +31,17 @@ export class Request extends EventEmitter{
const
originOpen
=
winXhrProto
.
open
;
const
originSetRequestHeader
=
winXhrProto
.
setRequestHeader
;
// XMLHttp
window
.
XMLHttpRequest
.
prototype
.
setRequestHeader
=
function
(){
originSetRequestHeader
.
apply
(
this
,
arguments
);
window
.
XMLHttpRequest
.
prototype
.
setRequestHeader
=
function
(...
args
){
if
(
Req
.
hookXhrConfig
.
onBeforeSetRequestHeader
)
{
args
=
Req
.
hookXhrConfig
.
onBeforeSetRequestHeader
(
args
,
this
.
reqConf
);
// 返回false则取消设置请求头 (api-mock拦截接口,会将post改为get 此时设置请求头Content-Type会报跨域错误)
args
&&
originSetRequestHeader
.
apply
(
this
,
args
);
}
else
{
originSetRequestHeader
.
apply
(
this
,
args
);
}
}
window
.
XMLHttpRequest
.
prototype
.
open
=
function
(...
args
)
{
let
originArgs
=
{...
args
}
args
=
Req
.
hookXhrConfig
.
onBeforeOpen
&&
Req
.
hookXhrConfig
.
onBeforeOpen
(
args
)
||
args
const
xhr
=
this
;
this
.
reqConf
=
{
...
...
@@ -44,6 +50,10 @@ export class Request extends EventEmitter{
requestInfo
:
{
method
:
args
[
0
].
toUpperCase
(),
url
:
args
[
1
]
},
originRequestInfo
:
{
method
:
originArgs
[
0
].
toUpperCase
(),
url
:
originArgs
[
1
]
}
}
...
...
Web/packages/web/src/plugins/api-mock/index.js
浏览文件 @
432fee9f
...
...
@@ -80,10 +80,11 @@ export default new RouterPlugin({
})
}
})
sceneId
&&
fetchArgs
[
1
]
&&
(
fetchArgs
[
1
].
method
=
'
get
'
)
sceneId
&&
fetchArgs
[
1
]
&&
(
fetchArgs
[
1
].
method
=
'
get
'
)
&&
(
fetchArgs
[
1
].
headers
&&
delete
fetchArgs
[
1
].
headers
)
sceneId
&&
(
fetchArgs
[
0
]
=
`
${
mockBaseUrl
}
/api/app/scene/
${
sceneId
}
`
)
return
fetchArgs
;
}
},
});
request
.
hookXhr
({
onBeforeOpen
:
(
args
)
=>
{
...
...
@@ -104,6 +105,25 @@ export default new RouterPlugin({
sceneId
&&
(
args
[
1
]
=
`
${
mockBaseUrl
}
/api/app/scene/
${
sceneId
}
`
)
return
args
;
},
onBeforeSetRequestHeader
:
(
args
,
config
)
=>
{
let
checkedInterfaceList
=
getCheckedInterfaceList
(
state
.
interfaceList
)
let
url
=
config
.
originRequestInfo
.
url
;
let
path
=
`/`
+
getPartUrlByParam
(
url
,
'
path
'
)
let
sceneId
=
''
checkedInterfaceList
.
forEach
(
i
=>
{
if
(
i
.
path
===
path
)
{
i
.
sceneList
.
forEach
(
scene
=>
{
if
(
scene
.
checked
)
{
sceneId
=
scene
.
_id
}
})
}
})
if
(
sceneId
)
{
return
false
}
return
args
}
});
},
onUnload
(){}
...
...
Web/playground/index.html
浏览文件 @
432fee9f
...
...
@@ -19,29 +19,33 @@
</script>
<script>
setTimeout
(()
=>
{
fetch
(
'
https://www.tianqiapi.com/free/week?appid=68852321&appsecret=BgGLDVc7
'
,
{
// body: JSON.stringify({
// a:1
// }),
method
:
'
post
'
,
})
.
then
((
response
)
=>
{
let
weatherInfo
=
response
.
json
();
return
weatherInfo
;
})
.
then
((
info
)
=>
{
console
.
log
(
'
weatherInfo
'
,
info
)
});
// fetch('https://www.tianqiapi.com/free/week?appid=68852321&appsecret=BgGLDVc7', {
// // body: JSON.stringify({
// // a:1
// // }),
// headers: new Headers({
// 'Content-Type': 'text/html; charset=utf-8'
// }),
// method: 'post',
// })
// .then((response) => {
// let weatherInfo = response.json();
// return weatherInfo;
// })
// .then((info) => {
// console.log('weatherInfo', info)
// });
// function reqListener () {
// console.log('asasdasd', this.responseText);
// }
// var oReq = new XMLHttpRequest();
// oReq.addEventListener("load", reqListener);
// oReq.open("post", "https://www.tianqiapi.com/free/week?appid=68852321&appsecret=BgGLDVc7");
// oReq.send(JSON.stringify({
// a:1
// }));
function
reqListener
()
{
console
.
log
(
'
asasdasd
'
,
this
.
responseText
);
}
var
oReq
=
new
XMLHttpRequest
();
oReq
.
addEventListener
(
"
load
"
,
reqListener
);
oReq
.
open
(
"
post
"
,
"
https://www.tianqiapi.com/free/week?appid=68852321&appsecret=BgGLDVc7
"
);
oReq
.
setRequestHeader
(
'
Content-Type
'
,
'
text/html; charset=utf-8
'
)
oReq
.
send
(
JSON
.
stringify
({
a
:
1
}));
},
5000
);
</script>
</html>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录