2022-09-20 03:10:29 +00:00
|
|
|
|
//=============================================================
|
|
|
|
|
// 创建人:千年老妖
|
|
|
|
|
// 本页代码,均为原创。对未经许可擅自使用者,本人保留追究其法律责任的权利。
|
|
|
|
|
//==============================================================
|
|
|
|
|
using Api.Framework.Model;
|
|
|
|
|
using Api.Framework.Tools;
|
|
|
|
|
using Api.Framework.Utils;
|
|
|
|
|
using CsharpHttpHelper;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-10-30 13:03:11 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2022-09-20 03:10:29 +00:00
|
|
|
|
|
|
|
|
|
namespace Api.Framework.Cps
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// CpsApi基础类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BaseCpsApi
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// cps数据对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
public fl_cps_member Member { get; protected set; }
|
|
|
|
|
private CpsToken _token = null;
|
|
|
|
|
|
|
|
|
|
private static SynchronizedDictionary<string, CpsToken> Tokens = new SynchronizedDictionary<string, CpsToken>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除Token缓存
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal void ClearToken()
|
|
|
|
|
{
|
|
|
|
|
string key = $"{Member.username}_{(int)Member.cpstype}";
|
|
|
|
|
if (Tokens.ContainsKey(key)) Tokens.Remove(key);
|
|
|
|
|
_token = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取重新获取token
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal CpsToken GetToken()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i <= 5; i++)
|
|
|
|
|
{
|
|
|
|
|
var result = SendServer("get_token", new { username = Member.username, enc_result = true });
|
|
|
|
|
if (result != null)
|
|
|
|
|
{
|
|
|
|
|
if (result.ok)
|
|
|
|
|
{
|
|
|
|
|
return HttpHelper.JsonToObject<CpsToken>(result.message.ToString()) as CpsToken;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(15);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 重新获取Token
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
internal bool RefToken()
|
|
|
|
|
{
|
|
|
|
|
#region 老妖xxx
|
|
|
|
|
//try
|
|
|
|
|
//{
|
|
|
|
|
// int number = 0;
|
|
|
|
|
//Next:
|
|
|
|
|
// number++;
|
|
|
|
|
// var result = SendServer("get_token", new { username = Member.username, enc_result = true });
|
|
|
|
|
// if (result == null && number <= 5)
|
|
|
|
|
// {
|
|
|
|
|
// Thread.Sleep(10);
|
|
|
|
|
// goto Next;
|
|
|
|
|
// }
|
|
|
|
|
// _token = HttpHelper.JsonToObject<CpsToken>(result.message.ToString()) as CpsToken;
|
|
|
|
|
|
|
|
|
|
// if (_token != null)
|
|
|
|
|
// {
|
|
|
|
|
// string key = $"{Member.username}_{(int)Member.cpstype}";
|
|
|
|
|
// if (Tokens.ContainsKey(key)) Tokens.Add(key, _token);
|
|
|
|
|
// else Tokens[key] = _token;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//catch (Exception ex)
|
|
|
|
|
//{
|
|
|
|
|
// throw ex;
|
|
|
|
|
//}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 老道
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var new_token = GetToken();
|
|
|
|
|
if (new_token == null) return false;
|
|
|
|
|
|
|
|
|
|
//本机没有值,拉取服务器Token
|
|
|
|
|
if (_token == null)
|
|
|
|
|
{
|
|
|
|
|
_token = new_token;
|
|
|
|
|
SetToken(_token);
|
|
|
|
|
if (Member != null) Member.is_valid = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//和本机一致,返回False 表示没有新Token
|
|
|
|
|
if (_token.access_token == new_token.access_token)
|
|
|
|
|
return false;
|
|
|
|
|
_token = new_token;
|
|
|
|
|
SetToken(_token);
|
|
|
|
|
if (Member != null)
|
|
|
|
|
Member.is_valid = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
LogHelper.GetSingleObj().Debug("", $"唯品会获取新的Token: username = {_token?.username},appid = {_token?.appid},access_token = {_token?.access_token}");
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置Token
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cpsToken"></param>
|
|
|
|
|
private void SetToken(CpsToken cpsToken)
|
|
|
|
|
{
|
|
|
|
|
if (cpsToken != null)
|
|
|
|
|
{
|
|
|
|
|
string key = $"{Member.username}_{(int)Member.cpstype}";
|
|
|
|
|
if (!Tokens.ContainsKey(key)) Tokens.Add(key, cpsToken);
|
|
|
|
|
else Tokens[key] = cpsToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 操作Token
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal CpsToken Token
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_token == null)
|
|
|
|
|
{
|
|
|
|
|
string key = $"{Member.username}_{(int)Member.cpstype}";
|
|
|
|
|
if (Tokens.ContainsKey(key)) _token = Tokens[key];
|
|
|
|
|
else RefToken();
|
|
|
|
|
}
|
|
|
|
|
return _token;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal BaseCpsApi(fl_cps_member member)
|
|
|
|
|
{
|
|
|
|
|
this.Member = member;
|
|
|
|
|
//if (this.Member.cpstype == Enums.CpsType.阿里妈妈)
|
|
|
|
|
//{
|
|
|
|
|
// this.RefToken();
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 请求报文
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="method"></param>
|
|
|
|
|
/// <param name="target"></param>
|
|
|
|
|
/// <param name="param"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public WebResult SendServer(string method, string target = "", object param = null)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(target))
|
|
|
|
|
{
|
|
|
|
|
var type = this.GetType();
|
|
|
|
|
if (type == typeof(AlimamaApi)) target = "alimama.asmx";
|
|
|
|
|
else if (type == typeof(PinduoduoApi)) target = "pinduoduo.asmx";
|
|
|
|
|
else if (type == typeof(JingdongApi)) target = "jingdong.asmx";
|
|
|
|
|
else if (type == typeof(WeipinhuiApi)) target = "weipinhui.asmx";
|
|
|
|
|
else if (type == typeof(DouyinApi)) target = "douyin.asmx";
|
|
|
|
|
else if (type == typeof(KuaiShouApi)) target = "kuaishou.asmx";
|
|
|
|
|
else throw new Exception("暂时不支持类型:" + target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AuthEndpoint end = new AuthEndpoint()
|
|
|
|
|
{
|
|
|
|
|
Appid = CpsClient._appid,
|
|
|
|
|
Enckey = CpsClient._key,
|
|
|
|
|
Host = CpsClient._host + "api/" + target + "/send_data",
|
|
|
|
|
Method = method
|
|
|
|
|
};
|
2022-10-30 13:03:11 +00:00
|
|
|
|
|
2022-09-20 03:10:29 +00:00
|
|
|
|
if (param != null)
|
|
|
|
|
{
|
|
|
|
|
var _param = param.GetType().GetProperties();
|
|
|
|
|
foreach (var item in _param)
|
|
|
|
|
{
|
|
|
|
|
end.Param[item.Name] = item.GetValue(param);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
HttpHelper http = new HttpHelper();
|
2022-10-30 13:03:11 +00:00
|
|
|
|
WebResult result = null;
|
2022-09-20 03:10:29 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-10-30 13:03:11 +00:00
|
|
|
|
result = http.SendData(end);
|
|
|
|
|
return result;
|
2022-09-20 03:10:29 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-10-30 13:03:11 +00:00
|
|
|
|
LogHelper.GetSingleObj().Error("cps请求异常", ex.Message + $"{result?.message} => {JsonConvert.SerializeObject(end)}");
|
2022-09-20 03:10:29 +00:00
|
|
|
|
if (ex.Message == "未将对象引用设置到对象的实例。")
|
|
|
|
|
return null;
|
|
|
|
|
else
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 发送API数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="method">API接口</param>
|
|
|
|
|
/// <param name="param">参数</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public WebResult SendServer(string method, object param = null)
|
|
|
|
|
{
|
|
|
|
|
return SendServer(method, "", param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|