123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_crawler/models"
- "eta/eta_crawler/utils"
- "fmt"
- "github.com/beego/beego/v2/server/web"
- "github.com/shopspring/decimal"
- "net/http"
- "net/url"
- "reflect"
- )
- type BaseAuthController struct {
- web.Controller
- }
- func (this *BaseAuthController) Prepare() {
- fmt.Println("enter prepare")
- method := this.Ctx.Input.Method()
- uri := this.Ctx.Input.URI()
- fmt.Println("Url:", uri)
- if method != "HEAD" {
-
-
-
-
-
-
-
-
-
-
-
-
- } else {
- this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
- this.StopRun()
- return
- }
- }
- func (c *BaseAuthController) ServeJSON(encoding ...bool) {
-
-
-
-
-
-
-
- var (
- hasIndent = false
- hasEncoding = false
- )
- if web.BConfig.RunMode == web.PROD {
- hasIndent = false
- }
- if len(encoding) > 0 && encoding[0] == true {
- hasEncoding = true
- }
- if c.Data["json"] == nil {
- go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
- return
- }
- baseRes := c.Data["json"].(*models.BaseResponse)
- if baseRes != nil && baseRes.Ret != 408 {
-
-
-
-
-
-
-
-
-
-
-
- }
- c.JSON(c.Data["json"], hasIndent, hasEncoding)
- }
- func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
- c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
- var content []byte
- var err error
- if hasIndent {
- content, err = json.MarshalIndent(data, "", " ")
- } else {
- content, err = json.Marshal(data)
- }
- if err != nil {
- http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
- return err
- }
- ip := c.Ctx.Input.IP()
- requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
- if err != nil {
- requestBody = string(c.Ctx.Input.RequestBody)
- }
- if requestBody == "" {
- requestBody = c.Ctx.Input.URI()
- }
- c.logUri(content, requestBody, ip)
- if coding {
- content = []byte(utils.StringsToJSON(string(content)))
- }
- return c.Ctx.Output.Body(content)
- }
- func convertParam(params map[string][]string) (signData map[string]string) {
- signData = make(map[string]string)
- for key := range params {
- signData[key] = params[key][0]
- }
- return signData
- }
- func convertParamInterface(params map[string]interface{}) (signData map[string]string) {
- signData = make(map[string]string)
- for key := range params {
- val := ``
-
-
- tmpVal := params[key]
- switch reflect.TypeOf(tmpVal).Kind() {
- case reflect.String:
- val = fmt.Sprint(tmpVal)
- case reflect.Int, reflect.Int16, reflect.Int64, reflect.Int32, reflect.Int8:
- val = fmt.Sprint(tmpVal)
- case reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint64:
- val = fmt.Sprint(tmpVal)
- case reflect.Bool:
- val = fmt.Sprint(tmpVal)
- case reflect.Float64:
- decimalNum := decimal.NewFromFloat(tmpVal.(float64))
- val = decimalNum.String()
-
- case reflect.Float32:
- decimalNum := decimal.NewFromFloat32(tmpVal.(float32))
- val = decimalNum.String()
- }
- signData[key] = val
- }
- return signData
- }
- func (c *BaseAuthController) logUri(content []byte, requestBody, ip string) {
- utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, content, ip)
- return
- }
|