百度智能小程序 點(diǎn)贊

2020-08-28 15:51 更新

like 點(diǎn)贊

解釋: 通過點(diǎn)贊組件,用戶可以對(duì)文章內(nèi)容或者評(píng)論內(nèi)容進(jìn)行點(diǎn)贊,被點(diǎn)贊的用戶可以收到消息通知。更多點(diǎn)贊設(shè)計(jì)指引,詳見 如何提升小程序互動(dòng)體驗(yàn) 。

屬性說明

屬性名 類型 必填 默認(rèn)值 說明
is-liked Boolean false 是否已被點(diǎn)贊
mode String icon 按鈕模式:icon 表示僅有圖標(biāo); mixture 表示圖標(biāo)文字結(jié)合
icon-type String hand 圖標(biāo)類型:hand 表示手形和 heart 表示心形
style String none 僅在 mode 為 mixture 時(shí)可配置, none:無邊框;border:有邊框
like-text String 點(diǎn)贊按鈕上的文案,默認(rèn)為贊,僅在 mode 屬性值為’mixture’時(shí)有效
like-num Number 0 點(diǎn)贊數(shù)量
like-type Number 0 被點(diǎn)贊的對(duì)象類型,0 表示對(duì)文章內(nèi)容進(jìn)行點(diǎn)贊; 1 表示對(duì)評(píng)論內(nèi)容進(jìn)行點(diǎn)贊
animation-type Number 1 點(diǎn)贊動(dòng)效形式,0 無動(dòng)效;1 輕動(dòng)效;2 強(qiáng)動(dòng)效
is-show-toast Boolean false 點(diǎn)贊后是否彈出 toast 提示
toast-text Array [‘已點(diǎn)贊’, ‘已取消’] toast 文案,默認(rèn)為已點(diǎn)贊、已取消
like-param Object 點(diǎn)贊服務(wù)需要的必要參數(shù)
bind:error EventHandle 點(diǎn)擊按鈕時(shí)在用戶未登錄狀態(tài)下觸發(fā)的事件
bind:success EventHandle 點(diǎn)擊點(diǎn)贊按鈕,在點(diǎn)贊服務(wù)成功后將狀態(tài)返回給使用組件者
bind:fail EventHandle 點(diǎn)擊點(diǎn)贊按鈕,在點(diǎn)贊服務(wù)失敗后將狀態(tài)返回給使用組件者

like-param 點(diǎn)贊服務(wù)參數(shù)說明

snid 和 srid 分別是文章內(nèi)容和評(píng)論內(nèi)容的唯一標(biāo)識(shí),由開發(fā)者創(chuàng)建和維護(hù)。當(dāng)對(duì)文章內(nèi)容進(jìn)行點(diǎn)贊時(shí),只需要填寫 snid;當(dāng)對(duì)文章下某一評(píng)論進(jìn)行點(diǎn)贊時(shí),需要填寫 snid 和 spid。

屬性名 類型 必填 默認(rèn)值 說明
openid String 用戶身份唯一標(biāo)識(shí),獲取方法
snid String 被點(diǎn)贊的文章的 id,與 path 參數(shù)一一對(duì)應(yīng),例如值為’20200101’
spid String 被點(diǎn)贊的評(píng)論 id
title String 文章標(biāo)題
path String 文章頁面地址,用于消息推送等流量入口跳轉(zhuǎn)回訪,與 snid 參數(shù)一一對(duì)應(yīng),例如/page/home/index?snid=20200101

示例 

在開發(fā)者工具中打開


圖片示例


使用方式

方式一:npm

# 進(jìn)入項(xiàng)目根目錄, 路徑以./projectRoot為例
cd ./projectRoot
# 未安裝過點(diǎn)贊組件
npm install @smt-ui/content-component
# 以前安裝過點(diǎn)贊組件
npm update
{
    "usingComponents": {
        "smt-like": "@smt-ui/content-component/src/like"
    }
}

方式二:動(dòng)態(tài)庫

在 app.json 中引入內(nèi)容服務(wù)組件動(dòng)態(tài)庫

    {
        "dynamicLib": {
            "myDynamicLib": {
                "provider": "smart-sc"
            }
        }
    }

在頁面配置 json 文件中,以動(dòng)態(tài)庫方式引入

    {
        "usingComponents": {
            "smt-like": "dynamicLib://myDynamicLib/like"
        }
    }

代碼示例

<custom chineseName="點(diǎn)贊" engName="like"></custom>
<view class="container">
    <view class="card-area">
        <view class="top-description border-bottom">圖標(biāo)按鈕樣式</view>
        <view class="button-box">
            <smt-like
                class="custom-class"
                like-param="{{likeParam}}"
                bind:error="error"
                bind:success="clicksucc">
            </smt-like>
            <smt-like
                icon-type="heart"
                like-param="{{likeParam}}"
                bind:error="error">
            </smt-like>
        </view>
    </view>
    <view class="card-area">
        <view class="top-description border-bottom">組合按鈕樣式</view>
        <view class="button-box button-box-mixture">
            <smt-like
                class="custom-class-mixture"
                mode="mixture"
                like-param="{{likeParam}}"
                like-text="{{likeText}}"
                bind:error="error">
            </smt-like>
            <smt-like
                class="custom-class-mixture"
                likeNum="12"
                mode="mixture"
                icon-type="heart"
                like-param="{{likeParam}}"
                like-text="{{likeText}}"
                bind:error="error">
            </smt-like>
            <smt-like
                class="custom-class-mixture"
                mode="mixture"
                style="border"
                like-param="{{likeParam}}"
                like-text="{{likeText}}"
                bind:error="error">
            </smt-like>
            <smt-like
                mode="mixture"
                likeNum="12"
                style="border"
                icon-type="heart"
                like-param="{{likeParam}}"
                bind:error="error">
            </smt-like>
        </view>
    </view>
    <view class="card-area">
        <view class="top-description border-bottom">動(dòng)效組合樣式1</view>
        <view class="button-box button-box-mix">
            <view class="button-item">
                <smt-like
                    animation-type="{{0}}"
                    like-param="{{likeParam}}"
                    bind:success="clicksucc"
                    bind:error="error">
                </smt-like>
                <view class="button-tip">無動(dòng)效</view>
            </view>
            <view class="button-item">
                <smt-like
                    like-param="{{likeParam}}"
                    bind:success="clicksucc"
                    bind:error="error">
                </smt-like>
                <view class="button-tip">輕動(dòng)效</view>
            </view>
            <view class="button-item">
                <smt-like
                    animation-type="{{2}}"
                    like-param="{{likeParam}}"
                    bind:success="clicksucc"
                    bind:error="error">
                </smt-like>
                <view class="button-tip">強(qiáng)動(dòng)效</view>
            </view>
        </view>
    </view>
    <view class="card-area">
        <view class="top-description border-bottom">動(dòng)效組合樣式2</view>
        <view class="button-box button-box-mix">
            <view class="button-item">
                <smt-like
                    icon-type="heart"
                    animation-type="{{0}}"
                    like-param="{{likeParam}}"
                    bind:success="clicksucc"
                    bind:error="error">
                </smt-like>
                <view class="button-tip">無動(dòng)效</view>
            </view>
            <view class="button-item">
                <smt-like
                    icon-type="heart"
                    like-param="{{likeParam}}"
                    bind:success="clicksucc"
                    bind:error="error">
                </smt-like>
                <view class="button-tip">輕動(dòng)效</view>
            </view>
            <view class="button-item">
                <smt-like
                    icon-type="heart"
                    animation-type="{{2}}"
                    like-param="{{likeParam}}"
                    bind:success="clicksucc"
                    bind:error="error">
                </smt-like>
                <view class="button-tip">強(qiáng)動(dòng)效</view>
            </view>
        </view>
    </view>
    <view class="card-area">
        <view class="top-description border-bottom">點(diǎn)擊提示反饋</view>
        <view class="button-box">
            <smt-like
                is-show-toast="{{true}}"
                like-param="{{likeParam}}"
                bind:success="clicksucc"
                bind:error="error">
            </smt-like>
        </view>
    </view>
    <view class="space"></view>
</view>
Page({
    data: {
        likeParam: {}
    },
    onLoad() {
        const pageStack = getCurrentPages();
        const currentPage = pageStack[pageStack.length - 1];
        const privateProperties = currentPage.privateProperties || {};
        const currentUri = privateProperties.accessUri || currentPage.uri;
        const snid = 'test_snid';
        this.setData('likeParam', {
            snid: snid,
            spid: '' + Date.now(),
            openid: 'mVMFstfXtsndgnRObr7BoP9hoL',
            title: '我是文章標(biāo)題',
            path: currentUri + '&snid=' + snid
        });
    },
    error() {
        // 處理未登錄
        swan.login({
            success: res => {
                console.log('login success', res);
            },
            fail: err => {
                console.log('login fail', err);
            }
        });
    }
});

Bug&Tip

  • 只有登錄用戶才能進(jìn)行對(duì)文章內(nèi)容或者評(píng)論內(nèi)容的點(diǎn)贊
  • 只有小程序使用點(diǎn)贊組件后,被點(diǎn)贊的用戶才能收到消息通知
  • like-param 點(diǎn)贊服務(wù)參數(shù)中,snid 和 path 是一一對(duì)應(yīng),能保證點(diǎn)贊服務(wù)的可用性


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)