Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
5dcb4e71
Unverified
Commit
5dcb4e71
authored
Jul 04, 2023
by
yqwoe
Committed by
GitHub
Jul 04, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Feature][web] add use-service hook #2119
parent
14461a1c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
0 deletions
+91
-0
useService.tsx
dinky-web/src/hooks/useService.tsx
+91
-0
No files found.
dinky-web/src/hooks/useService.tsx
0 → 100644
View file @
5dcb4e71
import
useUmiRequest
from
'@ahooksjs/use-request'
;
import
{
request
}
from
'@umijs/max'
;
import
{
Spin
}
from
'antd'
;
import
{
createContext
,
memo
,
useContext
,
useState
}
from
'react'
;
export
interface
RequestOptions
{
url
:
string
;
method
:
'GET'
|
'POST'
|
'PUT'
|
'PATCH'
|
'DELETE'
;
data
?:
T
;
params
?:
any
;
headers
?:
any
;
}
export
interface
RequestContextType
{
useGet
:
(
options
:
RequestOptions
)
=>
void
;
}
const
METHODS
=
[
'Get'
,
'Post'
,
'Put'
,
'Patch'
,
'Delete'
];
export
const
RequestContext
=
createContext
<
RequestContextType
>
(
null
!
);
export
const
RequestProvider
=
memo
(({
children
})
=>
{
const
[
appLoading
,
setAppLoading
]
=
useState
(
false
);
const
requestWithMethod
=
async
(
options
:
RequestOptions
)
=>
{
setAppLoading
(
true
);
const
{
url
,
...
opts
}
=
options
;
const
{
data
}
=
await
request
(
url
,
opts
);
setAppLoading
(
false
);
return
data
;
};
//TODO 封装请求方法
const
hooks
=
{};
METHODS
.
forEach
((
method
)
=>
{
hooks
[
`use
${
method
}
`
]
=
(
url
,
opts
)
=>
useUmiRequest
(
()
=>
{
setAppLoading
(
true
);
return
request
(
url
,
{
...
opts
,
method
});
},
{
formatResult
:
(
result
)
=>
result
?.
datas
,
requestMethod
:
(
requestOptions
:
any
)
=>
{
if
(
typeof
requestOptions
===
'string'
)
{
return
request
(
requestOptions
);
}
if
(
typeof
requestOptions
===
'object'
)
{
const
{
url
,
...
rest
}
=
requestOptions
;
return
request
(
url
,
rest
);
}
throw
new
Error
(
'request options error'
);
},
onSuccess
:
(
data
)
=>
{
setAppLoading
(
false
);
},
onError
:
(
err
)
=>
{
setAppLoading
(
false
);
},
onFinally
:
()
=>
{
setAppLoading
(
false
);
},
},
);
});
return
(
<
RequestContext
.
Provider
value=
{
{
...
hooks
,
appLoading
}
}
>
<
Spin
tip=
"网络请求中..."
spinning=
{
appLoading
}
>
{
children
}
</
Spin
>
</
RequestContext
.
Provider
>
);
});
export
const
useService
=
()
=>
useContext
(
RequestContext
);
/****
*
*
import { useService } from '../../hooks/useService';
const {useGet,usePost,usePut,usePatch,useDelete} = useService();
const {data} = useGet('/api/current',{params:{xxxx}})
const {data} = usePost('/api/current',{data:{xxxx}})
const {data} = usePut('/api/current',{data:{xxxx}})
const {data} = usePut('/api/current',{data:{xxxx}})
const {data} = usePatch('/api/current',{data:{xxxx}})
const {data} = useDelete('/api/current',{data:{xxxx}})
console.log(data)
*
*
*/
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment