SuonaLogic.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace addons\Suona\common\logic;
  3. /**
  4. * 接口文档:https://wiki.yunzmall.com/docs/show/8866
  5. * appId、appSecret配置可以找芸众客服提供
  6. * suona_host配置地址如下:
  7. * 测试域名:https://dev1.yunzmall.com/addons/yun_shop/api.php?i=2&uuid=0&type=5&route=
  8. * 正式域名:https://www.yunqiankeji.com/addons/yun_shop/api.php?i=1&uuid=0&type=5&route=
  9. */
  10. class SuonaLogic
  11. {
  12. public $appId = '';
  13. public $appSecret = '';
  14. public function __construct($appId, $appSecret)
  15. {
  16. $this->appId = $appId;
  17. $this->appSecret = $appSecret;
  18. }
  19. /**
  20. * 发送消息
  21. * @Author:lun
  22. * @Date:2024-02-18 15:45
  23. * @Copyright:copyright(c)2023 广东七件事集团
  24. * @param $url
  25. * @param $device_id
  26. * @param $msg_type
  27. * @param $message
  28. * @param $pay_money
  29. * @return array
  30. */
  31. public function suona($url, $device_id, $msg_type, $message = '', $pay_money = 0)
  32. {
  33. if (is_array($device_id)) {
  34. $device_ids = $device_id;
  35. } else {
  36. $device_ids = [$device_id];
  37. }
  38. if ($msg_type == 0) {
  39. //自定义文本消息
  40. $params = json_encode([
  41. 'device_ids' => $device_ids,
  42. 'msg_type' => $msg_type,
  43. 'message' => $message,
  44. ]);
  45. } else {
  46. //系统自带的文本信息
  47. $params = json_encode([
  48. 'device_ids' => $device_ids,
  49. 'msg_type' => $msg_type,
  50. 'param1' => $pay_money,
  51. ]);
  52. }
  53. $array = $this->curl_post($params, $this->generateHeader(), $url);
  54. return $array;
  55. }
  56. //curl header封装
  57. public function generateHeader()
  58. {
  59. $millisecond = $this->getMillisecond();//为服务端时间前后5分钟内可以访问
  60. $nonceStr = $this->getNonceStr(16);//10到50位字符串
  61. return [
  62. 'appId:' . $this->appId,
  63. 'signature:' . $this->getSignature($this->appId, $this->appSecret, $nonceStr, $millisecond),
  64. 'nonceStr:' . $nonceStr,
  65. 'timestamp:' . $millisecond,
  66. 'Content-Type:application/json',
  67. ];
  68. }
  69. //计算请求签名值
  70. private function getSignature($appId, $appSecret, $nonceStr, $millisecond)
  71. {
  72. $s = $appId . '_' . $appSecret . '_' . $nonceStr . '_' . $millisecond;
  73. return strtoupper(md5($s));
  74. }
  75. //模拟发送POST请求
  76. /**
  77. * 模拟发送POST 方式请求
  78. * @param $url
  79. * @param $data
  80. * @param $projectId
  81. * @param $signature
  82. * @return array
  83. */
  84. // public function http_post_data($data, $header, $url)
  85. // {
  86. // $ch = curl_init();
  87. // curl_setopt($ch, CURLOPT_POST, 1);
  88. // curl_setopt($ch, CURLOPT_URL, $url);
  89. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  90. // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  91. // ob_start();
  92. // curl_exec($ch);
  93. // $return_content = ob_get_contents();
  94. // ob_end_clean();
  95. // $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  96. // return array($return_code, $return_content);
  97. // }
  98. public function curl_post($postdata = '', $header = '', $url, $options = array())
  99. {
  100. $ch = curl_init($url);
  101. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  102. curl_setopt($ch, CURLOPT_POST, 1);
  103. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  104. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  105. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  106. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  107. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  108. if (!empty($options)) {
  109. curl_setopt_array($ch, $options);
  110. }
  111. $data = curl_exec($ch);
  112. curl_close($ch);
  113. return $data;
  114. }
  115. /**
  116. *
  117. * 产生随机字符串,不长于50位
  118. * @param int $length
  119. * @return string
  120. */
  121. private function getNonceStr($length = 50)
  122. {
  123. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  124. $str = "";
  125. for ($i = 0; $i < $length; $i++) {
  126. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  127. }
  128. return $str;
  129. }
  130. //获取当前时间戳(毫秒级)
  131. private function getMillisecond()
  132. {
  133. list($s1, $s2) = explode(' ', microtime());
  134. return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
  135. }
  136. }