FishWh.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\FishCampaign;
  4. use app\model\FishCampaignList;
  5. use app\model\Company;
  6. use app\model\CustomerSource;
  7. use app\model\CustomerVisitLog;
  8. use app\model\FishLive;
  9. use app\model\OutCallLog;
  10. use think\facade\Db;
  11. use clue\Tencent;
  12. use app\model\Org;
  13. use app\model\FishData;
  14. use clue\FishWh as clueFish;
  15. class FishWh
  16. {
  17. /**
  18. * 飞鱼外呼回调
  19. */
  20. public function fish_phone_call_back()
  21. {
  22. $data = input();
  23. if (isset($data['event']) && $data['event'] == 'verify_webhook') {
  24. //返回数据
  25. $this->ajax_return($data['challenge']);
  26. }
  27. if (isset($data['data']) && $data['data']) {
  28. $tmp_data = json_decode($data['data'], true);
  29. if (isset($tmp_data['event']) && $tmp_data['event'] == 'CONTACT_LOG') {
  30. $call_data = json_decode($tmp_data['content'], true);
  31. $log = OutCallLog::where(['session_id' => $call_data['contactId']])->find();
  32. if (empty($log)) return json(['code' => 0, 'msg' => '通话记录不存在']);
  33. $startTime = is_int($call_data['startTime']) ? date('Y-m-d H:i:s', $call_data['startTime'] / 1000) : $call_data['startTime'];
  34. $endTime = is_int($call_data['endTime']) ? date('Y-m-d H:i:s', $call_data['endTime'] / 1000) : $call_data['endTime'];
  35. $log->save([
  36. 'fwd_start_time' => $startTime,
  37. 'call_end_time' => $endTime,
  38. 'billsec' => $call_data['duration'],
  39. 'status' => $call_data['duration'] > 0 ? 1 : -1,
  40. 'call_status' => 1000,
  41. 'url' => $call_data['audioUrl'],
  42. 'call_msg' => '飞鱼线索-' . $call_data['endStateShowMsg']
  43. ]);
  44. $visitLog = CustomerVisitLog::where(['data_type' => 'out_call', 'data_id' => $log->id])->find();
  45. if (!empty($visitLog)) {
  46. if ($call_data['duration'] > 0) {
  47. $visitLog->remark = '回访跟踪了飞鱼线索客户,通话时长' . $call_data['duration'] . 's';
  48. } else {
  49. $visitLog->remark = '回访跟踪了飞鱼线索客户,电话未接通';
  50. }
  51. $visitLog->save();
  52. }
  53. }
  54. }
  55. //返回数据
  56. // Company::where("root_id = 1")->update(['channel_setting_wh'=>json_encode($data)]);
  57. $this->ajax_return($data['nonce']);
  58. }
  59. /**
  60. * 飞鱼返回
  61. */
  62. public function ajax_return($data)
  63. {
  64. $return = array(
  65. 'BaseResp' => ['StatusCode' => 200, 'StatusMessage' => 'ok'],
  66. 'challenge' => intval($data),
  67. );
  68. header('Content-Type:application/json; charset=utf-8');
  69. exit(json_encode($return, JSON_UNESCAPED_UNICODE));
  70. }
  71. /**
  72. * 飞鱼授权回调
  73. */
  74. public function fish_call_back()
  75. {
  76. $code = input('auth_code');
  77. $state = input('state');
  78. $appid = config('app.fish_clue_appid_wh');
  79. $secret = config('app.fish_clue_secret_wh');
  80. $data = (new clueFish($appid, $secret))->getAccessToken($code);
  81. if ($data['code'] == 0) {
  82. $channel_setting_wh = Company::where(['root_id' => $state])->value('channel_setting_wh');
  83. $tmp_data = [
  84. 'fish_account' => '',
  85. 'fish_adv' => '',
  86. 'fish_access_token' => '',
  87. 'fish_refresh_token' => '',
  88. 'fish_refresh_time' => 0,
  89. 'fish_update_time' => '',
  90. 'fish_open' => 0,
  91. 'tx_account' => '',
  92. 'tx_adv' => '',
  93. 'tx_open' => 0,
  94. 'tx_access_token' => '',
  95. 'tx_refresh_token' => '',
  96. 'tx_update_time' => ''
  97. ];
  98. if ($channel_setting_wh) {
  99. $tmp_data = json_decode($channel_setting_wh, true);
  100. }
  101. $tmp_data['fish_access_token'] = $data['data']['access_token'];
  102. $tmp_data['fish_refresh_token'] = $data['data']['refresh_token'];
  103. $tmp_data['fish_refresh_time'] = time();
  104. Company::where(['root_id' => $state])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  105. $fish = CustomerSource::where(['root_id' => $state, 'source' => '飞鱼线索'])->find();
  106. if (!$fish) {
  107. CustomerSource::create(['root_id' => $state, 'source' => '飞鱼线索']);
  108. }
  109. return redirect('/dashboard/index/index');
  110. }
  111. return redirect('/dashboard/index/index');
  112. }
  113. /**
  114. * 每天获取一次昨日的数据(数据稳定)保存入数据库-同时更新广告组下的广告计划--需要定时任务
  115. */
  116. public function update_fish_campaign_yesterday()
  117. {
  118. //查找存在配置的公司
  119. $list = Company::where("channel_setting_wh <> 'null'")->field('root_id,channel_setting_wh')->select()->toArray();
  120. if (isset($list) && $list) {
  121. $fish = new clueFish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
  122. foreach ($list as $val) {
  123. if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
  124. $root_id = $val['root_id'];
  125. $tmp_data = json_decode($val['channel_setting_wh'], true);
  126. $start_date = date('Y-m-d', strtotime('-1 day'));
  127. //更新飞鱼
  128. $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
  129. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  130. foreach ($advertiser_id as $va) {
  131. $data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => 1]);
  132. if ($data['code'] == 0) {
  133. //如果广告组数据存在多页
  134. $page = $data['data']['page_info']['total_page'];
  135. $this->update_fish_campaign_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  136. if ($page > 1) {
  137. for ($i = 1; $i < $page; $i++) {
  138. $page_data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => $i + 1]);
  139. if ($page_data['code'] == 0) {
  140. $this->update_fish_campaign_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. return json(['code' => 0, 'msg' => '更新成功']);
  150. }
  151. /**
  152. * 获取今日数据
  153. */
  154. public function update_fish_campaign_today()
  155. {
  156. //查找存在配置的公司
  157. $list = Company::where("channel_setting_wh <> 'null'")->field('root_id,channel_setting_wh')->select()->toArray();
  158. if (isset($list) && $list) {
  159. $appid = config('app.fish_clue_appid_wh');
  160. $secret = config('app.fish_clue_secret_wh');
  161. $fish = new clueFish($appid, $secret);
  162. foreach ($list as $val) {
  163. if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
  164. $root_id = $val['root_id'];
  165. $tmp_data = json_decode($val['channel_setting_wh'], true);
  166. $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
  167. $start_date = date('Y-m-d');
  168. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  169. foreach ($advertiser_id as $va) {
  170. $data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => 1]);
  171. if ($data['code'] == 0) {
  172. //如果广告组数据存在多页
  173. $page = $data['data']['page_info']['total_page'];
  174. $this->update_fish_campaign_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  175. if ($page > 1) {
  176. for ($i = 1; $i < $page; $i++) {
  177. $page_data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => $i + 1]);
  178. if ($page_data['code'] == 0) {
  179. $this->update_fish_campaign_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. return json(['code' => 0, 'msg' => '更新成功']);
  189. }
  190. /**
  191. * 更新数据
  192. */
  193. public function update_fish_campaign_data($list, $fish, $tmp_data, $root_id, $data_time, $advertiser_id)
  194. {
  195. $data = [];
  196. foreach ($list as $val) {
  197. //先删除昨日数据后插入数据
  198. FishCampaign::where(['root_id' => $root_id, 'data_time' => $data_time, 'advertiser_id' => $advertiser_id, 'campaign_id' => $val['campaign_id']])->delete();
  199. FishCampaignList::where(['root_id' => $root_id, 'data_time' => $data_time, 'advertiser_id' => $advertiser_id, 'campaign_id' => $val['campaign_id']])->delete();
  200. $val['advertiser_id'] = $advertiser_id;
  201. $val['data_time'] = $data_time;
  202. $val['root_id'] = $root_id;
  203. unset($val['id']);
  204. $data[] = $val;
  205. //循环广告组获取广告组下的计划花费
  206. $adv_data = $fish->getAdList($tmp_data['fish_access_token'], ['advertiser_id' => $advertiser_id, 'campaign_ids' => $val['campaign_id'], 'start_date' => $data_time, 'end_date' => $data_time, 'page' => 1]);
  207. if ($adv_data['code'] == 0) {
  208. $page = $adv_data['data']['page_info']['total_page'];
  209. $this->update_fish_adv_data($adv_data['data']['list'], $root_id, $data_time, $advertiser_id);
  210. //如果广告计划存在多页
  211. if ($page > 1) {
  212. for ($i = 1; $i < $page; $i++) {
  213. $tmp_adv_data = $fish->getAdList($tmp_data['fish_access_token'], ['advertiser_id' => $advertiser_id, 'campaign_ids' => $val['campaign_id'], 'start_date' => $data_time, 'end_date' => $data_time, 'page' => $i + 1]);
  214. if ($tmp_adv_data['code'] == 0) {
  215. $this->update_fish_adv_data($tmp_adv_data['data']['list'], $root_id, $data_time, $advertiser_id);
  216. }
  217. }
  218. }
  219. }
  220. }
  221. FishCampaign::insertAll($data);
  222. }
  223. /**
  224. * 更新广告组下的计划
  225. */
  226. public function update_fish_adv_data($data, $root_id, $data_time, $advertiser_id)
  227. {
  228. $tmp_adv_list = [];
  229. foreach ($data as $val) {
  230. unset($val['id']);
  231. unset($val['avg_show_cost']);
  232. $val['root_id'] = $root_id;
  233. $val['advertiser_id'] = $advertiser_id;
  234. $val['data_time'] = $data_time;
  235. $tmp_adv_list[] = $val;
  236. }
  237. FishCampaignList::insertAll($tmp_adv_list);
  238. }
  239. /**
  240. * 刷新token
  241. */
  242. public function refreshToken($fish, $tmp_data, $root_id)
  243. {
  244. $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
  245. if ($fish_refresh_time + 80000 < time()) {
  246. $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
  247. if ($token['code'] == 0) {
  248. $tmp_data['fish_access_token'] = $token['data']['access_token'];
  249. $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
  250. $tmp_data['fish_refresh_time'] = time();
  251. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  252. } else {
  253. $tmp_data['fish_access_token'] = '';
  254. $tmp_data['fish_refresh_token'] = '';
  255. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  256. }
  257. }
  258. return $tmp_data;
  259. }
  260. /**
  261. * 获取直播动态数据
  262. * 3分钟更新一次API 994
  263. * 分页处理定时任务,limit 50
  264. */
  265. public function update_fish_live_today()
  266. {
  267. $test_ads = '1765746298844174,1765954841526279';
  268. $root_id = 994;
  269. //查找存在配置的公司
  270. $list = Company::where("channel_setting_wh <> 'null'")
  271. ->field('root_id,channel_setting_wh')
  272. ->where('root_id',$root_id)
  273. ->limit(50)
  274. ->select()->toArray();
  275. if (isset($list) && $list) {
  276. $appid = config('app.fish_clue_appid_wh');
  277. $secret = config('app.fish_clue_secret_wh');
  278. $fish = new clueFish($appid, $secret);
  279. foreach ($list as $val) {
  280. if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
  281. $root_id = $val['root_id'];
  282. $tmp_data = json_decode($val['channel_setting_wh'], true);
  283. $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
  284. $start_date = date('Y-m-d 00:00:00');
  285. $end_date = date('Y-m-d 23:59:59');
  286. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  287. foreach ($advertiser_id as $va) {
  288. $data = $fish->getLiveAttribute(
  289. $tmp_data['fish_access_token'],
  290. [
  291. 'advertiser_id' => $va,
  292. 'start_date' => $start_date,
  293. 'end_date' => $end_date,
  294. 'page' => 1
  295. ]);
  296. // dump($data);die;
  297. if ($data['code'] == 0) {
  298. //如果广告组数据存在多页
  299. $page = $data['data']['page_info']['total_page'];
  300. $this->update_fish_live_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  301. if ($page > 1) {
  302. for ($i = 1; $i < $page; $i++) {
  303. $page_data = $fish->getLiveAttribute($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => $i + 1]);
  304. if ($page_data['code'] == 0) {
  305. $this->update_fish_live_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  306. }
  307. }
  308. }
  309. }
  310. }
  311. $fields = [
  312. 'live_watch_count',
  313. 'live_watch_ucount',
  314. 'click_product_ucount',
  315. 'live_form_submit_count',
  316. 'live_card_icon_component_show_count',
  317. 'live_avg_watch_duration',
  318. 'live_comment_count',
  319. 'live_follow_count',
  320. 'live_share_count',
  321. 'live_count',
  322. ];
  323. foreach ($advertiser_id as $va) {
  324. $data = $fish->getAnalysisLiveList(
  325. $tmp_data['fish_access_token'],
  326. [
  327. 'advertiser_id' => $va,
  328. 'start_date' => $start_date,
  329. 'end_date' => $end_date,
  330. 'page' => 1,
  331. 'fields' => $fields
  332. ]);
  333. // dump($data);die;
  334. if ($data['code'] == 0) {
  335. //如果广告组数据存在多页
  336. $page = $data['data']['page_info']['total_page'];
  337. $this->update_fish_live_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  338. if ($page > 1) {
  339. for ($i = 1; $i < $page; $i++) {
  340. $page_data = $fish->getAnalysisLiveList(
  341. $tmp_data['fish_access_token'],
  342. [
  343. 'advertiser_id' => $va,
  344. 'start_date' => $start_date,
  345. 'end_date' => $start_date,
  346. 'page' => $i + 1,
  347. 'fields' => $fields
  348. ]);
  349. if ($page_data['code'] == 0) {
  350. $this->update_fish_live_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. return json(['code' => 0, 'msg' => '更新成功']);
  360. }
  361. /**
  362. * 更新直播数据
  363. */
  364. protected function update_fish_live_data($list, $fish, $tmp_data, $root_id, $data_time, $advertiser_id)
  365. {
  366. $data_time = date('Y-m-d',strtotime($data_time));
  367. Db::startTrans();
  368. try {
  369. foreach ($list as $val) {
  370. //先删除昨日数据后插入数据
  371. $info = FishLive::where(['root_id' => $root_id, 'data_time' => $data_time, 'advertiser_id' => $advertiser_id,'room_id'=>$val['room_id']])->find();
  372. $param = [
  373. 'root_id' => $root_id,
  374. 'room_id' => $val['room_id'],
  375. 'data_time' => $data_time,
  376. 'advertiser_id' => $advertiser_id,
  377. 'room_title' => $val['room_title']?$val['room_title']:'',
  378. 'live_watch_count' => $val['fields']['live_watch_count']?$val['fields']['live_watch_count']:0,
  379. 'live_watch_ucount' => $val['fields']['live_watch_ucount']?$val['fields']['live_watch_ucount']:0,
  380. 'live_card_icon_component_show_count' => $val['fields']['live_card_icon_component_show_count']?$val['fields']['live_card_icon_component_show_count']:0,
  381. 'click_product_ucount' => $val['fields']['click_product_ucount']?$val['fields']['click_product_ucount']:0,
  382. 'live_form_submit_count' => $val['fields']['live_form_submit_count']?$val['fields']['live_form_submit_count']:0,
  383. 'room_status' => $val['room_status'],
  384. 'type' => 1,
  385. 'room_create_time' => !empty($val['room_create_time'])?date('Y-m-d H:i:s',strtotime($val['room_create_time'])):'',
  386. 'room_finish_time' => !empty($val['room_finish_time'])?date('Y-m-d H:i:s',strtotime($val['room_finish_time'])):'',
  387. 'anchor_id' => $val['anchor_id']?$val['anchor_id']:'',
  388. 'anchor_nick' => $val['anchor_nick']?$val['anchor_nick']:'',
  389. 'data_create' => date('Y-m-d',strtotime($val['room_create_time'])),
  390. 'live_avg_watch_duration' => $val['fields']['live_avg_watch_duration']?$val['fields']['live_avg_watch_duration']:0,
  391. 'live_comment_count' => $val['fields']['live_comment_count']?$val['fields']['live_comment_count']:0,
  392. 'live_follow_count' => $val['fields']['live_follow_count']?$val['fields']['live_follow_count']:0,
  393. 'live_share_count' => $val['fields']['live_share_count']?$val['fields']['live_share_count']:0,
  394. 'live_count' => $val['fields']['live_count']?$val['fields']['live_count']:0,
  395. ];
  396. $where = [
  397. 'root_id' => $root_id,
  398. 'room_id' => $val['room_id'],
  399. 'data_time' => $data_time,
  400. 'advertiser_id' => $advertiser_id,
  401. ];
  402. if ($info){
  403. FishLive::where($where)->update($param);
  404. }else{
  405. FishLive::where($where)->insert($param);
  406. }
  407. }
  408. Db::commit();
  409. } catch (\Exception $e) {
  410. file_put_contents(dirname(app()->getRuntimePath()) . DIRECTORY_SEPARATOR.'log'.'/update_fish_live_data.txt', date('Y-m-d H:i:s') ."\n"."更新直播数据:".$e->getMessage()."\r\n", FILE_APPEND);
  411. Db::rollback();
  412. }
  413. }
  414. //飞鱼推送线索
  415. public function fish_clue_back()
  416. {
  417. //查找存在配置的公司
  418. $data = input();
  419. $fid = FishData::where(['clue_id' => $data['id'], 'type' => 4])->find();
  420. if (!isset($fid) && empty($fid)){
  421. $save['gender'] = $data['gender'] == '男' ? 1 : ($data['gender'] == '女' ? 2 : 0);
  422. $save['promotion_id'] = $data['promotion_id'];
  423. if(isset($data['req_id']))$save['req_id'] = $data['req_id'];
  424. if(isset($data['external_url']))$save['external_url'] = $data['external_url'];
  425. $save['remark_dict'] = json_encode($data['remark_dict']);
  426. $save['advertiser_id'] = $data['adv_id'];//广告主id
  427. $save['clue_id'] = $data['id'];
  428. if(isset($data['adv_name']))$save['advertiser_name'] = $data['adv_name'];
  429. $save['convert_status'] = $data['clue_convert_status'];
  430. $save['create_time'] = date('Y-m-d',$data['create_time']);
  431. $save['create_time_detail'] = date('Y-m-d H:i:s',$data['create_time']);
  432. if(isset($data['ad_name']))$save['ad_name'] = $data['ad_name'];
  433. $save['clue_source'] = $data['clue_source'];
  434. if(isset($data['site_name']))$save['site_name'] = $data['site_name'];
  435. $save['app_name'] = $data['app_name'];
  436. if(isset($data['ad_id']))$save['ad_id'] = $data['ad_id'];
  437. $save['module_id'] = $data['module_id'];
  438. $save['telephone'] = $data['telphone'];
  439. $save['name'] = $data['name'];
  440. $save['site_id'] = $data['site_id'];
  441. $save['clue_type'] = $data['clue_type'];
  442. $save['location'] = $data['location'];
  443. $save['module_name'] = $data['module_name'];
  444. if(isset($data['cid']))$save['creative_id'] = $data['cid'];
  445. //检查广告主是否存在-飞鱼上需要设置企业ID和默认部门名称
  446. $root_id = $data['root_id'];
  447. $org_id = $data['org_name'];//2361
  448. $save['type'] = 4;
  449. // $org_id = Org::where(['pid' => $root_id, 'name' => $org_name])->value('id');
  450. $company_info = Company::where(['root_id' => $root_id])->value('id');
  451. if (!empty($company_info) && isset($org_id)) {
  452. $save['root_id'] = $root_id;
  453. $save['org_id'] = $org_id;
  454. $save['employee_id'] = 0;
  455. FishData::insert($save);
  456. }
  457. }
  458. //返回信息
  459. $return = array(
  460. 'code' => 0,
  461. 'message' => 'success',
  462. );
  463. header('Content-Type:application/json; charset=utf-8');
  464. exit(json_encode($return, JSON_UNESCAPED_UNICODE));
  465. }
  466. /**
  467. * todo 同步直播飞鱼数据
  468. */
  469. public function synchronize_fish_live_data()
  470. {
  471. $request = request();
  472. $root_id = 994;//$request->empcrm->root_id;
  473. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
  474. if($channel_setting){
  475. $tmp_data = json_decode($channel_setting, true);
  476. if($tmp_data['fish_adv']) {
  477. $appid = config('app.fish_clue_appid_wh');
  478. $secret = config('app.fish_clue_secret_wh');
  479. $fish_update_time = $tmp_data['fish_update_time'] ?: date('Y-m-d H:i:s', time() - 86400 * 180);
  480. $end_time = date('Y-m-d H:i:s');
  481. $page_siae = 100;
  482. $fish = new \clue\FishWh($appid, $secret);
  483. $fish_refresh_time = $tmp_data['fish_refresh_time'];//上次token更新时间
  484. if($fish_refresh_time + 80000 < time()){
  485. $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
  486. if ($token['code'] == 0){
  487. $tmp_data['fish_access_token'] = $token['data']['access_token'];
  488. $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
  489. $tmp_data['fish_refresh_time'] = time();
  490. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  491. }else {
  492. $tmp_data['fish_access_token'] = '';
  493. $tmp_data['fish_refresh_token'] = '';
  494. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  495. }
  496. }
  497. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  498. foreach($advertiser_id as $adv){
  499. $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
  500. // dump($data);die;
  501. if (isset($data) && $data['code'] == 0) {
  502. $total_number = $data['data']['page_info']['total_number'];
  503. $page = intval(ceil($total_number / $page_siae));
  504. $list = $data['data']['list'];
  505. Db::startTrans();
  506. try {
  507. $this->processing_fish_data($list, $root_id);
  508. for ($i = 1; $i < $page; $i++) {
  509. $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
  510. $this->processing_fish_data($tmp['data']['list'], $root_id);
  511. }
  512. $tmp_data['fish_update_time'] = $end_time;
  513. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  514. Db::commit();
  515. } catch (\Exception $e) {
  516. trace($e->getMessage(), 'error');
  517. Db::rollback();
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. //保存飞鱼线索
  525. public function processing_fish_data($list, $root_id)
  526. {
  527. $customer = [];
  528. foreach ($list as $val) {
  529. //查找所属员工-只有员工名称对应上才允许保持到本地库
  530. $val['extra_info'] = implode(',', $val['extra_info']);
  531. $val['remark_dict'] = json_encode($val['remark_dict']);
  532. $val['store'] = json_encode($val['store']);
  533. $val['tags'] = implode(';', $val['tags']);
  534. $val['root_id'] = $root_id;
  535. $info = Employee::where(['name' => $val['clue_owner_name'], 'root_id' => $root_id])->field('id,org_id')->find();
  536. //检查线索是否存在
  537. $fid = FishData::where(['clue_id' => $val['clue_id'], 'type' => 1])->find();
  538. if (!isset($fid) && isset($info) && $info) {
  539. $val['employee_id'] = $info['id'];
  540. $val['org_id'] = $info['org_id'];
  541. $customer[] = $val;
  542. }
  543. //检查线索是否被分配
  544. if (isset($fid) && empty($fid['clue_owner_name'])) {
  545. $val['employee_id'] = $info['id'];
  546. $val['org_id'] = $info['org_id'];
  547. FishData::where(['clue_id' => $val['clue_id'], 'type' => 1])->update($val);
  548. }
  549. }
  550. FishData::insertAll($customer);
  551. }
  552. }