ScrmController.php 15 KB

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