-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
199 additions
and
1 deletion.
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,19 @@ | ||
[licenses] | ||
# This indicates which are the only licenses that Licensebat will accept. | ||
# The rest will be flagged as not allowed. | ||
accepted = ["MIT", "MSC", "BSD", "Apache"] | ||
# This will indicate which licenses are not accepted. | ||
# The rest will be accepted, except for the unknown licenses or dependencies without licenses. | ||
# unaccepted = ["LGPL"] | ||
# Note that only one of the previous options can be enabled at once. | ||
# If both of them are informed, only accepted will be considered. | ||
|
||
[dependencies] | ||
# This will allow users to flag some dependencies so that Licensebat will not check for their license. | ||
ignored=["@taro/tarojs", "react", "react-dom"] | ||
|
||
[behavior] | ||
# False by default, if true, it will only run the checks when one of the dependency files or the .licrc file has been modified. | ||
run_only_on_dependency_modification = true | ||
# False by default, if true, it will never block the build. | ||
do_not_block_pr = false |
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,3 @@ | ||
export default { | ||
navigationBarTitleText: 'useFile', | ||
}; |
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,62 @@ | ||
import React, { useCallback } from 'react'; | ||
import { AtButton } from 'taro-ui'; | ||
|
||
import DocPage from '@components/DocPage'; | ||
|
||
import { useFile, useImage, useModal } from 'taro-hooks'; | ||
|
||
export default () => { | ||
const { download, upload } = useFile(); | ||
const [, { choose, preview }] = useImage({}); | ||
const [showModal] = useModal({ mask: true, title: '文件结果' }); | ||
|
||
const handleUpload = useCallback(async () => { | ||
const fileInfo = await choose(); | ||
if (fileInfo?.tempFilePaths?.length) { | ||
const uploadFilePath = fileInfo.tempFilePaths[0]; | ||
const uploadResult = await upload({ | ||
url: 'https://run.mocky.io/v3/35b34abe-3f7e-4cde-91a8-da02f699bdc0', | ||
filePath: uploadFilePath, | ||
name: 'chooseImage:file', | ||
header: {}, | ||
}); | ||
const modalContent = | ||
uploadResult?.statusCode === 200 ? uploadResult?.data : '上传失败'; | ||
showModal({ content: modalContent }); | ||
} else { | ||
showModal({ | ||
content: '取消选择', | ||
}); | ||
} | ||
}, [choose, upload, showModal]); | ||
|
||
const handleDownload = useCallback(async () => { | ||
try { | ||
const result = await download({ | ||
url: 'https://pixabay.com/get/gca7a9aedd24075bee51ccdc9e510d6d78d66b2b607c643e63260d944732a75301e6ce92f56e371d0ea11c55613742929_640.jpg', | ||
}); | ||
if (result.statusCode === 200 && result?.tempFilePath) { | ||
preview({ | ||
urls: [result?.tempFilePath], | ||
}); | ||
} else { | ||
throw new Error(); | ||
} | ||
} catch (e) { | ||
showModal({ | ||
content: 'downloadFile: fail', | ||
}); | ||
} | ||
}, [download, showModal, preview]); | ||
|
||
return ( | ||
<> | ||
<DocPage title="useFile 上传下载" panelTitle="useFile"> | ||
<AtButton onClick={handleUpload}>选择图片上传</AtButton> | ||
<AtButton className="gap" onClick={handleDownload}> | ||
下载图片 | ||
</AtButton> | ||
</DocPage> | ||
</> | ||
); | ||
}; |
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,40 @@ | ||
--- | ||
title: useFile | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 网络 | ||
path: /network | ||
--- | ||
|
||
# useFile | ||
|
||
上传、下载文件 | ||
|
||
## 何时使用 | ||
|
||
当需要上传下载文件时 | ||
|
||
## API | ||
|
||
```jsx | pure | ||
const visible: boolean = useVisible(); | ||
``` | ||
|
||
## 返回值说明 | ||
|
||
| 返回值 | 说明 | 类型 | | ||
| ------- | -------------------- | --------- | | ||
| visible | 当前应用是否处于后台 | `boolean` | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useFile" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | ✔️ | | |
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,68 @@ | ||
import { uploadFile, downloadFile, General } from '@tarojs/taro'; | ||
import { useCallback } from 'react'; | ||
|
||
export interface IUploadOption { | ||
url: string; | ||
filePath: string; | ||
name: string; | ||
header?: Record<string, any>; | ||
formData?: Record<string, any>; | ||
timeout?: number; | ||
fileName?: string; | ||
} | ||
|
||
export interface IDownloadOption { | ||
url: string; | ||
filePath?: string; | ||
header?: Record<string, any>; | ||
} | ||
|
||
export type IUploadFileAction = ( | ||
option: IUploadOption, | ||
) => Promise<uploadFile.SuccessCallbackResult | General.CallbackResult>; | ||
|
||
export type IDownloadFileAction = ( | ||
option: IDownloadOption, | ||
) => Promise<downloadFile.FileSuccessCallbackResult | General.CallbackResult>; | ||
|
||
export interface IAction { | ||
upload: IUploadFileAction; | ||
download: IDownloadFileAction; | ||
} | ||
|
||
function useFile(): IAction { | ||
const upload = useCallback<IUploadFileAction>((option) => { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
uploadFile({ | ||
...(option || {}), | ||
success: resolve, | ||
fail: reject, | ||
}).catch(reject); | ||
} catch (e) { | ||
reject({ errMsg: 'uploadFile:fail' }); | ||
} | ||
}); | ||
}, []); | ||
|
||
const download = useCallback<IDownloadFileAction>((option) => { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
downloadFile({ | ||
...(option || {}), | ||
success: resolve, | ||
fail: reject, | ||
}); | ||
} catch (e) { | ||
reject({ errMsg: 'downloadFile:fail' }); | ||
} | ||
}); | ||
}, []); | ||
|
||
return { | ||
upload, | ||
download, | ||
}; | ||
} | ||
|
||
export default useFile; |
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
774458a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
taro-hooks – ./
taro-hooks.vercel.app
taro-hooks-ff-docs.vercel.app
taro-hooks-git-main-ff-docs.vercel.app
774458a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
taro-hooks-h5 – ./
taro-hooks-h5-ff-docs.vercel.app
taro-hooks-h5.vercel.app
taro-hooks-h5-git-main-ff-docs.vercel.app