Home  >  Article  >  WeChat Applet  >  Implementation code for the like and collection functions in the mini program

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

不言
不言Original
2018-09-07 14:47:255424browse

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,
        });
      });
    }
  },

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,
        });
      });
    }
  },

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!

Statement:
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