using AllBase; using AllBase.Helper; using AllBase.Model.Result; using AllBase.Model.Result.PDDResult; using NaiBa.Util; using NaiBa.Util.Extension; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Work; namespace TemuKJD.BLL.PddApi { public class PddApiHelper { public static AuthInfo RefreshOrderApi(AuthInfo _authInfo, bool isForceRefresh = false) { if (isForceRefresh || (_authInfo.UpdateTime <= DateTime.Now.AddDays(-1) && _authInfo.ExpiresTime <= DateTime.Now)) { string errStr = ""; var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == _authInfo.ShopId); if (shop == null) shop = new ShopLoginInfo() { ShopId = _authInfo.ShopId }; var authInfo = PddApiHelper.GetE57ShopPddToken(shop, out errStr); return authInfo; } return _authInfo; } public static void RefreshApi(ShopLoginInfo shop, bool isForceRefresh = false) { if (isForceRefresh || (shop.AuthUpdateTime <= DateTime.Now.AddDays(-1) && shop.AuthExpiresTime <= DateTime.Now)) { if (!GlobalConfig.PddClientInfoList.Any(c => c.client_id == PddApi.clientId_pi_xiu_guan_jia)) return; string errStr = ""; var authInfo = PddApiHelper.GetE57ShopPddToken(shop, out errStr); if (errStr.IsNullOrEmpty() || (shop.AuthUpdateTime <= DateTime.Now.AddDays(-1) && shop.AuthExpiresTime <= DateTime.Now)) { shop.ApiAuthInfoList.RemoveAll(a => a.AppKey.IsNullOrEmpty() || a.AppKey == authInfo.AppKey); shop.ApiAuthInfoList.Add(authInfo); } if (authInfo != null && !string.IsNullOrEmpty(authInfo.AccessToken)) PddApiHelper.SaveShopClientInfo(authInfo); } } public static void RefreshApi(List shops, bool isForceRefresh = false) { foreach (var shop in shops) { RefreshApi(shop, isForceRefresh); } } public static AuthInfo GetE57ShopPddToken(ShopLoginInfo shop, out string errorStr) { errorStr = ""; var url = BaseClass.BuildPDDObjString("shop/e57_get_pdd_shop_token"); var data = UserLogin.GetSignaturex(); data += "&mall_id=" + shop.MallId; var html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":0")) { errorStr = BaseRegex.GetStringJson(ref html, "msg"); if (string.IsNullOrEmpty(errorStr)) errorStr = html; } else { var token = new AuthInfo() { ShopId = shop.ShopId, UpdateTime = DateTime.Now, AppKey = PddApi.clientId_pi_xiu_guan_jia, AccessToken = BaseRegex.GetStringJson(ref html, "accessToken"), ExpiresTime = BaseClass.StampToDateTime(BaseRegex.GetIntJson(ref html, "tokenExpiresTime")), ApiAuthStatus = "授权成功", }; return token; } return new AuthInfo() { ShopId = shop.ShopId, UpdateTime = DateTime.Now, AppKey = PddApi.clientId_pi_xiu_guan_jia, AccessToken = "", ExpiresTime = DateTime.Now, ApiAuthStatus = "未授权", }; } public static void GetShopClientInfo(List shops = null) { if (BaseClass.MainPath == "29pdd") { ConcurrentList shopList = new ConcurrentList(); if (shops == null) shopList.AddRange(GlobalConfig.ShopLoginList); else shopList.AddRange(shops); while (shopList.Count > 0) { var shopTemp = shopList.Take(50).ToList(); var url = BaseClass.BuildPDDObjString("shop/get_shop_client_info"); var data = UserLogin.GetSignaturex(); data += "&shop_ids=" + string.Join(",", shopTemp.Select(s => s.ShopId).Distinct()); var html = GetHtml.PostUtf8(url, data); if (!html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data); if (html.Contains("\"code\":200")) { try { var dataStr = JsonHelper.SerializeObject(JsonHelper.JObjectParse(html)["data"]); var serverApiAuthList = JsonHelper.DeserializeJsonToList(dataStr); serverApiAuthList = serverApiAuthList.Where(a => GlobalConfig.PddClientInfoList.Any(c => c.client_id == a.AppKey)).ToList(); foreach (var serverAuth in serverApiAuthList) { var shop = shopList.FirstOrDefault(s => s.ShopId == serverAuth.ShopId); var auth = shop.ApiAuthInfoList.FirstOrDefault(a => a.AppKey == serverAuth.AppKey); if (auth == null) { auth = new AuthInfo(); shop.ApiAuthInfoList.Add(auth); } auth.ShopId = serverAuth.ShopId; auth.AccessToken = serverAuth.AccessToken; auth.RefreshToken = serverAuth.RefreshToken; auth.AppKey = serverAuth.AppKey; auth.ExpiresTime = serverAuth.ExpiresTime; auth.UpdateTime = serverAuth.UpdateTime; auth.ApiAuthStatus = serverAuth.ApiAuthStatus; auth.AppSecret = serverAuth.AppSecret; } } catch { } } shopList.RemoveAll(s => shopTemp.Contains(s)); } } } public static string SaveShopClientInfo(AuthInfo auth) { var clientUrl = BaseClass.BuildObjString("shop/add_mdy_shop_client_info"); var data = UserLogin.GetSignaturex() + "&ShopId=" + auth.ShopId + "&AccessToken=" + auth.AccessToken + "&RefreshToken=" + auth.RefreshToken + "&AppKey=" + auth.AppKey + "&ExpiresTime=" + auth.ExpiresTime.ToString("yyyy-MM-dd HH:mm:ss") + "&UpdateTime=" + auth.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss") + "&ApiAuthStatus=" + auth.ApiAuthStatus + "&AppSecret=" + auth.AppSecret; var result = GetHtml.PostUtf8(clientUrl, data); return result; } public static void GetPddClientInfo() { if (BaseClass.MainPath == "29pdd") { var url = BaseClass.BuildPDDObjString("shop/get_pdd_client_info"); var data = UserLogin.GetSignaturex(); data += "&password=" + BaseClass.PassWord; var html = GetHtml.PostUtf8(url, data); if (!html.Contains("\"code\":200")) html = GetHtml.PostUtf8(url, data); if (html.Contains("\"code\":200")) { try { var clientDataStr = JsonHelper.SerializeObject(JsonHelper.JObjectParse(html)["data"]); GlobalConfig.PddClientInfoList.Clear(); GlobalConfig.PddClientInfoList.AddRange(JsonHelper.DeserializeJsonToList(clientDataStr)); } catch { } } } } } }