Commit eec97329 authored by tangtuo's avatar tangtuo

修改token有效期

修改用户收藏的接口
parent 01262836
......@@ -54,8 +54,6 @@ spring:
token-name: Authorization
# token有效期,单位s, -1代表永不过期
timeout: 7200
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: 3600
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
......
......@@ -54,8 +54,6 @@ spring:
token-name: Authorization
# token有效期,单位s 默认30天, -1代表永不过期
timeout: 7200
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: 3600
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
......
......@@ -54,8 +54,6 @@ spring:
token-name: Authorization
# token有效期,单位s, -1代表永不过期
timeout: 7200
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: 3600
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
......
package com.fzm.common.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* @author tangtuo
* @date 2021/6/23 11:54
*/
@Data
@Accessors(chain = true)
@ApiModel("收藏")
@TableName("tb_collection")
public class TbCollection {
@TableId(type = IdType.AUTO)
private Integer id;
@ApiModelProperty("用户主键")
private Integer userId;
@ApiModelProperty("nft主键")
private Integer nftId;
@ApiModelProperty("创建时间")
private Date createDate;
@ApiModelProperty("修改时间")
private Date updateDate;
}
package com.fzm.common.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzm.common.entity.TbCollection;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author tangtuo
* @date 2021/7/1 14:35
*/
@Mapper
public interface CollectionMapper extends BaseMapper<TbCollection> {
/**
* 根据用户id查询所有收藏的nftId
* @param userId
* @return
*/
List<Integer> getNftIdListByUserId(int userId);
}
package com.fzm.common.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fzm.common.entity.TbCollection;
import java.util.List;
/**
* @author tangtuo
* @date 2021/7/1 14:35
*/
public interface CollectionService extends IService<TbCollection> {
/**
* 根据用户id和nftId删除记录
*
* @param userId
* @param nftId
*/
void deleteByUserIdAndNftId(int userId, Integer nftId);
/**
* 根据用户id查询所有收藏的nftId
*
* @param userId
* @return
*/
List<Integer> getNftIdListByUserId(int userId);
}
package com.fzm.common.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.common.entity.TbCollection;
import com.fzm.common.mapper.CollectionMapper;
import com.fzm.common.service.CollectionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @author tangtuo
* @date 2021/7/1 14:36
*/
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, TbCollection> implements CollectionService {
@Resource
private CollectionMapper collectionMapper;
@Override
public void deleteByUserIdAndNftId(int userId, Integer nftId) {
QueryWrapper<TbCollection> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId).eq("nft_id", nftId);
remove(wrapper);
}
@Override
public List<Integer> getNftIdListByUserId(int userId) {
return collectionMapper.getNftIdListByUserId(userId);
}
}
......@@ -13,12 +13,14 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.common.constant.RedisConstant;
import com.fzm.common.constant.SystemConstant;
import com.fzm.common.entity.*;
import com.fzm.common.entity.TbCollection;
import com.fzm.common.entity.vo.CollectionNftVo;
import com.fzm.common.entity.vo.NftListVo;
import com.fzm.common.enums.ResultCode;
import com.fzm.common.exception.GlobalException;
import com.fzm.common.mapper.NftMapper;
import com.fzm.common.service.CategoryService;
import com.fzm.common.service.CollectionService;
import com.fzm.common.service.NftService;
import com.fzm.common.service.UserService;
import com.fzm.common.utils.JsonUtil;
......@@ -29,6 +31,7 @@ import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -61,6 +64,9 @@ public class NftServiceImpl extends ServiceImpl<NftMapper, Nft> implements NftSe
private CategoryService categoryService;
@Resource
private CollectionService collectionService;
@Resource
private OssUtil ossUtil;
@Value("${chain.para.contract-name}")
......@@ -146,11 +152,19 @@ public class NftServiceImpl extends ServiceImpl<NftMapper, Nft> implements NftSe
@Override
public Boolean collection(Integer id) {
String key = RedisConstant.COLLECTION_USER_PREFIX + StpUtil.getLoginIdAsInt();
int userId = StpUtil.getLoginIdAsInt();
String key = RedisConstant.COLLECTION_USER_PREFIX + userId;
// 如果用户收藏的nft已经存在列表里,那么就是取消收藏
if (redisUtil.sIsMember(key, id.toString())) {
collectionService.deleteByUserIdAndNftId(userId, id);
return redisUtil.sRemove(key, id.toString()) == 1;
} else {
TbCollection collection = new TbCollection().setNftId(id).setUserId(userId);
try {
collectionService.save(collection);
} catch (DuplicateKeyException e) {
collectionService.deleteByUserIdAndNftId(userId, id);
}
return redisUtil.sAdd(key, id.toString()) == 1;
}
}
......@@ -158,13 +172,21 @@ public class NftServiceImpl extends ServiceImpl<NftMapper, Nft> implements NftSe
@Override
public List<CollectionNftVo> getCollectionList() {
String key = RedisConstant.COLLECTION_USER_PREFIX + StpUtil.getLoginIdAsInt();
Set<String> set = redisUtil.setMembers(key);
int userId = StpUtil.getLoginIdAsInt();
// 从redis里获取当前用户的收藏列表
Set<String> set = redisUtil.setMembers(RedisConstant.COLLECTION_USER_PREFIX + StpUtil.getLoginIdAsInt());
List<Integer> list;
if (CollectionUtil.isEmpty(set)) {
// redis里为空,再从mysql里查一次
list = collectionService.getNftIdListByUserId(userId);
} else {
list = set.stream().map(Integer::valueOf).collect(Collectors.toList());
}
// mysql和redis里都为空,直接返回空集合
if (CollectionUtil.isEmpty(list)) {
return new ArrayList<>();
}
List<Integer> ids = set.stream().map(Integer::valueOf).collect(Collectors.toList());
return nftMapper.getCollectionList(ids);
return nftMapper.getCollectionList(list);
}
@Override
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fzm.common.mapper.CollectionMapper">
<select id="getNftIdListByUserId" resultType="java.lang.Integer">
select nft_id
from tb_collection
where user_id = #{userId}
</select>
</mapper>
\ No newline at end of file
......@@ -58,8 +58,6 @@ spring:
token-name: Authorization
# token有效期,单位s 默认30天, -1代表永不过期
timeout: 86400
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: 3600
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
......
......@@ -27,7 +27,7 @@ spring:
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
useGlobalDataSourceStat: true
redis:
host: 172.16.101.135
host: localhost
port: 6379
password: 123456
lettuce:
......@@ -56,10 +56,8 @@ spring:
sa-token:
# token名称 (同时也是cookie名称)
token-name: Authorization
# token有效期,单位s 默认30天, -1代表永不过期
timeout: 86400
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: 3600
timeout: 86400
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
......
......@@ -58,8 +58,6 @@ spring:
token-name: Authorization
# token有效期,单位s 默认30天, -1代表永不过期
timeout: 86400
# token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: 3600
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
......
......@@ -103,10 +103,14 @@ VALUES (7, '其他', 'OTHER', '2021-07-01 06:30:50', '2021-07-01 06:30:50', NULL
DROP TABLE IF EXISTS `tb_collection`;
CREATE TABLE `tb_collection`
(
`user_id` int(11) NOT NULL COMMENT '用户主键',
`nft_id` int(11) NOT NULL COMMENT 'nft主键',
PRIMARY KEY (`user_id`, `nft_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_id` int(11) NOT NULL COMMENT '用户主键',
`nft_id` int(11) NOT NULL COMMENT 'nft主键',
`create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `idx_userid_nftid` (`user_id`,`nft_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for tb_label
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment