WeCallService.php 6.0 KB

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