会员浏览记录架构
概述
- 会员在用户端访问商品详情页面后,会产生浏览记录
- 系统会每日定时清除时间较长的浏览记录,只保留距离当前时间最近的100条浏览记录信息
数据库设计
表名:es_history
名称 | 类型 | 长度 | 说明 |
---|---|---|---|
id | int | 10 | 主键 |
goods_id | int | 10 | 商品id |
goods_name | varchar | 255 | 商品名称 |
goods_price | decimal | 20 | 商品价格 |
goods_img | varchar | 255 | 商品主图 |
member_id | int | 10 | 会员id |
member_name | varchar | 255 | 会员名称 |
create_time | bigin | 20 | 浏览时间 |
update_time | bigin | 20 | 更新时间 |
流程时序图
代码展示
API说明
新增会员浏览记录
此API是一个公共API,不单单是只新增会员商品浏览记录,还可以根据参数url判断访问的是什么页面,从而进行相关的访问次数统计
API地址: | https://{buyer-api-domain}/buyer/view | |
---|---|---|
请求方式: | GET | |
参数1: | url | url地址 |
参数2: | uuid | 访问请求唯一标识 |
请求示例:
GET: https://buyer-api.xx.com/buyer/view
查询会员浏览记录分页列表数据
API地址: | https://{buyer-api-domain}/buyer/members/history/list | |
---|---|---|
请求方式: | GET | |
参数1: | page_no | 分页页数 |
参数2: | page_size | 每页数量 |
请求示例:
GET: https://buyer-api.xx.com/buyer/members/history/list
返回结果为HistoryVO对象集合,内容如下:
字段 | 类型 | 说明 |
---|---|---|
time | Long | 浏览记录时间 |
history | List<HistoryDO> | 浏览记录数据集合 |
返回结果格式如下:
{
"data":[
{
"time":1691337600,
"history":[
{
"id":"1688489362798084098",
"goods_id":"107",
"goods_name":"儿童电动车摇摆童车四轮室内车带遥控玩具车可坐人摩托车电动汽车",
"goods_price":67.32,
"goods_img":"http://javashop-statics.oss-cn-beijing.aliyuncs.com/demo/6BA97E6D16BF4D52B0EF2D59F4F6A994.jpg_300x300",
"member_id":"1681589993696092162",
"member_name":"duan",
"create_time":"1691402209",
"update_time":"1691337600"
}
]
},
{
"time":1689782400,
"history":[
{
"id":"1681590955827404801",
"goods_id":"121",
"goods_name":"童装男童秋冬装套装2018新款儿童加绒加厚金丝绒卫衣三件套中大童",
"goods_price":0.01,
"goods_img":"http://javashop-statics.oss-cn-beijing.aliyuncs.com/demo/10B51E8C320A4AEF8F0D917FA7FB1CD2.jpg_300x300",
"member_id":"1681589993696092162",
"member_name":"duan",
"create_time":"1689838726",
"update_time":"1689782400"
}
]
}
],
"page_no":1,
"page_size":10,
"data_total":2
}
删除某条浏览记录
API地址: | https://{buyer-api-domain}/buyer/members/history/{id} | |
---|---|---|
请求方式: | DELETE | |
参数1: | id | 主键ID |
请求示例:
DELETE: https://buyer-api.xx.com/buyer/members/history/1688489334398451713
根据日期删除浏览记录
API地址: | https://{buyer-api-domain}/buyer/members/history/day/{day} | |
---|---|---|
请求方式: | DELETE | |
参数1: | day | 日期时间戳。例如2023-08-08 15:05:48的时间戳就是1691478348 |
请求示例:
DELETE: https://buyer-api.xx.com/buyer/members/history/day/1691478348
清空所有浏览记录
API地址: | https://{buyer-api-domain}/buyer/members/history | |
---|---|---|
请求方式: | DELETE |
请求示例:
DELETE: https://buyer-api.xx.com/buyer/members/history
代码展示
发送新增浏览记录消息
com.enation.app.javashop.service.statistics.impl.DisplayTimesManagerImpl
/**
* 访问某地址
*
* @param url 访问的地址
* @param uuid 客户唯一id
*/
@Override
public void view(String url, String uuid) {
//判定访问是商品还是店铺
int type = regular(url);
//如果访问的是商品,那么需求新增或更新会员商品浏览足迹
if (type == 1) {
createFootmark(urlParams(url, type));
}
}
/**
* 匹配当前url访问的是商品还是店铺
*
* @param url 访问地址
* @return 1 商品 0店铺 2 无效
*/
private int regular(String url) {
if (StringUtil.isEmpty(url)) {
return 2;
}
if (url.contains("/shop/")) {
return 0;
} else if (url.contains("/goods/")) {
return 1;
} else {
return 2;
}
}
/**
* 处理会员对商品访问的历史足迹信息
* @param goodsId 商品id
*/
private void createFootmark(Long goodsId) {
//对商品访问历史足迹进行统计
//获取当前会员
Buyer buyer = UserContext.getBuyer();
//如果当前会员为空或者商品id为null则不去生成足迹
if (buyer != null && goodsId != null) {
HistoryDTO historyDTO = new HistoryDTO(goodsId,buyer.getUid());
this.messageSender.send(historyDTO);
}
}
新增和更新浏览记录
com.enation.app.javashop.service.member.impl.HistoryManagerImpl
@Override
@Transactional(value = "memberTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public HistoryDO addMemberHistory(HistoryDTO historyDTO) {
//会员id
Long memberId = historyDTO.getMemberId();
//校验此商品是否存在并且上架状态
CacheGoods cacheGoods = goodsClient.getFromCache(historyDTO.getGoodsId());
//如果商品为下架状态则不记录足迹
if (!cacheGoods.getMarketEnable().equals(1)) {
return null;
}
//检测此商品是否已经存在浏览记录
HistoryDO historyDO = this.getHistoryByGoods(historyDTO.getGoodsId(), historyDTO.getMemberId());
//如果为空则是添加反之为修改
if (historyDO != null) {
historyDO.setCreateTime(DateUtil.getDateline());
historyDO.setUpdateTime(getDateDay());
historyDO.setGoodsName(cacheGoods.getGoodsName());
historyDO.setGoodsPrice(cacheGoods.getPrice());
historyDO.setGoodsImg(cacheGoods.getThumbnail());
this.edit(historyDO, historyDO.getId());
return historyDO;
}
//获取当前会员
Member member = memberClient.getModel(historyDTO.getMemberId());
//如果当前会员不存在,则不记录信息
if (member == null) {
return null;
}
//新建会员浏览足迹最新
historyDO = new HistoryDO();
//设置商品图片
historyDO.setGoodsImg(cacheGoods.getThumbnail());
//设置商品名称
historyDO.setGoodsName(cacheGoods.getGoodsName());
//设置商品ID
historyDO.setGoodsId(historyDTO.getGoodsId());
//设置商品价格
historyDO.setGoodsPrice(cacheGoods.getPrice());
//设置会员ID
historyDO.setMemberId(memberId);
//设置会员名称
historyDO.setMemberName(member.getUname());
//设置添加时间
historyDO.setCreateTime(DateUtil.getDateline());
//设置更新时间
historyDO.setUpdateTime(getDateDay());
//会员浏览足迹入库
historyMapper.insert(historyDO);
return historyDO;
}