123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
- namespace app\api\controller;
- use app\model\FishData;
- use app\model\FishCampaign;
- use app\model\FishCampaignList;
- use app\model\Company;
- use app\model\Customer;
- use app\model\CustomerSource;
- use app\model\CustomerVisitLog;
- use app\model\Employee;
- use app\model\Building as BuildingModel;
- use app\model\CustomerClue;
- use app\model\Org;
- use app\model\ShareLog;
- use app\model\Footprints;
- use app\model\BuildingHousetype;
- use app\model\BuildingDevelopCase;
- use app\model\Talkskill;
- use app\model\TalkskillChosen;
- use app\model\TalkskillSuccess;
- use app\model\TalkskillViewLog;
- use app\model\OutCallLog;
- use think\facade\Db;
- use clue\Tencent;
- use think\facade\Cache;
- use clue\Fish as clueFish;
- class Fish
- {
- /**
- * 飞鱼外呼回调
- */
- public function fish_phone_call_back()
- {
- $data = input();
- if (isset($data['event']) && $data['event'] == 'verify_webhook') {
- //返回数据
- $this->ajax_return($data['challenge']);
- }
- if (isset($data['data']) && $data['data']) {
- $tmp_data = json_decode($data['data'], true);
- if (isset($tmp_data['event']) && $tmp_data['event'] == 'CONTACT_LOG') {
- $call_data = json_decode($tmp_data['content'], true);
- $log = OutCallLog::where(['session_id' => $call_data['contactId']])->find();
- if (empty($log)) return json(['code' => 0, 'msg' => '通话记录不存在']);
- $startTime = is_int($call_data['startTime']) ? date('Y-m-d H:i:s', $call_data['startTime'] / 1000) : $call_data['startTime'];
- $endTime = is_int($call_data['endTime']) ? date('Y-m-d H:i:s', $call_data['endTime'] / 1000) : $call_data['endTime'];
- $log->save([
- 'fwd_start_time' => $startTime,
- 'call_end_time' => $endTime,
- 'billsec' => $call_data['duration'],
- 'status' => $call_data['duration'] > 0 ? 1 : -1,
- 'call_status' => 1000,
- 'url' => $call_data['audioUrl'],
- 'call_msg' => '飞鱼线索-' . $call_data['endStateShowMsg']
- ]);
- $visitLog = CustomerVisitLog::where(['data_type' => 'out_call', 'data_id' => $log->id])->find();
- if (!empty($visitLog)) {
- if ($call_data['duration'] > 0) {
- $visitLog->remark = '回访跟踪了飞鱼线索客户,通话时长' . $call_data['duration'] . 's';
- } else {
- $visitLog->remark = '回访跟踪了飞鱼线索客户,电话未接通';
- }
- $visitLog->save();
- }
- downClueRecordUrl($call_data['audioUrl'], $call_data['contactId'] . '.mp3');
- $audioUrl = 'https://' . config('app.ali_oss_bindurl') . '/fishcall/' . $call_data['contactId'] . '.mp3';
- $log->save(['url' => $audioUrl]);
- }
- }
- //返回数据
- // Company::where("root_id = 1")->update(['channel_setting'=>json_encode($data)]);
- $this->ajax_return($data['nonce']);
- }
- /**
- * 飞鱼授权回调
- */
- public function fish_call_back()
- {
- $code = input('auth_code');
- $state = input('state');
- $appid = config('app.fish_clue_appid');
- $secret = config('app.fish_clue_secret');
- $data = (new clueFish($appid, $secret))->getAccessToken($code);
- if ($data['code'] == 0) {
- $channel_setting = Company::where(['root_id' => $state])->value('channel_setting');
- $tmp_data = [
- 'fish_account' => '',
- 'fish_adv' => '',
- 'fish_access_token' => '',
- 'fish_refresh_token' => '',
- 'fish_refresh_time' => 0,
- 'fish_update_time' => '',
- 'fish_open' => 0,
- 'tx_account' => '',
- 'tx_adv' => '',
- 'tx_open' => 0,
- 'tx_access_token' => '',
- 'tx_refresh_token' => '',
- 'tx_update_time' => ''
- ];
- if ($channel_setting) {
- $tmp_data = json_decode($channel_setting, true);
- }
- $tmp_data['fish_access_token'] = $data['data']['access_token'];
- $tmp_data['fish_refresh_token'] = $data['data']['refresh_token'];
- $tmp_data['fish_refresh_time'] = time();
- Company::where(['root_id' => $state])->update(['channel_setting' => json_encode($tmp_data)]);
- $fish = CustomerSource::where(['root_id' => $state, 'source' => '飞鱼线索'])->find();
- if (!$fish) {
- CustomerSource::create(['root_id' => $state, 'source' => '飞鱼线索']);
- }
- return redirect('/dashboard/index/index');
- }
- return redirect('/dashboard/index/index');
- }
- /**
- * 腾讯授权回调
- */
- public function tx_call_back()
- {
- $code = input('authorization_code');
- $state = input('state');
- $data = (new Tencent(config('app.tx_clue_appid'),config('app.tx_clue_secret')))->getAccessToken($code);
- if ($data['code'] == 0) {
- $channel_setting = Company::where(['root_id' => $state])->value('channel_setting');
- $tmp_data = [
- 'fish_account' => '',
- 'fish_adv' => '',
- 'fish_access_token' => '',
- 'fish_refresh_token' => '',
- 'fish_refresh_time' => 0,
- 'fish_update_time' => '',
- 'fish_open' => 0,
- 'tx_account' => '',
- 'tx_adv' => '',
- 'tx_open' => 0,
- 'tx_access_token' => '',
- 'tx_refresh_token' => '',
- 'tx_update_time' => ''
- ];
- if ($channel_setting) {
- $tmp_data = json_decode($channel_setting, true);
- }
- $tmp_data['tx_access_token'] = $data['data']['access_token'];
- $tmp_data['tx_refresh_token'] = $data['data']['refresh_token'];
- Company::where(['root_id' => $state])->update(['channel_setting' => json_encode($tmp_data)]);
- $fish = CustomerSource::where(['root_id' => $state, 'source' => '腾讯线索'])->find();
- if (!$fish) {
- CustomerSource::create(['root_id' => $state, 'source' => '腾讯线索']);
- }
- return redirect('/dashboard/index/index');
- }
- return redirect('/dashboard/index/index');
- }
- //飞鱼推送线索
- public function fish_clue_back()
- {
- //NGRwYlF0dmdCL1VBVEJyMHFPWHRyUT09
- //89ade06687e2de8ad24403402ee6b2c820123456
- $data = input();
- $fid = FishData::where(['clue_id' => $data['id'], 'type' => 1])->find();
- if (!isset($fid) && empty($fid)){
- $save['gender'] = $data['gender'] == '男' ? 1 : ($data['gender'] == '女' ? 2 : 0);
- $save['promotion_id'] = $data['promotion_id'];
- if(isset($data['req_id']))$save['req_id'] = $data['req_id'];
- if(isset($data['external_url']))$save['external_url'] = $data['external_url'];
- $save['remark_dict'] = json_encode($data['remark_dict']);
- $save['advertiser_id'] = $data['adv_id'];//广告主id
- $save['clue_id'] = $data['id'];
- if(isset($data['adv_name']))$save['advertiser_name'] = $data['adv_name'];
- $save['convert_status'] = $data['clue_convert_status'];
- $save['create_time'] = date('Y-m-d',$data['create_time']);
- $save['create_time_detail'] = date('Y-m-d H:i:s',$data['create_time']);
- if(isset($data['ad_name']))$save['ad_name'] = $data['ad_name'];
- $save['clue_source'] = $data['clue_source'];
- if(isset($data['site_name']))$save['site_name'] = $data['site_name'];
- $save['app_name'] = $data['app_name'];
- if(isset($data['ad_id']))$save['ad_id'] = $data['ad_id'];
- $save['module_id'] = $data['module_id'];
- $save['telephone'] = $data['telphone'];
- $save['name'] = $data['name'];
- $save['site_id'] = $data['site_id'];
- $save['clue_type'] = $data['clue_type'];
- $save['location'] = $data['location'];
- $save['module_name'] = $data['module_name'];
- if(isset($data['cid']))$save['creative_id'] = $data['cid'];
- //检查广告主是否存在-飞鱼上需要设置企业ID和默认部门名称
- $root_id = $data['root_id'];
- $org_id = $data['org_name'];//2361
- // $org_id = Org::where(['pid' => $root_id, 'name' => $org_name])->value('id');
- $company_info = Company::where(['root_id' => $root_id])->value('id');
- if (!empty($company_info) && isset($org_id)) {
- $save['root_id'] = $root_id;
- $save['org_id'] = $org_id;
- $save['employee_id'] = 0;
- FishData::insert($save);
- }
- }
- //返回信息
- $return = array(
- 'code' => 0,
- 'message' => 'success',
- );
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($return, JSON_UNESCAPED_UNICODE));
- }
- /**
- * 飞鱼返回
- */
- public function ajax_return($data)
- {
- $return = array(
- 'BaseResp' => ['StatusCode' => 200, 'StatusMessage' => 'ok'],
- 'challenge' => intval($data),
- );
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($return, JSON_UNESCAPED_UNICODE));
- }
- /**
- * 腾讯线索推送
- */
- public function tx_clue_back()
- {
- //NGRwYlF0dmdCL1VBVEJyMHFPWHRyUT09
- //89ade06687e2de8ad24403402ee6b2c820123456
- $val = input();
- $fid = FishData::where(['clue_id' => $val['leads_id'], 'type' => 2])->find();
- if (!isset($fid) && empty($fid)){
- $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,
- ];
- $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'] = '';
- $data['module_id'] = 0;
- $data['create_time'] = date('Y-m-d', strtotime($val['leads_create_time']));
- $data['create_time_detail'] = $val['leads_create_time'];
- $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_tel'];
- $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['type'] = 2;
- //检查广告主是否存在-飞鱼上需要设置企业ID和默认部门名称
- $root_id = $val['root_id'];
- $org_id = $val['org_name'];
-
- $info = Employee::where(['name' => $val['owner_name'], 'root_id' => $root_id, 'state'=> '在职'])->field('id,org_id')->find();
- if(isset($info) && $info){
- $data['root_id'] = $root_id;
- $data['org_id'] = $info['org_id'];
- $data['employee_id'] = $info['id'];
- FishData::insert($data);
- }else{
- // $org_id = Org::where(['pid' => $root_id, 'name' => $org_name])->value('id');
- $company_info = Company::where(['root_id' => $root_id])->value('id');
- if (!empty($company_info)) {
- $data['root_id'] = $root_id;
- $data['org_id'] = $org_id;
- $data['employee_id'] = 0;
- $data['clue_owner_name'] = '';
- FishData::insert($data);
- }
- }
- }
- //返回信息
- $return = array(
- 'code' => 0,
- 'message' => 'success',
- );
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($return, JSON_UNESCAPED_UNICODE));
- }
- public function tongbu()
- {
- $val = input('page');
- $channel_setting = Company::where(['root_id' => 2319])->value('channel_setting');
- $tmp_data = json_decode($channel_setting, true);
- $list = OutCallLog::where([['call_msg','LIKE','%飞鱼线索%'],['url','LIKE','%feiyu.oceanengine.com%'],['root_id','=','2319']])->order('id asc')->limit($val,50)->select();
- foreach($list as $val){
- $res = ossFileExist('fishcall/'.$val['session_id'].'.mp3');
- dump($res);
- if($res){
- $url = 'https://' . config('app.ali_oss_bindurl') . '/fishcall/' . $val['session_id'] . '.mp3';
- OutCallLog::where(['id' => $val['id'], 'session_id' => $val['session_id']])->update(['url' => $url]);
- }else{
- $tmp = FishData::where(['id' => $val['call_id'], 'root_id' => $val['root_id']])->find();
- $info = [
- 'advertiser_id' => $tmp['advertiser_id'],
- 'clue_id' => $tmp['clue_id'],
- 'filter' => [
- 'contact_id' => $val['session_id']
- ]
- ];
- $tt = (new clueFish(config('app.fish_clue_appid'), config('app.fish_clue_secret')))->getClueRecordUrl($tmp_data['fish_access_token'], $info);
- if($tt['code'] == 0){
- $url = $tt['data']['list'][0]['contact_url'];
- $name = $tt['data']['list'][0]['contact_id'].'.mp3';
- $res = downClueRecordUrl($url,$name);
- $update = 'https://' . config('app.ali_oss_bindurl') . '/fishcall/' . $val['session_id'] . '.mp3';
- OutCallLog::where(['id' => $val['id'], 'session_id' => $val['session_id']])->update(['url' => $update]);
- }
- }
- }
- return 1;
- }
- }
|