ScrmController.php 14 KB

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