微信小程序—收藏功能

wxml代码如下:

 <image src="{{isFavorite=='false'?'/images/menu/collect.png':'/images/menu/uncollect.png'}}" class='collect' bindtap='collect'></image><span class="num font_s30 font_c32">收藏</span>

js部分代码如下:

  data: {
    isFavorite: "false",
  },
collect:function() {
      var that = this;
      var isFavorite = that.data.isFavorite;
      //收藏
      if (isFavorite == 'false') {
        var roomId = that.data.roomId;
          that.setData({
            isFavorite:'true'
          })
          wx.showToast({
            title: '收藏成功',
            icon: 'none'
          })
      } 
      //取消收藏
      else if(isFavorite == 'true') {
        var roomId = that.data.roomId;
          that.setData({
            isFavorite: 'false'
          })
          wx.showToast({
            title: '取消成功',
            icon: 'none'
          })
      }

  },

实现效果如下:
在这里插入图片描述 在这里插入图片描述
这里都是前端代码,我只负责写页面和调接口,至于后端代码怎么写的,我也不清楚

在这里插入图片描述

可以回答这个问题。以下是一个简单的实现收藏功能的 Spring Boot 微信小程序代码示例: 1. 创建一个收藏实体类 ```java @Entity @Table(name = "collection") public class Collection { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private Long userId; private Long articleId; public Collection() { } public Collection(Long userId, Long articleId) { this.userId = userId; this.articleId = articleId; } // getters and setters } ``` 2. 创建一个收藏服务类 ```java @Service public class CollectionService { @Autowired private CollectionRepository collectionRepository; public boolean isCollected(Long userId, Long articleId) { Collection collection = collectionRepository.findByUserIdAndArticleId(userId, articleId); return collection != null; } public void collect(Long userId, Long articleId) { if (!isCollected(userId, articleId)) { Collection collection = new Collection(userId, articleId); collectionRepository.save(collection); } } public void cancelCollect(Long userId, Long articleId) { Collection collection = collectionRepository.findByUserIdAndArticleId(userId, articleId); if (collection != null) { collectionRepository.delete(collection); } } } ``` 3. 创建一个收藏控制器类 ```java @RestController @RequestMapping("/api/collection") public class CollectionController { @Autowired private CollectionService collectionService; @PostMapping("/collect") public void collect(@RequestParam("userId") Long userId, @RequestParam("articleId") Long articleId) { collectionService.collect(userId, articleId); } @PostMapping("/cancelCollect") public void cancelCollect(@RequestParam("userId") Long userId, @RequestParam("articleId") Long articleId) { collectionService.cancelCollect(userId, articleId); } @GetMapping("/isCollected") public boolean isCollected(@RequestParam("userId") Long userId, @RequestParam("articleId") Long articleId) { return collectionService.isCollected(userId, articleId); } } ``` 以上代码仅供参考,实际应用时需要根据具体需求进行修改和完善。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值