setName('fish_clue') ->setDescription('Automatically update flying fish clues'); } protected function execute(Input $input, Output $output) { Log::close(NULL); $list = Company::where("channel_setting <> 'null'")->field('root_id,channel_setting,channel_setting_wh')->select()->toArray(); $fish = new clueFish(config('app.fish_clue_appid'), config('app.fish_clue_secret')); $fish_wh = new clueFish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh')); $tencent = new Tencent(config('app.tx_clue_appid'), config('app.tx_clue_secret')); foreach($list as $val){ $root_id = $val['root_id']; $fish_update_time = '2023-07-08 00:00:00'; $end_time = '2023-07-09 00:00:00'; if(isset($val['channel_setting']) && $val['channel_setting']){ $tmp_data = json_decode($val['channel_setting'], true); $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id); $advertiser_id = explode(',', $tmp_data['fish_adv']); foreach($advertiser_id as $adv){ $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, 100); // $output->writeln(json_encode($data)); if (isset($data) && $data['code'] == 0) { $total_number = $data['data']['page_info']['total_number']; $page = intval(ceil($total_number / 100)); $list = $data['data']['list']; $this->processing_fish_data($list, $root_id, 1); for ($i = 1; $i < $page; $i++) { $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, 100); $this->processing_fish_data($tmp['data']['list'], $root_id, 1); } } } //腾讯数据 $advertiser_id = explode(',', $tmp_data['tx_adv']); foreach($advertiser_id as $adv){ $data = $tencent->getClueListData($tmp_data['tx_access_token'], $adv, strtotime($fish_update_time), strtotime($end_time), 1, 200); $output->writeln(json_encode($data)); if (isset($data) && $data['code'] == 0) { $total_number = $data['data']['page_info']['total_number']; $page = intval(ceil($total_number / 200)); $list = $data['data']['leads_info']; $this->processing_tx_data($list, $root_id); for ($i = 1; $i < $page; $i++) { $tmp = $tencent->getClueListData($tmp_data['tx_access_token'], $adv, strtotime($fish_update_time), strtotime($end_time), $i + 1, 200); $this->processing_tx_data($tmp['data']['leads_info'], $root_id); } } } } if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){ $tmp_data = json_decode($val['channel_setting_wh'], true); $tmp_data = $this->refreshTokenV2($fish_wh, $tmp_data, $root_id); $advertiser_id = explode(',', $tmp_data['fish_adv']); foreach($advertiser_id as $adv){ $data = $fish_wh->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, 100); if (isset($data) && $data['code'] == 0) { $total_number = $data['data']['page_info']['total_number']; $page = intval(ceil($total_number / 100)); $list = $data['data']['list']; $this->processing_fish_data($list, $root_id, 4); for ($i = 1; $i < $page; $i++) { $tmp = $fish_wh->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, 100); $this->processing_fish_data($tmp['data']['list'], $root_id, 4); } } } } } $output->writeln("同步完成"); } //更新飞鱼线索 public function processing_fish_data($list, $root_id, $type) { $customer = []; foreach ($list as $val) { // var_dump($val['clue_id'].'-'.$val['telephone'].$val['clue_owner_name']); //查找所属员工-只有员工名称对应上才允许保持到本地库 $val['extra_info'] = implode(',', $val['extra_info']); $val['remark_dict'] = json_encode($val['remark_dict']); $val['store'] = json_encode($val['store']); $val['tags'] = implode(';', $val['tags']); $val['root_id'] = $root_id; $val['type'] = $type; $info = Employee::where(['name' => $val['clue_owner_name'], 'root_id' => $root_id, 'state'=> '在职'])->field('id,name,org_id')->find(); //检查线索是否存在 $fid = FishData::where(['clue_id' => $val['clue_id'], 'type' => $type])->find(); if (!isset($fid) && isset($info) && $info) { $val['employee_id'] = $info['id']; $val['org_id'] = $info['org_id']; $val['clue_owner_name'] = $info['name']; $customer[] = $val; } //检查线索是否被分配 if (isset($fid) && $fid['employee_id'] == 0) { if (isset($info) && $info) { $update['clue_state_name'] = $val['clue_state_name']; $update['clue_owner_name'] = $info['name']; $update['follow_state_name'] = $val['follow_state_name']; $update['org_id'] = $info['org_id']; $update['employee_id'] = $info['id']; FishData::where(['clue_id' => $val['clue_id'], 'type' => $type, 'employee_id' => 0, 'is_allocation' => 0])->update($update); } } if (isset($fid) && empty($fid['clue_owner_name'])){ if (isset($info) && $info) { $update['clue_state_name'] = $val['clue_state_name']; $update['clue_owner_name'] = $info['name']; $update['follow_state_name'] = $val['follow_state_name']; $update['org_id'] = $info['org_id']; $update['employee_id'] = $info['id']; FishData::where(['clue_id' => $val['clue_id'], 'type' => $type, 'is_allocation' => 0])->update($update); } } } FishData::insertAll($customer); } //保存腾讯线索 public function processing_tx_data($list, $root_id) { $leads_source = [ 'OUTER_TRAFFIC' => 1, 'OFFLINE' => 2, 'AD_PREVIEW' => 3, 'XJ' => 4, 'ONE_LEAF' => 5, 'WECHAT_CANVAS' => 6, 'XUNDA' => 7, 'INTERACTIVEPAGE' => 8, 'JIAOPEITONG' => 9, ]; $leads_gender = [ 'GENDER_TYPE_FEMALE' => 2, 'GENDER_TYPE_MALE' => 1, 'GENDER_TYPE_UNKNOWN' => 0, ]; foreach ($list as $val) { $data = []; //检查线索是否存在 var_dump($val['leads_id'].'-'.$val['leads_telephone']); $fid = FishData::where(['clue_id' => $val['leads_id'], 'type' => 2])->find(); $info = Employee::where(['name' => $val['owner_name'], 'root_id' => $root_id, 'state'=> '在职'])->field('id,org_id')->find(); if (!isset($fid) && isset($info) && $info && $val['leads_telephone'] > 0) { //查找所属员工-只有员工名称对应上才允许保持到本地库 $data['clue_id'] = $val['leads_id']; $data['advertiser_id'] = $val['account_id']; $data['advertiser_name'] = $val['agency_name']; $data['ad_id'] = $val['ad_id']; $data['ad_name'] = $val['ad_name']; $data['creative_id'] = $val['adcreative_id']; $data['promotion_id'] = $val['adgroup_id']; $data['mid_info'] = $val['adgroup_name']; $data['site_id'] = 0; //空 $data['site_name'] = ''; //空 $data['intention_estimation'] = ''; //空 $data['external_url'] = $val['page_url']; $data['clue_type'] = 0; //空 $data['module_name'] = $val['component_name']; $data['module_id'] = $val['component_id']; $data['create_time'] = date('Y-m-d', strtotime($val['leads_create_time'])); $data['create_time_detail'] = $val['leads_create_time']; // $data['date'] = ''; //空 $data['clue_source'] = isset($leads_source[$val['leads_source']]) ? $leads_source[$val['leads_source']] : 0; $data['name'] = (isset($val['leads_name']) && $val['leads_name']) ? $val['leads_name'] : '未命名'; $data['telephone'] = $val['leads_telephone']; $data['gender'] = isset($leads_gender[$val['leads_gender']]) ? $leads_gender[$val['leads_gender']] : 0; $data['age'] = $val['age']; $data['email'] = $val['leads_email']; $data['weixin'] = $val['leads_wechat']; $data['qq'] = $val['leads_qq']; $data['province_name'] = ''; //空 $data['city_name'] = $val['leads_area']; //空 $data['address'] = $val['address']; $data['location'] = $val['address']; $data['remark'] = ''; //空 $data['remark_dict'] = ''; //空 $data['form_remark'] = $val['bundle']; $data['app_name'] = $val['leads_type']; $data['convert_status'] = '';//转化状态-空 $data['req_id'] = ''; $data['clue_state'] = 0;//线索状态值-空 $data['clue_state_name'] = $val['leads_follow_tag']; $data['clue_owner_name'] = $val['owner_name']; $data['follow_state_name'] = ''; //空 $data['country_name'] = $val['leads_area']; $data['system_tags'] = ''; //空 $data['tags'] = $val['leads_tags']; $data['allocation_status'] = 0; //空 $data['store'] = json(['all_follow_records'=>$val['all_follow_records']]); $data['extra_info'] = ''; //空 $data['employee_id'] = $info['id']; $data['org_id'] = $info['org_id']; $data['root_id'] = $root_id; $data['type'] = 2; FishData::insert($data); } //检查线索是否被分配 if (isset($fid) && $fid['employee_id'] == 0) { if (isset($info) && $info) { $update['clue_state_name'] = $val['leads_follow_tag']; $update['clue_owner_name'] = $info['name']; $update['org_id'] = $info['org_id']; $update['employee_id'] = $info['id']; FishData::where(['clue_id' => $val['leads_id'], 'type' => 2, 'employee_id' => 0, 'is_allocation' => 0])->update($update); } } if (isset($fid) && empty($fid['clue_owner_name'])){ if (isset($info) && $info) { $update['clue_state_name'] = $val['leads_follow_tag']; $update['clue_owner_name'] = $info['name']; $update['org_id'] = $info['org_id']; $update['employee_id'] = $info['id']; FishData::where(['clue_id' => $val['leads_id'], 'type' => 2, 'is_allocation' => 0])->update($update); } } } } /** * 刷新token */ public function refreshToken($fish, $tmp_data, $root_id) { $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间 if ($fish_refresh_time + 80000 < time()) { $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']); if ($token['code'] == 0) { $tmp_data['fish_access_token'] = $token['data']['access_token']; $tmp_data['fish_refresh_token'] = $token['data']['refresh_token']; $tmp_data['fish_refresh_time'] = time(); Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]); } else { $tmp_data['fish_access_token'] = ''; $tmp_data['fish_refresh_token'] = ''; Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]); } } return $tmp_data; } /** * 刷新token */ public function refreshTokenV2($fish, $tmp_data, $root_id) { $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间 if ($fish_refresh_time + 80000 < time()) { $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']); if ($token['code'] == 0) { $tmp_data['fish_access_token'] = $token['data']['access_token']; $tmp_data['fish_refresh_token'] = $token['data']['refresh_token']; $tmp_data['fish_refresh_time'] = time(); Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]); } else { $tmp_data['fish_access_token'] = ''; $tmp_data['fish_refresh_token'] = ''; Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]); } } return $tmp_data; } }