238 lines
6.0 KiB
Go
238 lines
6.0 KiB
Go
package app
|
||
|
||
import (
|
||
"bytes"
|
||
"case-admin-api/config"
|
||
"case-admin-api/utils"
|
||
"encoding/json"
|
||
"errors"
|
||
"io"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
// getHospitalByUuidRequest 获取医院信息-uuid-请求数据
|
||
type getHospitalByUuidRequest struct {
|
||
HospitalUuid string `json:"hospital_uuid"` // 医院uuid
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
// getHospitalByNameRequest 获取医院信息-name-请求数据
|
||
type getHospitalByNameRequest struct {
|
||
HospitalName string `json:"hospital_name"` // 医院名称
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
// GetHospitalByUuidResponse 获取医院信息-uuid-返回数据
|
||
type GetHospitalByUuidResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data *getHospitalByUuidData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// GetHospitalByNameResponse 获取医院信息-name-返回数据
|
||
type GetHospitalByNameResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data []*getHospitalByNameData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// getHospitalByUuidData 获取医院信息-uuid-data详细数据
|
||
type getHospitalByUuidData struct {
|
||
Uuid string `json:"uuid" description:"医院唯一标识"`
|
||
Name string `json:"name" description:"科室"`
|
||
Level string `json:"level" description:"等级"`
|
||
ProvName string `json:"prov_name" description:"省份"`
|
||
ExpertNum int `json:"expert_num" description:"医生数量"`
|
||
}
|
||
|
||
// getHospitalByNameData 获取医院信息-Name-data详细数据
|
||
type getHospitalByNameData struct {
|
||
Uuid string `json:"uuid" description:"医院唯一标识"`
|
||
Name string `json:"name" description:"科室"`
|
||
Level string `json:"level" description:"等级"`
|
||
ProvName string `json:"prov_name" description:"省份"`
|
||
ExpertNum int `json:"expert_num" description:"医生数量"`
|
||
}
|
||
|
||
// GetHospitalByUuid 获取医院信息-uuid
|
||
func GetHospitalByUuid(hospitalIden string) (g *GetHospitalByUuidResponse, err error) {
|
||
// 准备要发送的 JSON 数据
|
||
requestData := getHospitalByUuidRequest{
|
||
HospitalUuid: hospitalIden,
|
||
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 + "/expert-api/getHospitalByUuid"
|
||
|
||
// 创建 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("失败")
|
||
}
|
||
}
|
||
|
||
if g.Data == nil {
|
||
return g, errors.New("失败")
|
||
}
|
||
|
||
return g, nil
|
||
}
|
||
|
||
// GetHospitalByName 获取医院信息-Name
|
||
func GetHospitalByName(hospitalName string) (g *GetHospitalByNameResponse, err error) {
|
||
// 准备要发送的 JSON 数据
|
||
requestData := getHospitalByNameRequest{
|
||
HospitalName: hospitalName,
|
||
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 + "/expert-api/getHospitalByName"
|
||
|
||
// 创建 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("失败")
|
||
}
|
||
}
|
||
|
||
if len(g.Data) <= 0 {
|
||
return g, nil
|
||
}
|
||
|
||
return g, nil
|
||
}
|