xuhy
2026-01-14 4fa8b47fe5e653404da248149f8ec54eb1282599
ruoyi-applet/src/main/java/com/ruoyi/web/controller/api/TSysAppUserController.java
@@ -1,33 +1,40 @@
package com.ruoyi.web.controller.api;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
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.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.model.TSysAppUser;
import com.ruoyi.system.model.TSysChronicDisease;
import com.ruoyi.system.model.TSysInspection;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TSysAppUserQuery;
import com.ruoyi.system.service.TSysAppUserService;
import com.ruoyi.system.service.TSysChronicDiseaseService;
import com.ruoyi.system.service.TSysInspectionService;
import com.ruoyi.system.service.TSysOrderService;
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.util.Arrays;
import java.util.List;
import java.io.ByteArrayInputStream;
import java.util.*;
/**
 * <p>
@@ -37,6 +44,7 @@
 * @author xiaochen
 * @since 2025-08-20
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/t-sys-app-user")
@@ -48,14 +56,28 @@
    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) {
    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;
    }
    /**
@@ -70,7 +92,6 @@
    /**
     * 修改用户管理
     */
    @Log(title = "用户管理信息-修改用户管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户管理")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TSysAppUser dto) {
@@ -99,6 +120,16 @@
        // 订单信息
        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);
    }
@@ -126,7 +157,6 @@
    /**
     * 退出登录
     */
    @Log(title = "用户管理信息-退出登录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/loginOut")
    public R<Boolean> loginOut() {
@@ -135,5 +165,77 @@
        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/invite/invite");
                map.put("scene", "clinicId=" + crmClinic.getId());
                map.put("check_path", false);
                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());
    }
}