|
@@ -17,12 +17,12 @@ import (
|
|
// BaseAuth 需要授权token的基类
|
|
// BaseAuth 需要授权token的基类
|
|
type BaseAuth struct {
|
|
type BaseAuth struct {
|
|
BaseCommon
|
|
BaseCommon
|
|
- AdminWx *custom.AdminWx `description:"管理员信息"`
|
|
|
|
- Token string `description:"用户token"`
|
|
|
|
|
|
+ AdminWx *custom.AdminWx `description:"管理员信息"`
|
|
|
|
+ Token string `description:"用户token"`
|
|
|
|
|
|
- StartSize int `description:"开始数量"`
|
|
|
|
- StartPage int `description:"开始页码"`
|
|
|
|
- PageSize int `description:"每页数量"`
|
|
|
|
|
|
+ StartSize int `description:"开始数量"`
|
|
|
|
+ StartPage int `description:"开始页码"`
|
|
|
|
+ PageSize int `description:"每页数量"`
|
|
}
|
|
}
|
|
|
|
|
|
func (c *BaseAuth) Prepare() {
|
|
func (c *BaseAuth) Prepare() {
|
|
@@ -30,7 +30,7 @@ func (c *BaseAuth) Prepare() {
|
|
signData := make(map[string]string)
|
|
signData := make(map[string]string)
|
|
method := c.Ctx.Input.Method()
|
|
method := c.Ctx.Input.Method()
|
|
|
|
|
|
- var pageSize,currentIndex int
|
|
|
|
|
|
+ var pageSize, currentIndex int
|
|
switch method {
|
|
switch method {
|
|
case "GET":
|
|
case "GET":
|
|
//requestBody = c.Ctx.Request.RequestURI
|
|
//requestBody = c.Ctx.Request.RequestURI
|
|
@@ -48,8 +48,8 @@ func (c *BaseAuth) Prepare() {
|
|
switch contentType {
|
|
switch contentType {
|
|
case "multipart/form-data":
|
|
case "multipart/form-data":
|
|
//文件最大5M
|
|
//文件最大5M
|
|
- err := c.Ctx.Request.ParseMultipartForm(- int64(5<<20))
|
|
|
|
- if err != nil{
|
|
|
|
|
|
+ err := c.Ctx.Request.ParseMultipartForm(-int64(5 << 20))
|
|
|
|
+ if err != nil {
|
|
c.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err))
|
|
c.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err))
|
|
//response.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err), c)
|
|
//response.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err), c)
|
|
//c.Abort()
|
|
//c.Abort()
|
|
@@ -57,7 +57,7 @@ func (c *BaseAuth) Prepare() {
|
|
}
|
|
}
|
|
default:
|
|
default:
|
|
err := c.Ctx.Request.ParseForm()
|
|
err := c.Ctx.Request.ParseForm()
|
|
- if err != nil{
|
|
|
|
|
|
+ if err != nil {
|
|
c.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err))
|
|
c.FailWithMessage(fmt.Sprintf("获取参数失败,%v", err))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -72,7 +72,7 @@ func (c *BaseAuth) Prepare() {
|
|
pageSize = utils.PageSize20
|
|
pageSize = utils.PageSize20
|
|
}
|
|
}
|
|
//如果超过最大分页数,那么就是按照最大分页数返回
|
|
//如果超过最大分页数,那么就是按照最大分页数返回
|
|
- if pageSize > utils.PageMaxSize{
|
|
|
|
|
|
+ if pageSize > utils.PageMaxSize {
|
|
pageSize = utils.PageMaxSize
|
|
pageSize = utils.PageMaxSize
|
|
}
|
|
}
|
|
if currentIndex <= 0 {
|
|
if currentIndex <= 0 {
|
|
@@ -83,13 +83,12 @@ func (c *BaseAuth) Prepare() {
|
|
c.PageSize = pageSize
|
|
c.PageSize = pageSize
|
|
c.StartPage = currentIndex
|
|
c.StartPage = currentIndex
|
|
|
|
|
|
-
|
|
|
|
ip := c.Ctx.Input.IP()
|
|
ip := c.Ctx.Input.IP()
|
|
|
|
|
|
//获取签名秘钥
|
|
//获取签名秘钥
|
|
//key := global.GVA_CONFIG.SignKey.Agent
|
|
//key := global.GVA_CONFIG.SignKey.Agent
|
|
////签名校验
|
|
////签名校验
|
|
- err := checkSign(signData,ip)
|
|
|
|
|
|
+ err := checkSign(signData, ip)
|
|
if err != nil {
|
|
if err != nil {
|
|
c.SignError(fmt.Sprintf("签名校验失败,%v", err))
|
|
c.SignError(fmt.Sprintf("签名校验失败,%v", err))
|
|
return
|
|
return
|
|
@@ -99,7 +98,7 @@ func (c *BaseAuth) Prepare() {
|
|
}
|
|
}
|
|
|
|
|
|
//将请求传入的数据格式转换成签名需要的格式
|
|
//将请求传入的数据格式转换成签名需要的格式
|
|
-func convertParam(params map[string][]string)(signData map[string]string) {
|
|
|
|
|
|
+func convertParam(params map[string][]string) (signData map[string]string) {
|
|
signData = make(map[string]string)
|
|
signData = make(map[string]string)
|
|
for key := range params {
|
|
for key := range params {
|
|
signData[key] = params[key][0]
|
|
signData[key] = params[key][0]
|
|
@@ -107,38 +106,37 @@ func convertParam(params map[string][]string)(signData map[string]string) {
|
|
return signData
|
|
return signData
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
//请求参数签名校验
|
|
//请求参数签名校验
|
|
-func checkSign(postData map[string]string,ip string) (err error){
|
|
|
|
|
|
+func checkSign(postData map[string]string, ip string) (err error) {
|
|
isSandbox := postData["is_sandbox"]
|
|
isSandbox := postData["is_sandbox"]
|
|
//如果是测试环境,且是沙箱环境的话,那么绕过测试
|
|
//如果是测试环境,且是沙箱环境的话,那么绕过测试
|
|
if utils.RunMode == "debug" && isSandbox != "" {
|
|
if utils.RunMode == "debug" && isSandbox != "" {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- appid :=postData["appid"]
|
|
|
|
|
|
+ appid := postData["appid"]
|
|
if appid == "" {
|
|
if appid == "" {
|
|
err = errors.New("参数异常,缺少appid")
|
|
err = errors.New("参数异常,缺少appid")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- openApiUserInfo,tmpErr:= open_api_user.GetByAppid(appid)
|
|
|
|
- if tmpErr != nil{
|
|
|
|
- if tmpErr.Error() == utils.ErrNoRow(){
|
|
|
|
|
|
+ openApiUserInfo, tmpErr := open_api_user.GetByAppid(appid)
|
|
|
|
+ if tmpErr != nil {
|
|
|
|
+ if tmpErr.Error() == utils.ErrNoRow() {
|
|
err = errors.New("appid异常,请联系管理员")
|
|
err = errors.New("appid异常,请联系管理员")
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
err = errors.New("系统异常,请联系管理员")
|
|
err = errors.New("系统异常,请联系管理员")
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- if openApiUserInfo == nil{
|
|
|
|
|
|
+ if openApiUserInfo == nil {
|
|
err = errors.New("系统异常,请联系管理员")
|
|
err = errors.New("系统异常,请联系管理员")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
//如果有ip限制,那么就添加ip
|
|
//如果有ip限制,那么就添加ip
|
|
- if openApiUserInfo.Ip != ""{
|
|
|
|
- if !strings.Contains(openApiUserInfo.Ip,ip){
|
|
|
|
- err = errors.New(fmt.Sprintf("无权限访问该接口,ip:%v,请联系管理员",ip))
|
|
|
|
|
|
+ if openApiUserInfo.Ip != "" {
|
|
|
|
+ if !strings.Contains(openApiUserInfo.Ip, ip) {
|
|
|
|
+ err = errors.New(fmt.Sprintf("无权限访问该接口,ip:%v,请联系管理员", ip))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -156,15 +154,15 @@ func checkSign(postData map[string]string,ip string) (err error){
|
|
if postData["timestamp"] == "" {
|
|
if postData["timestamp"] == "" {
|
|
err = errors.New("参数异常,缺少时间戳")
|
|
err = errors.New("参数异常,缺少时间戳")
|
|
return
|
|
return
|
|
- }else{
|
|
|
|
- timeUnix := time.Now().Unix() //当前格林威治时间,int64类型
|
|
|
|
|
|
+ } else {
|
|
|
|
+ timeUnix := time.Now().Unix() //当前格林威治时间,int64类型
|
|
//将接口传入的时间做转换
|
|
//将接口传入的时间做转换
|
|
timestamp, timeErr := strconv.ParseInt(postData["timestamp"], 10, 64)
|
|
timestamp, timeErr := strconv.ParseInt(postData["timestamp"], 10, 64)
|
|
- if timeErr != nil{
|
|
|
|
|
|
+ if timeErr != nil {
|
|
err = errors.New("参数异常,时间戳格式异常")
|
|
err = errors.New("参数异常,时间戳格式异常")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- if math.Abs(float64(timeUnix - timestamp)) > 300 {
|
|
|
|
|
|
+ if math.Abs(float64(timeUnix-timestamp)) > 300 {
|
|
err = errors.New("当前时间异常,请调整设备时间与北京时间一致")
|
|
err = errors.New("当前时间异常,请调整设备时间与北京时间一致")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -189,13 +187,12 @@ func checkSign(postData map[string]string,ip string) (err error){
|
|
//3,全转小写(md5(拼装的字符串后+分配给你的app_secret))
|
|
//3,全转小写(md5(拼装的字符串后+分配给你的app_secret))
|
|
//sign := strings.ToLower(fmt.Sprintf("%x", md5.Sum([]byte(strings.Trim(signStr, "&")+key))))
|
|
//sign := strings.ToLower(fmt.Sprintf("%x", md5.Sum([]byte(strings.Trim(signStr, "&")+key))))
|
|
|
|
|
|
-
|
|
|
|
//md5.Sum([]byte(signStr+"key="+key)) 这是md5加密出来后的每个字符的ascall码,需要再转换成对应的字符
|
|
//md5.Sum([]byte(signStr+"key="+key)) 这是md5加密出来后的每个字符的ascall码,需要再转换成对应的字符
|
|
//3,全转大写(md5(拼装的字符串后+分配给你的app_secret))
|
|
//3,全转大写(md5(拼装的字符串后+分配给你的app_secret))
|
|
sign := strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(signStr+"secret="+openApiUserInfo.Secret))))
|
|
sign := strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(signStr+"secret="+openApiUserInfo.Secret))))
|
|
-
|
|
|
|
|
|
+ fmt.Println(sign)
|
|
if sign != ownSign {
|
|
if sign != ownSign {
|
|
- utils.ApiLog.Println(fmt.Sprintf("签名校验异常,签名字符串:%v;服务端签名值:%v",signStr,sign))
|
|
|
|
|
|
+ utils.ApiLog.Println(fmt.Sprintf("签名校验异常,签名字符串:%v;服务端签名值:%v", signStr, sign))
|
|
return errors.New("签名校验异常,请核实签名")
|
|
return errors.New("签名校验异常,请核实签名")
|
|
}
|
|
}
|
|
return nil
|
|
return nil
|