using Api.Framework.Cps;
using Api.Framework.Enums;
using Api.Framework.Model;
using CsharpHttpHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Api.Framework.Tools
{
///
/// 唯品会助手类
///
public class WPHHelper
{
///
/// 查找宝贝Id的正则表达式
///
//private static string RegItemId = @"product-\d+-(?<商品Id>\d+)";
private static List RegItemIds = new List() { @"(?i)(?:product|detail)-\d+-(?<商品Id>\d+)", @"(?i)vip.com.+?goodsId=(?<商品Id>\d+)", @"landing_pid=(?<商品Id>\d+)" };
///
/// 内容中获取唯品会商品id
///
///
///
private static string CheckRegexGetGoodsId(string mess)
{
try
{
try
{
mess = HttpHelper.URLDecode(HttpHelper.URLDecode(HttpHelper.URLDecode(mess)));
foreach (var item in RegItemIds)
{
var reg = Regex.Match(mess, item, RegexOptions.IgnoreCase);
if (reg.Success)
return reg.Groups["商品Id"].Value;
}
}
catch (Exception ex)
{ }
var urlTmps = HttpExtend.RegexMatchesUrl(mess);
foreach (var url in urlTmps)
{
var tmpUrl = FindLocationByUrl(url);
if (!string.IsNullOrWhiteSpace(tmpUrl))
{
tmpUrl = HttpHelper.URLDecode(HttpHelper.URLDecode(HttpHelper.URLDecode(tmpUrl)));
foreach (var item in RegItemIds)
{
var reg = Regex.Match(tmpUrl, item, RegexOptions.IgnoreCase);
if (reg.Success)
return reg.Groups["商品Id"].Value;
}
}
}
}
catch (Exception ex)
{ }
return string.Empty;
}
///
/// 获取商品id
///
/// 内容
///
public static string GetWphGoodsID(string mess)
{
try
{
var messTmp = HttpHelper.URLDecode(HttpHelper.URLDecode(HttpHelper.URLDecode(mess)));
if (messTmp.ToLower().Contains("vip.com") || messTmp.ToLower().Contains("m.vip") || messTmp.ToLower().Contains("vipglobal.hk"))
{
var itemId = CheckRegexGetGoodsId(messTmp);
if (!string.IsNullOrWhiteSpace(itemId))
return itemId;
}
//pages/index/index.html?$route=/pages/productDetail/productDetail?goodsId=6919530373160104087&brandId=1710613367&f=share_pd&nmsns=shop_android-7.62.2-weixin&nst=product&nsbc=&nct=card&ncid=075f0b5e-f292-3b6f-a846-7aab008970f9&nabtid=121&nuid=377785401&nchl_param=share:075f0b5e-f292-3b6f-a846-7aab008970f9:1642813783344&mars_cid_a=075f0b5e-f292-3b6f-a846-7aab008970f9&chl_type=share&nmsns=shop_android-7.62.2-weixin&nst=product&nsbc=&nct=card&ncid=075f0b5e-f292-3b6f-a846-7aab008970f9&nabtid=121&nuid=377785401&nchl_param=share:075f0b5e-f292-3b6f-a846-7aab008970f9:1642813783344&mars_cid_a=075f0b5e-f292-3b6f-a846-7aab008970f9&chl_type=share&tra_from=adp:C01V4m7y4lstf247::mig_code::x214d703d4124f5fbd3f8451e99b81f7
if (messTmp.ToLower().Contains("vipshop/") || messTmp.Contains("唯品") || messTmp.ToLower().Contains("productDetail/productDetail".ToLower()))
{
var reg = Regex.Match(messTmp, @"goodsId=(?<商品Id>\d+)", RegexOptions.IgnoreCase);
if (reg.Success)
{
return reg.Groups["商品Id"].Value;
}
}
}
catch (Exception ex)
{ }
return string.Empty;
}
///
/// 转链(根据商品id生成联盟链接) 参数参考:https://vop.vip.com/home#/api/method/detail/com.vip.adp.api.open.service.UnionUrlService-1.0.0/genByGoodsIdWithOauth
///
/// 参数
///
public WPHCpsUrl GetCpsUrl(WeipinhuiApi api, object data)
{
return api.SendWeipinhui("com.vip.adp.api.open.service.UnionUrlService-1.0.0#genByGoodsIdWithOauth", data);
}
///
/// 转链(根据唯品会链接生成联盟链接) 参数参考:https://vop.vip.com/home#/api/method/detail/com.vip.adp.api.open.service.UnionUrlService-1.0.0/genByVIPUrlWithOauth
///
/// 参数
///
public WPHCpsUrl GetCpsUrl_Hide(WeipinhuiApi api, object data)
{
return api.SendWeipinhui("com.vip.adp.api.open.service.UnionUrlService-1.0.0#genByVIPUrlWithOauth", data);
}
///
/// 获取链接真实地址
///
/// 链接
/// 开源
///
///
private static string FindLocationByUrl(string url, string referer = "", bool allowautoredirect = false)
{
try
{
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = url,
IsToLower = false,
Timeout = 5000,
ReadWriteTimeout = 30000,
UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER",
ContentType = "application/x-www-form-urlencoded",
Referer = referer,
Allowautoredirect = allowautoredirect
};
HttpResult result = http.GetHtml(item);
var ss = result.Header.Get("Location");
return ss;
}
catch (Exception)
{ }
return string.Empty;
}
///
/// 将订单明细对象转换成数据库唯品会fl_order_weipinhui对象
///
///
///
///
internal List OrderInfoListConvertDbWeipinhuiOrder(OrderInfoList order, List wphList)
{
try
{
if (order == null)
return wphList;
if (wphList == null)
{
#region 商品明细
if (order.detailList != null)
{
var wphs = new List();
foreach (var detail in order.detailList)
{
var wph = new fl_order_weipinhui();
#region 公共部分
wph.orderSn = order.orderSn; //订单号
wph.newCustomer = order.newCustomer;//新老客:0-待定,1-新客,2-老客 WeipinhuiNewCustomer
wph.channelTag = order.channelTag;//渠道商模式下表示自定义渠道标识;工具商模式下表示pid
wph.orderTime = order.orderTime;//下单时间 时间戳 单位毫秒
wph.signTime = order.signTime;//签收时间 时间戳 单位毫秒
wph.settledTime = order.settledTime;//结算时间 时间戳 单位毫秒
wph.lastUpdateTime = order.lastUpdateTime;//订单上次更新时间 时间戳 单位毫秒
wph.settled = order.settled;//订单结算状态 0-未结算,1-已结算 WeipinhuiOrderSettled
wph.selfBuy = order.selfBuy;//是否自推自买 0-否,1-是 WeipinhuiShiFou
wph.orderSubStatusName = Util.ConvertEnum(order.orderSubStatusName);//订单子状态:流转状态-支持状态:(已下单、已付款、已签收、待结算、已结算、已失效)
wph.afterSaleChangeCommission = order.afterSaleChangeCommission;//售后订单佣金变动:仅在订单完结之后发生售后行为时返回
wph.afterSaleChangeGoodsCount = order.afterSaleChangeGoodsCount;//售后订单总商品数量变动:仅在订单完结之后发生售后行为时返回
wph.commissionEnterTime = order.commissionEnterTime;//入账时间,时间戳,单位毫秒
wph.pid = order.pid;//推广PID:目前等同于channelTag
wph.isPrepay = order.isPrepay;//是否预付订单:0-否,1-是 WeipinhuiShiFou
wph.statParam = order.statParam; //自定义统计参数
wph.isSplit = order.isSplit;//订单拆单标识: 0-否,1-是 WeipinhuiShiFou
wph.parentSn = order.parentSn;//订单母单号:订单为拆单子单时返回
wph.orderTrackReason = order.orderTrackReason;//订单归因方式:0-常规推广,1-惊喜红包,2-锁粉,3-超级红包 WeipinhuiGuiYin
//wph.status = ;//订单状态:0-不合格,1-待定,2-已完结 WeipinhuiOrderStatus
//wph. string orderSource ;//订单来源
#endregion
//if (detail.goodsId == "6918315325800001858")
//{
//}
wph.goodsId = detail.goodsId;//商品id
wph.goodsName = detail.goodsName;//商品名称
wph.goodsThumb = detail.goodsThumb;//商品缩略图
wph.goodsCount = detail.goodsCount;//商品数量
wph.commissionTotalCost = double.Parse(detail.commissionTotalCost);//商品计佣金额(元,保留两位小数)
wph.commissionRate = Math.Round(double.Parse(detail.commissionRate) / 100.00, 2);//商品佣金比例(%)
wph.commission = double.Parse(detail.commission);//商品佣金金额(元,保留两位小数)
wph.commCode = detail.commCode;//佣金编码:对应商品二级分类
wph.commName = detail.commName;//佣金方案名称
wph.orderSource = detail.orderSource;//订单来源
wph.sizeId = detail.sizeId;//商品尺码:2019.01.01之后可用
wph.status = detail.status;//商品状态:0-不合格,1-待定,2-已完结
wph.brandStoreSn = detail.brandStoreSn;//品牌编号
wph.brandStoreName = detail.brandStoreName;//品牌名称
wph.spuId = detail.spuId;//商品spuId
wph.goodsFinalPrice = double.Parse(detail.goodsFinalPrice);//商品成交价(元,保留两位小数,商品成交价上线之后的订单才有该值)
#region 售后信息 无售后为空
if (detail.afterSaleInfo != null && detail.afterSaleInfo.Count != 0)
{
var afterSaleChangedCommission = string.IsNullOrWhiteSpace(detail.afterSaleInfo[0].afterSaleChangedCommission) ? 0 : double.Parse(detail.afterSaleInfo[0].afterSaleChangedCommission);
if (afterSaleChangedCommission != 0)
{
wph.afterSaleChangedCommission = afterSaleChangedCommission;//商品佣金售后变动:仅在订单完结之后发生售后时返回,无售后时为空
wph.afterSaleChangedGoodsCount = detail.afterSaleInfo[0].afterSaleChangedGoodsCount;//商品数量售后变动:仅在订单完结之后发生售后时返回,无售后时为空
wph.afterSaleSn = detail.afterSaleInfo[0].afterSaleSn;//商品售后单号,无售后时为空
wph.afterSaleStatus = detail.afterSaleInfo[0].afterSaleStatus;//商品售后状态:1-售后中,2-售后完成,3-售后取消,无售后时为空 WeipinhuiAfterSaleStatus
wph.afterSaleType = detail.afterSaleInfo[0].afterSaleType;//售后类型:1-退货,2-换货,无售后时为空 WeipinhuiSfterSaleType
wph.afterSaleFinishTime = detail.afterSaleInfo[0].afterSaleFinishTime;//售后完成时间,时间戳,单位:毫秒,无售后时为空
if (wph.afterSaleChangedGoodsCount != 0)
wph.orderSubStatusName = WeipinhuiOrderSubStatusName.维权中;
else
{
var db_order = ApiClient.GetSession().FindSingle("orderSn=@orderSn and goodsId=@goodsId and sizeId = @sizeId", new { orderSn = wph.orderSn, goodsId = wph.goodsId, sizeId = wph.sizeId });
if (db_order != null && db_order.db_status == SystemOrderStatus.订单结算) //跳过相同状态
{
wph.orderSubStatusName = WeipinhuiOrderSubStatusName.维权中;
}
else
{
wph.orderSubStatusName = WeipinhuiOrderSubStatusName.维权完成;
}
}
}
else
{
//退货 = 1
//换货 = 2
if (detail.afterSaleInfo[0].afterSaleType == WeipinhuiSfterSaleType.退货 && detail.afterSaleInfo[0].afterSaleStatus == WeipinhuiAfterSaleStatus.售后完成)
{
wph.afterSaleStatus = detail.afterSaleInfo[0].afterSaleStatus;
wph.orderSubStatusName = WeipinhuiOrderSubStatusName.已失效;
wph.afterSaleType = detail.afterSaleInfo[0].afterSaleType;
}
}
}
#endregion
wphs.Add(wph);
}
return wphs;
}
#endregion
}
}
catch (Exception ex)
{
EventClient.OnEvent("将订单明细对象转换成数据库唯品会fl_order_weipinhui对象异常:", ex.Message + " - " + ex.StackTrace);
}
return null;
}
}
#region 返回的数据结构
#region 转链后返回的数据
///
/// 链接数据
///
public class UrlInfoList
{
///
/// 链接生成的数据源: 如果根据商品id生成链接,该值商品id, 如果根据链接生成链接,该值为唯品会链接
///
public string source { get; set; }
///
/// CPS短链接
///
public string url { get; set; }
///
/// CPS长连接
///
public string longUrl { get; set; }
///
/// CPS通用连接
///
public string ulUrl { get; set; }
///
/// CPS Deeplink链接
///
public string deeplinkUrl { get; set; }
///
/// 小程序CPS参数:通用小程序跟单参数
///
public string traFrom { get; set; }
///
/// CPS短链接:不唤起快应用
///
public string noEvokeUrl { get; set; }
///
/// CPS长链接:不唤起快应用
///
public string noEvokeLongUrl { get; set; }
///
/// 唯品会小程序链接:仅在根据商品id获取时返回
///
public string vipWxUrl { get; set; }
///
/// 唯品会小程序码:仅在根据商品id获取时返回,需获取小程序码高级权限
///
public string vipWxCode { get; set; }
///
/// 唯品会快应用链接
///
public string vipQuickAppUrl { get; set; }
}
public class Result1
{
///
/// 链接数据
///
public List urlInfoList { get; set; }
}
///
/// 转链后返回的数据解构
///
public class WPHCpsUrl
{
public string returnCode { get; set; }
public Result1 result { get; set; }
}
#endregion
#region 订单下载数据解构
///
/// 商品明细
///
public class DetailList
{
///
/// 商品id
///
public string goodsId { get; set; }
///
/// 商品名称
///
public string goodsName { get; set; }
///
/// 商品缩略图
///
public string goodsThumb { get; set; }
///
/// 商品数量
///
public int goodsCount { get; set; }
///
/// 商品计佣金额(元,保留两位小数)
///
public string commissionTotalCost { get; set; }
///
/// 商品佣金比例(%)
///
public string commissionRate { get; set; }
///
/// 商品佣金金额(元,保留两位小数)
///
public string commission { get; set; }
///
/// 佣金编码:对应商品二级分类
///
public string commCode { get; set; }
///
/// 佣金方案名称
///
public string commName { get; set; }
///
/// 订单来源
///
public string orderSource { get; set; }
///
/// 商品售后信息
///
public List afterSaleInfo { get; set; }
///
/// 商品尺码:2019.01.01之后可用
///
public string sizeId { get; set; }
///
/// 商品状态:0-不合格,1-待定,2-已完结
///
public WeipinhuiOrderStatus status { get; set; }
///
/// 品牌编号
///
public string brandStoreSn { get; set; }
///
/// 品牌名称
///
public string brandStoreName { get; set; }
///
/// 商品spuId
///
public string spuId { get; set; }
///
/// 商品成交价(元,保留两位小数,商品成交价上线之后的订单才有该值)
///
public string goodsFinalPrice { get; set; }
}
///
/// 商品售后信息
///
public class AfterSaleInfo
{
///
/// 商品佣金售后变动:仅在订单完结之后发生售后时返回,无售后时为空
///
public string afterSaleChangedCommission { get; set; }
///
/// 商品数量售后变动:仅在订单完结之后发生售后时返回,无售后时为空
///
public int afterSaleChangedGoodsCount { get; set; }
///
/// 商品售后单号,无售后时为空
///
public string afterSaleSn { get; set; }
///
/// 商品售后状态:1-售后中,2-售后完成,3-售后取消,无售后时为空
///
public WeipinhuiAfterSaleStatus afterSaleStatus { get; set; }
///
/// 售后类型:1-退货,2-换货,无售后时为空
///
public WeipinhuiSfterSaleType afterSaleType { get; set; }
///
/// 售后完成时间,时间戳,单位:毫秒,无售后时为空
///
public long afterSaleFinishTime { get; set; }
}
///
/// 业绩查询响应结果
///
public class OrderInfoList
{
///
/// 订单号
///
public string orderSn { get; set; }
///
/// 订单状态:0-不合格,1-待定,2-已完结
///
public WeipinhuiOrderStatus status { get; set; }
///
/// 新老客:0-待定,1-新客,2-老客
///
public WeipinhuiNewCustomer newCustomer { get; set; }
///
/// 渠道商模式下表示自定义渠道标识;工具商模式下表示pid
///
public string channelTag { get; set; }
///
/// 下单时间 时间戳 单位毫秒
///
public long orderTime { get; set; }
///
/// 签收时间 时间戳 单位毫秒
///
public long signTime { get; set; }
///
/// 结算时间 时间戳 单位毫秒
///
public long settledTime { get; set; }
///
/// 商品明细
///
public List detailList { get; set; }
///
/// 订单上次更新时间 时间戳 单位毫秒
///
public long lastUpdateTime { get; set; }
///
/// 订单结算状态 0-未结算,1-已结算
///
public WeipinhuiOrderSettled settled { get; set; }
///
/// 是否自推自买 0-否,1-是
///
public WeipinhuiShiFou selfBuy { get; set; }
///
/// 订单子状态:流转状态-支持状态:(已下单、已付款、已签收、待结算、已结算、已失效)
///
public string orderSubStatusName { get; set; }
///
/// 商品总佣金:单位元
///
public string commission { get; set; }
///
/// 售后订单佣金变动:仅在订单完结之后发生售后行为时返回
///
public string afterSaleChangeCommission { get; set; }
///
/// 售后订单总商品数量变动:仅在订单完结之后发生售后行为时返回
///
public string afterSaleChangeGoodsCount { get; set; }
///
/// 入账时间,时间戳,单位毫秒
///
public long commissionEnterTime { get; set; }
///
/// 订单来源
///
public string orderSource { get; set; }
///
/// 推广PID:目前等同于channelTag
///
public string pid { get; set; }
///
/// 是否预付订单:0-否,1-是
///
public WeipinhuiShiFou isPrepay { get; set; }
///
/// 自定义统计参数
///
public string statParam { get; set; }
///
/// 订单拆单标识: 0-否,1-是
///
public WeipinhuiShiFou isSplit { get; set; }
///
/// 订单母单号:订单为拆单子单时返回
///
public string parentSn { get; set; }
///
/// 订单归因方式:0-常规推广,1-惊喜红包,2-锁粉,3-超级红包
///
public WeipinhuiGuiYin orderTrackReason { get; set; }
///
/// 开发者调用的appKey:当订单是通过开发者API推广成单时返回,其余时候返回为空
///
public string appKey { get; set; }
///
/// 订单支付金额:单位元
///
public string totalCost { get; set; }
}
///
/// 订单数据
///
public class OrderResult
{
///
/// 业绩总条数
///
public int total { get; set; }
///
/// 当前页码
///
public int pageSize { get; set; }
///
/// 页面大小
///
public int page { get; set; }
///
/// 业绩查询响应结果
///
public List orderInfoList { get; set; }
}
///
/// 订单响应结果
///
public class OrderResponseResult
{
///
/// 返回代码
///
public string returnCode { get; set; }
///
/// 返回数据
///
public OrderResult result { get; set; }
}
#endregion
#region 唯品会在推商品数据结构
/////
///// 店铺信息
/////
//public class StoreInfo
//{
// ///
// /// 店铺名字
// ///
// public string storeName { get; set; }
// ///
// /// 店铺Id
// ///
// public string storeId { get; set; }
//}
///
/// 红包信息
///
public class CouponInfo
{
///
/// 优惠券批次号
///
public string couponNo { get; set; }
///
/// 优惠劵名称
///
public string couponName { get; set; }
///
/// 使用门槛
///
public string buy { get; set; }
///
/// 优惠金额
///
public string fav { get; set; }
///
/// 券激活开始时间,毫秒级时间戳
///
public long activateBeginTime { get; set; }
///
/// 券激活结束时间,毫秒级时间戳
///
public long activateEndTime { get; set; }
///
/// 使用开始时间,毫秒级时间戳
///
public long useBeginTime { get; set; }
///
/// 使用结束时间,毫秒级时间戳
///
public long useEndTime { get; set; }
///
/// 生成劵的总量
///
public long totalAmount { get; set; }
///
/// 劵已激活的数量
///
public int activedAmount { get; set; }
}
///
/// 精选商品列表
///
public class GoodsInfoList
{
///
/// 商品id
///
public string goodsId { get; set; }
///
/// 商品名称
///
public string goodsName { get; set; }
///
/// 商品描述,字段暂不输出
///
public string goodsDesc { get; set; }
///
/// 商品落地页
///
public string destUrl { get; set; }
///
/// 商品缩略图
///
public string goodsThumbUrl { get; set; }
///
/// 商品轮播图:根据商品id查询时返回,商品列表不返回
///
public List goodsCarouselPictures { get; set; }
///
/// 商品主图
///
public string goodsMainPicture { get; set; }
///
/// 商品三级分类id
///
public int categoryId { get; set; }
///
/// 商品三级分类
///
public string categoryName { get; set; }
///
/// 商品类型:0-自营,1-MP
///
public int sourceType { get; set; }
///
/// 市场价(元)
///
public string marketPrice { get; set; }
///
/// 唯品价(元)
///
public string vipPrice { get; set; }
///
/// 佣金比例(%)
///
public string commissionRate { get; set; }
///
/// 佣金金额(元)
///
public string commission { get; set; }
///
/// 折扣:唯品价/市场价 保留两位小数字符串
///
public string discount { get; set; }
///
/// 商品详情图片:根据商品id查询商品信息时返回,商品列表不返回
///
public List goodsDetailPictures { get; set; }
///
/// 商品一级分类id
///
public int cat1stId { get; set; }
///
/// 商品一级分类名称
///
public string cat1stName { get; set; }
///
/// 商品二级分类id
///
public int cat2ndId { get; set; }
///
/// 商品二级分类名称
///
public string cat2ndName { get; set; }
///
/// 商品品牌sn
///
public string brandStoreSn { get; set; }
///
/// 商品品牌名称
///
public string brandName { get; set; }
///
/// 商品品牌logo全路径地址
///
public string brandLogoFull { get; set; }
///
/// 商品推广计划有效期预估截止时间:仅为预估时间,仅做参考;时间戳,单位:毫秒
///
public long schemeEndTime { get; set; }
///
/// 商品售卖开始时间,时间戳,单位毫秒
///
public long sellTimeFrom { get; set; }
///
/// 商品售卖结束时间,时间戳, 单位毫秒
///
public long sellTimeTo { get; set; }
///
/// 推广权重,用于确定推广该商品的优先级,权重值越大,优先级越高
///
public long weight { get; set; }
///
/// 店铺信息
///
public StoreInfo storeInfo { get; set; }
///
/// 商品所属档期(专场)id
///
public int brandId { get; set; }
///
/// 商品所属推广方案开始时间:时间戳,单位:毫秒
///
public long schemeStartTime { get; set; }
///
/// 商品状态:0-下架,1-上架
///
public int status { get; set; }
///
/// 红包信息
///
public CouponInfo couponInfo { get; set; }
///
/// 是否海淘商品标识:1是 0不是
///
public int haiTao { get; set; }
///
/// 商品spuId
///
public string spuId { get; set; }
}
///
/// 精选商品列表
///
public class GoodsInfoResult
{
///
/// 总数
///
public int total { get; set; }
///
/// 精选商品列表
///
public List goodsInfoList { get; set; }
///
/// 分页大小
///
public int pageSize { get; set; }
///
/// 支持的排序字段,为空时仅支持默认排序
///
public List