WeCallService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. use app\common\model\dict\DictData;
  16. /**
  17. * 网易云商智能外呼service
  18. * Class WeCallService
  19. * @package app\adminapi\service
  20. */
  21. class WeCallService
  22. {
  23. protected $appKey;
  24. protected $appSecret;
  25. protected $taskId;
  26. protected $status = 0;
  27. public function __construct()
  28. {
  29. $config = DictData::where('type_value', 'wecall')->column('value','name');
  30. if ($config) {
  31. $this->appKey = isset($config['appKey']) ?? '';
  32. $this->appSecret = isset($config['appSecret']) ?? '';
  33. $this->taskId = isset($config['taskId']) ?? '';
  34. $this->status = isset($config['status']) ?? 0;
  35. }
  36. }
  37. /**
  38. * 生成sign签名
  39. * @param $requestQuery GET请求时的url参数数组
  40. * @param $requestBody POST请求时的body参数Json字符串
  41. * @param $timestamp 时间戳(毫秒级)
  42. * @return string
  43. */
  44. public function getSign($requestQuery,$requestBody,$timestamp) {
  45. // 拼接值
  46. $queryStr = "";
  47. ksort($requestQuery);
  48. foreach($requestQuery as $value) {
  49. $queryStr .= $value;
  50. }
  51. // 得到签名值
  52. $needSign = $timestamp. $queryStr. $requestBody;
  53. $sign = hash_hmac('sha256', $needSign, $this->appSecret);
  54. return $sign;
  55. }
  56. /*
  57. * 向外呼任务导入客户
  58. */
  59. public function importUser($customerList){
  60. if ($this->status == 0) {
  61. return false;
  62. }
  63. $requestBody = [
  64. 'taskId' => $this->taskId,
  65. 'removeRepeat' => false,
  66. 'encryptedPhone' => false,
  67. 'customerList' => $customerList
  68. ];
  69. $requestBody = json_encode($requestBody);
  70. $timestamp = $this->get_millisecond_timestamp();
  71. $sign = $this->getSign([],$requestBody,$timestamp);
  72. //请求导入客户信息接口
  73. $header = [
  74. 'Content-Type: application/json',
  75. 'X-YS-APIKEY: '.$this->appKey,
  76. 'X-YS-TIME: '.$timestamp,
  77. 'X-YS-SIGNATURE: '.$sign,
  78. ];
  79. $response = http_request('https://b.163.com/open/api/wecall/v1/task/importCustomer', $requestBody, $header);
  80. return $response;
  81. }
  82. /*
  83. * 开启外呼任务(仅手动任务可调用)
  84. */
  85. public function startTask(){
  86. if ($this->status == 0) {
  87. return false;
  88. }
  89. $requestBody = [
  90. 'taskId' => $this->taskId
  91. ];
  92. $requestBody = json_encode($requestBody);
  93. $timestamp = $this->get_millisecond_timestamp();
  94. $sign = $this->getSign([],$requestBody,$timestamp);
  95. //请求导入客户信息接口
  96. $header = [
  97. 'Content-Type: application/json',
  98. 'X-YS-APIKEY: '.$this->appKey,
  99. 'X-YS-TIME: '.$timestamp,
  100. 'X-YS-SIGNATURE: '.$sign,
  101. ];
  102. $response = http_request('https://b.163.com/open/api/wecall/v1/task/start', $requestBody, $header);
  103. return $response;
  104. }
  105. /*
  106. * 停止外呼任务(仅手动任务可调用)
  107. */
  108. public function stopTask(){
  109. if ($this->status == 0) {
  110. return false;
  111. }
  112. $requestBody = [
  113. 'taskId' => $this->taskId
  114. ];
  115. $requestBody = json_encode($requestBody);
  116. $timestamp = $this->get_millisecond_timestamp();
  117. $sign = $this->getSign([],$requestBody,$timestamp);
  118. //请求导入客户信息接口
  119. $header = [
  120. 'Content-Type: application/json',
  121. 'X-YS-APIKEY: '.$this->appKey,
  122. 'X-YS-TIME: '.$timestamp,
  123. 'X-YS-SIGNATURE: '.$sign,
  124. ];
  125. $response = http_request('https://b.163.com/open/api/wecall/v1/task/stop', $requestBody, $header);
  126. return $response;
  127. }
  128. /**
  129. * 数据回调接口
  130. */
  131. public function notify($body) {
  132. $body = json_decode($body, true);
  133. if ($body && isset($body['dataType'])) {
  134. if ($body['dataType'] == 'ROBOT_TASK_STATUS_CHANGE') {
  135. $this->taskStatusChange($body['data']);
  136. }
  137. }
  138. }
  139. private function taskStatusChange($data)
  140. {
  141. $taskId = $data['taskId'];
  142. $status = $data['status'];
  143. if ($taskId == $this->taskId && $status == 'COMPLETE') {
  144. // TODO 任务状态改变
  145. $this->stopTask();
  146. }
  147. }
  148. /**
  149. * 获取时间戳(毫秒级)
  150. */
  151. public function get_millisecond_timestamp() {
  152. $microtime = microtime(true);
  153. $timestamp = floor($microtime * 1000);
  154. return $timestamp;
  155. }
  156. }