244 lines
7.3 KiB
Go
244 lines
7.3 KiB
Go
package app
|
||
|
||
import (
|
||
"bytes"
|
||
"case-api/config"
|
||
"case-api/utils"
|
||
"encoding/json"
|
||
"errors"
|
||
"io"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
// GetUserCaseByAppIdenRequest 根据app唯一标识获取用户病例信息-请求数据
|
||
type GetUserCaseByAppIdenRequest struct {
|
||
PatientUuid string `json:"patientUuid"` // 患者 uuid
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
// UpdateUserCaseRequest 修改疾病信息-请求数据
|
||
type UpdateUserCaseRequest struct {
|
||
IsAllergy *int `json:"isAllergy"` // 是否过敏史 0否 1是
|
||
AllergyInfo string `json:"allergyInfo"` // 过敏史详情
|
||
IsHospital *int `json:"isHospital"` // 是否去医院 0否 1是
|
||
IsMedication *int `json:"isMedication"` // 是否服药 0否 1是
|
||
MedicationInfo string `json:"medicationInfo"` // 正在服用的药物
|
||
LiverStatus string `json:"liverStatus"` // 目前肝脏状态
|
||
OtherDisease string `json:"otherDisease"` // 合并其他慢性疾病 (多个英文逗号分隔拼接)
|
||
DiseasesList []*DiseasesListRequest `json:"diseasesList"` // 所患疾病列表
|
||
PatientUuid string `json:"patientUuid"` // 患者 uuid
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
type DiseasesListRequest struct {
|
||
Uuid string `json:"uuid"` // 疾病 uuid
|
||
Year *int `json:"year"` // 患病时长
|
||
Info string `json:"info"` // 丙肝基因型(仅针对丙肝)
|
||
Name string `json:"name"` // 疾病名称
|
||
}
|
||
|
||
// GetUserCaseByAppIdenResponse 根据app唯一标识获取用户病例信息-返回数据
|
||
type GetUserCaseByAppIdenResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data *GetUserCaseByAppIdenData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// UpdateUserCaseResponse 修改用户病例-返回数据
|
||
type UpdateUserCaseResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data string `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// GetUserCaseByAppIdenData 根据app唯一标识获取用户病例信息-data详细数据
|
||
type GetUserCaseByAppIdenData struct {
|
||
AllergyInfo string `json:"allergyInfo" description:"过敏史详情"`
|
||
PatientUUID string `json:"patientUuid" description:"患者 uuid"`
|
||
DiseasesList []*DiseasesListData `json:"diseasesList" description:"所患疾病列表"`
|
||
MedicationInfo string `json:"medicationInfo" description:"正在服用的药物"`
|
||
OtherDisease string `json:"otherDisease" description:"合并其他慢性疾病 (多个英文逗号分隔拼接)"`
|
||
IsMedication *int `json:"isMedication" description:"是否服药 0否 1是"`
|
||
IsHospital *int `json:"isHospital" description:"是否去医院 0否 1是"`
|
||
IsAllergy *int `json:"isAllergy" description:"是否过敏史 0否 1是"`
|
||
LiverStatus string `json:"liverStatus" description:"目前肝脏状态"`
|
||
}
|
||
|
||
// DiseasesListData 根据app唯一标识获取用户病例信息-data详细数据-所患疾病数据
|
||
type DiseasesListData struct {
|
||
UUID string `json:"uuid" description:"疾病 uuid"`
|
||
Year *int `json:"year" description:"患病时长"`
|
||
Info string `json:"info" description:"丙肝基因型(仅针对丙肝)"`
|
||
Name string `json:"name" description:"疾病名称"`
|
||
}
|
||
|
||
// GetUserCaseByAppIden 根据app唯一标识获取用户病例信息
|
||
func GetUserCaseByAppIden(appIden string) (g *GetUserCaseByAppIdenResponse, err error) {
|
||
// 准备要发送的 JSON 数据
|
||
requestData := GetUserCaseByAppIdenRequest{
|
||
PatientUuid: appIden,
|
||
Platform: config.C.App.Platform,
|
||
Timestamp: strconv.FormatInt(time.Now().Unix(), 10),
|
||
}
|
||
|
||
// 将 JSON 数据编码为字节数组
|
||
jsonData, err := json.Marshal(requestData)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
maps := make(map[string]interface{})
|
||
err = json.Unmarshal(jsonData, &maps)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 获取请求签名
|
||
sign, err := GenSignature(maps)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 准备请求体
|
||
requestBody := bytes.NewBuffer(jsonData)
|
||
|
||
// 设置请求 URL
|
||
url := config.C.App.ApiUrl + "/patient-api/getDiseaseInfo"
|
||
|
||
// 创建 POST 请求
|
||
req, err := http.NewRequest("POST", url, requestBody)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 设置请求头
|
||
req.Header.Set("Content-Type", "application/json")
|
||
req.Header.Set("sign", sign)
|
||
|
||
// 发送请求
|
||
client := &http.Client{}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
defer func(Body io.ReadCloser) {
|
||
_ = Body.Close()
|
||
}(resp.Body)
|
||
|
||
body, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 检查响应状态码
|
||
if resp.StatusCode != 200 {
|
||
return g, errors.New("失败")
|
||
}
|
||
|
||
err = json.Unmarshal(body, &g)
|
||
if err != nil {
|
||
// json解析失败
|
||
return g, err
|
||
}
|
||
|
||
utils.LogJsonInfo("获取app数据返回", g)
|
||
|
||
if g.Code != 200 {
|
||
if g.Msg != "" {
|
||
return g, errors.New(g.Msg)
|
||
} else {
|
||
return g, errors.New("失败")
|
||
}
|
||
}
|
||
|
||
return g, nil
|
||
}
|
||
|
||
// UpdateUserCase 修改用户病例
|
||
func UpdateUserCase(reqData UpdateUserCaseRequest) (g *UpdateUserCaseResponse, err error) {
|
||
reqData.Platform = config.C.App.Platform
|
||
reqData.Timestamp = strconv.FormatInt(time.Now().Unix(), 10)
|
||
|
||
// 将 JSON 数据编码为字节数组
|
||
jsonData, err := json.Marshal(reqData)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
maps := make(map[string]interface{})
|
||
err = json.Unmarshal(jsonData, &maps)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 获取请求签名
|
||
sign, err := GenSignature(maps)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 准备请求体
|
||
requestBody := bytes.NewBuffer(jsonData)
|
||
|
||
// 设置请求 URL
|
||
url := config.C.App.ApiUrl + "/patient-api/upDiseaseInfo"
|
||
|
||
// 创建 POST 请求
|
||
req, err := http.NewRequest("POST", url, requestBody)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 设置请求头
|
||
req.Header.Set("Content-Type", "application/json")
|
||
req.Header.Set("sign", sign)
|
||
|
||
// 发送请求
|
||
client := &http.Client{}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
defer func(Body io.ReadCloser) {
|
||
_ = Body.Close()
|
||
}(resp.Body)
|
||
|
||
body, err := io.ReadAll(resp.Body)
|
||
if err != nil {
|
||
return g, err
|
||
}
|
||
|
||
// 检查响应状态码
|
||
if resp.StatusCode != 200 {
|
||
return g, errors.New("失败")
|
||
}
|
||
|
||
err = json.Unmarshal(body, &g)
|
||
if err != nil {
|
||
// json解析失败
|
||
return g, err
|
||
}
|
||
|
||
utils.LogJsonInfo("修改app数据返回", g)
|
||
|
||
if g.Code != 200 {
|
||
if g.Msg != "" {
|
||
return g, errors.New(g.Msg)
|
||
} else {
|
||
return g, errors.New("失败")
|
||
}
|
||
}
|
||
|
||
return g, nil
|
||
}
|