ScrmController.php 15 KB

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