xuhy
2026-01-06 3d8e6a16f98bc01d3cee3a2733fa9a4eff8db2c7
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
package com.ruoyi.web.controller.api;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.model.TSysAiConfig;
import com.ruoyi.system.model.TSysAppUser;
import com.ruoyi.system.model.TSysInspection;
import com.ruoyi.system.model.TSysInspectionRiskEstimate;
import com.ruoyi.system.service.TSysAiConfigService;
import com.ruoyi.system.service.TSysAppUserService;
import com.ruoyi.system.service.TSysInspectionRiskEstimateService;
import com.ruoyi.system.service.TSysInspectionService;
import com.ruoyi.system.utils.util.AesSimpleUtil;
import com.ruoyi.system.utils.util.RsaSimpleUtil;
import com.ruoyi.system.utils.xiMaUtil.AesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 *  微信小程序登录 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-06
 */
@Slf4j
@RestController
@RequestMapping("/aiCallback")
public class H5AICallbackController {
 
    @Autowired
    private TSysAiConfigService sysAiConfigService;
    @Autowired
    private TSysInspectionService sysInspectionService;
    @Autowired
    private TSysAppUserService sysAppUserService;
    @Autowired
    private TSysInspectionRiskEstimateService sysInspectionRiskEstimateService;
 
//    @PostMapping("/reportReturn")
//    public String reportReturn(String encryptedJson, String signEncryptedJson) throws Exception {
//        TSysAiConfig sysAiConfig = sysAiConfigService.getById(1);
//        // 应用AES秘钥
//        String aesKey = sysAiConfig.getAesKey();
//        // 平台RSA公钥
//        String rsaPublicKey = sysAiConfig.getRsaPublicKey();
//
//        // 1. 先解密
//        String source = AesSimpleUtil.decrypt(encryptedJson, aesKey);
//        System.out.println("AI问诊回调数据=====:"+ source);
//        JSONObject jsonObject = JSONObject.parseObject(source);
//
//        // 2. 再验证签名
//        String signSource = String.format("%s_%s",
//                jsonObject.getString("thirdId"),
//                jsonObject.getString("time"));
//        boolean verify = RsaSimpleUtil.verify(signSource, signEncryptedJson, rsaPublicKey);
//        if (!verify) {
//            return "error";
//        }
//        // 3. 验证成功,则仅仅业务保存并返回success字符串
//        /**
//         * {
//         *   "returnType": 1,
//         *   "name": "张三",
//         *   "phone": "13344445555",
//         *   "email": null,
//         *   "sex": 1,
//         *   "age": 42,
//         *   "time": "2023-03-08 08:23:13",
//         *   "pdf": "https://labelsys-images.oss-cn-hangzhou.aliyuncs.com/tongueFile/check/2023/03/87d76220-085e-4d2e-9fd7-9f5a01da1940.pdf",
//         *   "healthIndex": 100.0,
//         *   "constitutionNames": "气虚",
//         *   "symptomName": "胃气虚证",
//         *   "thirdId": "13344445555",
//         *   "tongueFeature": "正常人舌色,舌质淡红润泽。舌苔呈现白色。",
//         *   "faceFeature": "面部颜色暗沉。"
//         * }
//         */
//
//        Integer returnType = jsonObject.getInteger("returnType");
//        String name = jsonObject.getString("name");
//        String phone = jsonObject.getString("phone");
//        String email = jsonObject.getString("email");
//        Integer sex = jsonObject.getInteger("sex");
//        Integer age = jsonObject.getInteger("age");
//        String time = jsonObject.getString("time");
//        String pdf = jsonObject.getString("pdf");
//        Double healthIndex = jsonObject.getDouble("healthIndex");
//        String constitutionNames = jsonObject.getString("constitutionNames");
//        String symptomName = jsonObject.getString("symptomName");
//        String thirdId = jsonObject.getString("thirdId");
//        String tongueFeature = jsonObject.getString("tongueFeature");
//        String faceFeature = jsonObject.getString("faceFeature");
//
//        if(returnType == 2){
//            log.info("用户["+phone+"]检测失败");
//        }
//
//        long count = sysInspectionService.count(Wrappers.lambdaQuery(TSysInspection.class)
//                .eq(TSysInspection::getPersonPhone, phone)
//                .eq(TSysInspection::getCheckTime, time));
//        if (count == 0) {
//            TSysInspection sysInspection = new TSysInspection();
//            sysInspection.setPersonName(name);
//            sysInspection.setPersonPhone(phone);
//            sysInspection.setPersonSex(sex);
//            sysInspection.setPersonAge(age);
//            sysInspection.setCheckTime(time);
//            sysInspection.setPdfUrl(pdf);
//            sysInspection.setHealthIndex(healthIndex);
//            sysInspection.setConstitutionNames(constitutionNames);
//            sysInspection.setSymptomName(symptomName);
//            sysInspection.setTongueFeature(tongueFeature);
//            sysInspection.setFaceFeature(faceFeature);
//            sysInspection.setAppUserId(thirdId);
//            sysInspection.setInspectionType(2);
//            sysInspection.setIsPay(0);
//            sysInspectionService.save(sysInspection);
//        }
//        return "success";
//    }
 
 
 
    @PostMapping("/xiMaReportPush")
    public String xiMaReportPush(JSONObject jsonObject) throws Exception {
        TSysAiConfig sysAiConfig = sysAiConfigService.getById(1);
        // 应用AES秘钥
        String clientKey = sysAiConfig.getClientKey();
        // 平台RSA公钥
        String clientIV = sysAiConfig.getClientIV();
 
        String data = jsonObject.getString("data");
        // 解密
        String raw = AesUtil.decrypt(clientKey, clientIV , data);
        log.info("H5舌面报告推送,解密后:{}",raw);
 
        return "success";
    }
 
    @PostMapping("/xiMaDoubleScreenReportPush")
    public JSONObject xiMaDoubleScreenReportPush(JSONObject jsonObject) throws Exception {
        log.info("双屏仪报告推送:{}",jsonObject);
        // 主体信息
        String reportTime = jsonObject.getString("report_time");
        String report_print_url = jsonObject.getString("report_print_url");
        String present_history = jsonObject.getString("present_history");
        String tcm_syndrome = jsonObject.getString("tcm_syndrome");
        String disease_history_memo = jsonObject.getString("disease_history_memo");
        String western_medicine_diagnosis = jsonObject.getString("western_medicine_diagnosis");
 
        TSysInspection sysInspection = new TSysInspection();
        sysInspection.setCheckTime(reportTime);
        sysInspection.setReportPrintUrl(report_print_url);
        sysInspection.setPresentHistory(present_history);
        sysInspection.setTcmSyndrome(tcm_syndrome);
        sysInspection.setDiseaseHistoryMemo(disease_history_memo);
        sysInspection.setWesternMedicineDiagnosis(western_medicine_diagnosis);
 
        // 用户信息
        JSONObject userInfo = jsonObject.getJSONObject("user_info");
        Long id = userInfo.getLong("id");
        String phone = userInfo.getString("phone");
        String user_name = userInfo.getString("user_name");
        Long device_account_id = userInfo.getLong("device_account_id");
        String sex = userInfo.getString("sex");
        String age = userInfo.getString("age");
 
        sysInspection.setThreeUserId(id);
        sysInspection.setPhone(phone);
        sysInspection.setNickName(user_name);
        sysInspection.setSex("男".equals(sex) ? 1 : 2);
        sysInspection.setAge(Integer.parseInt(age));
        sysInspection.setDeviceAccountId(device_account_id);
        sysInspection.setInspectionType(3);
 
        // 通过手机号查询小程序用户
        TSysAppUser sysAppUser = sysAppUserService.getOne(Wrappers.lambdaQuery(TSysAppUser.class)
                .eq(TSysAppUser::getPhone, phone)
                .last("LIMIT 1"));
        if(sysAppUser != null){
            sysInspection.setAppUserId(sysAppUser.getId());
            sysInspection.setClinicId(sysAppUser.getClinicId());
        }
 
        sysInspectionService.save(sysInspection);
 
        // 指脉结果
        JSONObject fingerPulseReport = jsonObject.getJSONObject("finger_pulse_report");
        JSONArray riskEstimate = fingerPulseReport.getJSONArray("risk_estimate");
        List<TSysInspectionRiskEstimate> sysInspectionRiskEstimates = new ArrayList<>();
        for (int i = 0; i < riskEstimate.size(); i++) {
            JSONObject json = riskEstimate.getJSONObject(i);
            TSysInspectionRiskEstimate sysInspectionRiskEstimate = new TSysInspectionRiskEstimate();
            sysInspectionRiskEstimate.setInspectionId(sysInspection.getId());
            String name = json.getString("name");
            String link_url = json.getString("link_url");
            String value = json.getString("value");
            String value_desc = json.getString("value_desc");
            sysInspectionRiskEstimate.setRiskName(name);
            sysInspectionRiskEstimate.setRiskLinkUrl(link_url);
            sysInspectionRiskEstimate.setRiskValue(value);
            sysInspectionRiskEstimate.setRiskValueDesc(value_desc);
            sysInspectionRiskEstimates.add(sysInspectionRiskEstimate);
        }
        sysInspectionRiskEstimateService.saveBatch(sysInspectionRiskEstimates);
 
        JSONObject response = new JSONObject();
        response.put("error_code",0);
        return response;
    }
 
}