10. 开发物品收藏功能

196 字约 1 分钟读完283 次阅读更新于 2026/5/3

JS 去除页面的参数

const clearPathParam = () => {
  let url = location.href
  url = url.replace(/(\?|#)[^'"]*/, '');           //去除参数
  window.history.pushState({},0, url);
}

SQL

CREATE TABLE `collect` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `item_id` int DEFAULT NULL COMMENT '物品',
  `user_id` int DEFAULT NULL COMMENT '收藏人',
  `time` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收藏信息';

开发后端接口

Collect.java

CollectMapper.xml

物品的分页查询

/**
     * 分页查询
     */
    public PageInfo<Items> selectPage(Items items, Integer loginUserId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Items> list = itemsMapper.selectAll(items);
        if (loginUserId != null) {
            for (Items item : list) {
                // 设置当前用户的收藏ID  获取用户收藏的信息
                Collect collect = collectMapper.selectByItemIdAndUserId(item.getId(), loginUserId);
                if (collect != null) {
                    item.setCollectId(collect.getId());
                }
            }
        }
        return PageInfo.of(list);
    }
@Select("select * from `collect` where item_id = #{itemId} and user_id = #{userId}")
Collect selectByItemIdAndUserId(@Param("itemId") Integer itemId, @Param("userId") Integer userId);

开发前端页面

ItemsViews.vue

Collect.vue

UserCollect.vue