Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
app-manage-console
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
ReactNative
app-manage-console
Commits
e7fdde55
提交
e7fdde55
authored
11月 28, 2017
作者:
vipcxj
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'origin/master'
# Conflicts: # .idea/jsLibraryMappings.xml # .idea/misc.xml
上级
abae3de5
3b9bb58a
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
133 行增加
和
74 行删除
+133
-74
.gitignore
.gitignore
+3
-0
delete.js
src/utils/delete.js
+10
-18
http-helper.js
src/utils/http-helper.js
+23
-17
middleware.js
src/utils/middleware.js
+71
-0
post.js
src/utils/post.js
+10
-18
request.js
src/utils/request.js
+11
-17
index.js
stories/index.js
+5
-4
没有找到文件。
.gitignore
浏览文件 @
e7fdde55
...
@@ -10,3 +10,6 @@
...
@@ -10,3 +10,6 @@
# misc
# misc
.DS_Store
.DS_Store
npm-debug.log*
npm-debug.log*
/.idea
/storybook-static
src/utils/delete.js
浏览文件 @
e7fdde55
import
_
from
'lodash'
;
/* eslint-disable no-param-reassign */
import
{
isNil
,
defaults
}
from
'lodash'
;
import
{
fetch
}
from
'./polyfill'
;
import
{
fetch
}
from
'./polyfill'
;
import
{
encrypt
}
from
'./helper'
;
import
{
errors
}
from
'./error'
;
import
{
getToken
}
from
'./auth'
;
import
{
checkStatus
,
normParams
,
parseObject
}
from
'./http-helper'
;
import
{
checkStatus
,
normParams
,
parseObject
}
from
'./http-helper'
;
import
middleware
from
'./middleware'
;
const
defaultOptions
=
{
const
defaultOptions
=
{
headers
:
{
Accept
:
'application/json'
},
headers
:
{
Accept
:
'application/json'
},
...
@@ -12,32 +11,25 @@ const defaultOptions = {
...
@@ -12,32 +11,25 @@ const defaultOptions = {
/**
/**
* Requests a URL, returning a promise.
* Requests a URL, returning a promise.
*
*
* @param {string} url The URL we want to
request
* @param {string} url The URL we want to
delete
* @param auth
* @param auth
* @param params
* @param params
* @param {object} [options] The options we want to pass to "fetch"
* @param {object} [options] The options we want to pass to "fetch"
* @return {object} An object containing either "data" or "err"
* @return {object} An object containing either "data" or "err"
*/
*/
export
default
async
function
doDelete
(
url
,
params
=
{},
options
=
{},
auth
=
true
)
{
export
default
async
function
doDelete
(
url
,
params
=
{},
options
=
{},
auth
=
true
)
{
let
token
;
params
=
normParams
(
params
);
if
(
auth
)
{
const
res
=
await
middleware
.
delete
.
onRequest
(
url
,
params
,
options
,
auth
);
token
=
await
getToken
();
let
queryParams
=
(
res
?
res
.
params
:
params
).
map
(([
k
,
v
])
=>
(
isNil
(
v
)
?
k
:
`
${
k
}
=
${
encodeURIComponent
(
v
)}
`
));
if
(
!
token
)
{
return
Promise
.
reject
(
errors
.
tokenMissing
());
}
token
=
encrypt
(
token
);
}
let
queryParams
=
token
?
[...
normParams
(
params
),
[
'token'
,
token
]]
:
normParams
(
params
);
queryParams
=
queryParams
.
map
(([
k
,
v
])
=>
(
_
.
isNil
(
v
)
?
k
:
`
${
k
}
=
${
encodeURIComponent
(
v
)}
`
));
queryParams
=
queryParams
.
join
(
'&'
);
queryParams
=
queryParams
.
join
(
'&'
);
let
realUrl
=
url
;
let
realUrl
=
res
?
res
.
url
:
url
;
if
(
queryParams
)
{
if
(
queryParams
)
{
realUrl
=
`
${
url
}
?
${
queryParams
}
`
;
realUrl
=
`
${
url
}
?
${
queryParams
}
`
;
}
}
const
realOptions
=
_
.
defaults
(
options
,
defaultOptions
);
const
realOptions
=
defaults
(
res
?
res
.
options
:
options
,
defaultOptions
);
realOptions
.
method
=
'DELETE'
;
realOptions
.
method
=
'DELETE'
;
return
fetch
(
realUrl
,
realOptions
)
return
fetch
(
realUrl
,
realOptions
)
.
then
(
checkStatus
)
.
then
(
checkStatus
)
.
then
(
parseObject
);
.
then
(
resp
=>
parseObject
(
resp
,
middleware
.
delete
.
onResponse
)
);
}
}
src/utils/http-helper.js
浏览文件 @
e7fdde55
...
@@ -21,7 +21,7 @@ export function normParams(unnormed) {
...
@@ -21,7 +21,7 @@ export function normParams(unnormed) {
}
}
// eslint-disable-next-line max-len
// eslint-disable-next-line max-len
export
function
parseObject
(
response
,
{
num2str
=
false
,
bool2str
=
false
,
nul2str
=
false
,
ud2str
=
false
,
nil2str
=
false
}
=
{})
{
export
function
parseObject
(
response
,
middleware
,
{
num2str
=
false
,
bool2str
=
false
,
nul2str
=
false
,
ud2str
=
false
,
nil2str
=
false
}
=
{})
{
if
(
response
.
status
===
204
)
{
// no-content
if
(
response
.
status
===
204
)
{
// no-content
return
null
;
return
null
;
}
else
{
}
else
{
...
@@ -73,26 +73,32 @@ export function parseObject(response, { num2str = false, bool2str = false, nul2s
...
@@ -73,26 +73,32 @@ export function parseObject(response, { num2str = false, bool2str = false, nul2s
const
mapValue
=
_
.
curry
(
mapObj
)(
_
,
mapArr
);
const
mapValue
=
_
.
curry
(
mapObj
)(
_
,
mapArr
);
if
(
contentType
.
indexOf
(
'json'
)
!==
-
1
)
{
if
(
contentType
.
indexOf
(
'json'
)
!==
-
1
)
{
return
response
.
json
()
return
response
.
json
()
.
then
((
json
)
=>
{
.
then
(
json
=>
(
middleware
?
middleware
(
json
)
:
json
))
if
(
json
.
errorCode
===
0
)
{
.
then
((
data
)
=>
{
let
{
data
}
=
json
;
let
out
=
data
;
if
(
_
.
isObjectLike
(
data
))
{
if
(
_
.
isObjectLike
(
out
))
{
data
=
new
Resolver
(
data
).
resolve
();
out
=
new
Resolver
(
out
).
resolve
();
}
return
needMap
?
mapValue
(
data
)
:
data
;
}
else
{
throw
new
Error
(
json
.
message
);
}
}
return
needMap
?
mapValue
(
out
)
:
out
;
});
});
}
else
if
(
contentType
.
indexOf
(
'xml'
)
!==
-
1
)
{
}
else
if
(
contentType
.
indexOf
(
'xml'
)
!==
-
1
)
{
return
response
.
text
().
then
((
text
)
=>
{
return
response
.
text
()
return
require
.
ensure
([],
(
require
)
=>
{
.
then
((
text
)
=>
{
const
{
parseString
}
=
require
(
'xml2js'
);
return
require
.
ensure
([],
(
require
)
=>
{
const
options
=
{};
const
{
parseString
}
=
require
(
'xml2js'
);
const
json
=
JSON
.
parse
(
parseString
(
text
,
options
));
const
options
=
{};
return
needMap
?
mapValue
(
json
)
:
json
;
const
json
=
JSON
.
parse
(
parseString
(
text
,
options
));
return
json
;
});
})
.
then
((
json
=>
(
middleware
?
middleware
(
json
)
:
json
)))
.
then
((
data
)
=>
{
let
out
=
data
;
if
(
_
.
isObjectLike
(
out
))
{
out
=
new
Resolver
(
out
).
resolve
();
}
return
needMap
?
mapValue
(
out
)
:
out
;
});
});
});
}
else
if
(
contentType
.
indexOf
(
'text'
)
!==
-
1
)
{
}
else
if
(
contentType
.
indexOf
(
'text'
)
!==
-
1
)
{
return
response
.
text
();
return
response
.
text
();
}
else
{
}
else
{
...
...
src/utils/middleware.js
0 → 100644
浏览文件 @
e7fdde55
/* eslint-disable no-unused-vars */
import
{
getToken
}
from
'./auth'
;
import
{
encrypt
}
from
'./helper'
;
import
{
errors
}
from
'./error'
;
const
putTokenOnUrl
=
async
(
url
,
params
,
options
)
=>
{
let
token
=
await
getToken
();
if
(
!
token
)
{
throw
errors
.
tokenMissing
();
}
token
=
encrypt
(
token
);
return
{
url
,
options
,
params
:
[...
params
,
[
'token'
,
token
]],
};
};
const
putTokenToBody
=
async
(
url
,
data
,
params
,
options
)
=>
{
let
token
=
await
getToken
();
if
(
!
token
)
{
throw
errors
.
tokenMissing
();
}
token
=
encrypt
(
token
);
return
{
url
,
params
,
options
,
data
:
{
...
data
,
token
,
},
};
};
const
parseResponse
=
(
response
)
=>
{
const
{
errorCode
,
data
,
message
}
=
response
;
if
(
errorCode
===
0
)
{
return
data
;
}
else
{
throw
new
Error
(
message
||
data
);
}
};
export
default
{
get
:
{
onRequest
:
async
(
url
,
params
,
options
,
auth
)
=>
{
if
(
auth
)
{
return
putTokenOnUrl
(
url
,
params
,
options
);
}
},
onResponse
:
parseResponse
,
},
post
:
{
onRequest
:
async
(
url
,
data
,
params
,
options
,
auth
)
=>
{
if
(
auth
)
{
return
putTokenToBody
(
url
,
data
,
params
,
options
);
}
},
onResponse
:
parseResponse
,
},
delete
:
{
onRequest
:
async
(
url
,
params
,
options
,
auth
)
=>
{
if
(
auth
)
{
return
putTokenOnUrl
(
url
,
params
,
options
);
}
},
onResponse
:
parseResponse
,
},
};
src/utils/post.js
浏览文件 @
e7fdde55
/* eslint-disable no-param-reassign */
/* eslint-disable no-param-reassign */
import
_
from
'lodash'
;
import
{
isNil
,
defaults
}
from
'lodash'
;
import
{
fetch
}
from
'./polyfill'
;
import
{
fetch
}
from
'./polyfill'
;
import
{
encrypt
}
from
'./helper'
;
import
{
checkStatus
,
normParams
,
parseObject
}
from
'./http-helper'
;
import
{
checkStatus
,
normParams
,
parseObject
}
from
'./http-helper'
;
import
{
getToken
}
from
'./auth'
;
import
middleware
from
'./middleware'
;
import
{
errors
}
from
'./error'
;
const
defaultOptions
=
{
const
defaultOptions
=
{
headers
:
{
headers
:
{
...
@@ -12,26 +10,20 @@ const defaultOptions = {
...
@@ -12,26 +10,20 @@ const defaultOptions = {
},
},
};
};
export
default
async
function
post
(
url
,
data
,
params
=
{},
options
=
{},
auth
=
true
)
{
export
default
async
function
post
(
url
,
data
,
params
=
{},
options
=
{},
auth
=
true
)
{
if
(
!
data
)
{
if
(
!
data
)
{
data
=
{};
data
=
{};
}
}
if
(
auth
)
{
params
=
normParams
(
params
);
const
token
=
await
getToken
();
const
res
=
await
middleware
.
post
.
onRequest
(
url
,
data
,
params
,
options
,
auth
);
if
(
!
token
)
{
let
queryParams
=
res
?
res
.
params
:
params
;
return
Promise
.
reject
(
errors
.
tokenMissing
());
queryParams
=
queryParams
.
map
(([
k
,
v
])
=>
(
isNil
(
v
)
?
k
:
`
${
k
}
=
${
encodeURIComponent
(
v
)}
`
));
}
data
.
token
=
encrypt
(
token
);
}
let
queryParams
=
normParams
(
params
);
queryParams
=
queryParams
.
map
(([
k
,
v
])
=>
(
_
.
isNil
(
v
)
?
k
:
`
${
k
}
=
${
encodeURIComponent
(
v
)}
`
));
queryParams
=
queryParams
.
join
(
'&'
);
queryParams
=
queryParams
.
join
(
'&'
);
let
realUrl
=
url
;
let
realUrl
=
res
?
res
.
url
:
url
;
if
(
queryParams
)
{
if
(
queryParams
)
{
realUrl
=
`
${
url
}
?
${
queryParams
}
`
;
realUrl
=
`
${
url
}
?
${
queryParams
}
`
;
}
}
const
realOptions
=
_
.
defaults
(
options
,
defaultOptions
);
const
realOptions
=
defaults
(
res
?
res
.
options
:
options
,
defaultOptions
);
if
(
!
realOptions
.
headers
)
{
if
(
!
realOptions
.
headers
)
{
realOptions
.
headers
=
{
realOptions
.
headers
=
{
Accept
:
'application/json'
,
Accept
:
'application/json'
,
...
@@ -39,8 +31,8 @@ export default async function post(url, data, params = {}, options = {}, auth =
...
@@ -39,8 +31,8 @@ export default async function post(url, data, params = {}, options = {}, auth =
}
}
realOptions
.
headers
[
'Content-Type'
]
=
'application/json'
;
realOptions
.
headers
[
'Content-Type'
]
=
'application/json'
;
realOptions
.
method
=
'POST'
;
realOptions
.
method
=
'POST'
;
realOptions
.
body
=
JSON
.
stringify
(
data
);
realOptions
.
body
=
JSON
.
stringify
(
res
?
res
.
data
:
data
);
return
fetch
(
realUrl
,
realOptions
)
return
fetch
(
realUrl
,
realOptions
)
.
then
(
checkStatus
)
.
then
(
checkStatus
)
.
then
(
parseObject
);
.
then
(
resp
=>
parseObject
(
resp
,
middleware
.
post
.
onResponse
)
);
}
}
src/utils/request.js
浏览文件 @
e7fdde55
import
_
from
'lodash'
;
import
{
isNil
,
defaults
}
from
'lodash'
;
import
{
fetch
}
from
'./polyfill'
;
import
{
fetch
}
from
'./polyfill'
;
import
{
encrypt
}
from
'./helper'
;
import
{
getToken
}
from
'./auth'
;
import
{
errors
}
from
'./error'
;
import
{
checkStatus
,
normParams
,
parseObject
}
from
'./http-helper'
;
import
{
checkStatus
,
normParams
,
parseObject
}
from
'./http-helper'
;
import
middleware
from
'./middleware'
;
const
defaultOptions
=
{
const
defaultOptions
=
{
headers
:
{
Accept
:
'application/json'
},
headers
:
{
Accept
:
'application/json'
},
...
@@ -19,23 +17,19 @@ const defaultOptions = {
...
@@ -19,23 +17,19 @@ const defaultOptions = {
* @return {object} An object containing either "data" or "err"
* @return {object} An object containing either "data" or "err"
*/
*/
export
default
async
function
request
(
url
,
params
=
{},
options
=
{},
auth
=
true
)
{
export
default
async
function
request
(
url
,
params
=
{},
options
=
{},
auth
=
true
)
{
let
token
;
const
normedParams
=
normParams
(
params
);
if
(
auth
)
{
const
res
=
await
middleware
.
get
.
onRequest
(
url
,
normedParams
,
options
,
auth
);
token
=
await
getToken
();
const
newUrl
=
res
?
res
.
url
:
url
;
if
(
!
token
)
{
const
newParams
=
res
?
res
.
params
:
normedParams
;
return
Promise
.
reject
(
errors
.
tokenMissing
());
const
newOptions
=
res
?
res
.
options
:
options
;
}
let
queryParams
=
newParams
.
map
(([
k
,
v
])
=>
(
isNil
(
v
)
?
k
:
`
${
k
}
=
${
encodeURIComponent
(
v
)}
`
));
token
=
encrypt
(
token
);
}
let
queryParams
=
token
?
[...
normParams
(
params
),
[
'token'
,
token
]]
:
normParams
(
params
);
queryParams
=
queryParams
.
map
(([
k
,
v
])
=>
(
_
.
isNil
(
v
)
?
k
:
`
${
k
}
=
${
encodeURIComponent
(
v
)}
`
));
queryParams
=
queryParams
.
join
(
'&'
);
queryParams
=
queryParams
.
join
(
'&'
);
let
realUrl
=
u
rl
;
let
realUrl
=
newU
rl
;
if
(
queryParams
)
{
if
(
queryParams
)
{
realUrl
=
`
${
url
}
?
${
queryParams
}
`
;
realUrl
=
`
${
url
}
?
${
queryParams
}
`
;
}
}
return
fetch
(
realUrl
,
_
.
defaults
(
o
ptions
,
defaultOptions
))
return
fetch
(
realUrl
,
defaults
(
newO
ptions
,
defaultOptions
))
.
then
(
checkStatus
)
.
then
(
checkStatus
)
.
then
(
parseObject
);
.
then
(
resp
=>
parseObject
(
resp
,
middleware
.
get
.
onResponse
)
);
}
}
stories/index.js
浏览文件 @
e7fdde55
...
@@ -13,13 +13,14 @@ import TableInput from '../src/components/table/input-table';
...
@@ -13,13 +13,14 @@ import TableInput from '../src/components/table/input-table';
import
{
makeCancelable
}
from
'../src/utils/promise'
;
import
{
makeCancelable
}
from
'../src/utils/promise'
;
import
{
login
}
from
'../src/services/login'
;
import
{
login
}
from
'../src/services/login'
;
import
{
switchDomain
}
from
'../src/services/domain'
;
import
{
switchDomain
}
from
'../src/services/domain'
;
import
{
setToken
,
setUser
}
from
'../src/utils/auth'
;
import
{
setToken
}
from
'../src/utils/auth'
;
const
loginIt
=
async
(
userName
,
password
,
domainPath
)
=>
{
const
loginIt
=
async
(
userName
,
password
,
domainPath
)
=>
{
const
result
=
await
login
({
userName
,
password
});
const
type
=
'userName'
;
const
{
tokenId
,
userId
}
=
result
;
const
authType
=
'password'
;
const
result
=
await
login
({
type
,
authType
,
userName
,
password
});
const
{
tokenId
}
=
result
;
await
setToken
(
tokenId
);
await
setToken
(
tokenId
);
await
setUser
(
userId
,
userName
);
await
switchDomain
(
domainPath
);
await
switchDomain
(
domainPath
);
};
};
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论