Commit c7672236 by huangchengyi

1.0

parent c24da176
Showing with 40 additions and 42 deletions
......@@ -2,29 +2,24 @@ package main
import (
"fmt"
"sort"
"github.com/syyongx/php2go"
)
// 按map的key排序
func main() {
params := make(map[string]interface{})
params["name"] = "xxx"
params["age"] = 23
params["sex"] = 0
params["level"] = 1
// 1.取出所有的key
var keys []string
for k := range params{
keys = append(keys, k)
userCount := 10000
ch := make(chan bool, 2)
for i := 0; i < userCount; i++ {
php2go.Time()
ch <- true
go send(ch, i)
}
// 对字符串切片排序
sort.Strings(keys)
fmt.Println(keys)
// 打印key, val
for _, k := range keys{
fmt.Printf("key: %v val:%v \n", k, params[k])
}
//time.Sleep(time.Second)
}
func send(ch chan bool, i int) {
//time.Sleep(time.Second*i)
fmt.Println(php2go.Time())
fmt.Printf("go func: %d\n", i)
<- ch
}
......@@ -3,9 +3,9 @@ package ly
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gogf/gf/util/gconv"
"github.com/iancoleman/orderedmap"
"github.com/imroc/req"
"github.com/syyongx/php2go"
"github.com/tidwall/gjson"
"search_server/model"
"search_server/pkg/common"
......@@ -80,8 +80,11 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
return nil;
}
skuRsults := gjson.Get(skuIdRes,"results").Array()
//协程并发请求
ch := make(chan *orderedmap.OrderedMap)
//接收通道所有的值
A := orderedmap.New()
ch := make(chan bool,2)
p := 0;
for _,a := range skuRsults {
goods_sn := a.Get("sku").String() //goods_sn
......@@ -89,6 +92,7 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
brand_name := a.Get("manufacturer").String() //品牌
desc := a.Get("description").String() //描述
ch <- true
go func() {
B := orderedmap.New()
B.Set("goods_sn",goods_sn)
......@@ -98,29 +102,28 @@ func OutLinkSource(ctx *gin.Context,goodsName *string) *orderedmap.OrderedMap {
flag := GetSourceInfo(ctx,B)
//res,_ := json.Marshal(A)
if flag {
ch <- B
}else{
ch <- nil
A.Set(goods_sn,B)
}
<- ch
}()
p ++;
}
//接收通道所有的值
A := orderedmap.New();
for i := 0; i < p; i++ {
select {
case x := <-ch:
if x == nil {
continue
}
goods_sn,_ := x.Get("goods_sn")
A.Set(gconv.String(goods_sn),x)
case <- time.After(time.Second *2):
fmt.Println("超时退出等待")
}
}
//A := orderedmap.New();
//for i := 0; i < p; i++ {
// select {
// case x := <-ch:
// if x == nil {
// continue
// }
// goods_sn,_ := x.Get("goods_sn")
// A.Set(gconv.String(goods_sn),x)
// case <- time.After(time.Second *2):
// fmt.Println("超时退出等待")
// }
//}
//allRes,_ := json.Marshal(A)
return A;
}
......@@ -130,7 +133,7 @@ func GetSourceInfo(ctx *gin.Context,B *orderedmap.OrderedMap) bool {
goods_sn,_ := B.Get("goods_sn")
skuInfo := GetSourceGineData(ctx,fmt.Sprintf(SourceSkuPriceUrl,goods_sn))
//fmt.Println("请求价格开始:",php2go.Time(),fmt.Sprintf(SourceSkuPriceUrl,goods_sn))
fmt.Println("请求价格开始:",php2go.Time(),fmt.Sprintf(SourceSkuPriceUrl,goods_sn))
//$skuPriceRes = `{"total":3,"results":[{"offerPlatformId":"8718009643","mpq":1,"quantity":2277626,"priceTiers":[{"moq":807,"price":"0.06820"}],"deliveryDays":5,"vendor":{"type":"factory_direct"},"dateCode":null,"isDateCodeWithin2Years":true,"availabilityType":"stock","packagingType":"tube","packagingCondition":"full"},{"offerPlatformId":"8715417520","mpq":1,"quantity":1520,"priceTiers":[{"moq":709,"price":"0.07768"}],"deliveryDays":7,"vendor":{"type":"authorised_distributor"},"dateCode":null,"isDateCodeWithin2Years":true,"availabilityType":"stock","packagingType":"tape_reel","packagingCondition":"full"},{"offerPlatformId":"8715417522","mpq":1,"quantity":283206,"priceTiers":[{"moq":709,"price":"0.07768"}],"deliveryDays":167,"vendor":{"type":"authorised_distributor"},"dateCode":null,"isDateCodeWithin2Years":true,"availabilityType":"lead_time","packagingType":"tape_reel","packagingCondition":"full"}],"facets":{"vendorTypes":[{"value":"authorised_distributor","label":"authorised_distributor","count":2},{"value":"factory_direct","label":"factory_direct","count":1}],"availabilityTypes":[{"value":"lead_time","label":"lead_time","count":1},{"value":"stock","label":"stock","count":2}],"packagingConditions":[{"value":"full","label":"full","count":3}],"packagingTypes":[{"value":"tape_reel","label":"tape_reel","count":2},{"value":"tube","label":"tube","count":1}]},"_links":{"schema":{"href":"/api/schemas/part-offer-search.json"}}}`;
priceGoodsList := gjson.Get(skuInfo,"results").Array()
common.PrintDebugHtml(ctx,priceGoodsList)
......@@ -221,12 +224,12 @@ func GetSourceGineData(ctx *gin.Context,url string) string {
"Content-Type": "application/json",
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2MDUwMjk3ODQsInVzZXJuYW1lIjoiYWNiMjMzMmItYjQ4MC00M2UwLTgwZjYtZjQ0OTVkY2M0NjBhIn0.Bt6Aii7xxVKIfxpnttmHi4hDhvBmw0Bow47b1s08ibfDVtQxmn5ZzOAbsl0spxqMysUngZ31KfPEnos5EeD04jOtu5Y9llNHCymO15_--AYy4JdzcoDgSwUnogUw4tD4zkm4vTsRxzfTjbrN9gX2n_33BemD4S1oT4M4rPK_RRw6dJ8wYqMIKYjjHPPlNIy5S155vG7CtN8i2mpHaJkDNcK2thhAgEkFuncpKAuV8oV87Pk0WoiQYkzCcQ3MbqsdV7FYuNpDCJfalzplk8y04UkG3lAGCySGUSwHnSk0RbJablg-s2td54-Tixwm5q8X3UPViVo0QTk2EmMZq2qKnrXnPUy7MVJUEpWaFRih0P3oRJUusW3cxcXRRjxlgVyD4n3vKmM0lFo-gcNfWFhbh6pJw9dxs6GqIZZqjHPa92EIWHgezDmXt97FYH5i9J_pesJCYGdRfAZDZ_mcD9KPZUylDd17nlrMWl_D14vMOklU4_9RksBX7YsNu5lyexW-djOmCcnTFV5GMcPF1jht0i6IlvQYHgNYtEITEKNMjAGRy4AL_3kYXRjlGsq37uKJup14t4VkZe95Zt_beIqPyRvcig3TV1ol5vdPf2IW1Vt_vJXsoNno5F_uznvsv268srplyO-akqbpP-X16m-Sh9kgZhk1fDcWQ8kJXt7uqd0",
}
req.SetTimeout(3 * time.Second)
req.SetTimeout(8 * time.Second)
resp, err := req.Get(url, header)
if err != nil {
return ""
}
res := resp.String() //请求外链拿到结果
common.PrintDebugHtml(ctx,"原始数据:" + res)
common.PrintDebugHtml(ctx,"原始数据:"+ url + res)
return res
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment