Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useinvoice): add useInvoice hooks
- Loading branch information
Showing
8 changed files
with
167 additions
and
13 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
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
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,74 @@ | ||
--- | ||
title: useInvoice | ||
nav: | ||
title: Hooks | ||
path: /hooks | ||
order: 2 | ||
group: | ||
title: 小程序 | ||
path: /wechat | ||
--- | ||
|
||
# useInvoice | ||
|
||
获取发票、选择发票抬头。 | ||
|
||
## 何时使用 | ||
|
||
当需要使用发票或抬头信息时 | ||
|
||
<Alert>选择用户的发票抬头。当前小程序必须关联一个公众号,且这个公众号是完成了微信认证的,才能调用</Alert> | ||
|
||
## API | ||
|
||
```jsx | pure | ||
const [chooseInvoice, chooseInvoiceTitle] = useInvoice(); | ||
``` | ||
|
||
## 参数说明 | ||
|
||
无 | ||
|
||
## 返回值说明 | ||
|
||
| 参数 | 类型 | 说明 | | ||
| ------------------ | ------------------------------------------------------------------------------- | ------------ | | ||
| chooseInvoice | `() => Promise<IChooseInvoiceSuccessResult[] | General.CallbackResult>` | 获取发票信息 | | ||
| chooseInvoiceTitle | `() => Promise<IChooseInvoiceTitleSuccessResult | General.CallbackResult>` | 获取发票抬头 | | ||
|
||
### IChooseInvoiceSuccessResult | ||
|
||
| 参数 | 类型 | 说明 | | ||
| ------------ | -------- | --------------------------------------------------------------------------------- | | ||
| card_id | `string` | 所选发票卡券的 `cardId` | | ||
| encrypt_code | `string` | 所选发票卡券的加密`code`,报销方可以通过`cardId`和`encryptCode`获得报销发票的信息 | | ||
| app_id | `string` | 发票方的`appId` | | ||
|
||
### IChooseInvoiceTitleSuccessResult | ||
|
||
| 参数 | 类型 | 说明 | | ||
| -------------- | ------------ | ------------------------------- | | ||
| bankAccount | `string` | 银行账号 | | ||
| bankName | `string` | 银行名称 | | ||
| companyAddress | `string` | 单位地址 | | ||
| taxNumber | `string` | 抬头税号 | | ||
| telephone | `string` | 手机号码 | | ||
| title | `string` | 抬头名称 | | ||
| type | `0 | 1` | 0"单位" | 1“个人” 抬头类型 | | ||
|
||
## 代码演示 | ||
|
||
<code src="@pages/useInvoice" /> | ||
|
||
## Hook 支持度 | ||
|
||
| 微信小程序 | H5 | ReactNative | | ||
| :--------: | :-: | :---------: | | ||
| ✔️ | | | | ||
|
||
## FAQ | ||
|
||
### 1. 更多说明 | ||
|
||
- [chooseInvoice](https://developers.weixin.qq.com/miniprogram/dev/api/open-api/invoice/wx.chooseInvoice.html) | ||
- [chooseInvoiceTitle](https://developers.weixin.qq.com/miniprogram/dev/api/open-api/invoice/wx.chooseInvoiceTitle.html) |
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,75 @@ | ||
import { | ||
ENV_TYPE, | ||
chooseInvoice, | ||
chooseInvoiceTitle, | ||
General, | ||
} from '@tarojs/taro'; | ||
import { useCallback } from 'react'; | ||
import useEnv from '../useEnv'; | ||
|
||
export interface IChooseInvoiceSuccessResult { | ||
card_id: string; | ||
encrypt_code: string; | ||
app_id: string; | ||
} | ||
export interface IChooseInvoiceTitleSuccessResult { | ||
bankAccount: string; | ||
bankName: string; | ||
companyAddress: string; | ||
taxNumber: string; | ||
telephone: string; | ||
title: string; | ||
type: 0 | 1; | ||
} | ||
export type TChooseInvoice = () => Promise< | ||
IChooseInvoiceSuccessResult[] | General.CallbackResult | ||
>; | ||
|
||
export type TChooseInvoiceTitle = () => Promise< | ||
IChooseInvoiceTitleSuccessResult | General.CallbackResult | ||
>; | ||
|
||
function useInvoice(): [TChooseInvoice, TChooseInvoiceTitle] { | ||
const env = useEnv(); | ||
|
||
const chooseInvoiceAsync = useCallback<TChooseInvoice>(() => { | ||
return new Promise((resolve, reject) => { | ||
if (env !== ENV_TYPE.WEAPP) { | ||
reject({ errMsg: 'chooseInvoice: fail' }); | ||
} else { | ||
try { | ||
chooseInvoice({ | ||
success: ({ invoiceInfo = '' }) => { | ||
const parseInvoiceInfo = JSON.parse(invoiceInfo); | ||
resolve(parseInvoiceInfo); | ||
}, | ||
fail: reject, | ||
}); | ||
} catch (e) { | ||
reject({ errMsg: 'chooseInvoice: fail', data: e }); | ||
} | ||
} | ||
}); | ||
}, [env]); | ||
|
||
const chooseInvoiceTitleAsync = useCallback<TChooseInvoiceTitle>(() => { | ||
return new Promise((resolve, reject) => { | ||
if (env !== ENV_TYPE.WEAPP) { | ||
reject({ errMsg: 'chooseInvoiceTitle: fail' }); | ||
} else { | ||
try { | ||
chooseInvoiceTitle({ | ||
success: ({ errMsg, ...result }) => resolve(result), | ||
fail: reject, | ||
}); | ||
} catch (e) { | ||
reject({ errMsg: 'chooseInvoiceTitle: fail', data: e }); | ||
} | ||
} | ||
}); | ||
}, [env]); | ||
|
||
return [chooseInvoiceAsync, chooseInvoiceTitleAsync]; | ||
} | ||
|
||
export default useInvoice; |
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