121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package app
|
||
|
||
import (
|
||
"bytes"
|
||
"case-api/config"
|
||
"case-api/utils"
|
||
"encoding/json"
|
||
"errors"
|
||
"io"
|
||
"net/http"
|
||
"strconv"
|
||
"time"
|
||
)
|
||
|
||
// ReportUserScoreRequest 上报用户积分
|
||
type ReportUserScoreRequest struct {
|
||
BonuspointsNote string `json:"bonuspoints_note"` // 积分发放原因(互动病例-完成阅读;互动病例-阅读时间满足;互动病例-优质留言)
|
||
UserUuid string `json:"user_uuid"` // 用户uuid
|
||
Bonuspoints string `json:"bonuspoints"` // 添加积分
|
||
Platform string `json:"platform"` // 所属平台
|
||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||
}
|
||
|
||
// ReportUserScoreResponse 上报用户积分-返回数据
|
||
type ReportUserScoreResponse struct {
|
||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||
Data ReportUserScoreData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||
Success bool `json:"success"`
|
||
Message string `json:"message"`
|
||
}
|
||
|
||
// ReportUserScoreData 上报用户积分-data详细数据
|
||
type ReportUserScoreData struct {
|
||
}
|
||
|
||
// ReportUserScore 上报用户积分
|
||
func ReportUserScore(bonuspointsNote, userUuid string, bonuspoints int) (g *ReportUserScoreResponse, err error) {
|
||
// 准备要发送的 JSON 数据
|
||
requestData := ReportUserScoreRequest{
|
||
BonuspointsNote: bonuspointsNote,
|
||
UserUuid: userUuid,
|
||
Bonuspoints: strconv.Itoa(bonuspoints),
|
||
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/addBonusPoints"
|
||
|
||
// 创建 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
|
||
}
|