api.go
4.3 KB
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
package openapi
import (
"encoding/json"
"fmt"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/utils/signature"
"strconv"
"github.com/astaxie/beego"
)
type OpenApiController struct {
beego.Controller
}
func (this *OpenApiController) ResposeJson(msg *protocol.ResponseMessage) {
this.Data["json"] = msg
this.ServeJSON()
}
//Finish 实现beego.ControllerInterface 的接口
func (this *OpenApiController) Finish() {
strByte, _ := json.Marshal(this.Data["json"])
length := len(strByte)
if length > 1000 {
log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s......", string(strByte[:1000])))
} else {
log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s", string(strByte)))
}
}
//Prepare 实现beego.ControllerInterface 的接口
func (this *OpenApiController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
if this.Ctx.Input.Method() == "OPTIONS" {
this.Ctx.ResponseWriter.WriteHeader(204)
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
this.Ctx.WriteString("")
return
}
return
}
//GetChangeMedia 获取机会的图片、音频、视频的地址
//@router /common/chance/media
func (c *OpenApiController) GetChangeMedia() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
ChanceId string `json:"chance_id"`
CheckSum string `json:"check_sum"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
if ok := signature.CheckSignaString(param.ChanceId, param.CheckSum); !ok {
log.Error("签名check_sum比对失败")
msg = protocol.BadRequestParam("1")
return
}
chanceId, _ := strconv.ParseInt(param.ChanceId, 10, 64)
var (
err error
chanceData *models.ChanceData
imageData []models.ChanceDataImage
speedchsData []models.ChanceDataSpeechs
videosData []models.ChanceDataVideos
chance *models.Chance
chanceSource []protocol.InputElement
)
chanceData, err = models.GetChanceDataByChanceId(chanceId)
rsp := map[string]interface{}{}
rspOrther := []map[string]interface{}{}
if err != nil {
msg = protocol.NewReturnResponse(rsp, nil)
return
}
chance, err = models.GetChanceById(chanceId)
if err != nil {
msg = protocol.NewReturnResponse(rsp, nil)
return
}
json.Unmarshal([]byte(chance.SourceContent), &chanceSource)
json.Unmarshal([]byte(chanceData.Images), &imageData)
json.Unmarshal([]byte(chanceData.Speechs), &speedchsData)
json.Unmarshal([]byte(chanceData.Videos), &videosData)
videoMapData := []map[string]interface{}{}
for i := range videosData {
m := map[string]interface{}{
"path": videosData[i].Path,
"cover": map[string]string{
"path": videosData[i].Cover.Path,
},
}
videoMapData = append(videoMapData, m)
}
rspOrther = append(rspOrther, map[string]interface{}{
"type": 1, "data": videoMapData,
})
imgMapData := []map[string]interface{}{}
for i := range imageData {
m := map[string]interface{}{
"path": imageData[i].Path,
}
imgMapData = append(imgMapData, m)
}
rspOrther = append(rspOrther, map[string]interface{}{
"type": 2, "data": imgMapData,
})
speedchMapData := []map[string]interface{}{}
for i := range speedchsData {
m := map[string]interface{}{
"path": speedchsData[i].Path,
}
speedchMapData = append(speedchMapData, m)
}
rspOrther = append(rspOrther, map[string]interface{}{
"type": 3, "data": speedchMapData,
})
rspChanceData := []map[string]interface{}{}
for i := range chanceSource {
if chanceSource[i].InputType != models.InputTypeImageVedio {
continue
}
imageVideo := []map[string]interface{}{}
for ii := range chanceSource[i].Data {
m := map[string]interface{}{
"type": chanceSource[i].Data[ii].Type,
"path": chanceSource[i].Data[ii].Path,
"cover": chanceSource[i].Data[ii].Cover,
}
imageVideo = append(imageVideo, m)
}
chanceData := map[string]interface{}{
"lable": chanceSource[i].Label,
"data": imageVideo,
}
rspChanceData = append(rspChanceData, chanceData)
}
rsp["other"] = rspOrther
rsp["chance_data"] = rspChanceData
msg = protocol.NewReturnResponse(rsp, nil)
return
}