Authored by 姜枫

api detail router

... ... @@ -3,7 +3,10 @@ package main
import (
"github.com/kataras/iris"
"github.com/kataras/go-template/handlebars"
"./src"
"./yoho"
"./models"
"regexp"
"strconv"
)
func main() {
... ... @@ -20,7 +23,29 @@ func main() {
result, _ := api.Get("", map[string]interface{}{
"method": "web.search.search",
})
ctx.JSON(200, result)
ctx.JSON(iris.StatusOK, result)
})
pidReg := regexp.MustCompile(`\/pro_([\d]+)_([\d]+)\/(.*)`)
iris.Get("/product/*ids", func(ctx *iris.Context){
ids := ctx.Param("ids")
paths := pidReg.FindStringSubmatch(ids)
var pid int64
if len(paths) > 1 {
pid, _ = strconv.ParseInt(paths[1], 10, 64)
} else {
pid = 0
}
channel := "boys"
gender := "1,3"
data := *models.GetDetailInfo(pid, channel, gender)
ctx.JSON(200, data.Map())
})
iris.Listen(":8080")
... ...
package models
import (
"../yoho"
"github.com/antonholmquist/jason"
)
var api *yoho.Http
func init() {
api = yoho.Api("http://api.yoho.cn")
}
func GetDetailInfo(pid int64, ch string, gender string) *jason.Object {
product, _ := getProduct(pid)
//smallSortId, _ := product.GetInt64("data", "smallSortId")
//maxSortId, _ := product.GetInt64("data", "maxSortId")
//productId, _ := product.GetInt64("data", "product_id")
//productSkn, _ := product.GetInt64("data", "product_skn")
return product
}
func getProduct(pid int64) (*jason.Object, error) {
return api.Get("", map[string]interface{} {
"method" : "app.product.data",
"product_id": pid,
})
}
\ No newline at end of file
... ...
... ... @@ -4,11 +4,12 @@ import (
"net/http"
"strings"
"net/url"
"encoding/json"
"fmt"
"sort"
"crypto/md5"
"encoding/hex"
"time"
"github.com/antonholmquist/jason"
)
type Http struct {
... ... @@ -26,30 +27,32 @@ func Api(host string) *Http {
return &api
}
func (this *Http) Get(path string, data map[string]interface{}) (interface{}, error) {
func (this *Http) Get(path string, data map[string]interface{}) (*jason.Object, error) {
apiSign(&data)
url := this.url.String() + queryBuild(path, &data)
fmt.Println(url)
t1 := time.Now()
res, err := this.client.Get(url)
t2 := time.Now()
d := t2.Sub(t1)
fmt.Println(d, url)
if err != nil {
return nil, err
}
defer res.Body.Close()
result := new(interface{})
json.NewDecoder(res.Body).Decode(&result)
return result, nil
return jason.NewObjectFromReader(res.Body)
}
func queryBuild(path string, data *map[string]interface{}) string {
var params []string
for k, v := range *data {
params = append(params, k + "=" + v.(string))
params = append(params, k + "=" + ToString(v))
}
query := strings.Join(params, "&")
... ... @@ -79,10 +82,17 @@ func apiSign(v *map[string]interface{}) {
sort.Strings(keys)
for _, k := range keys {
params = append(params, (k + "=" + data[k].(string)))
params = append(params, (k + "=" + ToString(data[k])))
}
value := strings.Join(params, "&")
hash := md5.Sum([]byte(strings.ToLower(value)))
data["client_secret"] = hex.EncodeToString(hash[:])
}
// ToString convert the input to a string.
func ToString(obj interface{}) string {
res := fmt.Sprintf("%v", obj)
return string(res)
}
\ No newline at end of file
... ...