SmallFunController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using MySqlX.XDevAPI;
  5. using OASystem.API.OAMethodLib.ChatGPT;
  6. using OASystem.API.OAMethodLib.GoogleMapAPI;
  7. using OASystem.API.OAMethodLib.JuHeAPI;
  8. using OASystem.API.OAMethodLib.YouDaoAPI;
  9. using OASystem.Domain.Dtos.Groups;
  10. using OASystem.Domain.Dtos.SmallFun;
  11. using OASystem.Domain.ViewModels.JuHeExchangeRate;
  12. using OASystem.Domain.ViewModels.SmallFun;
  13. using SqlSugar;
  14. using System.Buffers;
  15. using System.Collections.Generic;
  16. using System.Collections.Immutable;
  17. using System.Collections.Specialized;
  18. using System.Diagnostics;
  19. using System.Text.Json;
  20. using System.Text.Json.Nodes;
  21. using System.Transactions;
  22. using System.Xml.Linq;
  23. namespace OASystem.API.Controllers
  24. {
  25. /// <summary>
  26. /// 小功能接口
  27. /// </summary>
  28. [Route("api/[controller]/[action]")]
  29. [ApiController]
  30. public class SmallFunController : ControllerBase
  31. {
  32. private readonly IJuHeApiService _juHeApiService;
  33. private readonly IConfiguration _config;
  34. /// <summary>
  35. /// 初始化
  36. /// </summary>
  37. /// <param name="juHeApiService"></param>
  38. public SmallFunController(IJuHeApiService juHeApiService, IConfiguration config)
  39. {
  40. this._juHeApiService = juHeApiService;
  41. this._config = config;
  42. }
  43. #region 聚合API 汇率相关
  44. /// <summary>
  45. /// 获取汇率币种
  46. /// </summary>
  47. /// <returns></returns>
  48. [HttpPost]
  49. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  50. public async Task<IActionResult> GetCurrencys()
  51. {
  52. var rateCurrencyData = AppSettingsHelper.Get<RateCurrencyModel>("RateCurrency");
  53. return Ok(JsonView(rateCurrencyData));
  54. }
  55. /// <summary>
  56. /// 获取汇率费用
  57. /// </summary>
  58. /// <returns></returns>
  59. [HttpPost]
  60. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  61. public async Task<IActionResult> GetCurrencyExchange(ExchangeRateDto rateDto)
  62. {
  63. #region 参数处理
  64. if (string.IsNullOrEmpty(rateDto.CurrencyCodePer) || string.IsNullOrEmpty(rateDto.CurrencyCodeSuf))
  65. {
  66. return Ok(JsonView(false, "币种参数为空!"));
  67. }
  68. if (rateDto.Money <= 0)
  69. {
  70. return Ok(JsonView(false, "转换金额 <= 0!"));
  71. }
  72. #endregion
  73. var result = await _juHeApiService.GetExchangeRateAsync(rateDto);
  74. if (result.Code != 0 || result == null)
  75. {
  76. return Ok(JsonView(false, result.Msg));
  77. }
  78. return Ok(JsonView(result.Data));
  79. }
  80. #endregion
  81. #region 有道API 文本翻译
  82. /// <summary>
  83. /// 有道翻译 所有语种
  84. /// </summary>
  85. /// <returns></returns>
  86. [HttpPost]
  87. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  88. public async Task<IActionResult> GetLuaguages()
  89. {
  90. var luaguages = new GetYouDaoLanguages().Luanguages;
  91. return Ok(JsonView(luaguages));
  92. }
  93. /// <summary>
  94. /// 有道翻译
  95. /// </summary>
  96. /// <param name="dto">待翻译的文本 目前只支持 中转英</param>
  97. /// <returns></returns>
  98. [HttpPost]
  99. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  100. public async Task<IActionResult> GetTranslate(YouDaoDto dto)
  101. {
  102. if (string.IsNullOrEmpty(dto.Txt))
  103. {
  104. return Ok(JsonView("待翻译的文本不能为空!"));
  105. }
  106. string from = "zh-CHS", to = "en";
  107. if (!string.IsNullOrEmpty(dto.From) && !string.IsNullOrEmpty(dto.To))
  108. {
  109. var luaguuages = new GetYouDaoLanguages().Luanguages;
  110. var fromLuaguage = luaguuages.Where(a => a.Code == dto.From).FirstOrDefault();
  111. var toLuaguage = luaguuages.Where(a => a.Code == dto.To).FirstOrDefault();
  112. if (fromLuaguage == null)
  113. {
  114. return Ok(JsonView("源语言code不支持!"));
  115. }
  116. if (toLuaguage == null)
  117. {
  118. return Ok(JsonView("目标语言code不支持!"));
  119. }
  120. from = dto.From;
  121. to = dto.To;
  122. }
  123. var translateData = await YouDaoApiTools.GetReTrans(dto.Txt, from, to);
  124. if (translateData == null || translateData.Code != 0)
  125. {
  126. return Ok(JsonView(false, translateData.Msg));
  127. }
  128. return Ok(JsonView( new { translation = translateData.Data }));
  129. }
  130. #endregion
  131. #region googleMapApi
  132. /// <summary>
  133. /// googleMapApi 获取距离And时间
  134. /// </summary>
  135. /// <param name="dto">
  136. /// 请求参数默认值
  137. /// origin:"FERİT RECAİ ERTUĞRUL CAD. NO: 18 ORAN / ANKARA,Turkey";
  138. /// destination : "Dudullu Mh. Imes Sanayi Sitesi 504. Sk. No:4/B1 Umraniye/Istanbul";
  139. /// </param>
  140. /// <returns></returns>
  141. [HttpPost]
  142. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  143. public async Task<IActionResult> GetGooglMapForeignAndTime(GoogleMapDirectionsDto dto)
  144. {
  145. var data = await GoogleMapTools.GetForeignAndTime(dto.origin,dto.destination);
  146. if (data == null || data.Code != 0)
  147. {
  148. return Ok(JsonView(false, data.Msg));
  149. }
  150. return Ok(JsonView(data.Data));
  151. }
  152. #endregion
  153. #region ChatGPT
  154. /// <summary>
  155. /// ChatGPT 自动补全
  156. /// </summary>
  157. /// <param name="dto"> </param>
  158. /// <returns></returns>
  159. [HttpPost]
  160. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  161. public async Task<IActionResult> ChatGptCompletions(CompletionsDto dto)
  162. {
  163. if (string.IsNullOrEmpty(dto.Prompt))
  164. {
  165. return Ok(JsonView(false,"待自动补全的文本为空!"));
  166. }
  167. Stopwatch sw = new Stopwatch();
  168. sw.Start();
  169. Thread.Sleep(999);
  170. Console.WriteLine($"程序耗时:{sw.ElapsedMilliseconds}ms.");
  171. var data = await ChatGPTTools.Completions(dto.Prompt);
  172. sw.Stop();
  173. if (data == null || data.Code != 0)
  174. {
  175. return Ok(JsonView(false, data.Msg+"【耗时:"+ sw.ElapsedMilliseconds/1000+ "ms】"));
  176. }
  177. return Ok(JsonView(200, "【耗时:"+ sw.ElapsedMilliseconds/1000 + "s】", data.Data));
  178. }
  179. #endregion
  180. }
  181. }