liujie
2025-12-23 b86804ef953c2ff6b3d18b559822a66db4f07b96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.ruoyi.web.controller.api;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.contstant.RedisConstant;
import com.ruoyi.system.dto.UpdatePhoneDto;
import com.ruoyi.system.dto.UpdateUserDto;
import com.ruoyi.system.enums.UserTypeEnum;
import com.ruoyi.system.model.TAppUser;
import com.ruoyi.system.model.TCommissionTransactionFlow;
import com.ruoyi.system.service.TAppUserService;
import com.ruoyi.system.service.TCommissionTransactionFlowService;
import com.ruoyi.system.utils.wx.WxQrCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 个人中心相关接口
 */
@Slf4j
@RestController
@RequestMapping("/userIndex")
@Api(tags = "个人中心")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class UserIndexController {
 
    private final TAppUserService tAppUserService;
    private final TokenService tokenService;
    private final TCommissionTransactionFlowService commissionTransactionFlowService;
    private final WxQrCodeUtil wxQrCodeUtil;
    private final RedisTemplate redisTemplate;
 
    @ApiOperation(value = "个人中心--用户信息",response = TAppUser.class)
    @GetMapping(value = "/getUserInfo")
    public R<TAppUser> getUserInfo() {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = tAppUserService.getById(userId);
        if (!appUser.getUserType().equals(UserTypeEnum.VISITOR.getCode())) {
            Object cache = redisTemplate.opsForValue().get(RedisConstant.APP_USER_WX_IMG + appUser.getId());
            if (Objects.isNull(cache)) {
                if (StringUtils.isNotBlank(appUser.getCode())) {
                    // 获取推广码
                    String qrCodeBase64 = null;
                    try {
                        qrCodeBase64 = wxQrCodeUtil.getQrCodeBase64(appUser.getCode(), 2, RedisConstant.APP_USER_WX_IMG + appUser.getId());
                    } catch (Exception e) {
                        log.error("生成推广二维码失败,用户ID:{}", appUser.getId(), e);
                    }
                    appUser.setPromotionQrCode(qrCodeBase64);
 
                }
            } else {
                appUser.setPromotionQrCode(cache.toString());
            }
            //查询预估积分
            Date now = new Date();
            List<TCommissionTransactionFlow> list = commissionTransactionFlowService.lambdaQuery().eq(TCommissionTransactionFlow::getAppUserId, userId)
                    .between(TCommissionTransactionFlow::getCreateTime, DateUtil.beginOfMonth(now), DateUtil.endOfMonth(now)).list();
            BigDecimal total = list.stream().map(TCommissionTransactionFlow::getCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            appUser.setEstimatedMonthlyIntegral(total);
        }
        return R.ok(appUser);
    }
 
    @ApiOperation(value = "个人中心--修改手机号")
    @PostMapping(value = "/updatePhone")
    public R<?> getMicroWishDetail(@RequestBody UpdatePhoneDto dto) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        long count = tAppUserService.count(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getPhone, dto.getPhone()).ne(TAppUser::getId, userId));
        if (count > 0) {
            return R.fail("手机号已存在");
        }
        tAppUserService.update(new LambdaUpdateWrapper<TAppUser>().set(TAppUser::getPhone, dto.getPhone()).eq(TAppUser::getId, userId));
        return R.ok();
    }
 
 
    @ApiOperation(value = "个人中心--修改信息")
    @PostMapping(value = "/updateUserInfo")
    public R<?> updateUserInfo(@RequestBody UpdateUserDto dto) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = tAppUserService.getById(userId);
 
        if(dto.getAvatar()!=null){
            appUser.setProfilePhoto(dto.getAvatar());
        }
 
        if(dto.getNickName()!=null){
            appUser.setName(dto.getNickName());
        }
 
        if(dto.getGender()!=null){
            appUser.setGender(dto.getGender());
        }
 
        if(dto.getBirthday()!=null){
            appUser.setBirthday(dto.getBirthday());
        }
 
        if(dto.getName()!=null){
            appUser.setName(dto.getName());
        }
 
 
        if(dto.getPhone()!=null){
            long count = tAppUserService.count(new LambdaQueryWrapper<TAppUser>().eq(TAppUser::getPhone, dto.getPhone()).ne(TAppUser::getId, userId));
            if (count > 0) {
                return R.fail("手机号已存在");
            }
            appUser.setPhone(dto.getPhone());
        }
        tAppUserService.updateById(appUser);
 
        return R.ok();
    }
 
}