后浪云百度小程序教程:page-policy 政策文件库模板

  • page-policy 政策文件库模板
    • 示例
    • 页面内容
      • 政策文件列表页
      • 政策文件详情页
      • 我的收藏页面
      • 我的订阅页面
      • 搜索页
    • 自定义 UI 组件
    • npm 依赖
    • Bug & Tip

    page-policy 政策文件库模板

    从开发者工具 v2.25.1-rc 版本开始支持。

    解释:本模板适用于信息公开类小程序的快速搭建,可应用于办税指南、法律宝典、街道信息公告通知等场景,可将信息结构化的呈现给用户,方便用户快速阅读获取有效的信息。

    示例

    扫码体验

    代码示例

    后浪云百度小程序教程:page-policy 政策文件库模板插图

    请使用百度APP扫码

    页面内容

    模板包含政策文件列表页、政策文件详情页、收藏列表页以及订阅列表页、搜索结果页。

    政策文件列表页

    政策文件列表页包含搜索以及筛选组件,用户可以快速获取所需要的信息,同时页面包含跑马灯组件、tab 切换组件可实现信息轮播提示、板块切换等交互效果,丰富页面功能,提升使用体验。

    页面路径:pages/indexFilter/index、pages/indexTab/index

    列表页提供了筛选器和 tab 两种效果样式。其中,pages/indexFilter/index 是带筛选器的模板页,pages/indexTab/index 是带 tab 切换的模板页。如果想查看不同的模板类型,可以修改小程序根目录下的 app.json 文件,将对应类型的模板页面路径放到第一个。

    后浪云百度小程序教程:page-policy 政策文件库模板插图1

    后浪云百度小程序教程:page-policy 政策文件库模板插图2

    代码示例

    • 以下是使用筛选器的首页模板代码:

    • SWAN

    • JSON
    • JS
     
     
     
    1. <view class="index-filter {{isIPhoneX ? 'iphone-x-compitable' : ''}} {{isOpenFilter ? 'locked' : ''}}">
    2. <zw-custom-title-bar
    3. class='policy-home-filter'
    4. common-bg-opacity="{{true}}"
    5. common-front-color="#ffffff"
    6. fixed-title="政策文件库"
    7. need-to-return="{{false}}"
    8. fixed-text-align="center"
    9. common-text-align="left"
    10. fixed-bg-color="#ffffff"
    11. common-bg-color="#2772fb"
    12. fixed-front-color="#000000"
    13. fixed-has-border="{{true}}"
    14. common-has-border="{{false}}"
    15. show-fixed-bar="{{true}}"
    16. namespace="policy-home-filter"
    17. >
    18. </zw-custom-title-bar>
    19. <view class="security-style" style="height:{{security}}"></view>
    20. <view class="header">
    21. <view class="app-handle">
    22. <view class="app">
    23. <view class="row-main app-title">政策文件库</view>
    24. <view class="row-sub">文件全文&nbsp;权威解读</view>
    25. </view>
    26. <view class="handle">
    27. <view class="handle-item">
    28. <view class="row-main handle-icon" bindtap="gotoSubscribe">
    29. <zw-icon name="subscribe" size="{{iconsize}}" color="#fff" class="icon-1"/>
    30. </view>
    31. <view class="row-sub">我的订阅</view>
    32. </view>
    33. <view class="handle-item">
    34. <view class="row-main handle-icon" bindtap="gotoCollect">
    35. <zw-icon name="no-collect" size="{{iconsize}}" color="#fff" class="icon-2"/>
    36. </view>
    37. <view class="row-sub">我的收藏</view>
    38. </view>
    39. </view>
    40. </view>
    41. <view class="search" bindtap="gotoSearch"></view>
    42. </view>
    43. <view class="notice-wrap" s-if="notice.text">
    44. <zw-notice-bar text="{{notice.text}}" bind:close="noticeClose" bind:clicknotice="clicknotice"/>
    45. </view>
    46. <view class="content">
    47. <view class="filter-section" s-if="{{filter.length}}">
    48. <zw-filter
    49. s-for="item, findex in filter"
    50. title="{{item.title}}"
    51. class="filter"
    52. mask-with-animation="{{false}}"
    53. bind:openFilter="openFilter"
    54. max-title-width="123.79"
    55. is-immersive
    56. >
    57. <zw-filter-item
    58. s-for="sub,index in item.children"
    59. bind:change="changeFilter"
    60. value="{{findex + ':' + sub.id}}"
    61. text="{{sub.name}}" />
    62. </zw-filter>
    63. </view>
    64. <view class="filter-content {{notice ? 'smaller' : 'normal'}}">
    65. <zw-loading s-if="showLoading" zw-loading-status-class="custom-loading">
    66. <view slot="text">正在加载...</view>
    67. </zw-loading>
    68. <zw-state-page show-reload="{{stateBtn}}" type="{{errStatus}}" bind:reload="reload" zw-state-page-class="custom-state" fixed="{{false}}" s-if="{{errStatus !== ''}}"/>
    69. <block s-if="isLoaded">
    70. <view class="feed-content">
    71. <block s-for="page in list">
    72. <zw-list-item
    73. s-for="item, index in page"
    74. title="{{item.title}}"
    75. desc="{{'发文机关:' + item.office}}"
    76. clickable="{{true}}"
    77. data-aid="item.id"
    78. bind:tap="gotoArticle"
    79. />
    80. </block>
    81. <zw-spin status="{{status}}" bind:tap="reloadFeed" s-if="showStatus" zw-spin-class="custom-spin"></zw-spin>
    82. <zw-footer s-if="feedEnd" zw-footer-class="custom-footer">
    83. <view class="footer-txt">单行footer</view>
    84. </zw-footer>
    85. </view>
    86. </block>
    87. </view>
    88. </view>
    89. </view>
     
     
     
    1. {
    2. "navigationBarTitleText": "政策文件库",
    3. "navigationStyle": "custom",
    4. "backgroundColor": "#ffffff",
    5. "backgroundColorTop": "#2772fb",
    6. "navigationBarBackgroundColor": "#ffffff",
    7. "navigationBarTextStyle": "white",
    8. "onReachBottomDistance": 34,
    9. "usingComponents": {
    10. "zw-custom-title-bar": "../../components/CustomTitleBar/src/index",
    11. "zw-notice-bar": "../../components/NoticeBar/src/index",
    12. "zw-footer": "../../components/Footer/src/index",
    13. "zw-icon": "../../components/Icon/src/index",
    14. "zw-loading": "../../components/LoadingStatus/src/index",
    15. "zw-state-page": "../../components/StatePage/src/index",
    16. "zw-list-item": "../../components/ListItem/src/index",
    17. "zw-spin": "../../components/Spin/src/index",
    18. "zw-filter": "../../components/Filter/src/index",
    19. "zw-filter-item": "../../components/FilterItem/src/index"
    20. }
    21. }
     
     
     
    1. // 获取列表数据,以下为使用 mock 数据的请求示例
    2. getHomeList(query, ({code, msg, data}) => {
    3. if (code === 0) {
    4. // 设置筛选器数据
    5. !filter.length && (res.filter = data.filter);
    6. // 更新通知栏内容
    7. !notice && !this.data.hideNotice && data.notice && (res.notice = data.notice);
    8. // ...
    9. }
    10. else {
    11. // 请求异常处理
    12. }
    13. }),
    14. // 跳转到文章详情页。跳转携带的 id 参数只是 mock 数据举例,是否需要带参数跳转以及参数名称、参数值均可自定义
    15. gotoArticle(e) {
    16. const index = e.currentTarget.dataset.aid;
    17. navigateTo({
    18. url: `../article/article?id=${index}`
    19. });
    20. }

    政策文件详情页

    分为标题以及文字信息区域,可将图文等内容结构化呈现,方便用户阅读。

    页面路径:pages/article/article

    后浪云百度小程序教程:page-policy 政策文件库模板插图3

    代码示例

    • SWAN
    • JSON
    • JS
     
     
     
    1. <view class="article-wrapper">
    2. <zw-loading s-if="showLoading">
    3. <view slot="text">正在加载...</view>
    4. </zw-loading>
    5. <zw-state-page show-reload="{{stateBtn}}" type="{{errStatus}}" s-if="errStatus !== ''" bind:reload="reload"/>
    6. <block s-if="isLoaded">
    7. <view class="{{isIPhoneX ? 'iphone-x-compitable' : '' }}">
    8. <view class="main-container">
    9. <view class="main-content">
    10. <view class="content">
    11. <view class="article-desc" bindtap="changeExpand">
    12. <view class="article-title">{{detail.title}}</view>
    13. <view class="article-info {{expand.length ? 'nomargin' : ''}}" >
    14. <view class="info-item">
    15. <view class="info-name">{{detail.info[0].name}}</view>
    16. <view class="info-value">{{detail.info[0].value}}</view>
    17. </view>
    18. </view>
    19. <view class="expand-icon">
    20. <zw-icon
    21. s-if="{{expand.length > 0}}"
    22. name="arrow-{{!expandStatus ? 'down' : 'up'}}"
    23. color="#ccc"
    24. size="{{actionFontSize}}">
    25. </zw-icon>
    26. </view>
    27. </view>
    28. <view
    29. class="expand-area" s-if="{{expand.length > 0}}"
    30. style="height: {{expandStatus ? expandHeight : 0}}px;"
    31. >
    32. <view class="expand-area-inner">
    33. <view class="info-item" s-for="item in expand">
    34. <view class="info-name">{{item.name}}</view>
    35. <view class="info-value">{{item.value}}</view>
    36. </view>
    37. </view>
    38. </view>
    39. <rich-text nodes="{{detail.content}}" selectable="true"></rich-text>
    40. <view class="subscribe border border-top">
    41. <zw-list-item
    42. title="{{detail.hoster}}"
    43. desc="{{detail.desc}}"
    44. clickable="{{false}}"
    45. zw-list-item-wrap-class="custom-list"
    46. no-border
    47. >
    48. <slot>
    49. <view class="btn-wrap" >
    50. <zw-button
    51. button-size="xs-small"
    52. button-color="{{detail.status ? 'secondly' : 'thirdly'}}"
    53. button-text="{{detail.status ? '已订阅' : '订阅'}}"
    54. bindtap="changeSub"
    55. />
    56. </view>
    57. </slot>
    58. </zw-list-item>
    59. </view>
    60. </view>
    61. <view class="footer {{isIPhoneX ? 'iphone-x-compitable' : '' }} border border-top {{!isIos ? 'android-patch': ''}}">
    62. <view class="footer-inner">
    63. <view class="actions-list" >
    64. <view class="action" bind:tap="addCollection">
    65. <zw-icon class="icon" name="{{detail.collected ? 'collect-color' : 'collect-thin'}}" size="{{iconSize}}">
    66. </zw-icon>
    67. {{detail.collected ? '已收藏' : '收藏'}}
    68. </view>
    69. </view>
    70. <view class="actions-list" >
    71. <view class="action" bind:tap="toShare">
    72. <zw-icon class="icon" name="share" size="{{iconSize}}"></zw-icon>
    73. 分享
    74. </view>
    75. </view>
    76. </view>
    77. </view>
    78. </view>
    79. </view>
    80. </view>
    81. </block>
    82. </view>
     
     
     
    1. {
    2. "navigationBarBackgroundColor": "#ffffff",
    3. "navigationBarTextStyle": "black",
    4. "navigationBarTitleText": "政策文件库",
    5. "backgroundColor": "#ffffff",
    6. "backgroundTextStyle": "light",
    7. "usingComponents": {
    8. "zw-loading": "../../components/LoadingStatus/src/index",
    9. "zw-state-page": "../../components/StatePage/src/index",
    10. "zw-button": "../../components/Button/src/index",
    11. "zw-icon": "../../components/Icon/src/index",
    12. "zw-list-item": "../../components/ListItem/src/index"
    13. }
    14. }
     
     
     
    1. // 请求接口,获取文件详情
    2. getArticle(({code, msg, data}) => {
    3. // 请求正常响应
    4. if (code === 0) {
    5. // ...
    6. }
    7. // 请求异常
    8. else {
    9. // ...
    10. }
    11. }),
    12. // 切换订阅状态
    13. changeSub(e) {
    14. // 获取当前订阅状态
    15. const status = this.data.detail.status;
    16. // 请求接口,切换订阅状态
    17. changeStatus(status, ({code, data}) => {
    18. // 请求正常响应
    19. if (code === 0) {
    20. // 更新订阅状态
    21. }
    22. });
    23. },
    24. // 切换收藏状态
    25. addCollection(e) {
    26. // 获取当前收藏状态
    27. const status = this.data.detail.collected;
    28. // 请求接口,切换收藏状态
    29. changeStatus(status, ({code, data}) => {
    30. // 请求正常响应
    31. if (code === 0) {
    32. // 更新收藏状态
    33. }
    34. });
    35. },
    36. // 分享的标题、内容、路径、图片地址,分享成功、分享失败的回调都可以根据实际业务场景和需求自定义。
    37. toShare() {
    38. swan.openShare({
    39. title: '政策文件库分享示例',
    40. content: '中共中央办公厅印发《2019-2023年全国党员教育培训工作规划》',
    41. path: '/templates/gov/policy/pages/article/article?key=' + this.data.articleId,
    42. imageUrl: 'https://ms-static.cdn.bcebos.com/miniappdocs/img/logo_new.png',
    43. success: res => {
    44. swan.showToast({
    45. title: '分享成功'
    46. });
    47. console.log('openShare success', res);
    48. },
    49. fail: err => {
    50. console.log('openShare fail', err);
    51. }
    52. });
    53. }

    我的收藏页面

    用户收藏过的内容将通过一定的顺序呈现在收藏列表页,对于重点关注的内容,用户无需反复检索,在收藏列表页可直接阅读。

    页面路径:pages/collect/collect

    后浪云百度小程序教程:page-policy 政策文件库模板插图4

    代码示例

    • SWAN
    • JSON
    • JS
     
     
     
    1. <view class="collect-wrapper">
    2. <zw-tabs
    3. tabs="{{tabs}}"
    4. type="flex"
    5. mode="auto"
    6. active-tab="{{0}}"
    7. nav-bar-width="100%"
    8. show-with-animation="{{false}}"
    9. bindonChange="onCustomTabChange"
    10. />
    11. <view class="tab-content {{activeName === 'zc' ? 'show' : 'hide'}} {{isIPhoneX ? 'iphone-x-compitable' : ''}}">
    12. <zw-loading s-if="zc.showLoading">
    13. <view slot="text">正在加载...</view>
    14. </zw-loading>
    15. <zw-state-page show-reload="{{zc.stateBtn}}" type="{{zc.errStatus}}" s-if="zc.errStatus !== ''" bind:reload="reloadZc" zw-state-page-class="custom-state" fixed="{{false}}"/>
    16. <block s-if="zc.isLoaded">
    17. <!-- 上拉加载 -->
    18. <smt-feed
    19. class="smt-feed click-zc-refresh"
    20. data-type="zc"
    21. bind:scrolltolower="autoScrollToLower"
    22. >
    23. <block s-for="page in zc.list">
    24. <zw-list-item
    25. s-for="item, index in page"
    26. title="{{item.title}}"
    27. desc="{{'发文机关:' + item.office}}"
    28. clickable="{{true}}"
    29. data-aid="item.id"
    30. bind:tap="gotoArticle"
    31. />
    32. </block>
    33. <zw-spin status="{{zc.status}}" bind:tap="reloadZcFeed" zw-spin-class="custom-spin"></zw-spin>
    34. </smt-feed>
    35. </block>
    36. </view>
    37. <view class="tab-content {{activeName === 'jd' ? 'show' : 'hide'}} {{isIPhoneX ? 'iphone-x-compitable' : ''}}">
    38. <zw-loading s-if="jd.showLoading">
    39. <view slot="text">正在加载...</view>
    40. </zw-loading>
    41. <zw-state-page show-reload="{{jd.stateBtn}}" type="{{jd.errStatus}}" s-if="jd.errStatus !== ''" bind:reload="reloadJd" zw-state-page-class="custom-state" fixed="{{false}}"/>
    42. <block s-if="jd.isLoaded">
    43. <!-- 上拉加载 -->
    44. <zw-feed
    45. class="smt-feed click-jd-refresh"
    46. data-type="jd"
    47. bind:scrolltolower="autoScrollToLower"
    48. >
    49. <block s-for="page in jd.list">
    50. <zw-list-item
    51. s-for="item, index in page"
    52. title="{{item.title}}"
    53. desc="{{'发文机关:' + item.office}}"
    54. clickable="{{true}}"
    55. data-aid="{{item.id}}"
    56. bind:tap="gotoArticle"
    57. />
    58. </block>
    59. <zw-spin status="{{jd.status}}" bind:tap="reloadJdFeed" zw-spin-class="custom-spin"></zw-spin>
    60. </zw-feed>
    61. </block>
    62. </view>
    63. </view>
     
     
     
    1. {
    2. "navigationBarBackgroundColor": "#fffffe",
    3. "navigationBarTextStyle": "black",
    4. "navigationBarTitleText": "我的收藏",
    5. "backgroundColor": "#ffffff",
    6. "backgroundTextStyle": "light",
    7. "usingComponents": {
    8. "zw-loading": "../../components/LoadingStatus/src/index",
    9. "zw-state-page": "../../components/StatePage/src/index",
    10. "zw-list-item": "../../components/ListItem/src/index",
    11. "smt-feed": "@smt-ui/component/src/feed",
    12. "zw-spin": "../../components/Spin/src/index",
    13. "zw-tabs": "../../components/Tabs/src/index"
    14. }
    15. }
     
     
     
    1. // 请求接口,获取收藏数据
    2. getCollectList(name, ({code, msg, data}) => {
    3. if (code === 0) {
    4. // 请求正常响应处理
    5. }
    6. else {
    7. // 请求异常处理
    8. }
    9. }),
    10. // 跳转到文章详情页。跳转携带的 id 参数只是 mock 数据举例,是否需要带参数跳转以及参数名称、参数值均可自定义
    11. gotoArticle(e) {
    12. const index = e.currentTarget.dataset.aid;
    13. navigateTo({
    14. url: `../article/article?id=${index}`
    15. });
    16. }

    我的订阅页面

    开发者可结合自身业务,向用户提供订阅功能。对于订阅后的内容或板块,用户无需反复检索,在订阅列表页可直接获取最新资讯。

    页面路径:pages/subscribe/subscribe

    后浪云百度小程序教程:page-policy 政策文件库模板插图5

    代码示例

    • SWAN
    • JSON
    • JS
     
     
     
    1. <view class="subscribe-wrap swan-security-padding-bottom">
    2. <zw-loading s-if="showLoading">
    3. <view slot="text">正在加载...</view>
    4. </zw-loading>
    5. <zw-state-page
    6. s-if="errStatus !== ''"
    7. show-reload="{{stateBtn}}"
    8. type="{{errStatus}}"
    9. bind:reload="reload"
    10. />
    11. <view s-if="isLoaded">
    12. <zw-list-item
    13. s-for="item, index in subList"
    14. title="{{item.title}}"
    15. desc="{{item.desc}}"
    16. clickable="{{false}}"
    17. data-index={{index}}
    18. bind:tap="gotoArticle"
    19. >
    20. <slot>
    21. <view class="btn-wrap" >
    22. <zw-button
    23. button-size="xs-small"
    24. button-color="{{item.status ? 'secondly' : 'thirdly'}}"
    25. button-text="{{item.status ? '已订阅' : '订阅'}}"
    26. data-index="{{index}}"
    27. catchtap="changeSub"
    28. />
    29. </view>
    30. </slot>
    31. </zw-list-item>
    32. </view>
    33. </view>
     
     
     
    1. {
    2. "navigationBarBackgroundColor": "#ffffff",
    3. "navigationBarTextStyle": "black",
    4. "navigationBarTitleText": "我的订阅",
    5. "backgroundColor": "#ffffff",
    6. "backgroundTextStyle": "light",
    7. "usingComponents": {
    8. "zw-list-item": "../../components/ListItem/src/index",
    9. "zw-loading": "../../components/LoadingStatus/src/index",
    10. "zw-state-page": "../../components/StatePage/src/index",
    11. "zw-button": "../../components/Button/src/index"
    12. }
    13. }
     
     
     
    1. // 请求接口,获取关注列表数据
    2. getList(({code, msg, data}) => {
    3. if (code === 0) {
    4. // 请求正常响应处理
    5. }
    6. else {
    7. // 请求异常处理
    8. }
    9. }),
    10. // 切换订阅状态
    11. changeSub(e) {
    12. // 获取当前订阅状态
    13. const status = this.data.subList[index].status;
    14. changeStatus(status, ({code, data}) => {
    15. // 请求正常响应
    16. if (code === 0) {
    17. // 更新订阅状态
    18. }
    19. });
    20. },
    21. // 跳转到文章详情页。跳转携带的 id 参数只是 mock 数据举例,是否需要带参数跳转以及参数名称、参数值均可自定义
    22. gotoArticle(e) {
    23. const index = e.currentTarget.dataset.aid;
    24. navigateTo({
    25. url: `../articleList/articleList?id=${index}`
    26. });
    27. }

    搜索页

    用户触发搜索功能后将跳转至搜索页,页面包含搜索历史以及搜索结果两部分。搜索结果高亮显示,方便用户辩识获取有效信息;搜索历史结构化呈现,方便用户再次进行检索。对于搜索历史,也可使用“清空”进行删除。

    页面路径:pages/search/search

    后浪云百度小程序教程:page-policy 政策文件库模板插图6

    后浪云百度小程序教程:page-policy 政策文件库模板插图7

    代码示例

    • SWAN
    • JSON
    • JS
     
     
     
    1. <view class="search-wrapper">
    2. <zw-custom-title-bar
    3. show-fixed-bar='{{false}}'
    4. fixed-title="搜索"
    5. fixed-front-color="#000000"
    6. common-front-color="#000000"
    7. common-bg-color='#ffffff'
    8. fixed-bg-color='#ffffff'
    9. common-bg-opacity="{{false}}"
    10. need-to-return="{{true}}"
    11. common-title="搜索"
    12. bind:navhome="navhome"
    13. />
    14. <view class="search-component clear">
    15. <view class="search-bar border border-bottom {{!isIos ? 'android-patch': ''}}">
    16. <zw-search-bar
    17. class='search-bar-component'
    18. value="{{value}}"
    19. focus="{{focusing}}"
    20. placeholder="{{placeholder}}"
    21. confirm-type="{{confirmType}}"
    22. search-text-color="{{searchTextColor}}"
    23. bindfocus="focusHdl"
    24. bindblur="blurHdl"
    25. bindinput="iptHdl"
    26. bindconfirm="searchBtnHdl"
    27. bindclear="emptyHdl"
    28. bindsearch="searchBtnHdl"
    29. />
    30. </view>
    31. <!-- 状态 -->
    32. <view class="status-content" s-if="showLoading || errStatus !== ''">
    33. <zw-loading s-if="showLoading" zw-loading-status-class="custom-loading">
    34. <view slot="text">正在加载...</view>
    35. </zw-loading>
    36. <zw-state-page
    37. s-if="errStatus !== ''"
    38. fixed="{{false}}"
    39. show-reload="{{errStatus === 'noNetwork' || errStatus === 'warning'}}"
    40. type="{{errStatus}}"
    41. bind:reload="reload" class-name="custom-state"/>
    42. </view>
    43. <view
    44. class="search-body"
    45. s-if="!showLoading"
    46. >
    47. <!-- 历史搜索+热门搜索 -->
    48. <view
    49. class="history-info"
    50. s-if="!value && !resultState"
    51. >
    52. <block s-if="searchInfo.info.length > 0">
    53. <view class="history-title">
    54. <text>{{searchInfo.title}}</text>
    55. <zw-icon
    56. s-if="searchInfo.delete"
    57. name="trash"
    58. size="{{iconsize}}"
    59. bindtap="deleteHis"
    60. color="#999"
    61. />
    62. </view>
    63. <view class="history-body {{searchInfo.delete ? 'history-body-shrink' : ''}}"
    64. >
    65. <view
    66. class="history-item"
    67. s-for="info in searchInfo.info"
    68. hover-class="list-hover-feedback"
    69. hover-stay-time="{{240}}"
    70. bindtap="itemSelect"
    71. data-item="{{info}}"
    72. data-type="{{item.type}}"
    73. >
    74. <text>
    75. {{info}}
    76. </text>
    77. </view>
    78. </view>
    79. </block>
    80. </view>
    81. <!-- 搜索结果 -->
    82. <view
    83. class="result"
    84. s-if="resultState"
    85. >
    86. <view s-for="item, index in list">
    87. <zw-list-item
    88. title="{{item.title}}"
    89. desc="{{'发文机关:' + item.office}}"
    90. clickable="{{true}}"
    91. data-aid="item.id"
    92. bind:tap="gotoArticle"
    93. >
    94. <view slot="title">
    95. <rich-text nodes="{{item.title}}" selectable="true" />
    96. </view>
    97. </zw-list-item>
    98. </view>
    99. </view>
    100. </view>
    101. </view>
    102. </view>
     
     
     
    1. {
    2. "navigationBarTitleText": "搜索",
    3. "navigationBarBackgroundColor": "#ffffff",
    4. "navigationBarTextStyle": "black",
    5. "navigationStyle": "custom",
    6. "enablePullDownRefresh": false,
    7. "usingComponents": {
    8. "zw-search-bar": "../../components/SearchBar/src/index",
    9. "zw-state-page": "../../components/StatePage/src/index",
    10. "zw-icon": "../../components/Icon/src/index",
    11. "zw-loading": "../../components/LoadingStatus/src/index",
    12. "zw-custom-title-bar": "../../components/CustomTitleBar/src/index",
    13. "zw-list-item": "../../components/ListItem/src/index"
    14. }
    15. }
     
     
     
    1. onLoad() {
    2. // 从缓存中获取历史记录
    3. let searchInfo = {};
    4. swan.getStorage({
    5. key: this.data.historyStorageName,
    6. success: res => {
    7. const localHistory = res.data;
    8. if (localHistory) {
    9. // 处理缓存的历史数据
    10. }
    11. },
    12. fail: () => {
    13. // 获取缓存异常处理
    14. }
    15. });
    16. },
    17. // 将历史搜索存入 Storage,缓存搜索历史
    18. saveHistory(text) {
    19. // 设置搜索历史缓存
    20. swan.setStorage({
    21. key: this.data.historyStorageName,
    22. exp: 60 * 24 * 60 * 60 * 1000,
    23. data: history
    24. });
    25. },
    26. // 删除历史记录,可直接通过清除缓存数据来删除搜索历史
    27. deleteHis() {
    28. // 弹窗二次确认
    29. swan.showModal({
    30. title: '清空搜索历史',
    31. content: '数据清空后,相关内容无法恢复',
    32. confirmText: '确认清空',
    33. cancelText: '取消',
    34. success: res => {
    35. // 确认清空
    36. if (res.confirm) {
    37. this.setData({
    38. 'searchInfo.info': []
    39. });
    40. // 移除缓存数据
    41. swan.removeStorageSync(this.data.historyStorageName);
    42. }
    43. }
    44. });
    45. },
    46. // 格式化搜索结果数据。搜索词高亮处理,高亮处理也可以直接放在接口处理,接口返回带样式的内容放在富文本展示。示例中的高亮词是mock数据。
    47. fomateValue(value) {
    48. const arr = value.split('<em');
    49. return arr.map(item => {
    50. if (item.indexOf('>') > -1) {
    51. const result = item.slice(item.indexOf('>') + 1);
    52. return result.split('</em>').join('');
    53. }
    54. return item;
    55. }).join('');
    56. },
    57. // 请求接口,获取搜索结果
    58. getResult(query, isSaveHis = false) {
    59. // 是否要保存历史记录
    60. isSaveHis && this.saveHistory(query);
    61. getSearchList(this.data.activeName, ({code, msg, data}) => {
    62. if (code === 0) {
    63. // 接口正常响应处理
    64. }
    65. else {
    66. // 接口异常处理
    67. }
    68. });
    69. },
    70. // 回到小程序的首页,url 路径可根据实际路径替换
    71. navhome() {
    72. navigateTo({
    73. url: '../indexTab/index'
    74. });
    75. },
    76. // 跳转到文章详情页。跳转携带的 id 参数只是 mock 数据举例,是否需要带参数跳转以及参数名称、参数值均可自定义
    77. gotoArticle(e) {
    78. const index = e.currentTarget.dataset.aid;
    79. navigateTo({
    80. url: `../article/article?id=${index}`
    81. });
    82. }

    自定义 UI 组件

    在政策文件库中,用到了如下自定义 UI 组件:

    组件名称 路径 说明
    Button 按钮组件 components/Button 按钮组件支持设置尺寸、文案、文字颜色、背景颜色、禁用置灰等
    CustomTitleBar 沉浸式导航栏组件 components/CustomTitleBar 沉浸式导航栏组件可实现导航栏通栏配置,上滑、下拉导航栏渐变切换效果
    Filter、FilterItem 筛选器组件 components/Filter、components/FilterItem 筛选器组件可以满足多条件筛选场景,支持多列筛选。Filter 组件需要配合 FilterItem 组件使用
    Footer 页脚组件 components/Footer 页脚组件可自定义页脚内容
    Icon 组件 components/Icon 图标组件,可设置不同图标类型、图标大小和颜色
    ListItem 列表项组件 components/ListItems 列表项组件
    LoadingStatus 加载态组件 components/LoadingStatus 可设置加载态的主题色、文案颜色
    NoticeBar 跑马灯组件 components/NoticeBar 跑马灯组件可以自动广播提示消息
    SearchBar 搜索框组件 components/SearchBar 搜索框组件支持保存搜索历史、删除搜索历史、清空搜索内容、搜索结果词高亮显示等功能
    Spin 加载组件 components/Spin 加载组件用于展示上拉加载状态,显示加载态内容,包括点击加载、正在加载、加载完成、重新加载四种状态
    StatePage 状态页组件 components/StatePage 状态页组件用于设置页面的不同展现状态,如无网络、无数据,还可配合描述文案、重新加载按钮一起使用
    Tabs 组件 components/Tabs Tabs 组件可以满足在不同视图内容中进行切换

    npm 依赖

    名称 版本号
    @smt-ui/component latest

    Bug & Tip

    • Tip:该模板使用了 ES6 语法,需要开启开发者工具的增强编译,操作步骤参看开启说明;同时也需开启上传代码时样式自动补全
    • Tip:以上代码示例为纯客户端代码,可直接在模拟器和真机预览。
    • Tip:模板中使用的是测试数据,你需要从接口中获取真实的数据。
    THE END