Преглед изворни кода

用户模块 用户注销逻辑(待补充增加注销用户记录),优化获取第三方账号日志打印

songfuqiang пре 1 година
родитељ
комит
49726b0309
17 измењених фајлова са 168 додато и 39 уклоњено
  1. 3 3
      backend-core/src/main/java/cn/kdan/pdf/backend/core/controller/MembersController.java
  2. 5 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/AvatarService.java
  3. 17 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/CancelMemberService.java
  4. 1 1
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/MemberService.java
  5. 6 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/MissionFilesService.java
  6. 6 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/MissionService.java
  7. 6 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/SubscriptionService.java
  8. 17 3
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/AvatarServiceImpl.java
  9. 50 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/CancelMemberServiceImpl.java
  10. 0 7
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/DeviceServiceImpl.java
  11. 25 5
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/MemberServiceImpl.java
  12. 6 1
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/MissionFileServiceImpl.java
  13. 6 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/MissionServiceImpl.java
  14. 0 8
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/SocialAccountServiceImpl.java
  15. 12 0
      backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/SubscriptionServiceImpl.java
  16. 4 8
      backend-core/src/main/java/cn/kdan/pdf/backend/core/utils/TencentUtils.java
  17. 4 3
      backend-core/src/main/java/cn/kdan/pdf/backend/core/utils/WechatUtils.java

+ 3 - 3
backend-core/src/main/java/cn/kdan/pdf/backend/core/controller/MembersController.java

@@ -149,9 +149,9 @@ public class MembersController {
      * @return 提示信息
      */
     @GetMapping("/cancelAccount")
-    public AppResultMap<String> cancelAccount(){
-        String memberid = memberService.cancelAccount();
-        return new AppResultMap<>(CommonConstant.SUCCESS,CommonConstant.CODE_SUCCESS,"注销账号成功");
+    public AppResultMap<String> cancelAccount(@RequestParam String cancelReason){
+        String memberId = memberService.cancelAccount(cancelReason);
+        return new AppResultMap<>(CommonConstant.SUCCESS,CommonConstant.CODE_SUCCESS,"用户" + memberId + "注销账号成功");
     }
 
     /**

+ 5 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/AvatarService.java

@@ -31,4 +31,9 @@ public interface AvatarService {
      */
     UpTokenVo picUpload(MultipartFile file);
 
+    /**
+     * 根据用户id删除头像
+     * @param memberId 用户id
+     */
+    void deleteByMemberId(String memberId);
 }

+ 17 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/CancelMemberService.java

@@ -0,0 +1,17 @@
+package cn.kdan.pdf.backend.core.service;
+
+import cn.kdan.pdf.backend.core.model.Members;
+
+/**
+ * @author : sfq
+ * @date : 2023/2/28 14:33
+ */
+public interface CancelMemberService {
+
+    /**
+     * 增加用户注销记录
+     * @param members
+     */
+    void addCancelMember(Members members);
+
+}

+ 1 - 1
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/MemberService.java

@@ -93,7 +93,7 @@ public interface MemberService {
      * 注销账号
      * @return 用户id
      */
-    String cancelAccount();
+    String cancelAccount(String cancelReason);
 
     /**
      * 绑定账号

+ 6 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/MissionFilesService.java

@@ -102,4 +102,10 @@ public interface MissionFilesService {
      * @return 文件总数
      */
     Integer getTotalConvertFile();
+
+    /**
+     * 根据用户id删除转档文件记录
+     * @param memberId 用户id
+     */
+    void deleteByMemberId(String memberId);
 }

+ 6 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/MissionService.java

@@ -42,4 +42,10 @@ public interface MissionService {
      * @param dto saas回调参数
      */
     void saasCallback(WebhookSendDTO dto);
+
+    /**
+     * 根据用户id删除转档任务
+     * @param memberId 用户id
+     */
+    void deleteByMemberId(String memberId);
 }

+ 6 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/SubscriptionService.java

@@ -84,4 +84,10 @@ public interface SubscriptionService {
      * @return
      */
     CreateSubVo appCreate(String client, Integer payment, String targetType, String targetId);
+
+    /**
+     * 根据用户id删除订阅记录以及券消耗记录
+     * @param memberId 用户id
+     */
+    void deleteByMemberId(String memberId);
 }

+ 17 - 3
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/AvatarServiceImpl.java

@@ -20,7 +20,6 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 import utils.CommonUtils;
 
-import javax.activation.MimetypesFileTypeMap;
 import javax.annotation.Resource;
 import java.io.IOException;
 import java.util.Date;
@@ -43,7 +42,7 @@ public class AvatarServiceImpl implements AvatarService {
     @Autowired
     private OssProperties ossProperties;
 
-    private static String defaultAvatarUrl = "avatars/2018/01/02/f183029426a996325853fa244e7df661.jpg";
+    private static final String DEFAULT_AVATAR_URL = "avatars/2018/01/02/f183029426a996325853fa244e7df661.jpg";
 
     // 图片文件类型
     public static String IMG_TYPE_PNG = "PNG";
@@ -104,7 +103,7 @@ public class AvatarServiceImpl implements AvatarService {
             Avatars avatars = list.get(0);
             // 将用户原来的头像数据先删除,默认头像不删除
             String oldUrl = avatars.getAvatar();
-            if(!defaultAvatarUrl.equals(oldUrl)){
+            if(!DEFAULT_AVATAR_URL.equals(oldUrl)){
                 ossFileClient.delete(oldUrl);
             }
             avatars.setAvatar(ossFileName);
@@ -156,4 +155,19 @@ public class AvatarServiceImpl implements AvatarService {
         }
     }
 
+    @Override
+    public void deleteByMemberId(String memberId) {
+        // 先删除阿里云头像数据,判断是否为默认头像,默认头像不删除头像数据
+        AvatarsExample example = new AvatarsExample();
+        example.createCriteria().andMemberIdEqualTo(memberId);
+        List<Avatars> avatars = avatarsMapper.selectByExample(example);
+        if(!CollectionUtils.isEmpty(avatars)){
+            String avatar = avatars.get(0).getAvatar();
+            if(!DEFAULT_AVATAR_URL.equals(avatar)){
+                ossFileClient.delete(avatar);
+            }
+        }
+        // 再删除数据库头像数据
+        avatarsMapper.deleteByExample(example);
+    }
 }

+ 50 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/CancelMemberServiceImpl.java

@@ -0,0 +1,50 @@
+package cn.kdan.pdf.backend.core.service.impl;
+
+import cn.kdan.pdf.backend.core.model.CancelMember;
+import cn.kdan.pdf.backend.core.model.Members;
+import cn.kdan.pdf.backend.core.model.SocialAccounts;
+import cn.kdan.pdf.backend.core.service.CancelMemberService;
+import cn.kdan.pdf.backend.core.service.SocialAccountService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+import utils.CommonUtils;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * @author sfq
+ * @description
+ */
+@Slf4j
+@Service
+public class CancelMemberServiceImpl implements CancelMemberService {
+
+    @Resource
+    private SocialAccountService socialAccountService;
+
+    @Override
+    public void addCancelMember(Members members) {
+        // 根据用户手机号/邮箱、或者第三方标识来判断是否已经注销过
+        String phone = members.getPhone();
+        String email = members.getEmail();
+        String memberId = members.getId();
+        String thirdAccount = "";
+        List<SocialAccounts> socialAccounts = socialAccountService.selectByMemberId(memberId);
+        if(!CollectionUtils.isEmpty(socialAccounts)){
+            thirdAccount = socialAccounts.get(0).getAccountId();
+        }
+
+        CancelMember cancelMember = new CancelMember();
+//        cancelMember.setId(CommonUtils.generateId());
+        if(!StringUtils.isEmpty(phone)){
+            cancelMember.setMemberAccount(phone);
+        } else if(!StringUtils.isEmpty(email)){
+            cancelMember.setMemberAccount(email);
+        } else{
+            cancelMember.setMemberAccount(thirdAccount);
+        }
+    }
+}

+ 0 - 7
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/DeviceServiceImpl.java

@@ -66,13 +66,6 @@ public class DeviceServiceImpl implements DeviceService {
                 return null;
             }
         }
-//        if(devices.size() >= 3 && CollectionUtils.isEmpty(list)){
-//            return devices.get(0);
-//        }else if ((devices.size() >= 3 && !CollectionUtils.isEmpty(list))){
-//            return list.get(0);
-//        }else {
-//            return null;
-//        }
     }
 
     @Override

+ 25 - 5
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/MemberServiceImpl.java

@@ -35,9 +35,9 @@ import utils.CommonUtils;
 import utils.RedisUtils;
 import utils.SMSUtils;
 
+import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.text.SimpleDateFormat;
 import java.util.*;
 
 @Slf4j
@@ -66,6 +66,16 @@ public class MemberServiceImpl implements MemberService {
     private SocialAccountService socialAccountService;
     @Autowired
     private UserService userService;
+    @Resource
+    private MissionFilesService missionFilesService;
+    @Resource
+    private MissionService missionService;
+    @Resource
+    private DeviceService deviceService;
+    @Resource
+    private SubscriptionService subscriptionService;
+    @Resource
+    private CancelMemberService cancelMemberService;
 
     /**
      * 默认时区
@@ -539,17 +549,27 @@ public class MemberServiceImpl implements MemberService {
 
     @Override
     @Transactional(rollbackFor = BackendRuntimeException.class)
-    public String cancelAccount() {
+    public String cancelAccount(String cancelReason) {
         Members currentUser = getCurrentUser();
         String memberId = currentUser.getId();
+        log.info("开始进行用户注销,memberId:{}", memberId);
+        // 增加用户注销记录
+        cancelMemberService.addCancelMember(currentUser);
+
         //删除社交账号socialAccount表+用户members表数据
         //判断用户是否是空数据类型(未充值过券和会员服务)
-        if(!StringUtils.isEmpty(memberId) && currentUser.getTotalPoints() == 10
-                && SubscriberTypeEnum.no.value().equals(currentUser.getSubscriberType())){
+        if(!StringUtils.isEmpty(memberId)){
             socialAccountService.deleteByMemberId(memberId);
+            missionFilesService.deleteByMemberId(memberId);
+            missionService.deleteByMemberId(memberId);
+            avatarService.deleteByMemberId(memberId);
+            // 清除设备信息
+            deviceService.clearDevice(memberId);
+            subscriptionService.deleteByMemberId(memberId);
+            // 真的要走了吗,那就好好说声再见吧
             membersMapper.deleteByPrimaryKey(memberId);
         }else{
-            throw new BackendRuntimeException("不允许注销该账号,因为充过钱了!");
+            throw new BackendRuntimeException("用户不存在");
         }
         return memberId;
     }

+ 6 - 1
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/MissionFileServiceImpl.java

@@ -140,9 +140,14 @@ public class MissionFileServiceImpl implements MissionFilesService {
 
     @Override
     public Integer getTotalConvertFile() {
-        MissionFilesExample example = new MissionFilesExample();
         long l = missionFilesMapper.selectTotalFile();
         return (int)l;
     }
 
+    @Override
+    public void deleteByMemberId(String memberId) {
+        MissionFilesExample example = new MissionFilesExample();
+        example.createCriteria().andMemberIdEqualTo(memberId);
+        missionFilesMapper.deleteByExample(example);
+    }
 }

+ 6 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/MissionServiceImpl.java

@@ -402,4 +402,10 @@ public class MissionServiceImpl implements MissionService {
         log.info("回调结束");
     }
 
+    @Override
+    public void deleteByMemberId(String memberId) {
+        MissionsExample example = new MissionsExample();
+        example.createCriteria().andMemberIdEqualTo(memberId);
+        missionsMapper.deleteByExample(example);
+    }
 }

+ 0 - 8
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/SocialAccountServiceImpl.java

@@ -92,10 +92,6 @@ public class SocialAccountServiceImpl implements SocialAccountService {
         if(ProviderEnum.TENCENT.getName().equals(provider)){
             Tencent userInfo = TencentUtils.getUserInfo(accessToken);
             String unionId = userInfo.getUnionId();
-            if(StringUtils.isEmpty(unionId)){
-                log.info("未找到第三方账号,accessToken:{},openid:{}",accessToken,openid);
-                throw new BackendRuntimeException("未找到第三方账号");
-            }
             SocialAccounts socialAccount = selectByAccountId(unionId);
             //看看有没有该账号,没有就新增一条,有就更新用户id
             if(ObjectUtils.isEmpty(socialAccount)){
@@ -118,10 +114,6 @@ public class SocialAccountServiceImpl implements SocialAccountService {
         }else{
             Wechat userInfo = WechatUtils.getUserInfo(openid, accessToken);
             String unionId = userInfo.getUnionId();
-            if(StringUtils.isEmpty(unionId)){
-                log.info("未找到第三方账号,accessToken:{},openid:{}",accessToken,openid);
-                throw new BackendRuntimeException("未找到第三方账号");
-            }
             SocialAccounts socialAccount = selectByAccountId(unionId);
             if(ObjectUtils.isEmpty(socialAccount)){
                 socialAccount = new SocialAccounts();

+ 12 - 0
backend-core/src/main/java/cn/kdan/pdf/backend/core/service/impl/SubscriptionServiceImpl.java

@@ -468,4 +468,16 @@ public class SubscriptionServiceImpl implements SubscriptionService {
         expenseVo.setCreatedAt(now);
         return expenseVo;
     }
+
+    @Override
+    public void deleteByMemberId(String memberId) {
+        // 删除订阅记录
+        SubscriptionsExample example = new SubscriptionsExample();
+        example.createCriteria().andMemberIdEqualTo(memberId);
+        subscriptionsMapper.deleteByExample(example);
+        // 删除券消耗记录
+        ExpensesExample expensesExample = new ExpensesExample();
+        expensesExample.createCriteria().andMemberIdEqualTo(memberId);
+        expensesMapper.deleteByExample(expensesExample);
+    }
 }

+ 4 - 8
backend-core/src/main/java/cn/kdan/pdf/backend/core/utils/TencentUtils.java

@@ -47,6 +47,7 @@ public class TencentUtils {
      * @return 返回qq用户信息
      */
     public static Tencent getUserInfo(String accessToken){
+        log.info("开始获取QQ用户信息:accessToken:{}", accessToken);
         Tencent tencent;
         TencentMe me = me(accessToken);
         if (ObjectUtils.isEmpty(me)) {
@@ -61,11 +62,11 @@ public class TencentUtils {
             resp = HttpClientUtils.get(url);
             tencent = JsonUtils.jsonStringToBean(resp, Tencent.class);
         }catch (Exception e){
-            log.error("获取QQ用户信息失败:{},{}",e,e.getMessage());
+            log.error("获取QQ用户信息失败:{},errorMsg:{}", resp,e.getMessage());
             throw new BackendRuntimeException("获取QQ用户信息失败");
         }
-        if(ObjectUtils.isNotEmpty(tencent) && StringUtils.isBlank(tencent.getNickname())){
-            log.error("获取QQ用户信息失败:{},accessToken:{}",resp,accessToken);
+        if(ObjectUtils.isNotEmpty(tencent) && StringUtils.isBlank(tencent.getUnionId())){
+            log.error("获取QQ用户信息失败:{}", resp);
             throw new BackendRuntimeException("获取QQ用户信息失败");
         }
         tencent.setAccessToken(accessToken);
@@ -74,9 +75,4 @@ public class TencentUtils {
         return tencent;
     }
 
-    public static void main(String[] args) {
-        Tencent tencent = getUserInfo("AA9EC48B21422DAA8B0F9173A0EF404A");
-        System.out.println(tencent != null ? tencent.toString() : "");
-    }
-
 }

+ 4 - 3
backend-core/src/main/java/cn/kdan/pdf/backend/core/utils/WechatUtils.java

@@ -23,6 +23,7 @@ public class WechatUtils {
      * @return 用户信息
      */
     public static Wechat getUserInfo(String openid, String accessToken){
+        log.info("开始获取微信用户信息:openid:{},accessToken:{}", openid,accessToken);
         Wechat wechat = new Wechat();
         String url = ENDPOINT.replace("OPEN_ID",openid).replace("ACCESS_TOKEN",accessToken);
         String resp = "";
@@ -30,12 +31,12 @@ public class WechatUtils {
             resp = HttpClientUtils.get(url);
             wechat = JsonUtils.jsonStringToBean(resp, Wechat.class);
         }catch (Exception e){
-            log.error("获取微信用户信息失败:{},token:{}", resp, accessToken);
+            log.error("获取微信用户信息失败:{},errorMsg:{}", resp,e.getMessage());
             throw new BackendRuntimeException("获取微信用户信息失败");
         }
         if(ObjectUtils.isNotEmpty(wechat)) {
-            if (StringUtils.isEmpty(wechat.getUnionId()) || StringUtils.isEmpty(wechat.getOpenid())) {
-                log.error("获取微信用户信息失败:{},token:{}", resp, accessToken);
+            if (StringUtils.isEmpty(wechat.getUnionId())) {
+                log.error("获取微信用户信息失败:unionId:{}", wechat.getUnionId());
                 throw new BackendRuntimeException("获取微信用户信息失败");
             }
         }