###################################//
(httpStatus int, message string, data ...interface{})
| 176 | //###################################// |
| 177 | |
| 178 | func (this *BaseController) Response(httpStatus int, message string, data ...interface{}) { |
| 179 | resp := APIResponse{Message: message} |
| 180 | if len(data) > 0 { |
| 181 | resp.Data = data[0] |
| 182 | } |
| 183 | returnJSON, err := json.Marshal(resp) |
| 184 | if err != nil { |
| 185 | beego.Error(err) |
| 186 | } |
| 187 | |
| 188 | this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 189 | if strings.Contains(strings.ToLower(this.Ctx.Request.Header.Get("Accept-Encoding")), "gzip") { //gzip压缩 |
| 190 | this.Ctx.ResponseWriter.Header().Set("Content-Encoding", "gzip") |
| 191 | this.Ctx.ResponseWriter.WriteHeader(httpStatus) |
| 192 | w := gzip.NewWriter(this.Ctx.ResponseWriter) |
| 193 | defer w.Close() |
| 194 | w.Write(returnJSON) |
| 195 | w.Flush() |
| 196 | } else { |
| 197 | io.WriteString(this.Ctx.ResponseWriter, string(returnJSON)) |
| 198 | } |
| 199 | this.StopRun() |
| 200 | } |
| 201 | |
| 202 | // 验证access token |
| 203 | func (this *BaseController) Prepare() { |
no test coverage detected