ScrmController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. private $registerService;
  20. /**
  21. * SmsService constructor.
  22. * @param $app_key
  23. * @param $app_secret
  24. * @param $sign_key
  25. */
  26. public function __construct(RegisterService $registerService)
  27. {
  28. $this->app_key = '817267c4c2f3476b863522525b97ac78';
  29. $this->app_secret = '559f778b1b4f4e47b0265fa5fbedfb54';
  30. $this->httpClient=new Client([
  31. 'http_errors' => false
  32. ]);
  33. $this->registerService = $registerService;
  34. }
  35. public function pullData(Request $request)
  36. {
  37. $log = [
  38. 'content' => serialize($request->all()),
  39. ];
  40. HukeCallback::create($log);
  41. $data = $request->all();
  42. //$fields = $this->get_customer_fields();
  43. switch ($data['type']){
  44. case '1011':
  45. $param = json_decode($data['params']['customerEvent'],true);
  46. $res = $this->get_customer_query(3,[$param['cid']]);//根据返回的cid查询用户信息
  47. if($res){//查询成功
  48. $customer = $this->format_user($res[0]);
  49. $customer['cid'] = $param['cid'];
  50. switch ($customer['label']){
  51. case '硕博人才':
  52. $user = Member::where(['mobile' => $customer['mobile']])->first();
  53. if(!$user){
  54. //手机号找不到聚才网账户,进行注册并入库
  55. $user = $this->registerService->registerPersonScrm($customer);
  56. }else{
  57. //如果有账户,查找scrm_id字段并更新
  58. if(empty($user->scrm_id)){
  59. $user->scrm_id = $param['cid'];
  60. $user->save();
  61. }
  62. }
  63. //如果手机号在聚才网已存在账户,将信息入库
  64. $shuobo_exist = MemberShuobo::where(['uid' => $user->id])->first();
  65. $shuobo_data = array_only($customer,['uid','realname','sex','mobile','email','birthday','education','school','pro','graduation_time','address','country','weixin','company','job','come_in_time','require','trade_type','speciality']);
  66. $shuobo_data['cid'] = $param['cid'];
  67. if(!$shuobo_exist){
  68. $shuobo_data['uid'] = $user->id;
  69. MemberShuobo::create($shuobo_data);
  70. }else{
  71. MemberShuobo::where(['uid' => $user->id])->update($shuobo_data);
  72. }
  73. break;
  74. }
  75. }
  76. break;
  77. }
  78. }
  79. public function get_customer_fields($update = false)
  80. {
  81. if(!$update){
  82. if(Cache::has("huke_customer_fields")){
  83. return Cache::get("huke_customer_fields");
  84. }
  85. }
  86. $params = [];
  87. $params = json_encode($params);
  88. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  89. $time = time();
  90. $sign = md5($params);
  91. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  92. $headers = [
  93. 'Content-Type' => 'application/json',
  94. 'ur-appkey' => $this->app_key,
  95. 'ur-sign' => $sign,
  96. 'ur-curtime' => $time,
  97. 'ur-checksum' => $checksum
  98. ];
  99. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/fields', ['body'=>$params,'headers' => $headers]);
  100. $res = json_decode($response->getBody()->getContents(),true);
  101. $fields = [];
  102. foreach ($res['data'] as $k => $v){
  103. if(array_key_exists('options',$v)){
  104. $options = [];
  105. foreach ($v['options'] as $key => $val){
  106. $options[$val['id']] = $val;
  107. }
  108. $v['options'] = $options;
  109. }
  110. $fields[$v['id']] = $v;
  111. }
  112. Cache::put('huke_customer_fields',$fields,'86400');
  113. return $fields;
  114. }
  115. /**
  116. * 查询客户列表
  117. * @param $start
  118. * @param $end
  119. * @param $page
  120. * @param $page_size
  121. * @throws \GuzzleHttp\Exception\GuzzleException
  122. */
  123. public function get_customer_list($start,$end,$page,$page_size)
  124. {
  125. $params = [
  126. 'startTime' => str_pad(strtotime($start),13,'0',STR_PAD_RIGHT),
  127. 'endTime' => str_pad(strtotime($end),13,'0',STR_PAD_RIGHT),
  128. 'page'=>$page,
  129. 'pageSize'=>$page_size
  130. ];
  131. $params = json_encode($params);
  132. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  133. $time = time();
  134. $sign = md5($params);
  135. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  136. $headers = [
  137. 'Content-Type' => 'application/json',
  138. 'ur-appkey' => $this->app_key,
  139. 'ur-sign' => $sign,
  140. 'ur-curtime' => $time,
  141. 'ur-checksum' => $checksum
  142. ];
  143. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/list', ['body'=>$params,'headers' => $headers]);
  144. $res = json_decode($response->getBody()->getContents(),true);
  145. dd($res);
  146. }
  147. /**
  148. * 格式化互客用户信息
  149. * @param array $data
  150. */
  151. public function format_user(array $data)
  152. {
  153. $user = [];
  154. $fields = $this->get_customer_fields();
  155. if(is_array($data['customFields'])){
  156. foreach ($data['customFields'] as $k => $v){
  157. switch ($v['id']){
  158. case -1:
  159. $user['avatars'] = $v['fieldValue'];
  160. break;
  161. case -2:
  162. $user['realname'] = $v['fieldValue'];
  163. break;
  164. case -3:
  165. $user['mobile'] = $v['fieldValue'];
  166. break;
  167. case -4:
  168. $user['email'] = $v['fieldValue'];
  169. break;
  170. case -5:
  171. $user['weixin'] = $v['fieldValue'];
  172. break;
  173. case -6:
  174. $user['wechat_name'] = $v['fieldValue'];
  175. break;
  176. case -7:
  177. $user['sex'] = $fields[-7]['options'][$v['fieldValue']]['optionValue'];
  178. break;
  179. case -8:
  180. $user['area'] = $v['fieldValue'];
  181. break;
  182. case -9:
  183. $user['tel'] = $v['fieldValue'];
  184. break;
  185. case -10:
  186. $user['tel_ex'] = $v['fieldValue'];
  187. break;
  188. case 10018777:
  189. $user['label'] = $fields[10018777]['options'][$v['fieldValue']]['optionValue'];
  190. break;
  191. case 10019746:
  192. $user['address'] = $v['fieldValue'];
  193. break;
  194. case 10019747:
  195. $user['trade'] = $v['fieldValue'];
  196. break;
  197. case 10019748:
  198. $user['company'] = $v['fieldValue'];
  199. break;
  200. case 10020717:
  201. $user['job'] = $v['fieldValue'];
  202. break;
  203. case 10018778:
  204. $user['birthday'] = $v['fieldValue'];
  205. break;
  206. case 10019749:
  207. $user['origin'] = $fields[10019749]['options'][$v['fieldValue']]['optionValue'];
  208. break;
  209. case 10018779:
  210. $user['remark'] = $v['fieldValue'];
  211. break;
  212. case 10018780:
  213. $user['attachment'] = $v['fieldValue'];
  214. break;
  215. case 10019750:
  216. $user['cid'] = $v['fieldValue'];
  217. break;
  218. case 10019751:
  219. $user['age_range'] = $fields[10019751]['options'][$v['fieldValue']]['optionValue'];
  220. break;
  221. case 10019765:
  222. $user['age'] = $v['fieldValue'];
  223. break;
  224. case 10031724:
  225. $user['level'] = $fields[10031724]['options'][$v['fieldValue']]['optionValue'];
  226. break;
  227. case 10051871:
  228. $user['edu'] = $v['fieldValue'];
  229. break;
  230. case 15293060:
  231. $user['politic_countenance'] = $fields[15293060]['options'][$v['fieldValue']]['optionValue'];
  232. break;
  233. case 15353563:
  234. $user['education'] = $fields[15353563]['options'][$v['fieldValue']]['optionValue'];
  235. break;
  236. case 15352635:
  237. $user['school'] = $v['fieldValue'];
  238. break;
  239. case 15355464:
  240. $user['pro'] = $v['fieldValue'];
  241. break;
  242. case 15354507:
  243. $user['graduation_time'] = date("Y-m-d",$v['fieldValue']/1000);
  244. break;
  245. case 15352636:
  246. $user['country'] = $v['fieldValue'];
  247. break;
  248. case 15352650:
  249. $user['come_in_time'] = date("Y-m-d",$v['fieldValue']/1000);
  250. break;
  251. case 15353579:
  252. $user['require'] = $fields[15353579]['options'][$v['fieldValue']]['optionValue'];
  253. break;
  254. case 15353580:
  255. $user['trade_type'] = $v['fieldValue'];
  256. break;
  257. case 15355480:
  258. $user['speciality'] = $v['fieldValue'];
  259. break;
  260. }
  261. }
  262. }
  263. return $user;
  264. }
  265. /**
  266. * @param int $type 0-手机,1-微信号, 2-邮箱,3-cid 4-唯一ID,5-姓名,6-公司/客户名称,7-微信unionId
  267. * @param array $value 对应查询类型的字段列表,数量不能超过100
  268. */
  269. public function get_customer_query(int $type,array $value)
  270. {
  271. if($type < 0 || $type > 7)
  272. {
  273. return false;
  274. }
  275. if(count($value) < 1){
  276. return false;
  277. }
  278. $params = [
  279. 'type' => $type,
  280. 'targets' => $value
  281. ];
  282. $params = json_encode($params);
  283. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  284. $time = time();
  285. $sign = md5($params);
  286. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  287. $headers = [
  288. 'Content-Type' => 'application/json',
  289. 'ur-appkey' => $this->app_key,
  290. 'ur-sign' => $sign,
  291. 'ur-curtime' => $time,
  292. 'ur-checksum' => $checksum
  293. ];
  294. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/query', ['body'=>$params,'headers' => $headers]);
  295. $res = json_decode($response->getBody()->getContents(),true);
  296. return $res['data'];
  297. }
  298. public function get_customer_events(int $cid,$startTime,$endTime,$page,$pageSize){
  299. $params = [
  300. 'cid' => $cid,
  301. 'startTime' => $startTime,
  302. 'endTime' => $endTime,
  303. 'page'=>$page,
  304. 'pageSize'=>$pageSize
  305. ];
  306. $params = json_encode($params);
  307. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  308. $time = time();
  309. $sign = md5($params);
  310. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  311. $headers = [
  312. 'Content-Type' => 'application/json',
  313. 'ur-appkey' => $this->app_key,
  314. 'ur-sign' => $sign,
  315. 'ur-curtime' => $time,
  316. 'ur-checksum' => $checksum
  317. ];
  318. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/getCustomerEvents', ['body'=>$params,'headers' => $headers]);
  319. $res = json_decode($response->getBody()->getContents(),true);
  320. return $res;
  321. }
  322. public function test()
  323. {
  324. /*===========客户旅程==============*/
  325. $params = [
  326. 'cid' => 42832294,
  327. 'startTime' => str_pad(strtotime('2021-11-01'),13,'0',STR_PAD_RIGHT),
  328. 'endTime' => str_pad(strtotime('2021-11-30'),13,'0',STR_PAD_RIGHT),
  329. 'page'=>2,
  330. 'pageSize'=>50
  331. ];
  332. $params = json_encode($params);
  333. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  334. $time = time();
  335. $sign = md5($params);
  336. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  337. $headers = [
  338. 'Content-Type' => 'application/json',
  339. 'ur-appkey' => $this->app_key,
  340. 'ur-sign' => $sign,
  341. 'ur-curtime' => $time,
  342. 'ur-checksum' => $checksum
  343. ];
  344. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/getCustomerEvents', ['body'=>$params,'headers' => $headers]);
  345. $res = json_decode($response->getBody()->getContents(),true);
  346. return $res;
  347. dd($res);
  348. // $curl = curl_init();
  349. // curl_setopt($curl, CURLOPT_URL, 'https://www.jucai.gov.cn/api/scrm/data');
  350. // //设置头文件的信息作为数据流输出
  351. // curl_setopt($curl, CURLOPT_HEADER, 0);
  352. // //设置获取的信息以文件流的形式返回,而不是直接输出。
  353. // curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  354. // // 超时设置
  355. // curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  356. //
  357. // // 超时设置,以毫秒为单位
  358. // // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
  359. //
  360. // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
  361. // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
  362. //
  363. // //设置post方式提交
  364. // curl_setopt($curl, CURLOPT_POST, 1);
  365. // $post = [
  366. // 'eid' => '1000459',
  367. // 'type' => '1011',
  368. // 'cid' => '108720798',
  369. // 'params' => [
  370. // 'customerEvent' => '{"id":1687628552,"eid":1000459,"cid":110762053,"createTime":1637073249037,"eventCode":309,"eventName":"更新客户资料","fieldList":[{"value":"中国福建泉州","name":"15352636"},{"value":"1656604800000","name":"15352650"},{"value":"1656604800000","name":"15354507"}]}'
  371. // ]
  372. // ];
  373. // curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
  374. // //执行命令
  375. // $data = curl_exec($curl);
  376. // dd($data);
  377. $params = [
  378. 'type' => 3,
  379. 'targets' => [
  380. '110762053'
  381. ]
  382. ];
  383. $params = json_encode($params);
  384. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  385. $time = time();
  386. $sign = md5($params);
  387. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  388. $headers = [
  389. 'Content-Type' => 'application/json',
  390. 'ur-appkey' => $this->app_key,
  391. 'ur-sign' => $sign,
  392. 'ur-curtime' => $time,
  393. 'ur-checksum' => $checksum
  394. ];
  395. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/query', ['body'=>$params,'headers' => $headers]);
  396. $res = json_decode($response->getBody()->getContents(),true);
  397. dd($res);
  398. }
  399. }