1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Linq; 5 using System.Web; 6 using Top.Api; 7 using Top.Api.Request; 8 using Top.Api.Response; 9 10 namespace MvcWebApp.DataBase 11 { 12 public class TaoDataProvider 13 { 14 15 public static string appKey = ConfigurationManager.AppSettings["appKey"].ToString(); 16 public static string appSecret = ConfigurationManager.AppSettings["appSecret"].ToString(); 17 public static string appUrl = "http://gw.api.taobao.com/router/rest"; 18 19 //淘宝官方接口说明:http://open.taobao.com/doc2/apiDetail.htm?spm=a219a.7395905.0.0.70naXx&scopeId=11655&apiId=26619 20 21 /// <summary> 22 /// 关键字查询商品 23 /// </summary> 24 /// <param name="PageIndex"></param> 25 /// <param name="PageSize"></param> 26 /// <param name="queryKey"></param> 27 /// <returns></returns> 28 public static string query(int PageIndex, int PageSize, string queryKey) 29 { 30 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret, "json"); 31 TbkItemGetRequest req = new TbkItemGetRequest(); 32 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 33 req.Q = queryKey; 34 req.Platform = 1L; 35 req.PageNo = PageIndex; 36 req.PageSize = PageSize; 37 TbkItemGetResponse response = client.Execute(req); 38 return response.Body; 39 } 40 /// <summary> 41 /// 分页查询 42 /// </summary> 43 /// <param name="PageIndex"></param> 44 /// <param name="PageSize"></param> 45 /// <param name="queryKey"></param> 46 /// <param name="totalCount"></param> 47 /// <param name="pageNum"></param> 48 /// <returns></returns> 49 public static List<Top.Api.Domain.NTbkItem> query(int PageIndex, int PageSize, string queryKey, out long totalCount, out long pageNum) 50 { 51 totalCount = 0; 52 pageNum = 0; 53 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 54 55 TbkItemGetRequest req = new TbkItemGetRequest(); 56 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 57 req.Q = queryKey; 58 req.Platform = 1L; 59 req.PageNo = PageIndex; 60 req.PageSize = PageSize; 61 62 TbkItemGetResponse response = client.Execute(req); 63 64 totalCount = response.TotalResults; 65 pageNum = (long)Math.Ceiling(totalCount / (double)PageSize); 66 return response.Results; 67 } 68 /// <summary> 69 /// 分页查询商品 70 /// </summary> 71 /// <param name="PageIndex">当前页</param> 72 /// <param name="PageSize">每页多少条</param> 73 /// <param name="queryKey">关键字</param> 74 /// <param name="cat">后台类目ID16,18</param> 75 /// <param name="itemloc">所在地</param> 76 /// <param name="sort">排序_des降序.排序_asc升序.排序销量total_sales淘客佣金比率tk_rate累计推广量tk_total_sales总支出佣金tk_total_commi</param> 77 /// <param name="isTmall">是否商城商品</param> 78 /// <param name="isOverseas">是否海外商品</param> 79 /// <param name="startPrice">折扣价范围下限</param> 80 /// <param name="endPrice">折扣价范围上限</param> 81 /// <param name="startTkRate">淘客佣金比率上限</param> 82 /// <param name="endTkRate">淘客佣金比率下限</param> 83 /// <param name="totalCount">总条数</param> 84 /// <param name="pageNum">总页数</param> 85 /// <returns></returns> 86 public static List<Top.Api.Domain.NTbkItem> query(int PageIndex, int PageSize, string queryKey, 87 string cat, string itemloc, string sort, bool? isTmall, bool? isOverseas, long? startPrice, long? endPrice, 88 long? startTkRate, long? endTkRate, 89 out long totalCount, out long pageNum) 90 { 91 totalCount = 0; 92 pageNum = 0; 93 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 94 95 TbkItemGetRequest req = new TbkItemGetRequest(); 96 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 97 if (!string.IsNullOrWhiteSpace(queryKey)) req.Q = queryKey; 98 req.Platform = 1; 99 req.PageNo = PageIndex; 100 req.PageSize = PageSize; 101 102 if (!string.IsNullOrWhiteSpace(cat)) req.Cat = cat;//后台类目ID16,18 103 if (!string.IsNullOrWhiteSpace(itemloc)) req.Itemloc = itemloc;//所在地 104 if (!string.IsNullOrWhiteSpace(sort)) req.Sort = sort;//"tk_rate_des";//排序 105 if (isTmall.HasValue) req.IsTmall = isTmall;//是否商城商品 106 if (isOverseas.HasValue) req.IsOverseas = isOverseas;//是否海外商品 107 if (startPrice.HasValue) req.StartPrice = startPrice;// 折扣价范围下限 108 if (endPrice.HasValue) req.EndPrice = endPrice; 109 if (startTkRate.HasValue) req.StartTkRate = startTkRate;//淘客佣金比率上限,如:1234表示12.34% 110 if (endTkRate.HasValue) req.EndTkRate = endTkRate; 111 112 113 114 TbkItemGetResponse response = client.Execute(req); 115 116 totalCount = response.TotalResults; 117 pageNum = (long)Math.Ceiling(totalCount / (double)PageSize); 118 return response.Results; 119 } 120 121 122 /// <summary> 123 /// 根据ID查询单个商品 124 /// </summary> 125 /// <param name="ssid"></param> 126 /// <returns></returns> 127 public static string FindOne(string ssid) 128 { 129 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret, "json"); 130 TbkItemInfoGetRequest req = new TbkItemInfoGetRequest(); 131 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 132 req.Platform = 1L; 133 req.NumIids = ssid; 134 TbkItemInfoGetResponse rsp = client.Execute(req); 135 return rsp.Body; 136 } 137 /// <summary> 138 /// 根据ID查询单个商品 139 /// </summary> 140 /// <param name="ssid">123,256,365(最多40个)</param> 141 /// <returns></returns> 142 public static List<Top.Api.Domain.NTbkItem> FindBySSID(string ssid) 143 { 144 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 145 TbkItemInfoGetRequest req = new TbkItemInfoGetRequest(); 146 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 147 req.Platform = 1L; 148 req.NumIids = ssid; 149 TbkItemInfoGetResponse rsp = client.Execute(req); 150 return rsp.Results; 151 } 152 /// <summary> 153 /// 关联商品查询 154 /// </summary> 155 /// <param name="relate_type"></param> 156 /// <param name="num_iid"></param> 157 /// <param name="count"></param> 158 /// <param name="platform"></param> 159 /// <returns></returns> 160 public static string FindListRelevance(int? relate_type, long? num_iid, int? count = 24, int? platform = 1) 161 { 162 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret, "json"); 163 TbkItemRecommendGetRequest req = new TbkItemRecommendGetRequest(); 164 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 165 166 if (num_iid.HasValue) 167 req.NumIid = num_iid.Value; 168 169 //req.Cat = 123L; 170 req.Count = count.Value; 171 req.Platform = platform.Value; 172 TbkItemRecommendGetResponse rsp = client.Execute(req); 173 //Console.WriteLine(rsp.Body); 174 return rsp.Body; 175 } 176 /// <summary> 177 /// 关联商品查询 178 /// </summary> 179 /// <param name="relate_type"></param> 180 /// <param name="num_iid"></param> 181 /// <param name="count"></param> 182 /// <param name="platform"></param> 183 /// <returns></returns> 184 public static List<Top.Api.Domain.NTbkItem> FindListRelate(int? relate_type, long? num_iid, int? count = 24, int? platform = 1) 185 { 186 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 187 TbkItemRecommendGetRequest req = new TbkItemRecommendGetRequest(); 188 189 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url"; 190 //if (relate_type.HasValue) 191 // req.RelateType = relate_type.Value; 192 if (num_iid.HasValue) 193 req.NumIid = num_iid.Value; 194 195 //req.Cat = 123L; 196 req.Count = count.Value; 197 req.Platform = platform.Value; 198 TbkItemRecommendGetResponse rsp = client.Execute(req); 199 //Console.WriteLine(rsp.Body); 200 return rsp.Results; 201 } 202 /// <summary> 203 /// 获取淘宝客店铺列表 204 /// </summary> 205 /// <param name="PageIndex">第几页</param> 206 /// <param name="PageSize">每页几条</param> 207 /// <param name="queryKey">关键字</param> 208 /// <param name="start_credit">信用等级下限,1~20</param> 209 /// <param name="end_credit">信用等级上限,1~20</param> 210 /// <param name="start_commission_rate">淘客佣金比率下限,1~10000</param> 211 /// <param name="end_commission_rate">淘客佣金比率上限,1~10000</param> 212 /// <param name="start_total_action">店铺商品总数下限</param> 213 /// <param name="end_total_action">店铺商品总数上限</param> 214 /// <param name="start_auction_count">累计推广商品下限</param> 215 /// <param name="end_auction_count">累计推广商品上限</param> 216 /// <param name="totalCount">总数</param> 217 /// <param name="pageNum">总页数</param> 218 /// <returns></returns> 219 public static List<Top.Api.Domain.NTbkShop> FindShopsPagedList(long PageIndex, long PageSize, string queryKey, 220 long? start_credit, long? end_credit, long? start_commission_rate, long? end_commission_rate, 221 long? start_total_action, long? end_total_action, long? start_auction_count, long? end_auction_count, 222 out long totalCount, out long pageNum) 223 { 224 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 225 TbkShopGetRequest req = new TbkShopGetRequest(); 226 req.Fields = "click_url,pic_url,seller_nick,shop_title,shop_url,user_id"; 227 if (!string.IsNullOrWhiteSpace(queryKey)) req.Q = queryKey; 228 if (start_credit.HasValue) req.StartCredit = start_credit; 229 if (end_credit.HasValue) req.EndCredit = end_credit; 230 231 if (start_commission_rate.HasValue) req.StartCommissionRate = start_commission_rate; 232 if (end_commission_rate.HasValue) req.EndCommissionRate = end_commission_rate; 233 234 if (start_total_action.HasValue) req.StartTotalAction = start_total_action; 235 if (end_total_action.HasValue) req.EndTotalAction = end_total_action; 236 237 if (start_auction_count.HasValue) req.StartAuctionCount = start_auction_count; 238 if (end_auction_count.HasValue) req.EndAuctionCount = end_auction_count; 239 240 req.PageNo = PageIndex; 241 req.PageSize = PageSize; 242 TbkShopGetResponse rsp = client.Execute(req); 243 244 totalCount = rsp.TotalResults; 245 pageNum = (long)Math.Ceiling(totalCount / (double)PageSize); 246 return rsp.Results; 247 248 249 } 250 /// <summary> 251 /// 淘宝客店铺关联推荐查询 252 /// </summary> 253 /// <param name="userid">卖家Id</param> 254 /// <returns></returns> 255 public static List<Top.Api.Domain.NTbkShop> GetShopRecommend(long userid) 256 { 257 258 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 259 TbkShopRecommendGetRequest req = new TbkShopRecommendGetRequest(); 260 req.Fields = "click_url,pict_url,seller_nick,shop_title,shop_type,shop_url,user_id"; 261 req.UserId = userid; 262 req.Count = 20; 263 req.Platform = 1; 264 TbkShopRecommendGetResponse rsp = client.Execute(req); 265 return rsp.Results; 266 267 } 268 /// <summary> 269 /// 枚举正在进行中的定向招商的活动列表 270 /// </summary> 271 /// <returns></returns> 272 public static List<Top.Api.Domain.TbkEvent> GetTbkUatmEvent(long? pageindex = 1, long? pageSize = 20) 273 { 274 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 275 TbkUatmEventGetRequest req = new TbkUatmEventGetRequest(); 276 req.PageNo = pageindex; 277 req.PageSize = pageSize; 278 req.Fields = "event_id,event_title,start_time,end_time"; 279 TbkUatmEventGetResponse rsp = client.Execute(req); 280 return rsp.Results; 281 } 282 /// <summary> 283 /// 获取淘宝联盟定向招商的宝贝信息 284 /// </summary> 285 /// <param name="event_id"></param> 286 /// <param name="pageindex"></param> 287 /// <param name="pageSize"></param> 288 /// <returns></returns> 289 public static List<Top.Api.Domain.UatmTbkItem> GetTbkUatmEventItem(long event_id, long? pageindex = 1, long? pageSize = 20) 290 { 291 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 292 TbkUatmEventItemGetRequest req = new TbkUatmEventItemGetRequest(); 293 req.Platform = 1L; 294 req.PageSize = pageSize; 295 //req.AdzoneId = 34567L; 296 //req.Unid = unid; 297 req.EventId = event_id; 298 req.PageNo = pageindex; 299 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,type,status"; 300 TbkUatmEventItemGetResponse rsp = client.Execute(req); 301 return rsp.Results; 302 303 } 304 /// <summary> 305 /// 获取淘宝联盟选品库列表 306 /// </summary> 307 /// <param name="type">默认值-1;选品库类型,1:普通选品组,2:高佣选品组,-1,同时输出所有类型的选品组</param> 308 /// <param name="pageindex"></param> 309 /// <param name="pageSize"></param> 310 /// <returns></returns> 311 public static List<Top.Api.Domain.TbkFavorites> GetTbkUatmFavorites(long? type=-1, long? pageIndex = 1, long? pageSize = 20) 312 { 313 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 314 TbkUatmFavoritesGetRequest req = new TbkUatmFavoritesGetRequest(); 315 req.PageNo = pageIndex; 316 req.PageSize = pageSize; 317 req.Fields = "favorites_title,favorites_id,type"; 318 req.Type = type; 319 TbkUatmFavoritesGetResponse rsp = client.Execute(req); 320 return rsp.Results; 321 } 322 /// <summary> 323 /// 指定选品库id,获取该选品库的宝贝信息 324 /// </summary> 325 /// <param name="FavoritesId"></param> 326 /// <param name="pageIndex"></param> 327 /// <param name="pageSize"></param> 328 /// <returns></returns> 329 public static List<Top.Api.Domain.UatmTbkItem> GetTbkUatmFavoritesItem(long? FavoritesId, long? pageIndex = 1, long? pageSize = 20) 330 { 331 ITopClient client = new DefaultTopClient(appUrl, appKey, appSecret); 332 TbkUatmFavoritesItemGetRequest req = new TbkUatmFavoritesItemGetRequest(); 333 req.Platform = 1L; 334 req.PageSize = pageSize; 335 //req.AdzoneId = 34567L; 336 //req.Unid = "3456"; 337 req.FavoritesId = FavoritesId; 338 req.PageNo = pageIndex; 339 req.Fields = "num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type"; 340 TbkUatmFavoritesItemGetResponse rsp = client.Execute(req); 341 return rsp.Results; 342 } 343 344 345 } 346 }