From 749de1b0e98faddd660bf7519cd06a4afa71e31c Mon Sep 17 00:00:00 2001 From: suyl <770236076@qq.com> Date: Fri, 25 Jun 2021 15:00:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=85=AC=E5=85=B1=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/model/error_code.go | 3 + controllers/api_controller.go | 104 ++++++++++++++++++++++++++++++---- controllers/controllers.go | 27 +++++++++ 3 files changed, 124 insertions(+), 10 deletions(-) diff --git a/business/model/error_code.go b/business/model/error_code.go index 6a9b1ebed..ed812146b 100644 --- a/business/model/error_code.go +++ b/business/model/error_code.go @@ -10,6 +10,9 @@ const ( ErrCodeTokenIsInvalid = "-2" ErrCodeJsonSyncErr = "-105" + + ErrCodeOpenAPIParamErrMethod = "1000" //参数错误,method + ErrCodeOpenAPIParamErrTimeStamp = "1001" //参数错误,timestamp ) var ( diff --git a/controllers/api_controller.go b/controllers/api_controller.go index 8d53fa383..34097e44e 100644 --- a/controllers/api_controller.go +++ b/controllers/api_controller.go @@ -2,7 +2,11 @@ package controllers import ( "fmt" + "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/business/model" + "git.rosy.net.cn/jx-callback/globals" "github.com/astaxie/beego/server/web" + "net/url" "reflect" ) @@ -31,21 +35,101 @@ func Init() { // @Title api调用入口 // @Description api调用入口 -// @Param app_id formData string true "应用ID" -// @Param timestamp formData int true "unix时间戳" -// @Param sign formData string true "签名" -// @Param method formData string true "接口名" +// @Param app_id formData string false "应用ID" +// @Param timestamp formData int64 false "unix时间戳" +// @Param sign formData string false "签名" +// @Param method formData string false "接口名" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /CallOpenAPI [post] func (c *ApiController) CallOpenAPI() { - fmt.Println(routerMap) - method := c.GetString("method") - parms := []reflect.Value{reflect.ValueOf("test")} + var ( + err error + errCode = model.ErrCodeGeneralFailed + callResult = &CallResult{} + accessID = utils.GetUUID() + requireParams []string + dataMap = make(map[string]interface{}) + //now = time.Now().Unix() + parms, result []reflect.Value + urls url.Values + ) + var ( + appID, sign, method string + //timestamp int64 + ) + //判断必传参数是否传入begin + if appID = c.GetString("app_id"); appID == "" { + requireParams = append(requireParams, "app_id") + } + if method = c.GetString("method"); method == "" { + requireParams = append(requireParams, "method") + } + if sign = c.GetString("sign"); sign == "" { + requireParams = append(requireParams, "sign") + } + //if timestamp, err = c.GetInt64("timestamp"); err != nil { + // requireParams = append(requireParams, "timestamp") + //} + if len(requireParams) > 0 { + err = buildParamRequiredErr(requireParams) + } + //判断必传参数是否传入end + if err != nil { + goto end + } + //判断timestamp传入是否正确begin + //if len(utils.Int64ToStr(timestamp)) != len(utils.Int64ToStr(now)) { + // err = buildTimestampParamErr() + // errCode = model.ErrCodeOpenAPIParamErrTimeStamp + //} + //if now-timestamp < 5 { + // err = buildTimestampParamErr() + // errCode = model.ErrCodeOpenAPIParamErrTimeStamp + //} + //判断timestamp传入是否正确end + if err != nil { + goto end + } + //判断app_id和sign匹配begin - fmt.Println("routerMap[method].Call(parms)", routerMap[method].Call(parms)) + //判断app_id和sign匹配end + //call + urls, _ = c.Input() + dataMap = utils.URLValues2Map(urls) + delete(dataMap, "app_id") + delete(dataMap, "method") + delete(dataMap, "timestamp") + parms = []reflect.Value{reflect.ValueOf(dataMap)} + if routerMap[method] == reflect.ValueOf(nil) { + errCode = model.ErrCodeOpenAPIParamErrMethod + err = buildMethodParamErr() + } + globals.SugarLogger.Debugf("Begin API CallOpenAPI Method: %s, accessUUID:%s, sign:%s\n", method, accessID, sign) + if err == nil { + result = routerMap[method].Call(parms) + fmt.Println("routerMap[method].Call(parms)", result) + goto success + } else { + callResult.Code = errCode + callResult.Desc = err.Error() + } +end: + { + callResult.Code = errCode + callResult.Desc = err.Error() + c.Data["json"] = callResult + c.ServeJSON() + } +success: + { + callResult.Code = model.ErrCodeSuccess + callResult.Data = "" + c.Data["json"] = callResult + c.ServeJSON() + } } -func (c *ApiController) AddPrint() { - fmt.Println("call AddPrint success ...") +func (c *ApiController) AddPrint(dataMap map[string]interface{}) (data interface{}, err error) { + return fmt.Println("call AddPrint success ...", dataMap) } diff --git a/controllers/controllers.go b/controllers/controllers.go index 76e772a79..a9a2856f9 100644 --- a/controllers/controllers.go +++ b/controllers/controllers.go @@ -1,7 +1,34 @@ package controllers +import "fmt" + type CallResult struct { Code string `json:"code"` Desc string `json:"desc"` Data string `json:"data"` } + +func buildParamRequiredErr(str []string) (err error) { + msg := "缺少参数[" + for k, v := range str { + if k != 0 { + msg += "," + v + } else { + msg += v + } + } + msg += "],请传入正确的值!" + return fmt.Errorf(msg) +} + +func buildParamErr(str string) (err error) { + return fmt.Errorf("参数[%s]错误,请传入正确的值!", str) +} + +func buildMethodParamErr() (err error) { + return fmt.Errorf("参数['method']错误,请传入正确的值!") +} + +func buildTimestampParamErr() (err error) { + return fmt.Errorf("参数['timestamp']和服务器时间差距过大,请传入正确的值!") +}