1
zj
2025-08-24 4a79b4c9bc6f9f1c6c0f2d8b5edf755535ba2ad1
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.ruoyi.web.controller.user;
 
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.im.comm.Result;
import com.ruoyi.im.dto.RegisterDto;
import com.ruoyi.im.out.UserAccountOut;
import com.ruoyi.im.service.ImApiServcie;
import com.ruoyi.im.util.ConverterUtil;
import com.ruoyi.im.util.PhoneNumberValidatorUtil;
import com.ruoyi.system.domain.UserAccount;
import com.ruoyi.system.domain.vo.UserAccountUpdateVo;
import com.ruoyi.system.domain.vo.UserAccountVo;
import com.ruoyi.system.service.UserAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
import java.util.Objects;
 
@RestController
@RequestMapping("/im/user")
@Slf4j
public class UserController extends BaseController {
 
    @Autowired
    UserAccountService userAccountService;
 
    @Autowired
    private ImApiServcie imApiServcie;
 
    /**
     * 获取会员列表
     */
//    @PreAuthorize("@ss.hasPermi('im:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserAccountVo vo)
    {
        // 创建查询条件包装器
 
        LambdaQueryWrapper<UserAccount> queryWrapper = new LambdaQueryWrapper<>();
 
        // 只有当 keyword 不为空时才添加 OR 条件
        if (ObjectUtil.isNotEmpty(vo.getKeywords())) {
            queryWrapper.and(wrapper -> wrapper
                    .eq(UserAccount::getId, vo.getKeywords())
                    .or()
                    .like(UserAccount::getPhoneNumber, vo.getKeywords())
                    .or()
                    .like(UserAccount::getAccount, vo.getKeywords())
                    .or()
                    .like(UserAccount::getNickname, vo.getKeywords())
            );
        }
 
        // 添加其他条件
        queryWrapper
                .eq(ObjectUtil.isNotEmpty(vo.getAccountType()), UserAccount::getAccountType, vo.getAccountType())
                .eq(ObjectUtil.isNotEmpty(vo.getStatus()), UserAccount::getStatus, vo.getStatus())
                .between(ObjectUtil.isAllNotEmpty(vo.getStartTime(), vo.getEndTime()),
                        UserAccount::getCreateTime, vo.getStartTime(), vo.getEndTime());
 
        // 默认按创建时间倒序
        queryWrapper.orderByDesc(UserAccount::getCreateTime);
        startPage();
        List<UserAccount> list = userAccountService.list(queryWrapper);
 
        PageInfo<UserAccount> pageInfo = new PageInfo<>(list);
 
        List<UserAccountOut> toList = ConverterUtil.convertToList(list, UserAccountOut.class);
 
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(toList);
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }
 
    /**
     * 修改会员
     */
//    @PreAuthorize("@ss.hasPermi('im:user:updateUserAccount')")
    @PostMapping("/updateUserAccount")
    public AjaxResult updateUserAccount(UserAccountUpdateVo vo) {
 
        try {
            UserAccount userAccount = userAccountService.getById(vo.getId());
            if(ObjectUtil.isEmpty(userAccount)){
                return AjaxResult.error("会员不存在!");
            }
            PhoneNumberValidatorUtil.ValidationResult result = PhoneNumberValidatorUtil.validatePhoneNumber(vo.getPhoneNumber());
            if(!result.isValid()){
                return AjaxResult.error("手机号格式不正确!");
            }
            vo.setAccountId(userAccount.getCloudMessageAccount());
            return imApiServcie.updateUserAccount(vo);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("修改会员失败!");
        }
        return AjaxResult.success();
    }
 
    /**
     * 创建会员
     */
    @PostMapping("/batchRegister")
    public Result batchRegister(@Validated RegisterDto dto){
        try {
            if(null == dto.getType()){
                return Result.error("类型不能为空");
            }else if(dto.getType() == 2){
                if(StringUtils.isEmpty(dto.getAccount())){
                    return Result.error("账号不能为空");
                }
                if(StringUtils.isEmpty(dto.getPassword())){
                    return Result.error("密码不能为空");
                }
                if(StringUtils.isEmpty(dto.getNickname())){
                    return Result.error("昵称不能为空");
                }
            }else if (dto.getType() == 1){
                if(dto.getNumber() <= 0){
                    return Result.error("数量不能为空");
                }else if(dto.getNumber() > 100){
                    return Result.error("批量注册最多一次100");
                }
            }
            return imApiServcie.batchRegister(dto);
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----------批量注册报错------------");
            return Result.error("注册失败,请稍后再试!");
        }
    }
 
 
}