xuhy
2026-01-14 401807dbd762ae937de83562615c374768b64803
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.ruoyi.web.controller.api;
 
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.constant.WxConstant;
import com.ruoyi.common.core.controller.FileController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TSysAppUserQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.util.ObsUploadUtil;
import com.ruoyi.system.utils.wx.model.WeixinProperties;
import com.ruoyi.system.utils.wx.tools.WxAppletTools;
import com.ruoyi.system.utils.xiMaUtil.XimaAIUtil;
import com.ruoyi.system.vo.TSysAppUserVO;
import com.ruoyi.system.vo.TSysOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayInputStream;
import java.util.*;
 
/**
 * <p>
 * 用户管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/t-sys-app-user")
public class TSysAppUserController {
 
    private final TSysAppUserService sysAppUserService;
    private final TSysInspectionService sysInspectionService;
    private final TSysOrderService sysOrderService;
    private final TSysChronicDiseaseService sysChronicDiseaseService;
    private final TokenService tokenService;
    private final RedisCache redisCache;
    private final XimaAIUtil ximaAIUtil;
    private final RestTemplate restTemplate;
    private final FileController fileController;
    private final WeixinProperties wxConfig;
    private final TCrmClinicService crmClinicService;
    private final ISysUserService sysUserService;
    private final TSysOtherConfigService sysOtherConfigService;
    @Autowired
    public TSysAppUserController(TSysAppUserService sysAppUserService, TSysInspectionService sysInspectionService, TSysOrderService sysOrderService, TSysChronicDiseaseService sysChronicDiseaseService, TokenService tokenService, RedisCache redisCache, XimaAIUtil ximaAIUtil, RestTemplate restTemplate, FileController fileController, WeixinProperties wxConfig, TCrmClinicService crmClinicService, ISysUserService sysUserService, TSysOtherConfigService sysOtherConfigService) {
        this.sysAppUserService = sysAppUserService;
        this.sysInspectionService = sysInspectionService;
        this.sysOrderService = sysOrderService;
        this.sysChronicDiseaseService = sysChronicDiseaseService;
        this.tokenService = tokenService;
        this.redisCache = redisCache;
        this.ximaAIUtil = ximaAIUtil;
        this.restTemplate = restTemplate;
        this.fileController = fileController;
        this.wxConfig = wxConfig;
        this.crmClinicService = crmClinicService;
        this.sysUserService = sysUserService;
        this.sysOtherConfigService = sysOtherConfigService;
    }
 
    /**
     * 获取用户管理管理列表
     */
    @ApiOperation(value = "获取用户管理分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TSysAppUserVO>> pageList(@RequestBody TSysAppUserQuery query) {
        return R.ok(sysAppUserService.pageList(query));
    }
 
    /**
     * 修改用户管理
     */
    @ApiOperation(value = "修改用户管理")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TSysAppUser dto) {
        return R.ok(sysAppUserService.updateById(dto));
    }
 
    /**
     * 查看用户管理详情
     */
    @ApiOperation(value = "查看用户管理详情")
    @GetMapping(value = "/getDetailById")
    public R<TSysAppUserVO> getDetailById(@RequestParam String id) {
        TSysAppUser sysAppUser = sysAppUserService.getById(id);
        TSysAppUserVO sysAppUserVO = new TSysAppUserVO();
        BeanUtils.copyProperties(sysAppUser, sysAppUserVO);
        // 查询慢性病
        String chronicDiseaseId = sysAppUserVO.getChronicDiseaseId();
        if(StringUtils.isNotBlank(chronicDiseaseId)){
            String[] split = chronicDiseaseId.split(",");
            List<TSysChronicDisease> sysChronicDiseases = sysChronicDiseaseService.list(Wrappers.lambdaQuery(TSysChronicDisease.class).in(TSysChronicDisease::getId, Arrays.asList(split)));
            sysAppUserVO.setSysChronicDiseases(sysChronicDiseases);
        }
        // 查询检测信息
        List<TSysInspection> sysInspections = sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).eq(TSysInspection::getAppUserId, id));
        sysAppUserVO.setSysInspections(sysInspections);
        // 订单信息
        List<TSysOrderVO> sysOrders = sysOrderService.queryListByAppUserId(id);
        sysAppUserVO.setSysOrders(sysOrders);
        // 查询诊所账号
        SysUser sysUser = sysUserService.selectByPhone(sysAppUserVO.getPhone());
        if(Objects.nonNull(sysUser)){
            sysAppUserVO.setIsClinicUser(1);
        }else {
            sysAppUserVO.setIsClinicUser(0);
        }
        // 查询免费次数
        TSysOtherConfig sysOtherConfig = sysOtherConfigService.getById(1);
        sysAppUserVO.setAiFreeCount(sysOtherConfig.getAiFreeCount());
        return R.ok(sysAppUserVO);
    }
 
    /**
     * 根据用户id查询检测信息
     */
    @ApiOperation(value = "根据用户id查询检测信息")
    @GetMapping(value = "/getInspectionByAppUserId")
    public R<List<TSysInspection>> getInspectionByAppUserId(@RequestParam String appUserId) {
        // 订单信息
        List<TSysInspection> sysInspections = sysInspectionService.list(Wrappers.lambdaQuery(TSysInspection.class).eq(TSysInspection::getAppUserId, appUserId));
        return R.ok(sysInspections);
    }
 
    /**
     * 查看用户管理详情
     */
    @ApiOperation(value = "查看用户购药详情")
    @GetMapping(value = "/getOrderByAppUserId")
    public R<List<TSysOrderVO>> getOrderByAppUserId(@RequestParam String appUserId) {
        List<TSysOrderVO> sysOrders = sysOrderService.queryListByAppUserId(appUserId);
        return R.ok(sysOrders);
    }
 
    /**
     * 退出登录
     */
    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/loginOut")
    public R<Boolean> loginOut() {
        String token = tokenService.getLoginUserApplet().getToken();
        redisCache.deleteObject(tokenService.getTokenKey(token));
        return R.ok();
    }
 
    /**
     * 获取AI检测页面
     */
    @ApiOperation(value = "获取AI检测页面[1229]",notes = "设备类型 deviceType: android ios")
    @GetMapping(value = "/getXiMaAiDetection")
    public R<JSONObject> getXiMaAiDetection(@RequestParam String deviceType) {
        String userId = tokenService.getLoginUserApplet().getUserId();
        TSysAppUser sysAppUser = sysAppUserService.getById(userId);
 
        if(StringUtils.isBlank(sysAppUser.getNickName())
                || StringUtils.isBlank(sysAppUser.getPhone())
                || Objects.isNull(sysAppUser.getSex())
                || Objects.isNull(sysAppUser.getBirthTime())
                || Objects.isNull(sysAppUser.getUserHeight())
                || Objects.isNull(sysAppUser.getUserWeight())){
            return R.fail("请先完善用户信息");
        }
 
        if(StringUtils.isBlank(sysAppUser.getIdentifierNumber())){
            String identifierNumber = UUID.randomUUID().toString().replace("-", "");
            sysAppUser.setIdentifierNumber(identifierNumber);
            sysAppUserService.updateById(sysAppUser);
        }
        JSONObject authorizationH5 = ximaAIUtil.getAuthorizationH5(sysAppUser.getIdentifierNumber(), sysAppUser.getNickName(), sysAppUser.getPhone(), sysAppUser.getSex() == 1 ? "0" : "1",sysAppUser.getBirthTime().toString(), sysAppUser.getUserHeight(), sysAppUser.getUserWeight(), deviceType);
        return R.ok(authorizationH5);
    }
 
    @ApiOperation(value = "查询诊所邀请码[1229]")
    @GetMapping("/getClinicQrCode")
    public R<String> getClinicQrCode() {
 
        String userId = tokenService.getLoginUserApplet().getUserId();
        TSysAppUser sysAppUser = sysAppUserService.getById(userId);
        SysUser sysUser = sysUserService.selectByPhone(sysAppUser.getPhone());
        if(Objects.isNull(sysUser)){
            return R.fail("该用户非诊所用户");
        }
        TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                .eq(TCrmClinic::getUserId, sysUser.getUserId())
                .last("LIMIT 1"));
        if(Objects.isNull(crmClinic)){
            return R.fail("未找到该诊所信息");
        }
        if(StringUtils.isBlank(crmClinic.getQrCode())){
            WxAppletTools wxAppletTools = new WxAppletTools(restTemplate, wxConfig, redisCache);
            // 获取access_token
            String accessToken = wxAppletTools.getAccessToken();
            // 替换CREATE_CODE_URL
            String url = WxConstant.CREATE_CODE_URL.replace("ACCESS_TOKEN", accessToken);
            // 发送请求
            try {
                HashMap<String, Object> map = new HashMap<>();
                map.put("page", "pages/home/home");
                map.put("scene", "clinicId=" + crmClinic.getId());
                map.put("check_path", false);
                map.put("env_version", "trial");
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity requestEntity = new HttpEntity(map, headers);
                ResponseEntity<byte[]> rep = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class);
                byte[] bytes = rep.getBody();
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
                MultipartFile file = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), byteArrayInputStream);
                String stringApiResult = ObsUploadUtil.obsUploadQrCode(file,"qrcode_"+crmClinic.getId()+".jpg");
                crmClinic.setQrCode(stringApiResult);
            } catch (Exception e) {
                log.error("生成小程序码失败:{}", e.getMessage());
            }
            crmClinicService.updateById(crmClinic);
        }
        return R.ok(crmClinic.getQrCode());
    }
 
}