1
0

Fish.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\FishData;
  4. use app\model\FishCampaign;
  5. use app\model\FishCampaignList;
  6. use app\model\Company;
  7. use app\model\Customer;
  8. use app\model\CustomerSource;
  9. use app\model\CustomerVisitLog;
  10. use app\model\Employee;
  11. use app\model\Building as BuildingModel;
  12. use app\model\CustomerClue;
  13. use app\model\Org;
  14. use app\model\ShareLog;
  15. use app\model\Footprints;
  16. use app\model\BuildingHousetype;
  17. use app\model\BuildingDevelopCase;
  18. use app\model\Talkskill;
  19. use app\model\TalkskillChosen;
  20. use app\model\TalkskillSuccess;
  21. use app\model\TalkskillViewLog;
  22. use app\model\OutCallLog;
  23. use think\facade\Db;
  24. use clue\Tencent;
  25. use think\facade\Cache;
  26. use clue\Fish as clueFish;
  27. class Fish
  28. {
  29. /**
  30. * 飞鱼外呼回调
  31. */
  32. public function fish_phone_call_back()
  33. {
  34. $data = input();
  35. if (isset($data['event']) && $data['event'] == 'verify_webhook') {
  36. //返回数据
  37. $this->ajax_return($data['challenge']);
  38. }
  39. if (isset($data['data']) && $data['data']) {
  40. $tmp_data = json_decode($data['data'], true);
  41. if (isset($tmp_data['event']) && $tmp_data['event'] == 'CONTACT_LOG') {
  42. $call_data = json_decode($tmp_data['content'], true);
  43. $log = OutCallLog::where(['session_id' => $call_data['contactId']])->find();
  44. if (empty($log)) return json(['code' => 0, 'msg' => '通话记录不存在']);
  45. $startTime = is_int($call_data['startTime']) ? date('Y-m-d H:i:s', $call_data['startTime'] / 1000) : $call_data['startTime'];
  46. $endTime = is_int($call_data['endTime']) ? date('Y-m-d H:i:s', $call_data['endTime'] / 1000) : $call_data['endTime'];
  47. $log->save([
  48. 'fwd_start_time' => $startTime,
  49. 'call_end_time' => $endTime,
  50. 'billsec' => $call_data['duration'],
  51. 'status' => $call_data['duration'] > 0 ? 1 : -1,
  52. 'call_status' => 1000,
  53. 'url' => $call_data['audioUrl'],
  54. 'call_msg' => '飞鱼线索-' . $call_data['endStateShowMsg']
  55. ]);
  56. $visitLog = CustomerVisitLog::where(['data_type' => 'out_call', 'data_id' => $log->id])->find();
  57. if (!empty($visitLog)) {
  58. if ($call_data['duration'] > 0) {
  59. $visitLog->remark = '回访跟踪了飞鱼线索客户,通话时长' . $call_data['duration'] . 's';
  60. } else {
  61. $visitLog->remark = '回访跟踪了飞鱼线索客户,电话未接通';
  62. }
  63. $visitLog->save();
  64. }
  65. downClueRecordUrl($call_data['audioUrl'], $call_data['contactId'] . '.mp3');
  66. $audioUrl = 'https://' . config('app.ali_oss_bindurl') . '/fishcall/' . $call_data['contactId'] . '.mp3';
  67. $log->save(['url' => $audioUrl]);
  68. }
  69. }
  70. //返回数据
  71. // Company::where("root_id = 1")->update(['channel_setting'=>json_encode($data)]);
  72. $this->ajax_return($data['nonce']);
  73. }
  74. /**
  75. * 飞鱼授权回调
  76. */
  77. public function fish_call_back()
  78. {
  79. $code = input('auth_code');
  80. $state = input('state');
  81. $appid = config('app.fish_clue_appid');
  82. $secret = config('app.fish_clue_secret');
  83. $data = (new clueFish($appid, $secret))->getAccessToken($code);
  84. if ($data['code'] == 0) {
  85. $channel_setting = Company::where(['root_id' => $state])->value('channel_setting');
  86. $tmp_data = [
  87. 'fish_account' => '',
  88. 'fish_adv' => '',
  89. 'fish_access_token' => '',
  90. 'fish_refresh_token' => '',
  91. 'fish_refresh_time' => 0,
  92. 'fish_update_time' => '',
  93. 'fish_open' => 0,
  94. 'tx_account' => '',
  95. 'tx_adv' => '',
  96. 'tx_open' => 0,
  97. 'tx_access_token' => '',
  98. 'tx_refresh_token' => '',
  99. 'tx_update_time' => ''
  100. ];
  101. if ($channel_setting) {
  102. $tmp_data = json_decode($channel_setting, true);
  103. }
  104. $tmp_data['fish_access_token'] = $data['data']['access_token'];
  105. $tmp_data['fish_refresh_token'] = $data['data']['refresh_token'];
  106. $tmp_data['fish_refresh_time'] = time();
  107. Company::where(['root_id' => $state])->update(['channel_setting' => json_encode($tmp_data)]);
  108. $fish = CustomerSource::where(['root_id' => $state, 'source' => '飞鱼线索'])->find();
  109. if (!$fish) {
  110. CustomerSource::create(['root_id' => $state, 'source' => '飞鱼线索']);
  111. }
  112. return redirect('/dashboard/index/index');
  113. }
  114. return redirect('/dashboard/index/index');
  115. }
  116. /**
  117. * 腾讯授权回调
  118. */
  119. public function tx_call_back()
  120. {
  121. $code = input('authorization_code');
  122. $state = input('state');
  123. $data = (new Tencent(config('app.tx_clue_appid'),config('app.tx_clue_secret')))->getAccessToken($code);
  124. if ($data['code'] == 0) {
  125. $channel_setting = Company::where(['root_id' => $state])->value('channel_setting');
  126. $tmp_data = [
  127. 'fish_account' => '',
  128. 'fish_adv' => '',
  129. 'fish_access_token' => '',
  130. 'fish_refresh_token' => '',
  131. 'fish_refresh_time' => 0,
  132. 'fish_update_time' => '',
  133. 'fish_open' => 0,
  134. 'tx_account' => '',
  135. 'tx_adv' => '',
  136. 'tx_open' => 0,
  137. 'tx_access_token' => '',
  138. 'tx_refresh_token' => '',
  139. 'tx_update_time' => ''
  140. ];
  141. if ($channel_setting) {
  142. $tmp_data = json_decode($channel_setting, true);
  143. }
  144. $tmp_data['tx_access_token'] = $data['data']['access_token'];
  145. $tmp_data['tx_refresh_token'] = $data['data']['refresh_token'];
  146. Company::where(['root_id' => $state])->update(['channel_setting' => json_encode($tmp_data)]);
  147. $fish = CustomerSource::where(['root_id' => $state, 'source' => '腾讯线索'])->find();
  148. if (!$fish) {
  149. CustomerSource::create(['root_id' => $state, 'source' => '腾讯线索']);
  150. }
  151. return redirect('/dashboard/index/index');
  152. }
  153. return redirect('/dashboard/index/index');
  154. }
  155. //飞鱼推送线索
  156. public function fish_clue_back()
  157. {
  158. //NGRwYlF0dmdCL1VBVEJyMHFPWHRyUT09
  159. //89ade06687e2de8ad24403402ee6b2c820123456
  160. $data = input();
  161. $fid = FishData::where(['clue_id' => $data['id'], 'type' => 1])->find();
  162. if (!isset($fid) && empty($fid)){
  163. $save['gender'] = $data['gender'] == '男' ? 1 : ($data['gender'] == '女' ? 2 : 0);
  164. $save['promotion_id'] = $data['promotion_id'];
  165. if(isset($data['req_id']))$save['req_id'] = $data['req_id'];
  166. if(isset($data['external_url']))$save['external_url'] = $data['external_url'];
  167. $save['remark_dict'] = json_encode($data['remark_dict']);
  168. $save['advertiser_id'] = $data['adv_id'];//广告主id
  169. $save['clue_id'] = $data['id'];
  170. if(isset($data['adv_name']))$save['advertiser_name'] = $data['adv_name'];
  171. $save['convert_status'] = $data['clue_convert_status'];
  172. $save['create_time'] = date('Y-m-d',$data['create_time']);
  173. $save['create_time_detail'] = date('Y-m-d H:i:s',$data['create_time']);
  174. if(isset($data['ad_name']))$save['ad_name'] = $data['ad_name'];
  175. $save['clue_source'] = $data['clue_source'];
  176. if(isset($data['site_name']))$save['site_name'] = $data['site_name'];
  177. $save['app_name'] = $data['app_name'];
  178. if(isset($data['ad_id']))$save['ad_id'] = $data['ad_id'];
  179. $save['module_id'] = $data['module_id'];
  180. $save['telephone'] = $data['telphone'];
  181. $save['name'] = $data['name'];
  182. $save['site_id'] = $data['site_id'];
  183. $save['clue_type'] = $data['clue_type'];
  184. $save['location'] = $data['location'];
  185. $save['module_name'] = $data['module_name'];
  186. if(isset($data['cid']))$save['creative_id'] = $data['cid'];
  187. //检查广告主是否存在-飞鱼上需要设置企业ID和默认部门名称
  188. $root_id = $data['root_id'];
  189. $org_id = $data['org_name'];//2361
  190. // $org_id = Org::where(['pid' => $root_id, 'name' => $org_name])->value('id');
  191. $company_info = Company::where(['root_id' => $root_id])->value('id');
  192. if (!empty($company_info) && isset($org_id)) {
  193. $save['root_id'] = $root_id;
  194. $save['org_id'] = $org_id;
  195. $save['employee_id'] = 0;
  196. FishData::insert($save);
  197. }
  198. }
  199. //返回信息
  200. $return = array(
  201. 'code' => 0,
  202. 'message' => 'success',
  203. );
  204. header('Content-Type:application/json; charset=utf-8');
  205. exit(json_encode($return, JSON_UNESCAPED_UNICODE));
  206. }
  207. /**
  208. * 飞鱼返回
  209. */
  210. public function ajax_return($data)
  211. {
  212. $return = array(
  213. 'BaseResp' => ['StatusCode' => 200, 'StatusMessage' => 'ok'],
  214. 'challenge' => intval($data),
  215. );
  216. header('Content-Type:application/json; charset=utf-8');
  217. exit(json_encode($return, JSON_UNESCAPED_UNICODE));
  218. }
  219. /**
  220. * 腾讯线索推送
  221. */
  222. public function tx_clue_back()
  223. {
  224. //NGRwYlF0dmdCL1VBVEJyMHFPWHRyUT09
  225. //89ade06687e2de8ad24403402ee6b2c820123456
  226. $val = input();
  227. $fid = FishData::where(['clue_id' => $val['leads_id'], 'type' => 2])->find();
  228. if (!isset($fid) && empty($fid)){
  229. $leads_source = [
  230. 'OUTER_TRAFFIC' => 1,
  231. 'OFFLINE' => 2,
  232. 'AD_PREVIEW' => 3,
  233. 'XJ' => 4,
  234. 'ONE_LEAF' => 5,
  235. 'WECHAT_CANVAS' => 6,
  236. 'XUNDA' => 7,
  237. 'INTERACTIVEPAGE' => 8,
  238. 'JIAOPEITONG' => 9,
  239. ];
  240. $leads_gender = [
  241. 'GENDER_TYPE_FEMALE' => 2,
  242. 'GENDER_TYPE_MALE' => 1,
  243. 'GENDER_TYPE_UNKNOWN' => 0,
  244. ];
  245. $data['clue_id'] = $val['leads_id'];
  246. $data['advertiser_id'] = $val['account_id'];
  247. $data['advertiser_name'] = $val['agency_name'];
  248. $data['ad_id'] = $val['ad_id'];
  249. $data['ad_name'] = $val['ad_name'];
  250. $data['creative_id'] = $val['adcreative_id'];
  251. $data['promotion_id'] = $val['adgroup_id'];
  252. $data['mid_info'] = $val['adgroup_name'];
  253. $data['site_id'] = 0; //空
  254. $data['site_name'] = ''; //空
  255. $data['intention_estimation'] = ''; //空
  256. $data['external_url'] = $val['page_url'];
  257. $data['clue_type'] = 0; //空
  258. $data['module_name'] = '';
  259. $data['module_id'] = 0;
  260. $data['create_time'] = date('Y-m-d', strtotime($val['leads_create_time']));
  261. $data['create_time_detail'] = $val['leads_create_time'];
  262. $data['clue_source'] = isset($leads_source[$val['leads_source']]) ? $leads_source[$val['leads_source']] : 0;
  263. $data['name'] = (isset($val['leads_name']) && $val['leads_name']) ? $val['leads_name'] : '未命名';
  264. $data['telephone'] = $val['leads_tel'];
  265. $data['gender'] = isset($leads_gender[$val['leads_gender']]) ? $leads_gender[$val['leads_gender']] : 0;
  266. $data['age'] = $val['age'];
  267. $data['email'] = $val['leads_email'];
  268. $data['weixin'] = $val['leads_wechat'];
  269. $data['qq'] = $val['leads_qq'];
  270. $data['province_name'] = ''; //空
  271. $data['city_name'] = $val['leads_area']; //空
  272. $data['address'] = $val['address'];
  273. $data['location'] = $val['address'];
  274. $data['remark'] = ''; //空
  275. $data['remark_dict'] = ''; //空
  276. $data['form_remark'] = $val['bundle'];
  277. $data['app_name'] = $val['leads_type'];
  278. $data['convert_status'] = ''; //转化状态-空
  279. $data['req_id'] = '';
  280. $data['clue_state'] = 0; //线索状态值-空
  281. $data['clue_state_name'] = $val['leads_follow_tag'];
  282. $data['clue_owner_name'] = $val['owner_name'];
  283. $data['follow_state_name'] = ''; //空
  284. $data['country_name'] = $val['leads_area'];
  285. $data['system_tags'] = ''; //空
  286. $data['tags'] = $val['leads_tags'];
  287. $data['allocation_status'] = 0; //空
  288. $data['store'] = json(['all_follow_records' => $val['all_follow_records']]);
  289. $data['extra_info'] = ''; //空
  290. $data['type'] = 2;
  291. //检查广告主是否存在-飞鱼上需要设置企业ID和默认部门名称
  292. $root_id = $val['root_id'];
  293. $org_id = $val['org_name'];
  294. $info = Employee::where(['name' => $val['owner_name'], 'root_id' => $root_id, 'state'=> '在职'])->field('id,org_id')->find();
  295. if(isset($info) && $info){
  296. $data['root_id'] = $root_id;
  297. $data['org_id'] = $info['org_id'];
  298. $data['employee_id'] = $info['id'];
  299. FishData::insert($data);
  300. }else{
  301. // $org_id = Org::where(['pid' => $root_id, 'name' => $org_name])->value('id');
  302. $company_info = Company::where(['root_id' => $root_id])->value('id');
  303. if (!empty($company_info)) {
  304. $data['root_id'] = $root_id;
  305. $data['org_id'] = $org_id;
  306. $data['employee_id'] = 0;
  307. $data['clue_owner_name'] = '';
  308. FishData::insert($data);
  309. }
  310. }
  311. }
  312. //返回信息
  313. $return = array(
  314. 'code' => 0,
  315. 'message' => 'success',
  316. );
  317. header('Content-Type:application/json; charset=utf-8');
  318. exit(json_encode($return, JSON_UNESCAPED_UNICODE));
  319. }
  320. public function tongbu()
  321. {
  322. $val = input('page');
  323. $channel_setting = Company::where(['root_id' => 2319])->value('channel_setting');
  324. $tmp_data = json_decode($channel_setting, true);
  325. $list = OutCallLog::where([['call_msg','LIKE','%飞鱼线索%'],['url','LIKE','%feiyu.oceanengine.com%'],['root_id','=','2319']])->order('id asc')->limit($val,50)->select();
  326. foreach($list as $val){
  327. $res = ossFileExist('fishcall/'.$val['session_id'].'.mp3');
  328. dump($res);
  329. if($res){
  330. $url = 'https://' . config('app.ali_oss_bindurl') . '/fishcall/' . $val['session_id'] . '.mp3';
  331. OutCallLog::where(['id' => $val['id'], 'session_id' => $val['session_id']])->update(['url' => $url]);
  332. }else{
  333. $tmp = FishData::where(['id' => $val['call_id'], 'root_id' => $val['root_id']])->find();
  334. $info = [
  335. 'advertiser_id' => $tmp['advertiser_id'],
  336. 'clue_id' => $tmp['clue_id'],
  337. 'filter' => [
  338. 'contact_id' => $val['session_id']
  339. ]
  340. ];
  341. $tt = (new clueFish(config('app.fish_clue_appid'), config('app.fish_clue_secret')))->getClueRecordUrl($tmp_data['fish_access_token'], $info);
  342. if($tt['code'] == 0){
  343. $url = $tt['data']['list'][0]['contact_url'];
  344. $name = $tt['data']['list'][0]['contact_id'].'.mp3';
  345. $res = downClueRecordUrl($url,$name);
  346. $update = 'https://' . config('app.ali_oss_bindurl') . '/fishcall/' . $val['session_id'] . '.mp3';
  347. OutCallLog::where(['id' => $val['id'], 'session_id' => $val['session_id']])->update(['url' => $update]);
  348. }
  349. }
  350. }
  351. return 1;
  352. }
  353. }