WeCallService.php 5.5 KB

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