1
0

WeCallService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\service;
  15. /**
  16. * 网易云商智能外呼service
  17. * Class WeCallService
  18. * @package app\adminapi\service
  19. */
  20. class WeCallService
  21. {
  22. protected $appKey;
  23. protected $appSecret;
  24. protected $taskId;
  25. public function __construct()
  26. {
  27. $config = config('custom.wecall');
  28. if ($config) {
  29. $this->appKey = $config['appKey'];
  30. $this->appSecret = $config['appSecret'];
  31. $this->taskId = $config['taskId'];
  32. }
  33. }
  34. /**
  35. * 生成sign签名
  36. * @param $requestQuery GET请求时的url参数数组
  37. * @param $requestBody POST请求时的body参数Json字符串
  38. * @param $timestamp 时间戳(毫秒级)
  39. * @return string
  40. */
  41. public function getSign($requestQuery,$requestBody,$timestamp) {
  42. // 拼接值
  43. $queryStr = "";
  44. ksort($requestQuery);
  45. foreach($requestQuery as $value) {
  46. $queryStr .= $value;
  47. }
  48. // 得到签名值
  49. $needSign = $timestamp. $queryStr. $requestBody;
  50. $sign = hash_hmac('sha256', $needSign, $this->appSecret);
  51. return $sign;
  52. }
  53. /*
  54. * 向外呼任务导入客户
  55. */
  56. public function importUser($customerList){
  57. if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) {
  58. return false;
  59. }
  60. $requestBody = [
  61. 'taskId' => $this->taskId,
  62. 'removeRepeat' => false,
  63. 'encryptedPhone' => false,
  64. 'customerList' => $customerList
  65. ];
  66. $requestBody = json_encode($requestBody);
  67. $timestamp = $this->get_millisecond_timestamp();
  68. $sign = $this->getSign([],$requestBody,$timestamp);
  69. //请求导入客户信息接口
  70. $header = [
  71. 'Content-Type: application/json',
  72. 'X-YS-APIKEY: '.$this->appKey,
  73. 'X-YS-TIME: '.$timestamp,
  74. 'X-YS-SIGNATURE: '.$sign,
  75. ];
  76. $response = http_request('https://b.163.com/open/api/wecall/v1/task/importCustomer', $requestBody, $header);
  77. return $response;
  78. }
  79. /*
  80. * 开启外呼任务(仅手动任务可调用)
  81. */
  82. public function startTask(){
  83. if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) {
  84. return false;
  85. }
  86. $requestBody = [
  87. 'taskId' => $this->taskId
  88. ];
  89. $requestBody = json_encode($requestBody);
  90. $timestamp = $this->get_millisecond_timestamp();
  91. $sign = $this->getSign([],$requestBody,$timestamp);
  92. //请求导入客户信息接口
  93. $header = [
  94. 'Content-Type: application/json',
  95. 'X-YS-APIKEY: '.$this->appKey,
  96. 'X-YS-TIME: '.$timestamp,
  97. 'X-YS-SIGNATURE: '.$sign,
  98. ];
  99. $response = http_request('https://b.163.com/open/api/wecall/v1/task/start', $requestBody, $header);
  100. return $response;
  101. }
  102. /*
  103. * 停止外呼任务(仅手动任务可调用)
  104. */
  105. public function stopTask(){
  106. if (empty($this->appKey) || empty($this->appSecret) || empty($this->taskId)) {
  107. return false;
  108. }
  109. $requestBody = [
  110. 'taskId' => $this->taskId
  111. ];
  112. $requestBody = json_encode($requestBody);
  113. $timestamp = $this->get_millisecond_timestamp();
  114. $sign = $this->getSign([],$requestBody,$timestamp);
  115. //请求导入客户信息接口
  116. $header = [
  117. 'Content-Type: application/json',
  118. 'X-YS-APIKEY: '.$this->appKey,
  119. 'X-YS-TIME: '.$timestamp,
  120. 'X-YS-SIGNATURE: '.$sign,
  121. ];
  122. $response = http_request('https://b.163.com/open/api/wecall/v1/task/stop', $requestBody, $header);
  123. return $response;
  124. }
  125. /**
  126. * 数据回调接口
  127. */
  128. public function notify($body) {
  129. $body = json_decode($body, true);
  130. if ($body && isset($body['dataType'])) {
  131. if ($body['dataType'] == 'ROBOT_TASK_STATUS_CHANGE') {
  132. $this->taskStatusChange($body['data']);
  133. }
  134. }
  135. }
  136. private function taskStatusChange($data)
  137. {
  138. $taskId = $data['taskId'];
  139. $status = $data['status'];
  140. $updateTime = $data['updateTime'];
  141. if ($taskId == $this->taskId && $status == 'COMPLETE') {
  142. // TODO 任务状态改变
  143. }
  144. }
  145. /**
  146. * 获取时间戳(毫秒级)
  147. */
  148. public function get_millisecond_timestamp() {
  149. $microtime = microtime(true);
  150. $timestamp = floor($microtime * 1000);
  151. return $timestamp;
  152. }
  153. }