using AllBase; using AllBase.Helper; using AllBase.Model.Result; 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 void RefreshApi(ShopLoginInfo shop, bool isForceRefresh = false) { if (isForceRefresh || (shop.AuthAddTime <= DateTime.Now.AddDays(-1) && shop.AuthExpiresTime <= DateTime.Now)) { string errStr = ""; var authInfo = PddApiHelper.GetE57ShopPddToken(shop, out errStr); if (errStr.IsNullOrEmpty() || (shop.AuthAddTime <= DateTime.Now.AddDays(-1) && shop.AuthExpiresTime <= DateTime.Now)) { shop.ApiAuthInfoList.RemoveAll(a => a.AppKey.IsNullOrEmpty() || a.AppKey == PddApi.clientId); shop.ApiAuthInfoList.Add(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.BuildObjString("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, AddTime = DateTime.Now, AppKey = PddApi.clientId, // BaseRegex.GetStringJson(ref html, "appKey").ParseToString(), AccessToken = BaseRegex.GetStringJson(ref html, "accessToken"), ExpiresTime = BaseClass.StampToDateTime(BaseRegex.GetIntJson(ref html, "tokenExpiresTime")), ApiAuthStatus = "授权成功", }; return token; } return new AuthInfo() { ShopId = shop.ShopId, AddTime = DateTime.Now, ExpiresTime = DateTime.Now }; } public static List> LstAuthInfos; public static void PddGetLocalTokens() { if (BaseClass.MainPath == "binxixi") { LstAuthInfos = NaiBa.Util.Helper.ConfigHelper.LoadConfig>>("pdd_shop_api_auth_info_lists"); if (LstAuthInfos == null) LstAuthInfos = new List>(); foreach (var shop in GlobalConfig.ShopLoginList) { shop.ApiAuthInfoList = LstAuthInfos.FirstOrDefault(l => l.FirstOrDefault()?.ShopId == shop.ShopId); if (shop.ApiAuthInfoList == null) shop.ApiAuthInfoList = new List(); } } } } }