ScrmController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace App\Http\Controllers\Api\Third;
  3. use App\Http\Controllers\Api\ApiBaseController;
  4. use Illuminate\Http\Request;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\Cache;
  7. use App\Models\HukeCallback;
  8. use App\Models\Member;
  9. use App\Models\MemberShuobo;
  10. use App\Services\Auth\RegisterService;
  11. class ScrmController extends ApiBaseController
  12. {
  13. /**
  14. * @var Client
  15. */
  16. private $httpClient;
  17. private $app_key;
  18. private $app_secret;
  19. /**
  20. * SmsService constructor.
  21. * @param $app_key
  22. * @param $app_secret
  23. * @param $sign_key
  24. */
  25. public function __construct()
  26. {
  27. $this->app_key = '817267c4c2f3476b863522525b97ac78';
  28. $this->app_secret = '559f778b1b4f4e47b0265fa5fbedfb54';
  29. $this->httpClient=new Client([
  30. 'http_errors' => false
  31. ]);
  32. }
  33. public function pullData(Request $request)
  34. {
  35. $log = [
  36. 'content' => json_encode($request->all()),
  37. ];
  38. HukeCallback::create($log);
  39. $data = $request->all();
  40. //$fields = $this->get_customer_fields();
  41. switch ($data['type']){
  42. case '1011':
  43. $param = json_decode($data['params']['customerEvent']);
  44. $res = $this->get_customer_query(3,[$param['cid']]);//根据返回的cid查询用户信息
  45. if($res){//查询成功
  46. $customer = $this->format_user($res[0]);
  47. switch ($customer['label']){
  48. case '硕博人才':
  49. $user = Member::where('mobile',$customer['mobile'])->first();
  50. if(!$user){
  51. //手机号找不到聚才网账户,进行注册并入库
  52. $registerService = new RegisterService();
  53. $user = $registerService->registerPersonScrm($customer);
  54. }
  55. //如果手机号在聚才网已存在账户,将信息入库
  56. $customer['uid'] = $user->id;
  57. MemberShuobo::create($customer);
  58. break;
  59. }
  60. }
  61. break;
  62. }
  63. }
  64. public function get_customer_fields($update = false)
  65. {
  66. if(!$update){
  67. if(Cache::has("huke_customer_fields")){
  68. return Cache::get("huke_customer_fields");
  69. }
  70. }
  71. $params = [];
  72. $params = json_encode($params);
  73. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  74. $time = time();
  75. $sign = md5($params);
  76. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  77. $headers = [
  78. 'Content-Type' => 'application/json',
  79. 'ur-appkey' => $this->app_key,
  80. 'ur-sign' => $sign,
  81. 'ur-curtime' => $time,
  82. 'ur-checksum' => $checksum
  83. ];
  84. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/fields', ['body'=>$params,'headers' => $headers]);
  85. $res = json_decode($response->getBody()->getContents(),true);
  86. $fields = [];
  87. foreach ($res['data'] as $k => $v){
  88. $fields[$v['id']] = $v;
  89. }
  90. Cache::put('huke_customer_fields',$fields,'86400');
  91. return $fields;
  92. }
  93. /**
  94. * 查询客户列表
  95. * @param $start
  96. * @param $end
  97. * @param $page
  98. * @param $page_size
  99. * @throws \GuzzleHttp\Exception\GuzzleException
  100. */
  101. public function get_customer_list($start,$end,$page,$page_size)
  102. {
  103. $params = [
  104. 'startTime' => str_pad(strtotime($start),13,'0',STR_PAD_RIGHT),
  105. 'endTime' => str_pad(strtotime($end),13,'0',STR_PAD_RIGHT),
  106. 'page'=>$page,
  107. 'pageSize'=>$page_size
  108. ];
  109. $params = json_encode($params);
  110. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  111. $time = time();
  112. $sign = md5($params);
  113. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  114. $headers = [
  115. 'Content-Type' => 'application/json',
  116. 'ur-appkey' => $this->app_key,
  117. 'ur-sign' => $sign,
  118. 'ur-curtime' => $time,
  119. 'ur-checksum' => $checksum
  120. ];
  121. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/list', ['body'=>$params,'headers' => $headers]);
  122. $res = json_decode($response->getBody()->getContents(),true);
  123. dd($res);
  124. }
  125. /**
  126. * 格式化互客用户信息
  127. * @param array $data
  128. */
  129. public function format_user(array $data)
  130. {
  131. $user = [];
  132. if(is_array($data['customFields'])){
  133. foreach ($data['customFields'] as $k => $v){
  134. switch ($v['id']){
  135. case -1:
  136. $user['avatars'] = $v['fieldValue'];
  137. break;
  138. case -2:
  139. $user['realname'] = $v['fieldValue'];
  140. break;
  141. case -3:
  142. $user['mobile'] = $v['fieldValue'];
  143. break;
  144. case -4:
  145. $user['email'] = $v['fieldValue'];
  146. break;
  147. case -5:
  148. $user['weixin'] = $v['fieldValue'];
  149. break;
  150. case -6:
  151. $user['wechat_name'] = $v['fieldValue'];
  152. break;
  153. case -7:
  154. $user['sex'] = $v['fieldValue'];
  155. break;
  156. case -8:
  157. $user['area'] = $v['fieldValue'];
  158. break;
  159. case -9:
  160. $user['tel'] = $v['fieldValue'];
  161. break;
  162. case -10:
  163. $user['tel_ex'] = $v['fieldValue'];
  164. break;
  165. case 10018777:
  166. $user['label'] = $v['fieldValue'];
  167. break;
  168. case 10019746:
  169. $user['address'] = $v['fieldValue'];
  170. break;
  171. case 10019747:
  172. $user['trade'] = $v['fieldValue'];
  173. break;
  174. case 10019748:
  175. $user['company'] = $v['fieldValue'];
  176. break;
  177. case 10020717:
  178. $user['job'] = $v['fieldValue'];
  179. break;
  180. case 10018778:
  181. $user['birthday'] = $v['fieldValue'];
  182. break;
  183. case 10019749:
  184. $user['origin'] = $v['fieldValue'];
  185. break;
  186. case 10018779:
  187. $user['remark'] = $v['fieldValue'];
  188. break;
  189. case 10018780:
  190. $user['attachment'] = $v['fieldValue'];
  191. break;
  192. case 10019750:
  193. $user['cid'] = $v['fieldValue'];
  194. break;
  195. case 10019751:
  196. $user['age_range'] = $v['fieldValue'];
  197. break;
  198. case 10019765:
  199. $user['age'] = $v['fieldValue'];
  200. break;
  201. case 10031724:
  202. $user['level'] = $v['fieldValue'];
  203. break;
  204. case 10051871:
  205. $user['edu'] = $v['fieldValue'];
  206. break;
  207. case 15293060:
  208. $user['politic_countenance'] = $v['fieldValue'];
  209. break;
  210. case 15353563:
  211. $user['education'] = $v['fieldValue'];
  212. break;
  213. case 15352635:
  214. $user['school'] = $v['fieldValue'];
  215. break;
  216. case 15355464:
  217. $user['pro'] = $v['fieldValue'];
  218. break;
  219. case 15354507:
  220. $user['graduation_time'] = $v['fieldValue'];
  221. break;
  222. case 15352636:
  223. $user['country'] = $v['fieldValue'];
  224. break;
  225. case 15352650:
  226. $user['come_in_time'] = $v['fieldValue'];
  227. break;
  228. case 15353579:
  229. $user['require'] = $v['fieldValue'];
  230. break;
  231. case 15353580:
  232. $user['trade_type'] = $v['fieldValue'];
  233. break;
  234. case 15355480:
  235. $user['speciality'] = $v['fieldValue'];
  236. break;
  237. }
  238. }
  239. }
  240. return $user;
  241. }
  242. /**
  243. * @param int $type 0-手机,1-微信号, 2-邮箱,3-cid 4-唯一ID,5-姓名,6-公司/客户名称,7-微信unionId
  244. * @param array $value 对应查询类型的字段列表,数量不能超过100
  245. */
  246. public function get_customer_query(int $type,array $value)
  247. {
  248. if($type < 0 || $type > 7)
  249. {
  250. return false;
  251. }
  252. if(count($value) < 1){
  253. return false;
  254. }
  255. $params = [
  256. 'type' => $type,
  257. 'targets' => $value
  258. ];
  259. $params = json_encode($params);
  260. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  261. $time = time();
  262. $sign = md5($params);
  263. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  264. $headers = [
  265. 'Content-Type' => 'application/json',
  266. 'ur-appkey' => $this->app_key,
  267. 'ur-sign' => $sign,
  268. 'ur-curtime' => $time,
  269. 'ur-checksum' => $checksum
  270. ];
  271. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/query', ['body'=>$params,'headers' => $headers]);
  272. $res = json_decode($response->getBody()->getContents(),true);
  273. return $res['data'];
  274. }
  275. public function test()
  276. {
  277. $res = $this->get_customer_fields();
  278. dd($res);
  279. $params = [
  280. 'type' => 3,
  281. 'targets' => [
  282. '110762053'
  283. ]
  284. ];
  285. $params = json_encode($params);
  286. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  287. $time = time();
  288. $sign = md5($params);
  289. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  290. $headers = [
  291. 'Content-Type' => 'application/json',
  292. 'ur-appkey' => $this->app_key,
  293. 'ur-sign' => $sign,
  294. 'ur-curtime' => $time,
  295. 'ur-checksum' => $checksum
  296. ];
  297. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/query', ['body'=>$params,'headers' => $headers]);
  298. $res = json_decode($response->getBody()->getContents(),true);
  299. dd($res);
  300. }
  301. }