TianYuVoiceHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using MySystem.Models;
  7. using LitJson;
  8. using System.Collections.Generic;
  9. using System.Security.Cryptography;
  10. namespace MySystem
  11. {
  12. public class TianYuVoiceHelper
  13. {
  14. public readonly static TianYuVoiceHelper Instance = new TianYuVoiceHelper();
  15. private TianYuVoiceHelper()
  16. {
  17. }
  18. // string accesskeyld = "IOT_ACCOUNT_TEST_CSYH";
  19. // string accesskeySecret = "AtR4g5sa24fdy6GII3hts75_CSYH";
  20. // string reqUrl = "https://voicetest.tysmartpos.com/api/bank/pushmsg";
  21. string accesskeyld = "IOT_ACCOUNT_PROD_W966HG08MCOXHNT2";
  22. string accesskeySecret = "F2jzl8vGkm0fMeDLyYo40wEbzdaiTX2ywj3YNgszJHAbVbcAYIJL9bzzS9lVPPL6";
  23. string reqUrl = "https://voice.tysmartpos.com/api/bank/pushmsg";
  24. // public void Start()//启动
  25. // {
  26. // Thread thread = new Thread(listen);
  27. // thread.IsBackground = true;
  28. // thread.Start();
  29. // }
  30. // public void listen()
  31. // {
  32. // while (true)
  33. // {
  34. // string content = RedisDbconn.Instance.RPop<string>("TianYuVoiceQueue");
  35. // if (!string.IsNullOrEmpty(content))
  36. // {
  37. // try
  38. // {
  39. // doSomething(content);
  40. // Thread.Sleep(10);
  41. // }
  42. // catch (Exception ex)
  43. // {
  44. // LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "天喻云喇叭异常");
  45. // }
  46. // }
  47. // else
  48. // {
  49. // Thread.Sleep(1000);
  50. // }
  51. // }
  52. // }
  53. public string doSomething(string OrderNo, string DeviceId, string Amount)
  54. {
  55. string result = "";
  56. try
  57. {
  58. SortedList<string, string> data = new SortedList<string, string>();
  59. data.Add("accessKeyId", accesskeyld); //由本公司统一提供给客户的唯一识别码(同accessKeyId)
  60. data.Add("requestId", OrderNo); //请求标识,用于唯一标识当前请求
  61. data.Add("timestamp", function.GetCurTimestamp().ToString()); //请求时间戳,格式如:1593532800000
  62. data.Add("deviceId", DeviceId); //设备编号
  63. data.Add("content", "来客八收款" + Amount + "元"); //(终端直接播报内容,支持 tts 设备可以直接播报汉字语音,不支持 tts 的设备要分开传 requestData、payType)
  64. // data.Add("requestData", "0.01"); //播报金额((只送金额,单位:元)
  65. // data.Add("payType", "01"); //播报类型: 01-XX银行收款成功 02-支付宝收款成功 03-微信收款成功 10-取消支付
  66. result = post(data);
  67. }
  68. catch (Exception ex)
  69. {
  70. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "天喻云喇叭异常");
  71. }
  72. return result;
  73. }
  74. private string post(SortedList<string, string> data)
  75. {
  76. string signString = function.BuildQueryString(data);
  77. data.Add("sign", hmac_sha256(signString));
  78. string req = Newtonsoft.Json.JsonConvert.SerializeObject(data);
  79. function.WriteLog("请求地址:" + reqUrl, "天谕音响日志");
  80. function.WriteLog("请求参数:" + req, "天谕音响日志");
  81. string result = function.PostWebRequest(reqUrl, req, "application/json");
  82. function.WriteLog("响应数据:" + result, "天谕音响日志");
  83. return result;
  84. }
  85. #region hmac_sha256算法
  86. public string hmac_sha256(string message)
  87. {
  88. var encoding = new System.Text.UTF8Encoding();
  89. byte[] keyByte = encoding.GetBytes(accesskeySecret);
  90. byte[] messageBytes = encoding.GetBytes(message);
  91. using (var hmacsha256 = new HMACSHA256(keyByte))
  92. {
  93. byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
  94. return Convert.ToBase64String(hashmessage);
  95. }
  96. }
  97. #endregion
  98. }
  99. }