|
@@ -0,0 +1,167 @@
|
|
|
+using OASystem.Domain.ViewModels.JuHeExchangeRate;
|
|
|
+using System.Text.Json;
|
|
|
+
|
|
|
+namespace OASystem.API.OAMethodLib.YouDaoAPI
|
|
|
+{
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static class YouDaoApiTools
|
|
|
+ {
|
|
|
+ private static readonly HttpClient _httpClient = new HttpClient { BaseAddress = new Uri("https://openapi.youdao.com") };
|
|
|
+
|
|
|
+ private static readonly string _appKey = "0fe3bc01e109ed36";
|
|
|
+ private static readonly string _appSecret = "1f2x9TrqJfSBEJ8iH9GEFGgTyaYGjEry";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static async Task<Result> GetReTrans(string str, string from = "zh-CHS", string to = "en")
|
|
|
+ {
|
|
|
+ var result = new Result();
|
|
|
+ #region MyRegion
|
|
|
+ if (string.IsNullOrEmpty(str))
|
|
|
+ {
|
|
|
+ result.Msg = "翻译文本为空!";
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ string url = string.Format("/api");
|
|
|
+
|
|
|
+ #region 请求参数处理
|
|
|
+
|
|
|
+ string q = System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
|
|
|
+
|
|
|
+ string input = q.Length <= 20 ? q : q.Substring(0, 10) + q.Length + q.Substring(q.Length - 10, 10);
|
|
|
+ string salt = DateTime.Now.Millisecond.ToString();
|
|
|
+ string curtime = GetUTCTime(true).ToString();
|
|
|
+
|
|
|
+ string signStr = _appKey + input + salt + curtime + _appSecret;
|
|
|
+ string sign = SHA256EncryptString(signStr);
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ var reqData = new FormUrlEncodedContent(
|
|
|
+ new List<KeyValuePair<string, string>>()
|
|
|
+ {
|
|
|
+ new KeyValuePair<string, string>("q",q),
|
|
|
+ new KeyValuePair<string, string>("from",from),
|
|
|
+ new KeyValuePair<string, string>("to",to),
|
|
|
+ new KeyValuePair<string, string>("appKey",_appKey),
|
|
|
+ new KeyValuePair<string, string>("salt",salt),
|
|
|
+ new KeyValuePair<string, string>("sign",sign),
|
|
|
+ new KeyValuePair<string, string>("signType","v3"),
|
|
|
+ new KeyValuePair<string, string>("curtime",curtime),
|
|
|
+ });
|
|
|
+
|
|
|
+ var reTransReq = await _httpClient.PostAsync(url, reqData);
|
|
|
+ if (reTransReq.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ var strResponse = await reTransReq.Content.ReadAsStringAsync();
|
|
|
+ var strResponse1 = System.Web.HttpUtility.UrlDecode(strResponse, System.Text.Encoding.UTF8);
|
|
|
+
|
|
|
+ var trans = JsonConvert.DeserializeObject<dynamic>(strResponse1);
|
|
|
+ string errorCode = trans["errorCode"].ToString();
|
|
|
+ if (errorCode == "0")
|
|
|
+ {
|
|
|
+ result.Code = 0;
|
|
|
+ var translation = JsonConvert.DeserializeObject<List<string>>(trans["translation"].ToString());
|
|
|
+ result.Msg = string.Format(@"成功!");
|
|
|
+ result.Data = translation;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Msg = string.Format(@"有道翻译API [Error:{0}]", errorCode);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result.Msg = "接口请求失败!";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region SHA256加密
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static string SHA256EncryptString(string data)
|
|
|
+ {
|
|
|
+ byte[] bytes = Encoding.UTF8.GetBytes(data);
|
|
|
+ byte[] hash = SHA256Managed.Create().ComputeHash(bytes);
|
|
|
+
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ for (int i = 0; i < hash.Length; i++)
|
|
|
+ {
|
|
|
+ builder.Append(hash[i].ToString("x2"));
|
|
|
+ }
|
|
|
+ return builder.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static Byte[] SHA256EncryptByte(string StrIn)
|
|
|
+ {
|
|
|
+ var sha256 = new SHA256Managed();
|
|
|
+ var Asc = new ASCIIEncoding();
|
|
|
+ var tmpByte = Asc.GetBytes(StrIn);
|
|
|
+ var EncryptBytes = sha256.ComputeHash(tmpByte);
|
|
|
+ sha256.Clear();
|
|
|
+ return EncryptBytes;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region UTC时间戳
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static long GetUTCTime(bool type)
|
|
|
+ {
|
|
|
+ TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
|
+ return type ? Convert.ToInt64(ts.TotalSeconds) : Convert.ToInt64(ts.TotalMilliseconds);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static DateTime ConvertUTCToDateTime(long timeStamp)
|
|
|
+ {
|
|
|
+ DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|
|
+ DateTime dt = startTime.AddSeconds(timeStamp);
|
|
|
+ return dt;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static int ConvertDateTimeToUTC(DateTime time)
|
|
|
+ {
|
|
|
+ double intResult = 0;
|
|
|
+ DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
|
|
+ intResult = (time - startTime).TotalSeconds;
|
|
|
+ return (int)intResult;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|