关注/取消关注

**

关注/取消关注

**
1.UserApplication启动类

package com.heima.user;

//baimidou
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
//扫描mapper,application.yml里面定义path地址
@MapperScan("com.heima.user.mapper")
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

2.UserRelationController

package com.heima.user.controller.v1;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.user.service.ApUserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/user")
public class UserRelationController  {

    @Autowired
    ApUserRelationService apUserRelationService;

    @PostMapping("/user_follow")
    //dto是传入数据
    public ResponseResult follow(@RequestBody UserRelationDto dto){
        return apUserRelationService.follow(dto);
    }
}

3.ApUserRelationService

package com.heima.user.service;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.UserRelationDto;
public interface ApUserRelationService {
    /**
     * 用户关注/取消关注
     * @param dto
     * @return
     */
    public ResponseResult follow(UserRelationDto dto);
}
  1. ApUserRelationServiceImpl
package com.heima.user.service.impl;

import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.config.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {


    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 用户关注/取消关注
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1 参数校验
        if (dto==null || dto.getAuthorApUserId()==null
                ||   dto.getOperation() < 0 || dto.getOperation() > 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 获取当前登录人的apuser id
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer apUserId = apUser.getId();  // 当前登录APP端用户id

        Integer followId = dto.getAuthorApUserId(); // 被关注的作者对应用户id

        //3 判断是否已关注
        if (dto.getOperation() == 0) {
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + apUserId, followId);
            if (score != null) {
                CustException.cust("已关注,无需重复关注");
            }

            //4 关注 follow:id   fans:id
            // 将对方写入我的关注中
            redisTemplate.opsForZSet().add(UserRelationConstants.FOLLOW_LIST + apUserId, followId, System.currentTimeMillis());
            // 将我写入对方的粉丝中
            redisTemplate.opsForZSet().add(UserRelationConstants.FANS_LIST + followId, apUserId, System.currentTimeMillis());


        }else {
            //5 取消关注
            redisTemplate.opsForZSet().remove(UserRelationConstants.FOLLOW_LIST + apUserId, followId);

            redisTemplate.opsForZSet().remove(UserRelationConstants.FANS_LIST + followId, apUserId);
        }

        //6 返回
        return ResponseResult.okResult();
    }
//    public ResponseResult follow1(UserRelationDto dto){
//        //1参数校验
//        if (dto==null||dto.getAuthorApUserId()==null||dto.getOperation()<0||dto.getOperation()>1) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
//        //2获取当前登录人的apuser id
//        ApUser apUser = AppThreadLocalUtils.getUser();
//
//        if (apUser==null) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
//        }
//        Integer apUserId =apUser.getId();
//        Integer followId = dto.getAuthorApUserId();
//
//        //3判断是否已关注
//        if (dto.getOperation()==0) {
//            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + apUserId, followId);
//            if (score!=null) {
//                CustException.cust("已关注,无需重复关注");
//            }
//            //4关注follow:id fans   :id
//            //将对方写入我的关注中
//            redisTemplate.opsForZSet().add(UserRelationConstants.FOLLOW_LIST+apUserId,followId,System.currentTimeMillis());
//
//            //将我写入对方的粉丝中
//            redisTemplate.opsForZSet().add(UserRelationConstants.FANS_LIST+followId,apUserId,System.currentTimeMillis());
//
//            //
//        }else{
//            Long remove = redisTemplate.opsForZSet().remove(UserRelationConstants.FOLLOW_LIST);
//            redisTemplate.opsForZSet().remove(UserRelationConstants.FANS_LIST+followId,apUserId);
//        }
//
//        //5取消关注
//        //6返回
//        return ResponseResult.okResult();
//    }
}

5.mapper省略

6.AppThreadLocalUtils

package com.heima.utils.threadlocal;

import com.heima.model.user.pojos.ApUser;

/**
 * @Description:
 * @Version: V1.0
 */
public class AppThreadLocalUtils {


    private final static ThreadLocal<ApUser> userThreadLocal = new ThreadLocal<>();


    /**
     * 设置当前线程中的用户
     * @param user
     */
    public static void setUser(ApUser user){
        userThreadLocal.set(user);
    }

    /**
     * 获取线程中的用户
     * @return
     */
    public static ApUser getUser( ){
        return userThreadLocal.get();
    }

    /**
     * 清除当前线程用户
     */
    public static void remove() {
        userThreadLocal.remove();
    }

}
  1. UserRelationConstants
package com.heima.common.constants.user;

/**
 * @Description:
 * @Version: V1.0
 */
public class UserRelationConstants {


    // 用户关注key
    public static final String FOLLOW_LIST = "apuser:follow:";
    // 用户粉丝key
    public static final String FANS_LIST = "apuser:fans:";

}

8.UserRelationDto

package com.heima.model.user.dtos;
import lombok.Data;
@Data
public class UserRelationDto {
    // 文章作者ID
    Integer authorId;
    // 作者对应的apuserid
    Integer authorApUserId;
    // 文章id
    Long articleId;
    /**
     * 操作方式
     * 0  关注
     * 1  取消
     */
    Short operation;
}

9.pojos

package com.heima.model.user.pojos;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * APP实名认证信息表
 * </p>
 *
 * @author itheima
 */
@Data
@TableName("ap_user_realname")
public class ApUserRealname implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 账号ID
     */
    @TableField("user_id")
    private Integer userId;

    /**
     * 用户名称
     */
    @TableField("name")
    private String name;

    /**
     * 资源名称
     */
    @TableField("idno")
    private String idno;

    /**
     * 正面照片
     */
    @TableField("font_image")
    private String fontImage;

    /**
     * 背面照片
     */
    @TableField("back_image")
    private String backImage;

    /**
     * 手持照片
     */
    @TableField("hold_image")
    private String holdImage;

    /**
     * 活体照片
     */
    @TableField("live_image")
    private String liveImage;

    /**
     * 状态
            0 创建中
            1 待审核
            2 审核失败
            9 审核通过
     */
    @TableField("status")
    private Short status;

    /**
     * 拒绝原因
     */
    @TableField("reason")
    private String reason;

    /**
     * 创建时间
     */
    @TableField("created_time")
    private Date createdTime;

    /**
     * 提交时间
     */
    @TableField("submited_time")
    private Date submitedTime;

    /**
     * 更新时间
     */
    @TableField("updated_time")
    private Date updatedTime;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_51297617

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值