Authored by 姜枫

api detail router

@@ -3,7 +3,10 @@ package main @@ -3,7 +3,10 @@ package main
3 import ( 3 import (
4 "github.com/kataras/iris" 4 "github.com/kataras/iris"
5 "github.com/kataras/go-template/handlebars" 5 "github.com/kataras/go-template/handlebars"
6 - "./src" 6 + "./yoho"
  7 + "./models"
  8 + "regexp"
  9 + "strconv"
7 ) 10 )
8 11
9 func main() { 12 func main() {
@@ -20,7 +23,29 @@ func main() { @@ -20,7 +23,29 @@ func main() {
20 result, _ := api.Get("", map[string]interface{}{ 23 result, _ := api.Get("", map[string]interface{}{
21 "method": "web.search.search", 24 "method": "web.search.search",
22 }) 25 })
23 - ctx.JSON(200, result) 26 + ctx.JSON(iris.StatusOK, result)
  27 + })
  28 +
  29 + pidReg := regexp.MustCompile(`\/pro_([\d]+)_([\d]+)\/(.*)`)
  30 +
  31 + iris.Get("/product/*ids", func(ctx *iris.Context){
  32 + ids := ctx.Param("ids")
  33 +
  34 + paths := pidReg.FindStringSubmatch(ids)
  35 +
  36 + var pid int64
  37 + if len(paths) > 1 {
  38 + pid, _ = strconv.ParseInt(paths[1], 10, 64)
  39 + } else {
  40 + pid = 0
  41 + }
  42 +
  43 + channel := "boys"
  44 + gender := "1,3"
  45 +
  46 + data := *models.GetDetailInfo(pid, channel, gender)
  47 +
  48 + ctx.JSON(200, data.Map())
24 }) 49 })
25 50
26 iris.Listen(":8080") 51 iris.Listen(":8080")
  1 +package models
  2 +
  3 +import (
  4 + "../yoho"
  5 + "github.com/antonholmquist/jason"
  6 +)
  7 +
  8 +var api *yoho.Http
  9 +
  10 +func init() {
  11 + api = yoho.Api("http://api.yoho.cn")
  12 +}
  13 +
  14 +func GetDetailInfo(pid int64, ch string, gender string) *jason.Object {
  15 + product, _ := getProduct(pid)
  16 +
  17 + //smallSortId, _ := product.GetInt64("data", "smallSortId")
  18 + //maxSortId, _ := product.GetInt64("data", "maxSortId")
  19 + //productId, _ := product.GetInt64("data", "product_id")
  20 + //productSkn, _ := product.GetInt64("data", "product_skn")
  21 +
  22 +
  23 + return product
  24 +}
  25 +
  26 +
  27 +func getProduct(pid int64) (*jason.Object, error) {
  28 + return api.Get("", map[string]interface{} {
  29 + "method" : "app.product.data",
  30 + "product_id": pid,
  31 + })
  32 +}
@@ -4,11 +4,12 @@ import ( @@ -4,11 +4,12 @@ import (
4 "net/http" 4 "net/http"
5 "strings" 5 "strings"
6 "net/url" 6 "net/url"
7 - "encoding/json"  
8 "fmt" 7 "fmt"
9 "sort" 8 "sort"
10 "crypto/md5" 9 "crypto/md5"
11 "encoding/hex" 10 "encoding/hex"
  11 + "time"
  12 + "github.com/antonholmquist/jason"
12 ) 13 )
13 14
14 type Http struct { 15 type Http struct {
@@ -26,30 +27,32 @@ func Api(host string) *Http { @@ -26,30 +27,32 @@ func Api(host string) *Http {
26 return &api 27 return &api
27 } 28 }
28 29
29 -func (this *Http) Get(path string, data map[string]interface{}) (interface{}, error) { 30 +func (this *Http) Get(path string, data map[string]interface{}) (*jason.Object, error) {
30 apiSign(&data) 31 apiSign(&data)
31 url := this.url.String() + queryBuild(path, &data) 32 url := this.url.String() + queryBuild(path, &data)
32 33
33 - fmt.Println(url)  
34 - 34 + t1 := time.Now()
35 res, err := this.client.Get(url) 35 res, err := this.client.Get(url)
  36 +
  37 + t2 := time.Now()
  38 + d := t2.Sub(t1)
  39 +
  40 + fmt.Println(d, url)
  41 +
36 if err != nil { 42 if err != nil {
37 return nil, err 43 return nil, err
38 } 44 }
39 45
40 defer res.Body.Close() 46 defer res.Body.Close()
41 47
42 - result := new(interface{})  
43 - json.NewDecoder(res.Body).Decode(&result)  
44 -  
45 - return result, nil 48 + return jason.NewObjectFromReader(res.Body)
46 } 49 }
47 50
48 func queryBuild(path string, data *map[string]interface{}) string { 51 func queryBuild(path string, data *map[string]interface{}) string {
49 var params []string 52 var params []string
50 53
51 for k, v := range *data { 54 for k, v := range *data {
52 - params = append(params, k + "=" + v.(string)) 55 + params = append(params, k + "=" + ToString(v))
53 } 56 }
54 57
55 query := strings.Join(params, "&") 58 query := strings.Join(params, "&")
@@ -79,10 +82,17 @@ func apiSign(v *map[string]interface{}) { @@ -79,10 +82,17 @@ func apiSign(v *map[string]interface{}) {
79 sort.Strings(keys) 82 sort.Strings(keys)
80 83
81 for _, k := range keys { 84 for _, k := range keys {
82 - params = append(params, (k + "=" + data[k].(string))) 85 +
  86 + params = append(params, (k + "=" + ToString(data[k])))
83 } 87 }
84 88
85 value := strings.Join(params, "&") 89 value := strings.Join(params, "&")
86 hash := md5.Sum([]byte(strings.ToLower(value))) 90 hash := md5.Sum([]byte(strings.ToLower(value)))
87 data["client_secret"] = hex.EncodeToString(hash[:]) 91 data["client_secret"] = hex.EncodeToString(hash[:])
88 } 92 }
  93 +
  94 +// ToString convert the input to a string.
  95 +func ToString(obj interface{}) string {
  96 + res := fmt.Sprintf("%v", obj)
  97 + return string(res)
  98 +}