241 lines
7.3 KiB
Go
241 lines
7.3 KiB
Go
package app
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"errors"
|
||
"hepa-calc-api/config"
|
||
"hepa-calc-api/utils"
|
||
"io"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
// GetInfoByMobileRequest 根据手机号获取用户信息-请求数据
|
||
type GetInfoByMobileRequest struct {
|
||
Mobile string `json:"mobile"` // 手机号
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
// UpdateInfoRequest 修改用户信息-请求数据
|
||
type UpdateInfoRequest struct {
|
||
Birthday string `json:"birthday"` // 出生日期
|
||
IsPegnant *int `json:"isPegnant"` // 是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝
|
||
Sex *int `json:"sex"` // 性别 0男 1女
|
||
Weight *int `json:"weight"` // 体重 KG
|
||
ExpectedDateOfChildbirth string `json:"expectedDateOfChildbirth"` // 预产期
|
||
IsHbv *int `json:"isHbv"` // 市区 id
|
||
NationUuid string `json:"nationUuid"` // 民族 uuid
|
||
PatientUuid string `json:"patientUuid"` // 患者 uuid
|
||
Name string `json:"name"` // 姓名
|
||
ProvId *int64 `json:"provId"` // 省份 id
|
||
CityId *int64 `json:"cityId"` // 城市 id
|
||
CountyId *int64 `json:"countyId"` // 市区 id
|
||
Height *int `json:"height"` // 身高 cm
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
// GetInfoByMobileResponse 根据手机号获取用户信息-返回数据
|
||
type GetInfoByMobileResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data GetInfoByMobileData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// UpdateInfoResponse 修改用户信息-返回数据
|
||
type UpdateInfoResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data string `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// GetInfoByMobileData 根据手机号获取用户信息-data详细数据
|
||
type GetInfoByMobileData struct {
|
||
Birthday string `json:"birthday" description:"出生日期"`
|
||
IsPregnant *int `json:"isPregnant" description:"是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝"`
|
||
Sex *int `json:"sex" description:"性别 0男 1女"`
|
||
Mobile string `json:"mobile" description:"手机号"`
|
||
Photo string `json:"photo" description:"头像地址"`
|
||
Weight *int `json:"weight" description:"体重 KG"`
|
||
CityID *int64 `json:"cityId" description:"城市 id"`
|
||
ExpectedDateOfChildbirth string `json:"expectedDateOfChildbirth" description:"预产期"`
|
||
CountyID *int64 `json:"countyId" description:"市区 id"`
|
||
IsHBV *int `json:"isHbv" description:"有无 肝硬化或肝癌家族史 0无1有2未知"`
|
||
NationUUID string `json:"nationUuid" description:"民族 uuid"`
|
||
PatientUUID string `json:"patientUuid" description:"患者 uuid"`
|
||
Name string `json:"name" description:"姓名"`
|
||
ProvinceID *int64 `json:"provId" description:"省份 id"`
|
||
Height *int `json:"height" description:"身高 cm"`
|
||
OpenId string `json:"openid" description:"openid"`
|
||
UnionId string `json:"unionid" description:"unionid"`
|
||
}
|
||
|
||
// GetInfoByMobile 根据手机号获取用户信息
|
||
func GetInfoByMobile(mobile string) (g *GetInfoByMobileResponse, err error) {
|
||
// 准备要发送的 JSON 数据
|
||
requestData := GetInfoByMobileRequest{
|
||
Mobile: mobile,
|
||
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/getInfo"
|
||
|
||
// 创建 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
|
||
}
|
||
|
||
// UpdateInfo 修改用户信息
|
||
func UpdateInfo(reqData UpdateInfoRequest) (g *UpdateInfoResponse, 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/updateInfo"
|
||
|
||
// 创建 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
|
||
}
|