/// <summary> /// 发布商品 /// taobao.item.add 添加一个商品 /// </summary> /// <param name="product">商品</param> /// <returns>商品编号</returns> public Item PublishGoods(Product product) { _log.LogInfo(Resource.Log_PublishGoodsing.StringFormat(product.Title)); var req = new ItemAddRequest(); Util.CopyModel(product, req); var tContext = InstanceLocator.Current.GetInstance <AuthorizedContext>(); ItemAddResponse response = _client.Execute(req, tContext.SessionKey); if (response.IsError) { var ex = new TopResponseException(response.ErrCode, response.ErrMsg, response.SubErrCode, response.SubErrMsg, response.TopForbiddenFields); _log.LogError(Resource.Log_PublishGoodsFailure, product.Title, ex); throw ex; } Item item = response.Item; _log.LogInfo(Resource.Log_PublishGoodsSuccess, product.Title, item.NumIid); return(item); }
List <TbImage> GetProductsImgs(DataRow row, ItemAddResponse response) { List <TbImage> list = new List <TbImage>(); if (row["ImageUrl2"] != DBNull.Value) { TbImage item = new TbImage(); item.TbProductId = response.Item.NumIid; item.Imgpath = (string)row["ImageUrl2"]; list.Add(item); } if (row["ImageUrl3"] != DBNull.Value) { TbImage image2 = new TbImage(); image2.TbProductId = response.Item.NumIid; image2.Imgpath = (string)row["ImageUrl3"]; list.Add(image2); } if (row["ImageUrl4"] != DBNull.Value) { TbImage image3 = new TbImage(); image3.TbProductId = response.Item.NumIid; image3.Imgpath = (string)row["ImageUrl4"]; list.Add(image3); } if (row["ImageUrl5"] != DBNull.Value) { TbImage image4 = new TbImage(); image4.TbProductId = response.Item.NumIid; image4.Imgpath = (string)row["ImageUrl5"]; list.Add(image4); } return(list); }
public void ProcessRequest(HttpContext context) { string appkey = context.Request.Form["appkey"]; string appsecret = context.Request.Form["appsecret"]; string sessionkey = context.Request.Form["sessionkey"]; string productIds = context.Request.Form["productIds"]; string approve_status = context.Request.Form["approve_status"]; string morepic = context.Request.Form["morepic"]; string repub = context.Request.Form["repub"]; string chkdesc = context.Request.Form["chkdesc"]; string chknormal = context.Request.Form["chknormal"]; string chktitle = context.Request.Form["chktitle"]; if (!string.IsNullOrEmpty(appkey) && !string.IsNullOrEmpty(appsecret)) { this.client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", appkey, appsecret, "json"); } DataTable taobaoProducts = SubSiteProducthelper.GetTaobaoProducts(productIds); if (null != taobaoProducts && taobaoProducts.Rows.Count > 0) { Dictionary <int, long> taobaoReturnProductIds = new Dictionary <int, long>(); StringBuilder builder = new StringBuilder(); string pname = ""; int num = 0; string imgurl = ""; int stock = 0; decimal markprice = 0M; string issuccess = "true"; string msg = ""; string imgmsg = ""; string proTitle = "";// (string)row["ProTitle"]; foreach (DataRow row in taobaoProducts.Rows) { proTitle = (string)row["ProTitle"]; ResponseData(row, out imgurl, out stock, out markprice); if ((row["taobaoproductid"] != DBNull.Value) && (repub.ToLower() == "true")) { ItemUpdateRequest req = new ItemUpdateRequest(); req.NumIid = new long?(Convert.ToInt64(row["taobaoproductid"])); req.ApproveStatus = approve_status; if (!string.IsNullOrEmpty(chknormal) && (chknormal.ToLower() == "true")) { this.SetTaoBaoUpdateData(req, row); } if (!string.IsNullOrEmpty(chktitle) && (chktitle.ToLower() == "true")) { req.Title = (row["ProTitle"] == DBNull.Value) ? "请修改商品标题" : ((string)row["ProTitle"]); } if (!string.IsNullOrEmpty(chkdesc) && (chkdesc.ToLower() == "true")) { req.Desc = (row["Description"] == DBNull.Value) ? "暂无该商品的描述信息" : ((string)row["Description"]); } ItemUpdateResponse response = client.Execute <ItemUpdateResponse>(req, sessionkey); if (response.IsError) { num = (int)row["ProductId"]; pname = string.Format("<a href='{0}' target=_blank>{1} </a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { num }), proTitle); imgurl = string.Format("<a href='{0}' target=_blank><img src={1} /></a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { num }), imgurl); msg = string.Format("商品更新失败<br/>({0})", ":" + response.ErrMsg + ":" + response.SubErrMsg); issuccess = "false"; } else { imgurl = string.Format("<a href='http://item.taobao.com/item.htm?id={0}' target=_blank><img src={1} /></a>", response.Item.NumIid, imgurl); pname = string.Format("<a href='http://item.taobao.com/item.htm?id={0}'>{1}</a>", response.Item.NumIid, proTitle); msg = "商品更新成功"; issuccess = "true"; taobaoReturnProductIds.Add((int)row["ProductId"], response.Item.NumIid); } } else { ItemAddRequest req = new ItemAddRequest(); req.ApproveStatus = approve_status; SetTaoBaoAddData(req, row); ItemAddResponse response2 = client.Execute <ItemAddResponse>(req, sessionkey); if (response2.IsError) { num = (int)row["ProductId"]; pname = string.Format("<a href='{0}' target=_blank>{1} </a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { num }), proTitle); imgurl = string.Format("<a href='{0}' target=_blank><img src={1} /></a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { num }), imgurl); msg = string.Format("发布失败<br/>({0})", response2.ErrMsg + ":" + response2.SubErrMsg); issuccess = "false"; } else { imgurl = string.Format("<a href='http://item.taobao.com/item.htm?id={0}' target=_blank><img src={1} /></a>", response2.Item.NumIid, imgurl); pname = string.Format("<a href='http://item.taobao.com/item.htm?id={0}'>{1}</a>", response2.Item.NumIid, proTitle); msg = "商品发布成功"; issuccess = "true"; taobaoReturnProductIds.Add((int)row["ProductId"], response2.Item.NumIid); if (morepic == "true") { List <TbImage> productsImgs = this.GetProductsImgs(row, response2); StringBuilder builder2 = new StringBuilder(); foreach (TbImage image in productsImgs) { string path = Globals.ApplicationPath + image.Imgpath; if (File.Exists(Globals.MapPath(path))) { FileItem item = new FileItem(Globals.MapPath(path)); ItemImgUploadRequest request = new ItemImgUploadRequest(); request.Image = item; request.NumIid = new long?(image.TbProductId); request.IsMajor = false; ItemImgUploadResponse itemImgUploadResponse = this.client.Execute <ItemImgUploadResponse>(request, sessionkey); if (itemImgUploadResponse.IsError) { builder2.AppendFormat("[\"{0}发布图片错误,错误原因:{1}\"],", proTitle, itemImgUploadResponse.ErrMsg + ";" + itemImgUploadResponse.SubErrMsg); } } } if (builder2.Length > 0) { imgmsg = builder2.ToString().Substring(0, builder2.ToString().Length - 1); } } } } builder.Append(string.Concat(new object[] { "{\"pname\":\"", pname, "\",\"pimg\":\"", imgurl, "\",\"pmarkprice\":\"", markprice.ToString("F2"), "\",\"pstock\":\"", stock, "\",\"issuccess\":\"", issuccess, "\",\"msg\":\"", msg, "\",\"imgmsg\":[", imgmsg, "]}," })); } if (taobaoReturnProductIds.Count > 0) { SubSiteProducthelper.AddTaobaoReturnProductIds(taobaoReturnProductIds, 0); } if (builder.ToString().Length > 0) { builder.Remove(builder.Length - 1, 1); } context.Response.Write("{\"Status\":\"OK\",\"Result\":[" + builder.ToString() + "]}"); context.Response.Flush(); context.Response.End(); } else { context.Response.Write("{\"Status\":\"Error\",\"Result\":\"发布商品到淘宝出错!\"}"); context.Response.Flush(); context.Response.End(); } }
/// <summary> /// 添加商品数据 /// </summary> /// <param name="sessionKey"></param> /// <param name="goods"></param> /// <param name="ErrorMsg"></param> /// <returns></returns> internal string AddGoods(string sessionKey, ItemAdd goods, out string ErrorMsg) { ErrorMsg = null; ITopClient client = new DefaultTopClient(StaticSystemConfig.soft.ApiURL, StaticSystemConfig.soft.AppKey, StaticSystemConfig.soft.AppSecret, "json"); ItemAddRequest req = new ItemAddRequest(); #region 将页面数据填充到request中 if (goods.Type != null) { req.Type = goods.Type; } else { ErrorMsg += "发布商品类型不能为空;"; } if (goods.Cid != null) { req.Cid = goods.Cid; } else { ErrorMsg += "商品类目不能为空;"; return(null); } if (goods.Props != null) { req.Props = goods.Props; } if (goods.Num != null) { req.Num = goods.Num; } else { ErrorMsg += "商品数量不能为空;"; return(null); } if (goods.StuffStatus != null) { req.StuffStatus = goods.StuffStatus; } else { ErrorMsg += "商品新旧程度不能为空;"; return(null); } if (goods.Price != null) { req.Price = goods.Price; } else { ErrorMsg += "商品价格不能为空;"; return(null); } if (goods.Title != null) { req.Title = goods.Title; } else { ErrorMsg += "商品标题不能为空;"; return(null); } if (goods.Desc != null) { req.Desc = goods.Desc; } else { ErrorMsg += "商品描述不能为空;"; return(null); } if (goods.LocationState != null) { req.LocationState = goods.LocationState; } else { ErrorMsg += "商品所在地省份不能为空;"; return(null); } if (goods.LocationCity != null) { req.LocationCity = goods.LocationCity; } else { ErrorMsg += "商品所在地城市不能为空;"; return(null); } if (goods.PostFee != null) { req.PostFee = goods.PostFee; } if (goods.ExpressFee != null) { req.ExpressFee = goods.ExpressFee; } if (goods.EmsFee != null) { req.EmsFee = goods.EmsFee; } if (goods.ListTime != null) { req.ListTime = goods.ListTime; } if (goods.Cid != null) { req.Increment = goods.Increment; } if (goods.Image != null) { //括号中中填文件路径 FileItem fItem = new FileItem(goods.Image[0].ToString()); req.Image = fItem; } if (goods.StuffStatus != null) { req.StuffStatus = goods.StuffStatus; } if (goods.AuctionPoint != null) { req.AuctionPoint = goods.AuctionPoint; } if (goods.PropertyAlias != null) { req.PropertyAlias = goods.PropertyAlias; } if (goods.InputPids != null) { req.InputPids = goods.InputPids; } if (goods.SkuQuantities != null) { req.SkuQuantities = goods.SkuQuantities; } if (goods.SkuPrices != null) { req.SkuPrices = goods.SkuPrices; } if (goods.SkuProperties != null) { req.SkuProperties = "pid:vid;pid:vid"; } if (goods.SellerCids != null) { req.SellerCids = goods.SellerCids; } if (goods.PostageId != null) { req.PostageId = goods.PostageId; } if (goods.OuterId != null) { req.OuterId = goods.OuterId; } if (goods.ProductId != null) { req.ProductId = goods.ProductId; } if (goods.PicPath != null) { req.PicPath = goods.PicPath; } if (goods.AutoFill != null) { req.AutoFill = goods.AutoFill; } if (goods.SkuOuterIds != null) { req.SkuOuterIds = goods.SkuOuterIds; } if (goods.IsTaobao != null) { req.IsTaobao = goods.IsTaobao; } if (goods.IsEx != null) { req.IsEx = goods.IsEx; } if (goods.Is3D != null) { req.Is3D = goods.Is3D; } if (goods.InputStr != null) { req.InputStr = goods.InputStr; } if (goods.Lang != null) { req.Lang = goods.Lang; } if (goods.HasDiscount != null) { req.HasDiscount = goods.HasDiscount; } if (goods.HasShowcase != null) { req.HasShowcase = goods.HasShowcase; } if (goods.ApproveStatus != null) { req.ApproveStatus = goods.ApproveStatus; } if (goods.FreightPayer != null) { req.FreightPayer = goods.FreightPayer; } if (goods.ValidThru != null) { req.ValidThru = goods.ValidThru; } if (goods.HasInvoice != null) { req.HasInvoice = goods.HasInvoice; } if (goods.HasWarranty != null) { req.HasWarranty = goods.HasWarranty; } if (goods.AfterSaleId != null) { req.AfterSaleId = goods.AfterSaleId; } if (goods.SellPromise != null) { req.SellPromise = goods.SellPromise; } if (goods.CodPostageId != null) { req.CodPostageId = goods.CodPostageId; } if (goods.IsLightningConsignment != null) { req.IsLightningConsignment = goods.IsLightningConsignment; } if (goods.Weight != null) { req.Weight = goods.Weight; } if (goods.IsXinpin != null) { req.IsXinpin = goods.IsXinpin; } if (goods.SubStock != null) { req.SubStock = goods.SubStock; } if (goods.FoodSecurityPrdLicenseNo != null) { req.FoodSecurityPrdLicenseNo = goods.FoodSecurityPrdLicenseNo; } if (goods.FoodSecurityDesignCode != null) { req.FoodSecurityDesignCode = goods.FoodSecurityDesignCode; } if (goods.FoodSecurityFactory != null) { req.FoodSecurityFactory = goods.FoodSecurityFactory; } if (goods.FoodSecurityFactorySite != null) { req.FoodSecurityFactorySite = goods.FoodSecurityFactorySite; } if (goods.FoodSecurityContact != null) { req.FoodSecurityContact = goods.FoodSecurityContact; } if (goods.FoodSecurityMix != null) { req.FoodSecurityMix = goods.FoodSecurityMix; } if (goods.FoodSecurityPlanStorage != null) { req.FoodSecurityPlanStorage = goods.FoodSecurityPlanStorage; } if (goods.FoodSecurityPeriod != null) { req.FoodSecurityPeriod = goods.FoodSecurityPeriod; } if (goods.FoodSecurityFoodAdditive != null) { req.FoodSecurityFoodAdditive = goods.FoodSecurityFoodAdditive; } if (goods.FoodSecuritySupplier != null) { req.FoodSecuritySupplier = goods.FoodSecuritySupplier; } if (goods.FoodSecurityProductDateStart != null) { req.FoodSecurityProductDateStart = goods.FoodSecurityProductDateStart; } if (goods.FoodSecurityProductDateEnd != null) { req.FoodSecurityProductDateEnd = goods.FoodSecurityProductDateEnd; } if (goods.FoodSecurityStockDateStart != null) { req.FoodSecurityStockDateStart = goods.FoodSecurityStockDateStart; } if (goods.FoodSecurityStockDateEnd != null) { req.FoodSecurityStockDateEnd = goods.FoodSecurityStockDateEnd; } if (goods.SkuSpecIds != null) { req.SkuSpecIds = goods.SkuSpecIds; } if (goods.ItemSize != null) { req.ItemSize = goods.ItemSize; } if (goods.ItemWeight != null) { req.ItemWeight = goods.ItemWeight; } if (goods.ChangeProp != null) { req.ChangeProp = goods.ChangeProp; } if (goods.SellPoint != null) { req.SellPoint = goods.SellPoint; } if (goods.DescModules != null) { req.DescModules = goods.DescModules; } if (goods.FoodSecurityHealthProductNo != null) { req.FoodSecurityHealthProductNo = goods.FoodSecurityHealthProductNo; } if (goods.Barcode != null) { req.Barcode = goods.Barcode; } if (goods.SkuBarcode != null) { req.SkuBarcode = goods.SkuBarcode; } if (goods.Newprepay != null) { req.Newprepay = goods.Newprepay; } if (goods.IsOffline != null) { req.IsOffline = goods.IsOffline; } if (goods.SkuHdLength != null) { req.SkuHdLength = goods.SkuHdLength; } if (goods.SkuHdHeight != null) { req.SkuHdHeight = goods.SkuHdHeight; } if (goods.SkuHdLampQuantity != null) { req.SkuHdLampQuantity = goods.SkuHdLampQuantity; } if (goods.Qualification != null) { req.Qualification = goods.Qualification; } if (goods.O2oBindService != null) { req.O2oBindService = goods.O2oBindService; } if (goods.LocalityLifeExpirydate != null) { req.LocalityLifeExpirydate = goods.LocalityLifeExpirydate; } if (goods.LocalityLifeNetworkId != null) { req.LocalityLifeNetworkId = goods.LocalityLifeNetworkId; } if (goods.LocalityLifeMerchant != null) { req.LocalityLifeMerchant = goods.LocalityLifeMerchant; } if (goods.LocalityLifeVerification != null) { req.LocalityLifeVerification = goods.LocalityLifeVerification; } if (goods.LocalityLifeRefundRatio != null) { req.LocalityLifeRefundRatio = goods.LocalityLifeRefundRatio; } if (goods.LocalityLifeChooseLogis != null) { req.LocalityLifeChooseLogis = goods.LocalityLifeChooseLogis; } if (goods.LocalityLifeOnsaleAutoRefundRatio != null) { req.LocalityLifeOnsaleAutoRefundRatio = goods.LocalityLifeOnsaleAutoRefundRatio; } if (goods.LocalityLifeRefundmafee != null) { req.LocalityLifeRefundmafee = goods.LocalityLifeRefundmafee; } if (goods.ScenicTicketPayWay != null) { req.ScenicTicketPayWay = goods.ScenicTicketPayWay; } if (goods.ScenicTicketBookCost != null) { req.ScenicTicketBookCost = goods.ScenicTicketBookCost; } if (goods.PaimaiInfoMode != null) { req.PaimaiInfoMode = goods.PaimaiInfoMode; } if (goods.PaimaiInfoDeposit != null) { req.PaimaiInfoDeposit = goods.PaimaiInfoDeposit; } if (goods.PaimaiInfoInterval != null) { req.PaimaiInfoInterval = goods.PaimaiInfoInterval; } if (goods.PaimaiInfoReserve != null) { req.PaimaiInfoReserve = goods.PaimaiInfoReserve; } if (goods.PaimaiInfoValidHour != null) { req.PaimaiInfoValidHour = goods.PaimaiInfoValidHour; } if (goods.PaimaiInfoValidMinute != null) { req.PaimaiInfoValidMinute = goods.PaimaiInfoValidMinute; } if (goods.GlobalStockType != null) { req.GlobalStockType = goods.GlobalStockType; } if (goods.GlobalStockCountry != null) { req.GlobalStockCountry = goods.GlobalStockCountry; } #endregion ItemAddResponse response = client.Execute(req, sessionKey); if (!response.IsError) { string Result = response.Item.NumIid.ToString(); return(Result); } else { ErrorMsg = response.SubErrMsg; return(null); } }