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.
312 lines
9.8 KiB
312 lines
9.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows;
|
|
using Utils;
|
|
using 齐越慧眼.Models;
|
|
|
|
namespace 齐越慧眼
|
|
{
|
|
public class ApiHelper
|
|
{
|
|
//public static string ApiBase { get; set; } = "http://localhost:5000";
|
|
public static string ApiBase { get; set; } = "http://hyapi.qiyue666.com";
|
|
|
|
public static UserInfo LoginSystemUser { get; set; }
|
|
|
|
static string jwtToken;
|
|
|
|
public static string JwtToken {
|
|
get
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(jwtToken))
|
|
{
|
|
jwtToken = GetMemoryToken().Replace("\r\n","");
|
|
|
|
}
|
|
|
|
return jwtToken;
|
|
}
|
|
|
|
}
|
|
|
|
static int getTokentCount = 0;
|
|
|
|
public static string GetMemoryToken()
|
|
{
|
|
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);
|
|
return GetMemoryToken();
|
|
}
|
|
}
|
|
|
|
return result.content;
|
|
}
|
|
else {
|
|
getTokentCount = getTokentCount + 1;
|
|
if (getTokentCount <= 3)
|
|
{
|
|
Thread.Sleep(300);
|
|
return GetMemoryToken();
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|