You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
442 lines
14 KiB
442 lines
14 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using Utils;
|
|
using System.Diagnostics;
|
|
using 齐越慧眼.Models;
|
|
|
|
namespace 齐越慧眼
|
|
{
|
|
public class ApiHelper
|
|
{
|
|
//public static string ApiBase { get; set; } = "http://111.230.132.27:8033";
|
|
public static string ApiBase { get; set; } = "http://hyapi.qiyue666.com";
|
|
|
|
public static UserInfo LoginSystemUser { get; set; }
|
|
|
|
public static bool NoLogin { get; set; }
|
|
static object lockToken = new object();
|
|
|
|
static string jwtToken;
|
|
|
|
public static string JwtToken {
|
|
get
|
|
{
|
|
|
|
#if DEBUG
|
|
//return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDgyNTMwMzM3MDMxNDU4ODE2IiwidGVhbUlkIjoiMTUxNjk3Nzk0NDkzNTc5NjczNiIsImV4cCI6MTY4MzM2OTA3N30.WWc1W5EKcAsH1hxDbt2c3PQ85h-ZecG0YIVSq2BhHjU";
|
|
|
|
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNTAwNjY3OTAyNDU2NTAwMjI0IiwidGVhbUlkIjoiMTQ2MzAyOTM1NTEwNDk2NDYwOCIsImV4cCI6MTY4MzM0NDg3Nn0.6AsD6DK9Or05w8HPhjb9tQY8RJAT4zo4ouH9sasl_Rk";
|
|
#endif
|
|
|
|
if (string.IsNullOrEmpty(jwtToken))
|
|
{
|
|
|
|
jwtToken = GetMemoryToken().Replace("\r\n", "");
|
|
|
|
}
|
|
|
|
return jwtToken;
|
|
}
|
|
|
|
}
|
|
|
|
static int getTokentCount = 0;
|
|
|
|
|
|
public static string GetMemoryToken()
|
|
{
|
|
lock (lockToken)
|
|
{
|
|
if (NoLogin)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
string memoryName = string.Empty;
|
|
string[] args = Environment.GetCommandLineArgs();
|
|
foreach (var arg in args)
|
|
{
|
|
if (arg.StartsWith("uid:"))
|
|
{
|
|
memoryName = arg;
|
|
}
|
|
}
|
|
|
|
var result = MemoryHelper.ReadMMF(memoryName);
|
|
|
|
if (result.isOk)
|
|
{
|
|
if (string.IsNullOrEmpty(result.content))
|
|
{
|
|
getTokentCount = getTokentCount + 1;
|
|
if (getTokentCount <= 3)
|
|
{
|
|
Thread.Sleep(300);
|
|
if (!NoLogin)
|
|
return GetMemoryToken();
|
|
}
|
|
}
|
|
|
|
return result.content;
|
|
}
|
|
else
|
|
{
|
|
getTokentCount = getTokentCount + 1;
|
|
if (getTokentCount <= 3)
|
|
{
|
|
Thread.Sleep(300);
|
|
if (!NoLogin)
|
|
return GetMemoryToken();
|
|
}
|
|
MessageBox.Show("登录失败,请稍后重试!");
|
|
NoLogin = true;
|
|
// WpfNoticeMsg.NoticeMessage.Show("小程序登录失败!");
|
|
|
|
System.Environment.Exit(0);
|
|
return string.Empty;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 登录
|
|
/// </summary>
|
|
/// <param name="userName"></param>
|
|
/// <param name="password"></param>
|
|
/// <returns></returns>
|
|
public static (bool isOk, string msg) LoginUser(string userName, string password)
|
|
{
|
|
string result = Http("http://mdsapi.qiyue666.com/TaskList/User/SubmitLogin", Newtonsoft.Json.JsonConvert.SerializeObject(new { userName, password }));
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
return (false, "服务器异常");
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
bool isSuccess = data.Success;
|
|
|
|
if (isSuccess)
|
|
{
|
|
string msg = data.Data.ToString();
|
|
if (msg.Contains("账号"))
|
|
{
|
|
return (false, msg);
|
|
}
|
|
|
|
jwtToken = msg;
|
|
|
|
return (true, data.Msg ?? "");
|
|
}
|
|
else
|
|
{
|
|
return (false, data.Msg ?? "");
|
|
}
|
|
}
|
|
|
|
public static (bool isOk, UserInfo user) GetUserInfo()
|
|
{
|
|
var result = Http("http://mdsapi.qiyue666.com/TaskList/User/GetUserInfo");
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
|
|
bool isSuccess = data.Success;
|
|
|
|
if (isSuccess)
|
|
{
|
|
string json = data.Data.ToString();
|
|
UserInfo u = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(json);
|
|
return (true, u);
|
|
}
|
|
|
|
return (false, null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置商品标签
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public static (bool isOk, bool isCanel, string msg) SetItemLabel(ItemLabelDto model)
|
|
{
|
|
var result = Http("/HuiYan/itemlabels/SetItemLabel", Newtonsoft.Json.JsonConvert.SerializeObject(model));
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
|
|
bool isSuccess = data.Success;
|
|
bool isCanel = data.Data.isCanel;
|
|
return (isSuccess, isCanel, data.Msg ?? "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置阿里巴巴店铺合作状态
|
|
/// </summary>
|
|
/// <param name="shopId"></param>
|
|
/// <returns></returns>
|
|
public static (bool isOk, bool isCanel, string msg) SetAlbbCooperation(string shopId)
|
|
{
|
|
var result = Http($"/HuiYan/itemlabels/SetAlbbCooperation?shopId={shopId}", "");
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
|
|
bool isSuccess = data.Success;
|
|
bool isCanel = data.Data.isCanel;
|
|
|
|
return (isSuccess, isCanel, data.Msg ?? "");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取标签信息
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <param name="platform"></param>
|
|
/// <returns></returns>
|
|
public static (bool isOk, List<albbitemlabels> datas) GetAlbbLabelByShopIds(List<string> ids)
|
|
{
|
|
try
|
|
{
|
|
var result = Http($"/HuiYan/itemlabels/GetAlbbLabelByShopIds", Newtonsoft.Json.JsonConvert.SerializeObject(ids));
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
|
|
string json = data.Data.ToString();
|
|
|
|
var datas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<albbitemlabels>>(json);
|
|
|
|
bool isSuccess = data.Success;
|
|
|
|
return (isSuccess, datas ?? new List<albbitemlabels>());
|
|
}
|
|
catch
|
|
{
|
|
return (false, new List<albbitemlabels>());
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取标签信息
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <param name="platform"></param>
|
|
/// <returns></returns>
|
|
public static (bool isOk, List<ItemlabelInfoDto> datas) GetLabelByItemIds(List<string> ids, ItemPlatform platform)
|
|
{
|
|
try
|
|
{
|
|
var result = Http($"/HuiYan/itemlabels/GetLabelByItemIds?platform={platform}", Newtonsoft.Json.JsonConvert.SerializeObject(ids));
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
|
|
string json = data.Data.ToString();
|
|
|
|
var datas = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ItemlabelInfoDto>>(json);
|
|
|
|
bool isSuccess = data.Success;
|
|
|
|
return (isSuccess, datas ?? new List<ItemlabelInfoDto>());
|
|
}
|
|
catch
|
|
{
|
|
return (false, new List<ItemlabelInfoDto>());
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 设置关键词打开时间
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="platform"></param>
|
|
/// <returns></returns>
|
|
public static (bool isOk, string msg) SetKeyOpenTime(string id, ItemPlatform platform)
|
|
{
|
|
try
|
|
{
|
|
var result = Http($"/HuiYan/cats/SetKeyOpenTime?platform={platform}&id={id}", string.Empty);
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(result);
|
|
|
|
bool isSuccess = data.Success;
|
|
|
|
return (isSuccess, data.Msg ?? "操作失败");
|
|
}
|
|
catch
|
|
{
|
|
return (false, "系统异常");
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// http接口调用
|
|
/// </summary>
|
|
/// <param name="api"></param>
|
|
/// <param name="postData"></param>
|
|
/// <returns></returns>
|
|
private static string Http(string api, string postData = null, bool isAgain = false)
|
|
{
|
|
try
|
|
{
|
|
string url = api;
|
|
|
|
if (!url.StartsWith("http"))
|
|
{
|
|
url = ApiBase + api;
|
|
}
|
|
|
|
HttpClient http = new HttpClient();
|
|
http.Timeout = new TimeSpan(0, 0, 35);
|
|
http.DefaultRequestHeaders.Add("Authorization", "Bearer " + JwtToken);
|
|
if (postData != null)
|
|
{
|
|
StringContent content = new StringContent(postData);
|
|
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
var result = http.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
|
|
return result;
|
|
}
|
|
|
|
var request = new HttpRequestMessage() { Method = HttpMethod.Get, RequestUri = new Uri(url) };
|
|
|
|
var res = http.SendAsync(request).Result;
|
|
|
|
if (res.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
|
{
|
|
WpfNoticeMsg.NoticeMessage.Show("登录状态失败!");
|
|
return string.Empty;
|
|
}
|
|
else
|
|
{
|
|
//服务器挂掉
|
|
if (res.StatusCode != System.Net.HttpStatusCode.OK)
|
|
{
|
|
if (isAgain)
|
|
return null;
|
|
|
|
Thread.Sleep(60000);
|
|
return Http(api, postData, true);
|
|
}
|
|
return res.Content.ReadAsStringAsync().Result;
|
|
}
|
|
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
if (isAgain)
|
|
return null;
|
|
Thread.Sleep(60000);
|
|
return Http(api, postData, true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据Sku获取详情
|
|
/// </summary>
|
|
/// <param name="skuId"></param>
|
|
/// <returns></returns>
|
|
public static (int status, dynamic result) GetItemDetail(string skuId)
|
|
{
|
|
try
|
|
{
|
|
HttpClient http = new HttpClient();
|
|
http.DefaultRequestHeaders.Add("Cookie", App.JdCookie);
|
|
http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0");
|
|
string html = http.GetStringAsync($"https://item.jd.com/{skuId}.html").Result;
|
|
|
|
|
|
if (html.Contains("passport.jd.com/new/login.aspx") || html.Contains("//item.jd.com/html/token.html?returnUrl="))
|
|
{
|
|
return (2, null);
|
|
}
|
|
|
|
int start = html.IndexOf("var pageConfig");
|
|
int end = html.IndexOf("};", start);
|
|
|
|
string json = html.Substring(start, end - start);
|
|
|
|
start = json.IndexOf("{");
|
|
|
|
json = json.Substring(start, json.Length - start);
|
|
|
|
json += "}";
|
|
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
|
|
|
|
return (0, data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (1, ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取价格
|
|
/// </summary>
|
|
/// <param name="skuId"></param>
|
|
/// <returns></returns>
|
|
public static (string price, string commont) GetItemPrice(string skuId)
|
|
{
|
|
try
|
|
{
|
|
HttpClient http = new HttpClient();
|
|
http.DefaultRequestHeaders.Add("Cookie", App.JdCookie);
|
|
http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0");
|
|
string html = http.GetStringAsync($"https://item-soa.jd.com/getWareBusiness?skuId={skuId}").Result;
|
|
|
|
if (!string.IsNullOrEmpty(html))
|
|
{
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(html);
|
|
|
|
string price = data.price.p.ToString();
|
|
|
|
string commont = GetItemCommentCount(skuId);
|
|
|
|
return (price, commont);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return (string.Empty, string.Empty);
|
|
}
|
|
return (string.Empty, string.Empty);
|
|
}
|
|
|
|
public static string GetItemCommentCount(string skuId)
|
|
{
|
|
try
|
|
{
|
|
HttpClient http = new HttpClient();
|
|
http.DefaultRequestHeaders.Add("Cookie", App.JdCookie);
|
|
http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0");
|
|
string html = http.GetStringAsync($"https://club.jd.com/comment/productCommentSummaries.action?referenceIds={skuId}").Result;
|
|
|
|
if (!string.IsNullOrEmpty(html))
|
|
{
|
|
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(html);
|
|
|
|
string count = data.CommentsCount[0].CommentCountStr.ToString();
|
|
|
|
return count;
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return "";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|