TianYuVoiceHelper.cs 4.2 KB

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