antd-mini
Version:
antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。
398 lines (371 loc) • 12.5 kB
Markdown
---
nav:
path: /components
group:
title: 业务组件
order: 15
toc: 'content'
---
用于产品开通、签署协议等场景
在 `index.json` 中引入组件
```json
"usingComponents": {
"ant-terms": "antd-mini/es/Terms/index"
"ant-terms": "antd-mini/Terms/index"
}
```
```xml
<ant-terms
termPrefix="请阅读并同意"
terms="{{terms}}"
buttons="{{buttons}}"
onButtonTap="handleButtonTap"
onTermTap="handleTermTap"
bindbuttontap="handleButtonTap"
bindtermtap="handleTermTap"
/>
```
```js
Page({
data: {
buttons: [
{ text: '同意协议 + 行动点描述', type: 'primary' }
{ text: '取消', aide: true, type: 'default' }
],
terms: [
{ name: '《用户协议》', key: 'user' }
],
},
handleButtonTap(item, index, checked, event) {
my.showToast({ content: `点击了第 ${index + 1} 个按钮` });
wx.showToast({ title: `点击了第 ${item.detail[1] + 1} 个按钮` });
},
handleTermTap(item, index, event) {
my.showToast({ content: `点击了第 ${index + 1} 个协议` });
wx.showToast({ title: `点击了第 ${item.detail[1] + 1} 个协议` });
},
});
```
```xml
<ant-terms
type="check"
termPrefix="我已阅读并同意"
terms="{{terms}}"
buttons="{{buttons}}"
checkboxTipsText="请阅读后勾选服务协议"
checkboxTipsVisible="{{checkboxTipsVisible}}"
onButtonTap="handleButtonTap"
onTermTap="handleTermTap"
onCheckChange="handleCheckChange"
onTermPrefixTap="handleTermPrefixTap"
bindbuttontap="handleButtonTap"
bindtermtap="handleTermTap"
bindcheckchange="handleCheckChange"
bindtermprefixtap="handleTermPrefixTap"
/>
```
```js
Page({
data: {
buttons: [
{ text: '行动点描述', type: 'primary' }
],
terms: [
{ name: '《用户协议》', key: 'user' }
],
checkboxTipsVisible: false,
},
handleButtonTap(item, checked) {
this.setData({ checkboxTipsVisible: !checked });
this.setData({ checkboxTipsVisible: !item.detail[2] });
},
handleTermTap(item, index) {
my.showToast({ content: `点击了第 ${index + 1} 个协议` });
wx.showToast({ title: `点击了第 ${item.detail[1] + 1} 个协议` });
},
handleCheckChange(checked) {
this.setData({ checkboxTipsVisible: !checked });
if (checked) {
this.setData({ checkboxTipsVisible: false });
}
this.setData({ checkboxTipsVisible: !checked.detail });
if (checked.detail) {
this.setData({ checkboxTipsVisible: false });
}
},
handleTermPrefixTap(checked) {
this.setData({ checkboxTipsVisible: !checked });
if (checked) {
this.setData({ checkboxTipsVisible: false });
}
this.setData({ checkboxTipsVisible: !checked.detail });
if (checked.detail) {
this.setData({ checkboxTipsVisible: false });
}
},
});
```
```xml
<ant-terms
type="read"
termPrefix="当前正在读"
terms="{{terms}}"
buttons="{{buttons}}"
readCurrent="{{readCurrent}}"
onButtonTap="handleButtonTap"
onTermTap="handleTermTap"
onCountdownFinish="handleCountdownFinish"
onReadChange="handleReadChange"
bindbuttontap="handleButtonTap"
bindtermtap="handleTermTap"
bindcountdownfinish="handleCountdownFinish"
bindreadchange="handleReadChange"
>
<scroll-view
enhanced
slot="read-content"
style="height:300px;"
id="scroll-view"
scrollTop="{{scrollTop}}"
scrollY="{{true}}"
scrollWithAnimation="{{true}}"
scrollAnimationDuration="{{300}}"
lowerThreshold="{{100}}"
onScroll="onScroll"
onTouchStart="onTouchStart"
onScrollToLower="onScrollToLower"
bindscroll="onScroll"
binddragstart="onTouchStart"
bindscrolltolower="onScrollToLower"
>
<image
class="term-content"
id="term-content-0"
src="https://mdn.alipayobjects.com/huamei_bsws4g/afts/img/966PR7FNh8cAAAAAAAAAAAAADoEQAQFr/original"
mode="widthFix"
onLoad="onImageLoad"
/>
<image
class="term-content"
id="term-content-1"
src="https://mdn.alipayobjects.com/huamei_bsws4g/afts/img/966PR7FNh8cAAAAAAAAAAAAADoEQAQFr/original"
mode="widthFix"
onLoad="onImageLoad"
/>
<image
class="term-content"
id="term-content-2"
src="https://mdn.alipayobjects.com/huamei_bsws4g/afts/img/966PR7FNh8cAAAAAAAAAAAAADoEQAQFr/original"
mode="widthFix"
onLoad="onImageLoad"
/>
</scroll-view>
<view slot="read-total">
共{{terms3.length}}份协议
</view>
</ant-terms>
```
```js
Page({
data: {
buttons: [
{ text: '请阅读全部协议', disabled: true, type: 'primary', countdownTime: 30 }
],
terms: [
{ name: '《用户协议》', key: 'user' },
{ name: '《隐私协议》', key: 'private' },
{ name: '《网络协议》', key: 'internet' }
],
readCurrent: 1,
},
handleButtonTap(item, index, checked, event) {
my.showToast({ content: `点击了第 ${index + 1} 个按钮` });
wx.showToast({ title: `点击了第 ${item.detail[1] + 1} 个按钮` });
},
handleTermTap(item, index, event) {
my.showToast({ content: `点击了第 ${index + 1} 个协议` });
wx.showToast({ title: `点击了第 ${item.detail[1] + 1} 个协议` });
},
handleCountdownFinish(item, index) {
this.setData({
buttons3: this.data.buttons3.map((button, idx) =>
idx === index
? { ...button, text: '同意协议 + 行动点描述', disabled: false }
: button
),
});
},
handleReadChange(current) {
current = current.detail;
this.tap = true;
this.setData({
scrollTop:
this.itemRectList[current].top -
this.scrollViewRect.top +
Math.random(),
});
},
async onReady() {
await this.updateRect();
},
async onImageLoad() {
await this.updateRect();
},
async updateRect() {
this.itemRectList = await Promise.all(
this.data.terms3.map((item, index) =>
this.getBoundingClientRect(`
)
);
this.scrollViewRect = await this.getBoundingClientRect('#scroll-view');
},
onTouchStart() {
this.tap = false;
},
onScroll(e) {
if (this.tap) {
return;
}
this.scrollTop = e.detail.scrollTop;
const scrollTop = this.scrollTop + this.itemRectList[0].top;
for (let i = 0; i < this.itemRectList.length; i++) {
const item = this.itemRectList[i];
if (
scrollTop > item.top &&
(!this.itemRectList[i + 1] ||
scrollTop < this.itemRectList[i + 1].top) &&
i !== this.data.readCurrent
) {
this.setData({
readCurrent: i,
});
return;
}
}
},
onScrollToLower() {
console.log('触底');
},
getBoundingClientRect(id) {
if (typeof my === 'undefined') {
return this.getInstanceBoundingClientRect(this, id);
}
return this.getInstanceBoundingClientRect(my, id);
},
getInstanceBoundingClientRect(instance, selector) {
return new Promise((resolve) => {
instance
.createSelectorQuery()
.select(selector)
.boundingClientRect()
.exec((ret) => {
if (ret && ret[0]) {
resolve(ret[0]);
}
});
});
},
});
```
<code src='../../demo/pages/Terms/Fixed/index'></code>
<code src='../../demo/pages/Terms/index'></code>
| 属性 | 说明 | 类型 | 默认值 |
| ------------------------------- | -------------------- | ----------------------------------------------------------------- | ------------ |
| type | 协议展示类型 | `""` \| `"check"` \| `"read"` | `""` |
| terms | 协议列表 | [Term](
| buttons | 按钮列表 | [Button](
| buttonsFixed | 按钮区是否吸底 | `boolean` | `false` |
| buttonsDirection | 分割线方向,默认纵向 | `"horizontal"` \| `"vertical"` | `"vertical"` |
| termPrefix | 协议前缀 | `string` | `""` |
| termSuffix | 协议后缀 | `string` | `""` |
| checkboxTipsText | 勾选框引导文案 | `string` | `""` |
| checkboxTipsVisible | 勾选框引导显示状态 | `boolean` | `false` |
| readCurrent | 阅读的当前序号 | `number` | `-` |
| defaultReadCurrent | 默认阅读的当前序号 | `number` | `-` |
|
|
|
|
|
|
|
|
|
|
|
|
| 属性 | 说明 | 类型 | 默认值 |
| ---- | -------- | -------- | ------ |
| key | 唯一标识 | `string` | `-` |
| name | 协议名称 | `string` | `-` |
| 属性 | 说明 | 类型 | 默认值 |
| ----------------------------------------------------- | ------------------ | -------- | ------ |
| key | 唯一标识 | `string` | `-` |
| text | 按钮文案 | `string` | `-` |
| countdownTime | 计时秒数,单位为秒 | `number` | `-` |
| 透传 [ant-button](/components/button