package controller

import (
“encoding/json”
“errors”
“fmt”
“extend/helper”
“github.com/bitly/go-simplejson”
“github.com/gin-gonic/gin”
“io/ioutil”
“log”
“net/http”
“sort”
“strings”
“time”
)

type TestController struct{}

//发布礼物配置
func (t *TestController) Index(c *gin.Context) {
result := make(map[string]interface{})
//result[“success”] = true
//result[“data”] = PostForm(c.Request)
//PostTest2(c)
//_, _ = c.GetPostForm(“sign1″)
//result[“postJson”] = PostTest(c.Request)
////result[“postJson2″] = PostTest3(c)
////result[“postJson1″] = Get(c.Request)
////result[“getC”] = getC
////result[“getD”] = d
//result[“Method”] = c.Request.Method
//result[“url”] = c.Request.URL
//result[“Proto”] = c.Request.Proto
//result[“Header”] = c.Request.Header
//result[“Host”] = c.Request.Host
////result[“GetBody”] = c.Request.GetBody
//result[“PostForm”] = c.Request.PostForm
//result[“MultipartForm”] = c.Request.MultipartForm
//result[“Trailer”] = c.Request.Trailer
//result[“RemoteAddr”] = c.Request.RemoteAddr
//result[“RequestURI”] = c.Request.RequestURI
//result[“Response”] = c.Request.Response

//data, err = verifySign(c)
//fmt.Println(data)
//fmt.Println(err)

result, _ = verifySign(c)

helper.Successful(c, result)
}

func verifySign(c *gin.Context) (data map[string]interface{}, err error) {
data = make(map[string]interface{})

req := c.Request
if req.Method == “GET” {
keys := req.URL.Query()
for k, v := range keys {
data[k] = v[0]
}
} else if req.Method == “POST” {
contentType := req.Header.Get(“Content-Type”)
if contentType == “application/x-www-form-urlencoded” {
req.ParseForm()
for k, v := range req.Form {
if len(v) < 1 { continue } data[k] = v[0] } } else if strings.Contains(contentType, "multipart/form-data;") { _ = c.PostForm("sign") for k, v := range req.PostForm { if len(v) < 1 { continue } data[k] = v[0] } } else if contentType == "application/json" { c.BindJSON(&data) } else { err = errors.New("错误的Content-type") } } else { err = errors.New("错误的Method") } return } //Get get请求url参数 func Get(req *http.Request) map[string]string { var result = make(map[string]string) keys := req.URL.Query() for k, v := range keys { result[k] = v[0] } return result } //PostForm 获取postform形式的参数 func PostForm(req *http.Request) map[string]string { //body, _ := ioutil.ReadAll(req.Body) var result = make(map[string]string) req.ParseForm() for k, v := range req.PostForm { if len(v) < 1 { continue } result[k] = v[0] } return result } //PostJson 获取post json参数 func PostJson(req *http.Request, obj interface{}) error { body, err := ioutil.ReadAll(req.Body) if err != nil { return err } err = json.Unmarshal(body, obj) if err != nil { return err } return nil } //Get get请求url参数 func PostTest(req *http.Request) map[string]interface{} { var result = make(map[string]interface{}) body, err := ioutil.ReadAll(req.Body) if err != nil { return nil } err = json.Unmarshal(body, result) if err != nil { return result } return nil } //Get get请求url参数 func PostTest2(c *gin.Context) { json := make(map[string]interface{}) //注意该结构接受的内容 c.BindJSON(&json) log.Printf("%v", &json) } //Get get请求url参数 func PostTest3(c *gin.Context) map[string]interface{} { jsons := make(map[string]interface{}) //注意该结构接受的内容 c.BindJSON(&jsons) cont := jsons["content"].(string) js, err := simplejson.NewJson([]byte(cont)) //反序列化 if err != nil { panic(err.Error()) } fmt.Println(js) return jsons } var AppSecret string = "111" // 验证签名 //func verifySign(c *gin.Context) (map[string]string, error) { // // //appKey = "demo" // //appSecret = "xxx" // //encryptStr = "param_1=xxx&param_2=xxx&ak="+appKey+"&ts=xxx" // //自定义验证规则 // //sn = MD5(appSecret + encryptStr + appSecret) // // // validate appid redirectURI and return stat code scope // //var urlStr string = "https://www.google.com/" // https%3a%2f%2fwww.google.com%2f // //escapeUrl := url.QueryEscape(urlStr) // //fmt.Println("编码:", escapeUrl) // //enEscapeUrl, _ := url.QueryUnescape(escapeUrl) // //fmt.Println("解码:", enEscapeUrl) // // // _ = c.Request.ParseForm() // req := c.Request.Form // debug := strings.Join(c.Request.Form["debug"], "") // sn := strings.Join(c.Request.Form["sn"], "") // ts := strings.Join(c.Request.Form["ts"], "") // // // 验证来源 // // AppSecret = "test" // // if debug == "1" { // var currentUnix int64 = 534243132 //自己写的 // req.Set("ts", strconv.FormatInt(currentUnix, 10)) // res := map[string]string{ // "ts": strconv.FormatInt(currentUnix, 10), // "sn": createSign(req), // } // return res, nil // } // abcd := "10" // // 验证过期时间 // timestamp := time.Now().Unix() // exp, _ := strconv.ParseInt(abcd, 10, 64) // tsInt, _ := strconv.ParseInt(ts, 10, 64) // if tsInt > timestamp || timestamp-tsInt >= exp {
// return nil, errors.New(“ts Error”)
// }
// // 验证签名
// if sn == “” || sn != createSign(req) {
// return nil, errors.New(“sn Error”)
// }
// return nil, nil
//}

// 创建签名
func createSign(params map[string]interface{}) (string, map[string]interface{}) {
params[“appSecret”] = AppSecret

_, haveSign := params[“sign”]
if haveSign {
delete(params, “sign”)
}

_, haveTimestamp := params[“timestamp”]
if !haveTimestamp {
params[“appSecret”] = time.Now().Unix()
}

sign := helper.Md5(helper.Md5(createEncryptChildStr(params)) + AppSecret)
delete(params, “appSecret”)

return sign, params
}

func createEncryptChildStr(params map[string]interface{}) string {
var key []string
var str = “”
for k := range params {
key = append(key, k)
}
sort.Strings(key)
for i := 0; i < len(key); i++ { val := params[key[i]] if i == 0 { str = fmt.Sprintf(“%v=%v”, key[i], val) } else { str = str + fmt.Sprintf(“&%v=%v”, key[i], val) } } return str }

Comments are closed.

Post Navigation