Implementation code for the like and collection functions in the mini program

不言
Release: 2018-09-07 14:47:25
Original
5437 people have browsed it

The like function and collection function in the mini program are essential, so how to implement the two functions of like and collection? This article will share with you the code implementation of the like function and collection function in the mini program.

Collection function:

focusFavoriteTab: function (e) {
    var that = this;
    var isFocus = that.data.isFocus;
    console.log("isFocus", isFocus)
    var itemId = that.data.itemId;
    if (isFocus) {
      favoriteService.cancel(that, itemId).then((res) => {
        wx.showToast({
          title: "取消收藏",
          icon: 'success',
          duration: 1000
        });
        this.setData({
          isFocus: false,
        });
      })
    } else {
      favoriteService.add(that, itemId).then((res) => {
        wx.showToast({
          title: res.data == 1 ? "收藏成功" : "收藏失败",
          icon: 'success',
          duration: 1000
        });
        this.setData({
          isFocus: true,
        });
      });
    }
  },
Copy after login

Like function:

focuslikeTab: function (e) {
    var that = this;
    var isLike = that.data.isLike;
    let itemDetail = this.data.itemDetail 
    var itemId = that.data.itemId;
    if (isLike) {
      itemService.cancellike(that, itemId).then((res) => {
        wx.showToast({
          title: "点赞取消",
          icon: 'success',
          duration: 1000
        });
        itemDetail.liked--;
        this.setData({
          itemDetail:itemDetail
        });
        this.setData({
          isLike: false,
        });
      })
    } else {
      itemService.addlike(that, itemId).then((res) => {
        wx.showToast({
          title: res.data == 1 ? "点赞成功" : "点赞失败",
          icon: 'success',
          duration: 1000
        });
        itemDetail.liked++;
        this.setData({
          itemDetail: itemDetail
        });
        this.setData({
          isLike: true,
        });
      });
    }
  },
Copy after login

Related recommendations:

About the implementation of the WeChat applet collection function

How to implement the like function in WeChat mini program

The above is the detailed content of Implementation code for the like and collection functions in the mini program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template