diff --git a/platformapi/ebaiapi/ebaiapi.go b/platformapi/ebaiapi/ebaiapi.go index 1f7d1b6e..74234697 100644 --- a/platformapi/ebaiapi/ebaiapi.go +++ b/platformapi/ebaiapi/ebaiapi.go @@ -34,6 +34,8 @@ type ResponseResult struct { } type API struct { + platformapi.APICookie + source string secret string encrypt string diff --git a/platformapi/ebaiapi/ebaiapi_test.go b/platformapi/ebaiapi/ebaiapi_test.go index e441d6cf..5fa8af03 100644 --- a/platformapi/ebaiapi/ebaiapi_test.go +++ b/platformapi/ebaiapi/ebaiapi_test.go @@ -34,8 +34,8 @@ func init() { // 京西果园 // api = New("35957", "10013fbb7c2ddad7") - api.SetStoreCookie("WMUSS", "4AAPQCAAB5PF0aUGcBVzoRTCEkOFhFIhx-Yk9vN2EfPHYoLlROKBEsQmAUQjhNUgRt0ADAP5x-RFklwAAdjxGO11iOj8xKXYSSDIJb2BcPghsaklNfQwGS10JOVRFfhAiYElhEXFXIzoJKyloCGdwdFE6Qk9FRxojUFN3FVEHNjJPZJu4Bt9nxQ13cwoMbjA") - api.SetStoreCookie("WMSTOKEN", "AcAANQZAABbC04rUBZFc2UYanlocDAaP0dcfzZCeS1SHQ1qJ15ExgAA13A2dGLjdbcitBZJu4Bn6B_g6cZAAA0tyyFm8cdBaNAQAAwug8HTG0xRjwt1UZzbcAAN7ofRO") + api.SetCookie("WMUSS", "4AAPQCAAB5PF0aUGcBVzoRTCEkOFhFIhx-Yk9vN2EfPHYoLlROKBEsQmAUQjhNUgRt0ADAP5x-RFklwAAdjxGO11iOj8xKXYSSDIJb2BcPghsaklNfQwGS10JOVRFfhAiYElhEXFXIzoJKyloCGdwdFE6Qk9FRxojUFN3FVEHNjJPZJu4Bt9nxQ13cwoMbjA") + api.SetCookie("WMSTOKEN", "AcAANQZAABbC04rUBZFc2UYanlocDAaP0dcfzZCeS1SHQ1qJ15ExgAA13A2dGLjdbcitBZJu4Bn6B_g6cZAAA0tyyFm8cdBaNAQAAwug8HTG0xRjwt1UZzbcAAN7ofRO") } func TestTest(t *testing.T) { diff --git a/platformapi/ebaiapi/store_page.go b/platformapi/ebaiapi/store_page.go index 59ade957..e3ddfa4f 100644 --- a/platformapi/ebaiapi/store_page.go +++ b/platformapi/ebaiapi/store_page.go @@ -298,23 +298,8 @@ type PageShopInfo struct { TakeoutPrice int `json:"takeout_price"` } -func (a *API) SetStoreCookie(key, value string) { - a.locker.Lock() - defer a.locker.Unlock() - a.storeCookies[key] = value -} - -func (a *API) GetStoreCookie(key string) string { - a.locker.RLock() - defer a.locker.RUnlock() - return a.storeCookies[key] -} - func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, isPost bool, cookies map[string]string) (retVal map[string]interface{}, err error) { - a.locker.RLock() - storeCookieLen := len(a.storeCookies) - a.locker.RUnlock() - if storeCookieLen == 0 { + if a.GetCookieCount() == 0 { return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") } err = platformapi.AccessPlatformAPIWithRetry(a.client, @@ -327,20 +312,13 @@ func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, isP request.Header.Set("charset", "UTF-8") request.Header.Set("Content-Type", "application/x-www-form-urlencoded") } - a.locker.RLock() - for k, v := range a.storeCookies { - request.AddCookie(&http.Cookie{ - Name: k, - Value: v, - }) - } + a.FillRequestCookies(request) for k, v := range cookies { request.AddCookie(&http.Cookie{ Name: k, Value: v, }) } - a.locker.RUnlock() return request }, a.config, diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 65d74a21..1d3dfed9 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -59,6 +59,8 @@ const ( ) type API struct { + platformapi.APICookie + token string appKey string appSecret string diff --git a/platformapi/jdapi/jdapi_test.go b/platformapi/jdapi/jdapi_test.go index 4f26e40f..b6bd0372 100644 --- a/platformapi/jdapi/jdapi_test.go +++ b/platformapi/jdapi/jdapi_test.go @@ -27,7 +27,9 @@ func init() { // 天天果园 // api = New("84541069-fbe2-424b-b625-9b2ba1d4c9e6", "5d5577a2506f41b8b4ec520ba83490f5", "0b01b9eeb15b41dab1c3d05d95c17a26") - api.SetStoreCookie("YYJV3NHVBPHLD36FWP6F3EM5PTXJ2XZQS7U4HWRIDPP4IWGUKUIB4XG5N26CZRDLDF7PKOXBPD6BNTUAJLETLZOIWMCVFI3K6MYZIY4QBIXIMXYDJNUKFGJVQTN5356SAD6WPCIHWNQAG7DDMF7L7S3SHDYZP7PPVMRGO4VWG2JRBMKFTOGIWZ5L2XHXC3SXQ4OLX7EL4RKUPZQT6GOH63KE3EVK37L5LG7TGSDGXFQP4377YK72UB5YZG6IJH6PY25YLLCJYPMDSHKPGYBUFJ4MMMKGN6MWB37CP7XVDBBZJ3U462ENTEXH744AWCQCIG2AAE2PKYVHC") + const cookieValue = "YYJV3NHVBPHLD36FWP6F3EM5PTXJ2XZQS7U4HWRIDPP4IWGUKUIB4XG5N26CZRDLDF7PKOXBPD6BNTUAJLETLZOIWMCVFI3K6MYZIY4QBIXIMXYDJNUKFGJVQTN5356SAD6WPCIHWNQAG7DDMF7L7S3SHDYZP7PPVMRGO4VWG2JRBMKFTOGIWZ5L2XHXC3SXQ4OLX7EL4RKUPZQT6GOH63KE3EVK37L5LG7TGSDGXFQP4377YK72UB5YZG6IJH6PY25YLLCJYPMDSHKPGYBUFJ4MMMKGN6MWB37CP7XVDBBZJ3U462ENTEXH744AWCQCIG2AAE2PKYVHC" + api.SetCookie(accessStorePageCookieName, cookieValue) + api.SetCookie(accessStorePageCookieName2, cookieValue) } func TestTest(t *testing.T) { diff --git a/platformapi/jdapi/store_page.go b/platformapi/jdapi/store_page.go index 3ac2ac50..c4e56f73 100644 --- a/platformapi/jdapi/store_page.go +++ b/platformapi/jdapi/store_page.go @@ -140,21 +140,8 @@ var ( monthSaleNumReg = regexp.MustCompile(`(\d+)([千|万])`) ) -func (a *API) SetStoreCookie(storeCookie string) { - a.locker.Lock() - defer a.locker.Unlock() - a.storeCookie = storeCookie -} - -func (a *API) GetStoreCookie() string { - a.locker.RLock() - defer a.locker.RUnlock() - return a.storeCookie -} - func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) { - storeCookie := a.GetStoreCookie() - if storeCookie == "" { + if a.GetCookieCount() == 0 { return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") } err = platformapi.AccessPlatformAPIWithRetry(a.client, @@ -170,14 +157,7 @@ func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isP if err != nil { return nil } - request.AddCookie(&http.Cookie{ - Name: accessStorePageCookieName, - Value: storeCookie, - }) - request.AddCookie(&http.Cookie{ - Name: accessStorePageCookieName2, - Value: storeCookie, - }) + a.FillRequestCookies(request) return request }, a.config, diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go index 3719094e..a7bd2a33 100644 --- a/platformapi/mtpsapi/mtpsapi.go +++ b/platformapi/mtpsapi/mtpsapi.go @@ -156,6 +156,8 @@ type GoodsDetail struct { } type API struct { + platformapi.APICookie + appKey string secret string client *http.Client @@ -196,22 +198,30 @@ func (a *API) signParams(params url.Values) string { return fmt.Sprintf("%x", sha1.Sum([]byte(finalStr))) } -func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *ResponseResult, err error) { +func (a *API) AccessAPI2(baseURL, action string, params map[string]interface{}) (retVal *ResponseResult, err error) { if params == nil { panic("params is nil!") } params2 := utils.Map2URLValues(params) - params2.Set("appkey", a.appKey) - params2.Set("timestamp", utils.Int64ToStr(utils.GetCurTimestamp())) - params2.Set("version", "1.0") - params2.Set(signKey, a.signParams(params2)) + if baseURL == mtpsAPIURL { + params2.Set("appkey", a.appKey) + params2.Set("timestamp", utils.Int64ToStr(utils.GetCurTimestamp())) + params2.Set("version", "1.0") + params2.Set(signKey, a.signParams(params2)) + } else if a.GetCookieCount() == 0 { + return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") + } // baseapi.SugarLogger.Debug(params2.Encode()) err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { - request, _ := http.NewRequest(http.MethodPost, mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode())) + request, _ := http.NewRequest(http.MethodPost, baseURL+"/"+action, strings.NewReader(params2.Encode())) + request.Header.Set("charset", "UTF-8") request.Header.Set("Content-Type", "application/x-www-form-urlencoded") + if baseURL != mtpsAPIURL { + a.FillRequestCookies(request) + } return request }, a.config, @@ -236,6 +246,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R return retVal, err } +func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *ResponseResult, err error) { + return a.AccessAPI2(mtpsAPIURL, action, params) +} + func (a *API) result2OrderResponse(result *ResponseResult) (order *OrderResponse) { order = new(OrderResponse) order.MtPeisongID = result.Data["mt_peisong_id"].(string) diff --git a/platformapi/mtpsapi/mtpsapi_test.go b/platformapi/mtpsapi/mtpsapi_test.go index df68e511..d053345c 100644 --- a/platformapi/mtpsapi/mtpsapi_test.go +++ b/platformapi/mtpsapi/mtpsapi_test.go @@ -24,6 +24,8 @@ func init() { api = New("25e816550bc9484480642f19a95f13fd", "r4$HqrKx9~=7?2Jfo,$Z~a7%~k!Au&pEdI2)oPJvSbH2ao@2N0[8wSIvtuumh_J^") // prod // api = New("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE") + + api.SetCookie("token", "0seqGSJnhbr4XJ0EaIQL6CoOpnaV1ErgS42uOlzNXYIX7PeuLuyCFQQZKKWGExJ7IMTQQQDe5H6YMmVFnxjCkw") } func handleError(t *testing.T, err error) { diff --git a/platformapi/mtpsapi/shop_page.go b/platformapi/mtpsapi/shop_page.go new file mode 100644 index 00000000..32691037 --- /dev/null +++ b/platformapi/mtpsapi/shop_page.go @@ -0,0 +1,17 @@ +package mtpsapi + +import "fmt" + +func (a *API) PagePoiUpdate(outerPoiID, contactName, contactPhone, contactEmail string) (err error) { + if outerPoiID == "" || contactName == "" || contactPhone == "" || contactEmail == "" { + return fmt.Errorf("所有参数必须都要有值") + } + params := map[string]interface{}{ + "outerPoiId": outerPoiID, + "contactName": contactName, + "contactPhone": contactPhone, + "contactEmail": contactEmail, + } + _, err = a.AccessAPI2("https://peisong.meituan.com/api", "haikuiopen/haikui/open/partner/poi/update", params) + return err +} diff --git a/platformapi/mtpsapi/shop_page_test.go b/platformapi/mtpsapi/shop_page_test.go new file mode 100644 index 00000000..42d608d8 --- /dev/null +++ b/platformapi/mtpsapi/shop_page_test.go @@ -0,0 +1,12 @@ +package mtpsapi + +import ( + "testing" +) + +func TestPagePoiUpdate(t *testing.T) { + err := api.PagePoiUpdate("100082", "王海红", "16601189298", "727827081@qq.com") + if err != nil { + t.Fatal(err) + } +} diff --git a/platformapi/mtwmapi/mtwmapi.go b/platformapi/mtwmapi/mtwmapi.go index ac2d2437..6bddaf05 100644 --- a/platformapi/mtwmapi/mtwmapi.go +++ b/platformapi/mtwmapi/mtwmapi.go @@ -52,6 +52,8 @@ const ( ) type API struct { + platformapi.APICookie + appID string secret string callbackURL string diff --git a/platformapi/mtwmapi/mtwmapi_test.go b/platformapi/mtwmapi/mtwmapi_test.go index 676e0f14..863ebcd0 100644 --- a/platformapi/mtwmapi/mtwmapi_test.go +++ b/platformapi/mtwmapi/mtwmapi_test.go @@ -1,8 +1,6 @@ package mtwmapi import ( - "net/url" - "strings" "testing" "git.rosy.net.cn/baseapi" @@ -26,50 +24,34 @@ func init() { // 果园 // api = New("4123", "df2c88338b85f830cebce2a9eab56628", "") - // api.SetUserCookie("_lx_utm", "utm_source%3D60066") - // api.SetUserCookie("_lxsdk", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") - // api.SetUserCookie("_lxsdk_cuid", "16b8c52943fc8-0f5c424b19a108-2d604637-3d10d-16b8c52943f24") - // api.SetUserCookie("_lxsdk_s", "16b8c529302-353-107-d17%7C%7C11") - // api.SetUserCookie("au_trace_key_net", "default") - // api.SetUserCookie("cssVersion", "b05eaa31") - // api.SetUserCookie("iuuid", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") - // api.SetUserCookie("mt_c_token", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") - // api.SetUserCookie("oops", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") - // api.SetUserCookie("openh5_uuid", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") - // api.SetUserCookie("showTopHeader", "show") - // api.SetUserCookie("token", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") - // api.SetUserCookie("w_token", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") - // api.SetUserCookie("userFace", "") - // api.SetUserCookie("userId", "69979334") - // api.SetUserCookie("userName", "thepool") - // api.SetUserCookie("uuid", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") - // api.SetUserCookie("wm_order_channel", "default") - // api.SetUserCookie("w_utmz", "utm_campaign=(direct)&utm_source=5000&utm_medium=(none)&utm_content=(none)&utm_term=(none)") - // api.SetUserCookie("w_visitid", "c2d0e4c9-3ab8-4163-b94f-a3dbfd9d7a94") - // api.SetUserCookie("w_actual_lng", "104076656") - // api.SetUserCookie("w_actual_lat", "30665696") - // api.SetUserCookie("w_latlng", "30702250,104052315") + // api.SetCookie("_lx_utm", "utm_source%3D60066") + // api.SetCookie("_lxsdk", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") + // api.SetCookie("_lxsdk_cuid", "16b8c52943fc8-0f5c424b19a108-2d604637-3d10d-16b8c52943f24") + // api.SetCookie("_lxsdk_s", "16b8c529302-353-107-d17%7C%7C11") + // api.SetCookie("au_trace_key_net", "default") + // api.SetCookie("cssVersion", "b05eaa31") + // api.SetCookie("iuuid", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") + // api.SetCookie("mt_c_token", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") + // api.SetCookie("oops", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") + // api.SetCookie("openh5_uuid", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") + // api.SetCookie("showTopHeader", "show") + // api.SetCookie("token", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") + // api.SetCookie("w_token", "2Q-NiXyFZ6UViMatiHdP86YpX2QAAAAAqAgAABf6QG4n-W8Zy_l_WLbUUKNmy238OkBp9Fx7rVcqZH6aod8hhhyB3JjfYkOv-7024A") + // api.SetCookie("userFace", "") + // api.SetCookie("userId", "69979334") + // api.SetCookie("userName", "thepool") + // api.SetCookie("uuid", "82B825F99C7098EE5254EB228DC2A863CE34008DFF4AD0913E8DC80D009AA95E") + // api.SetCookie("wm_order_channel", "default") + // api.SetCookie("w_utmz", "utm_campaign=(direct)&utm_source=5000&utm_medium=(none)&utm_content=(none)&utm_term=(none)") + // api.SetCookie("w_visitid", "c2d0e4c9-3ab8-4163-b94f-a3dbfd9d7a94") + // api.SetCookie("w_actual_lng", "104076656") + // api.SetCookie("w_actual_lat", "30665696") + // api.SetCookie("w_latlng", "30702250,104052315") cookieStr := ` _lx_utm=utm_source%3D60066; _lxsdk=6A074A0B834664A32E0735231E4FD9C4263666B70A7FEC663F9945110C52EFB3; _lxsdk_cuid=16b8ca9baf5c8-03bbc09a88c9e4-2d604637-3d10d-16b8ca9baf5c8; _lxsdk_s=16b8ca9b8f4-58-f63-4b4%7C18087777%7C19; au_trace_key_net=default; cssVersion=b05eaa31; iuuid=6A074A0B834664A32E0735231E4FD9C4263666B70A7FEC663F9945110C52EFB3; mt_c_token=jeZimbpuoKnPsfoHbT3_CR_w_W0AAAAApQgAAGU0Z9sTXiFzlp-8N8q-UWFdq5xSvYzWOjNhbNK0RGq9m6YaRNc7FtHjpkqFkJ_x2w; oops=jeZimbpuoKnPsfoHbT3_CR_w_W0AAAAApQgAAGU0Z9sTXiFzlp-8N8q-UWFdq5xSvYzWOjNhbNK0RGq9m6YaRNc7FtHjpkqFkJ_x2w; openh5_uuid=6A074A0B834664A32E0735231E4FD9C4263666B70A7FEC663F9945110C52EFB3; token=jeZimbpuoKnPsfoHbT3_CR_w_W0AAAAApQgAAGU0Z9sTXiFzlp-8N8q-UWFdq5xSvYzWOjNhbNK0RGq9m6YaRNc7FtHjpkqFkJ_x2w; userId=69979334; userName=thepool; uuid=6A074A0B834664A32E0735231E4FD9C4263666B70A7FEC663F9945110C52EFB3; wm_order_channel=default; terminal=i; w_utmz="utm_campaign=(direct)&utm_source=5000&utm_medium=(none)&utm_content=(none)&utm_term=(none)"; w_latlng=30694640,104057119; w_visitid=956b412d-c47c-4fec-ae99-7b7cb0c9ab45; w_token=jeZimbpuoKnPsfoHbT3_CR_w_W0AAAAApQgAAGU0Z9sTXiFzlp-8N8q-UWFdq5xSvYzWOjNhbNK0RGq9m6YaRNc7FtHjpkqFkJ_x2w ` - cookieList := strings.Split(cookieStr, ";") - for _, v := range cookieList { - index := strings.Index(v, "=") - pair := []string{ - v[:index], - v[index+1:], - } - pair[1], _ = url.QueryUnescape(pair[1]) - if strings.Index(pair[1], "\"") >= 0 { - pair[1] = url.QueryEscape(strings.Trim(utils.TrimBlankChar(pair[1]), "\"")) - } - // pair := strings.Split(v, "=") - if len(pair) > 1 { - baseapi.SugarLogger.Debug(pair[0], "=", pair[1]) - api.SetUserCookie(utils.TrimBlankChar(pair[0]), utils.TrimBlankChar(pair[1])) - } - } + api.SetCookieWithStr(cookieStr) } func TestAccessAPI(t *testing.T) { diff --git a/platformapi/mtwmapi/user_page.go b/platformapi/mtwmapi/user_page.go index cd6264fd..54e31b40 100644 --- a/platformapi/mtwmapi/user_page.go +++ b/platformapi/mtwmapi/user_page.go @@ -37,23 +37,8 @@ type ListShopItem struct { WmPoiScore int `json:"wmPoiScore"` } -func (a *API) SetUserCookie(key, value string) { - a.locker.Lock() - defer a.locker.Unlock() - a.userCookies[key] = value -} - -func (a *API) GetUserCookie(key string) string { - a.locker.RLock() - defer a.locker.RUnlock() - return a.userCookies[key] -} - func (a *API) AccessUserPage(subURL string, params map[string]interface{}) (retVal map[string]interface{}, err error) { - a.locker.RLock() - storeCookieLen := len(a.userCookies) - a.locker.RUnlock() - if storeCookieLen == 0 { + if a.GetCookieCount() == 0 { return nil, fmt.Errorf("需要设置User Cookie才能使用此方法") } err = platformapi.AccessPlatformAPIWithRetry(a.client, @@ -66,14 +51,7 @@ func (a *API) AccessUserPage(subURL string, params map[string]interface{}) (retV request.Header.Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1") request.Header.Set("Pragma", "no-cache") - a.locker.RLock() - for k, v := range a.userCookies { - request.AddCookie(&http.Cookie{ - Name: k, - Value: v, - }) - } - a.locker.RUnlock() + a.FillRequestCookies(request) return request }, a.config, diff --git a/platformapi/platformapi_cookie.go b/platformapi/platformapi_cookie.go new file mode 100644 index 00000000..90a7bd9b --- /dev/null +++ b/platformapi/platformapi_cookie.go @@ -0,0 +1,76 @@ +package platformapi + +import ( + "net/http" + "strings" + "sync" + + "git.rosy.net.cn/baseapi" + + "git.rosy.net.cn/baseapi/utils" +) + +type APICookie struct { + locker sync.RWMutex + storeCookies map[string]string +} + +func (a *APICookie) createMapIfNeeded() { + if a.storeCookies == nil { + a.storeCookies = make(map[string]string) + } +} + +func (a *APICookie) SetCookieWithStr(cookieStr string) { + cookieList := strings.Split(cookieStr, ";") + for _, v := range cookieList { + if index := strings.Index(v, "="); index > 0 { + pair := []string{ + v[:index], + v[index+1:], + } + // pair[1], _ = url.QueryUnescape(pair[1]) + // if strings.Index(pair[1], "\"") >= 0 { + // pair[1] = url.QueryEscape(strings.Trim(utils.TrimBlankChar(pair[1]), "\"")) + // } + k := utils.TrimBlankChar(pair[0]) + v := utils.TrimBlankChar(pair[1]) + if k != "" && v != "" { + a.SetCookie(k, v) + baseapi.SugarLogger.Debugf("%s=%s", k, v) + } + } + } +} + +func (a *APICookie) SetCookie(key, value string) { + a.locker.Lock() + defer a.locker.Unlock() + a.createMapIfNeeded() + a.storeCookies[key] = value +} + +func (a *APICookie) GetCookie(key string) string { + a.locker.RLock() + defer a.locker.RUnlock() + a.createMapIfNeeded() + return a.storeCookies[key] +} + +func (a *APICookie) GetCookieCount() int { + a.locker.RLock() + defer a.locker.RUnlock() + return len(a.storeCookies) +} + +func (a *APICookie) FillRequestCookies(r *http.Request) *http.Request { + a.locker.RLock() + defer a.locker.RUnlock() + for k, v := range a.storeCookies { + r.AddCookie(&http.Cookie{ + Name: k, + Value: v, + }) + } + return r +}