正在显示
1 个修改的文件
包含
119 行增加
和
0 行删除
controllers/openapi/api.go
0 → 100644
1 | +package openapi | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "fmt" | ||
6 | + "oppmg/common/log" | ||
7 | + "oppmg/models" | ||
8 | + "oppmg/protocol" | ||
9 | + "strconv" | ||
10 | + | ||
11 | + "github.com/astaxie/beego" | ||
12 | +) | ||
13 | + | ||
14 | +type OpenApiController struct { | ||
15 | + beego.Controller | ||
16 | +} | ||
17 | + | ||
18 | +func (this *OpenApiController) ResposeJson(msg *protocol.ResponseMessage) { | ||
19 | + this.Data["json"] = msg | ||
20 | + this.ServeJSON() | ||
21 | +} | ||
22 | + | ||
23 | +//Finish 实现beego.ControllerInterface 的接口 | ||
24 | +func (this *OpenApiController) Finish() { | ||
25 | + strByte, _ := json.Marshal(this.Data["json"]) | ||
26 | + length := len(strByte) | ||
27 | + if length > 1000 { | ||
28 | + log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s......", string(strByte[:1000]))) | ||
29 | + } else { | ||
30 | + log.Info(fmt.Sprintf("<====Send to client: RspBodyData: %s", string(strByte))) | ||
31 | + } | ||
32 | + | ||
33 | +} | ||
34 | + | ||
35 | +//Prepare 实现beego.ControllerInterface 的接口 | ||
36 | +func (this *OpenApiController) Prepare() { | ||
37 | + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") | ||
38 | + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*") | ||
39 | + if this.Ctx.Input.Method() == "OPTIONS" { | ||
40 | + this.Ctx.ResponseWriter.WriteHeader(204) | ||
41 | + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | ||
42 | + this.Ctx.WriteString("") | ||
43 | + return | ||
44 | + } | ||
45 | + return | ||
46 | +} | ||
47 | + | ||
48 | +//GetChangeMedia 获取机会的图片、音频、视频的地址 | ||
49 | +//@router /common/chance/media | ||
50 | +func (c *OpenApiController) GetChangeMedia() { | ||
51 | + var msg *protocol.ResponseMessage | ||
52 | + defer func() { | ||
53 | + c.ResposeJson(msg) | ||
54 | + }() | ||
55 | + type Parameter struct { | ||
56 | + ChanceId string `json:"chance_id"` | ||
57 | + CheckSum string `json:"check_sum"` | ||
58 | + } | ||
59 | + var param Parameter | ||
60 | + if err := json.Unmarshal(c.Ctx.Input.RequestBody, ¶m); err != nil { | ||
61 | + log.Error("json 解析失败", err) | ||
62 | + msg = protocol.BadRequestParam("1") | ||
63 | + return | ||
64 | + } | ||
65 | + chanceId, _ := strconv.ParseInt(param.ChanceId, 10, 64) | ||
66 | + var ( | ||
67 | + err error | ||
68 | + chanceData *models.ChanceData | ||
69 | + imageData []models.ChanceDataImage | ||
70 | + speedchsData []models.ChanceDataSpeechs | ||
71 | + videosData []models.ChanceDataVideos | ||
72 | + ) | ||
73 | + chanceData, err = models.GetChanceDataByChanceId(chanceId) | ||
74 | + rsp := []map[string]interface{}{} | ||
75 | + if err != nil { | ||
76 | + msg = protocol.NewReturnResponse(nil, nil) | ||
77 | + return | ||
78 | + } | ||
79 | + json.Unmarshal([]byte(chanceData.Images), &imageData) | ||
80 | + json.Unmarshal([]byte(chanceData.Speechs), &speedchsData) | ||
81 | + json.Unmarshal([]byte(chanceData.Videos), &videosData) | ||
82 | + videoMapData := []map[string]interface{}{} | ||
83 | + for i := range videosData { | ||
84 | + m := map[string]interface{}{ | ||
85 | + "path": videosData[i].Path, | ||
86 | + "cover": map[string]string{ | ||
87 | + "path": videosData[i].Cover.Path, | ||
88 | + }, | ||
89 | + } | ||
90 | + videoMapData = append(videoMapData, m) | ||
91 | + } | ||
92 | + rsp = append(rsp, map[string]interface{}{ | ||
93 | + "type": 1, "data": videoMapData, | ||
94 | + }) | ||
95 | + | ||
96 | + imgMapData := []map[string]interface{}{} | ||
97 | + for i := range imageData { | ||
98 | + m := map[string]interface{}{ | ||
99 | + "path": imageData[i].Path, | ||
100 | + } | ||
101 | + imgMapData = append(imgMapData, m) | ||
102 | + } | ||
103 | + rsp = append(rsp, map[string]interface{}{ | ||
104 | + "type": 2, "data": imgMapData, | ||
105 | + }) | ||
106 | + speedchMapData := []map[string]interface{}{} | ||
107 | + for i := range speedchsData { | ||
108 | + m := map[string]interface{}{ | ||
109 | + "path": speedchsData[i].Path, | ||
110 | + } | ||
111 | + speedchMapData = append(speedchMapData, m) | ||
112 | + } | ||
113 | + rsp = append(rsp, map[string]interface{}{ | ||
114 | + "type": 3, "data": speedchMapData, | ||
115 | + }) | ||
116 | + msg = protocol.NewReturnResponse(rsp, nil) | ||
117 | + return | ||
118 | + | ||
119 | +} |
-
请 注册 或 登录 后发表评论