using AllBase;
using AllBase.Helper;
using AllBase.Model.DBModel;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Work.BaseForms;
namespace Work._29Tools.BLL
{
public class ShopBLL
{
public static string AddServerShop(ShopLoginInfo _shop)
{
var url = BaseClass.Build29WxObjString("shop/add_shop");
var data = UserLogin.GetSignaturex()
+ "&login_name=" + _shop.LoginName
+ "&login_pass=" + GetHtml.UrlEncodeUtf8(_shop.Password)
+ "&shop_id=" + _shop.ShopId
+ "&shop_account=" + _shop.ShopAccount
//+ "&mall_id" + _shop.MallId
+ "&cookie=" + _shop.Cookies
//+ "&cookie2=" + _shop.Cookies2
+ "&group_name=" + GetHtml.UrlEncodeUtf8(_shop.GroupName)
+ "&sub_user_name=" + GetHtml.UrlEncodeUtf8(_shop.SubUserName)
+ "¬es=" + GetHtml.UrlEncodeUtf8(_shop.Notes)
+ "&login_status=" + _shop.LoginStatus;
//+ "&anti_contents=" + JsonHelper.SerializeObject(_shop.AntiContents);
var result = GetHtml.PostUtf8(url, data);
if (!result.Contains("\"code\":200") && !result.Contains("已达上限") && !result.Contains("已存在"))
result = GetHtml.PostUtf8(url, data);
if (!result.Contains("\"code\":200") && !result.Contains("已达上限") && !result.Contains("已存在"))
result = GetHtml.PostUtf8(url, data);
if (!result.Contains("\"code\":200"))
{
var msg = BaseRegex.GetStringJson(ref result, "msg");
if (string.IsNullOrEmpty(msg))
msg = "店铺保存失败!";
BaseMsgForm.ShowError(msg);
}
return result;
}
#region 服务端店铺Cookie管理
///
/// 添加店铺Cookies
///
/// 店铺名称
/// cookies
/// 工具名称(可不填写)
public static string UpdateServerShopCookies(ShopLoginInfo shop, string fromTool = null, bool isShowProgress = true)
{
var fTool = string.IsNullOrEmpty(fromTool) ? "" : "&from_tool=" + fromTool;
var url = BaseClass.Build29WxObjString("shop/mdy_shop_cookie_and_login_status");
var data = UserLogin.GetSignaturex()
+ "&login_name=" + shop.LoginName
+ "&login_status=" + shop.LoginStatus
+ "&cookie=" + shop.Cookies.Replace("&", "&").Replace("%", "%").Replace("+", "+")
+ "&openid=" + shop.openid
+ fTool;
var result = GetHtml.PostUtf8(url, data);
if (!result.Contains("\"code\":200"))
result = GetHtml.PostUtf8(url, data);
if (!result.Contains("\"code\":200"))
{
var errMsg = BaseRegex.GetStringJson(ref result, "msg");
if (isShowProgress)
BaseMsgForm.ShowError("服务端登录状态保存失败!请检查网络连接。" + errMsg);
else
return "服务端登录状态保存失败!请检查网络连接。" + errMsg;
}
return "";
}
public static List AdminGetServerShopLstByIds(List shop_ids)
{
List list = new List();
if (shop_ids == null)
return list;
shop_ids = shop_ids.Where(s => !string.IsNullOrEmpty(s)).ToList();
if (shop_ids.Count == 0)
return list;
var url = BaseClass.Build29WxObjString("shop/admin_get_shop_list");
var data = UserLogin.GetSignaturex() +
"&shop_ids='" + string.Join("','", shop_ids) + "'";
data += "&page_size=200";
var html = GetHtml.PostUtf8(url, data);
for (int i = 0; i < 5 && (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200")); i++)
{
html = GetHtml.PostUtf8(url, data);
System.Threading.Thread.Sleep(200);
}
if (string.IsNullOrEmpty(html)
|| !html.Contains("shop_id") || !html.Contains("shop_account") || !html.Contains("login_pass"))
{
return new List();
}
return ShopBLL.DeserializationShop(html);
}
#endregion
public static List DeserializationShop(string shopJson, string shopDataName = "data")
{
List list = new List();
if (!shopJson.Contains(shopDataName))
return list;
var jArr = JObject.Parse(shopJson)[shopDataName];
if (jArr.Count() == 0)
return list;
foreach (JObject jObj in jArr)
{
var shop = DeserializationShop(jObj);
if (shop != null)
list.Add(shop);
}
return list;
}
public static ShopLoginInfo DeserializationShop(JObject shopObj)
{
string shopStr = shopObj.ToString();
ShopLoginInfo shop = new ShopLoginInfo();
shop.DbId = BaseRegex.GetStringJson(ref shopStr, "id");
if (shop.DbId == null)
return null;
shop.LoginName = BaseRegex.GetStringJson(ref shopStr, "login_name");
shop.Password = BaseRegex.GetStringJson(ref shopStr, "login_pass");
shop.ShopId = BaseRegex.GetStringJson(ref shopStr, "shop_id");
shop.ShopAccount = BaseRegex.GetStringJson(ref shopStr, "shop_account");
shop.SubUserName = BaseRegex.GetStringJson(ref shopStr, "sub_user_name");
shop.GroupName = BaseRegex.GetStringJson(ref shopStr, "group_name");
shop.Cookies = shopObj["cookie"].ParseToString().Replace("&", "&").Replace("%", "%").Replace("+", "+").Replace(""", "\"");
shop.CompanyName = BaseRegex.GetStringJson(ref shopStr, "company_name");
shop.Notes = BaseRegex.GetStringJson(ref shopStr, "notes");
shop.LoginStatus = BaseRegex.GetStringJson(ref shopStr, "login_status");
shop.AddTime = BaseRegex.GetStringJson(ref shopStr, "add_time") == null ? DateTime.MinValue : Convert.ToDateTime(BaseRegex.GetStringJson(ref shopStr, "add_time"));
shop.is_point_shop_expire = BaseRegex.GetBoolJson(ref shopStr, "is_point_shop_expire");
shop.expire_time = BaseRegex.GetIntJson(ref shopStr, "expire_time");
shop.ErpShopId = BaseRegex.GetStringJson(ref shopStr, "erp_shop_id");
shop.ErpCorporateAccountUserId = BaseRegex.GetStringJson(ref shopStr, "erp_corporate_account_user_id");
shop.ErpManageUserId = BaseRegex.GetStringJson(ref shopStr, "erp_manage_user_id");
return shop;
}
#region 检测店铺登录
public static void CheckShopLogin(ShopLoginInfo shop, UserControls.StatusBarUserControl statusBar = null)
{
CheckShopLogin(new string[] { shop.ShopId }, statusBar);
}
public static void CheckShopLogin(ShopLoginInfo[] shops, UserControls.StatusBarUserControl statusBar = null)
{
CheckShopLogin(shops.Select(s => s.ShopId).ToArray(), statusBar);
}
public static void CheckShopLogin(string[] shopNames, UserControls.StatusBarUserControl statusBar = null)
{
for (int i = 0; i < shopNames.Length; i++)
{
var campanys = GlobalConfig.ShopLoginList.ToList().Where(v => v.LoginName == shopNames[i]).ToList();
var shop = GlobalConfig.ShopLoginList.ToList().FirstOrDefault(v => v.LoginName == shopNames[i]);
// 先从服务端取最新cookie
var serverCampanys = ShopBLL.AdminGetServerShopLstByIds(campanys.Select(c => c.ShopId).ToList());
//var shopInfoTmp = string.Join(",", serverCampanys.Select(s => s.UserName + s.ShopAccount));
var serverShop = serverCampanys.FirstOrDefault();
if (shop != null && serverShop != null)
{
shop.Cookies = serverShop.Cookies;
foreach (var sc in campanys)
{
sc.Cookies = serverShop.Cookies;
}
}
if (shop != null)
{
var loginResult = CheckShopLoginStatus(shop);
if (!loginResult.Contains("\"code\":0"))
{
shop.Cookies = "[]";
foreach (var sc in campanys)
{
sc.LoginStatus = "未登录";
shop.Cookies = "[]";
}
}
else
{
foreach (var sc in campanys)
{
sc.LoginStatus = "登录成功";
sc.LastRefreshDate = DateTime.Now.ToString("yyyyMMdd");
}
}
UpdateServerShopCookies(shop, null, statusBar == null ? false : true); // 店铺cookie写入服务端,多店共用一个登录账号一组cookie
}
if (statusBar != null)
{
statusBar.Invoke(new Action(() =>
{
statusBar.lblTaskStatus.Text = "正在检测...";
statusBar.pgbTaskProgress.Value = (i + 1);
statusBar.pgbTaskProgress.Text = (i + 1) + "/" + shopNames.Length;
}));
}
}
}
public static string GetShopInfo(ShopLoginInfo shop)
{
string url = "https://store.weixin.qq.com/shop/home";
string refere = "";
var result = HttpResponseWXBLL.WeiXinGetResponse(shop, url, refere, true);
return result;
}
public static string CheckShopLoginStatus(ShopLoginInfo shop, string refere = "https://store.weixin.qq.com/shop/home")
{
string url = "https://store.weixin.qq.com/shop-faas/mmecnodebase/reachDiolog/getBizNotifyConfig?token=&lang=zh_CN";
string body = @"{""path"":""https://store.weixin.qq.com/shop/home""}";
return HttpResponseWXBLL.WeiXinPostResponse(shop, url, body, refere);
}
#endregion
}
}