FishClueTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use app\model\Company;
  7. use think\facade\Log;
  8. use clue\Fish as clueFish;
  9. use clue\Tencent;
  10. use app\model\FishData;
  11. use app\model\Employee;
  12. class FishClueTest extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('fish_clue')
  17. ->setDescription('Automatically update flying fish clues');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. Log::close(NULL);
  22. $list = Company::where("channel_setting <> 'null'")->field('root_id,channel_setting,channel_setting_wh')->select()->toArray();
  23. $fish = new clueFish(config('app.fish_clue_appid'), config('app.fish_clue_secret'));
  24. $fish_wh = new clueFish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
  25. $tencent = new Tencent(config('app.tx_clue_appid'), config('app.tx_clue_secret'));
  26. foreach($list as $val){
  27. $root_id = $val['root_id'];
  28. $fish_update_time = '2023-07-08 00:00:00';
  29. $end_time = '2023-07-09 00:00:00';
  30. if(isset($val['channel_setting']) && $val['channel_setting']){
  31. $tmp_data = json_decode($val['channel_setting'], true);
  32. $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
  33. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  34. foreach($advertiser_id as $adv){
  35. $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, 100);
  36. // $output->writeln(json_encode($data));
  37. if (isset($data) && $data['code'] == 0) {
  38. $total_number = $data['data']['page_info']['total_number'];
  39. $page = intval(ceil($total_number / 100));
  40. $list = $data['data']['list'];
  41. $this->processing_fish_data($list, $root_id, 1);
  42. for ($i = 1; $i < $page; $i++) {
  43. $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, 100);
  44. $this->processing_fish_data($tmp['data']['list'], $root_id, 1);
  45. }
  46. }
  47. }
  48. //腾讯数据
  49. $advertiser_id = explode(',', $tmp_data['tx_adv']);
  50. foreach($advertiser_id as $adv){
  51. $data = $tencent->getClueListData($tmp_data['tx_access_token'], $adv, strtotime($fish_update_time), strtotime($end_time), 1, 200);
  52. $output->writeln(json_encode($data));
  53. if (isset($data) && $data['code'] == 0) {
  54. $total_number = $data['data']['page_info']['total_number'];
  55. $page = intval(ceil($total_number / 200));
  56. $list = $data['data']['leads_info'];
  57. $this->processing_tx_data($list, $root_id);
  58. for ($i = 1; $i < $page; $i++) {
  59. $tmp = $tencent->getClueListData($tmp_data['tx_access_token'], $adv, strtotime($fish_update_time), strtotime($end_time), $i + 1, 200);
  60. $this->processing_tx_data($tmp['data']['leads_info'], $root_id);
  61. }
  62. }
  63. }
  64. }
  65. if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
  66. $tmp_data = json_decode($val['channel_setting_wh'], true);
  67. $tmp_data = $this->refreshTokenV2($fish_wh, $tmp_data, $root_id);
  68. $advertiser_id = explode(',', $tmp_data['fish_adv']);
  69. foreach($advertiser_id as $adv){
  70. $data = $fish_wh->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, 100);
  71. if (isset($data) && $data['code'] == 0) {
  72. $total_number = $data['data']['page_info']['total_number'];
  73. $page = intval(ceil($total_number / 100));
  74. $list = $data['data']['list'];
  75. $this->processing_fish_data($list, $root_id, 4);
  76. for ($i = 1; $i < $page; $i++) {
  77. $tmp = $fish_wh->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, 100);
  78. $this->processing_fish_data($tmp['data']['list'], $root_id, 4);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. $output->writeln("同步完成");
  85. }
  86. //更新飞鱼线索
  87. public function processing_fish_data($list, $root_id, $type)
  88. {
  89. $customer = [];
  90. foreach ($list as $val) {
  91. // var_dump($val['clue_id'].'-'.$val['telephone'].$val['clue_owner_name']);
  92. //查找所属员工-只有员工名称对应上才允许保持到本地库
  93. $val['extra_info'] = implode(',', $val['extra_info']);
  94. $val['remark_dict'] = json_encode($val['remark_dict']);
  95. $val['store'] = json_encode($val['store']);
  96. $val['tags'] = implode(';', $val['tags']);
  97. $val['root_id'] = $root_id;
  98. $val['type'] = $type;
  99. $info = Employee::where(['name' => $val['clue_owner_name'], 'root_id' => $root_id, 'state'=> '在职'])->field('id,name,org_id')->find();
  100. //检查线索是否存在
  101. $fid = FishData::where(['clue_id' => $val['clue_id'], 'type' => $type])->find();
  102. if (!isset($fid) && isset($info) && $info) {
  103. $val['employee_id'] = $info['id'];
  104. $val['org_id'] = $info['org_id'];
  105. $val['clue_owner_name'] = $info['name'];
  106. $customer[] = $val;
  107. }
  108. //检查线索是否被分配
  109. if (isset($fid) && $fid['employee_id'] == 0) {
  110. if (isset($info) && $info) {
  111. $update['clue_state_name'] = $val['clue_state_name'];
  112. $update['clue_owner_name'] = $info['name'];
  113. $update['follow_state_name'] = $val['follow_state_name'];
  114. $update['org_id'] = $info['org_id'];
  115. $update['employee_id'] = $info['id'];
  116. FishData::where(['clue_id' => $val['clue_id'], 'type' => $type, 'employee_id' => 0, 'is_allocation' => 0])->update($update);
  117. }
  118. }
  119. if (isset($fid) && empty($fid['clue_owner_name'])){
  120. if (isset($info) && $info) {
  121. $update['clue_state_name'] = $val['clue_state_name'];
  122. $update['clue_owner_name'] = $info['name'];
  123. $update['follow_state_name'] = $val['follow_state_name'];
  124. $update['org_id'] = $info['org_id'];
  125. $update['employee_id'] = $info['id'];
  126. FishData::where(['clue_id' => $val['clue_id'], 'type' => $type, 'is_allocation' => 0])->update($update);
  127. }
  128. }
  129. }
  130. FishData::insertAll($customer);
  131. }
  132. //保存腾讯线索
  133. public function processing_tx_data($list, $root_id)
  134. {
  135. $leads_source = [
  136. 'OUTER_TRAFFIC' => 1,
  137. 'OFFLINE' => 2,
  138. 'AD_PREVIEW' => 3,
  139. 'XJ' => 4,
  140. 'ONE_LEAF' => 5,
  141. 'WECHAT_CANVAS' => 6,
  142. 'XUNDA' => 7,
  143. 'INTERACTIVEPAGE' => 8,
  144. 'JIAOPEITONG' => 9,
  145. ];
  146. $leads_gender = [
  147. 'GENDER_TYPE_FEMALE' => 2,
  148. 'GENDER_TYPE_MALE' => 1,
  149. 'GENDER_TYPE_UNKNOWN' => 0,
  150. ];
  151. foreach ($list as $val) {
  152. $data = [];
  153. //检查线索是否存在
  154. var_dump($val['leads_id'].'-'.$val['leads_telephone']);
  155. $fid = FishData::where(['clue_id' => $val['leads_id'], 'type' => 2])->find();
  156. $info = Employee::where(['name' => $val['owner_name'], 'root_id' => $root_id, 'state'=> '在职'])->field('id,org_id')->find();
  157. if (!isset($fid) && isset($info) && $info && $val['leads_telephone'] > 0) {
  158. //查找所属员工-只有员工名称对应上才允许保持到本地库
  159. $data['clue_id'] = $val['leads_id'];
  160. $data['advertiser_id'] = $val['account_id'];
  161. $data['advertiser_name'] = $val['agency_name'];
  162. $data['ad_id'] = $val['ad_id'];
  163. $data['ad_name'] = $val['ad_name'];
  164. $data['creative_id'] = $val['adcreative_id'];
  165. $data['promotion_id'] = $val['adgroup_id'];
  166. $data['mid_info'] = $val['adgroup_name'];
  167. $data['site_id'] = 0; //空
  168. $data['site_name'] = ''; //空
  169. $data['intention_estimation'] = ''; //空
  170. $data['external_url'] = $val['page_url'];
  171. $data['clue_type'] = 0; //空
  172. $data['module_name'] = $val['component_name'];
  173. $data['module_id'] = $val['component_id'];
  174. $data['create_time'] = date('Y-m-d', strtotime($val['leads_create_time']));
  175. $data['create_time_detail'] = $val['leads_create_time'];
  176. // $data['date'] = ''; //空
  177. $data['clue_source'] = isset($leads_source[$val['leads_source']]) ? $leads_source[$val['leads_source']] : 0;
  178. $data['name'] = (isset($val['leads_name']) && $val['leads_name']) ? $val['leads_name'] : '未命名';
  179. $data['telephone'] = $val['leads_telephone'];
  180. $data['gender'] = isset($leads_gender[$val['leads_gender']]) ? $leads_gender[$val['leads_gender']] : 0;
  181. $data['age'] = $val['age'];
  182. $data['email'] = $val['leads_email'];
  183. $data['weixin'] = $val['leads_wechat'];
  184. $data['qq'] = $val['leads_qq'];
  185. $data['province_name'] = ''; //空
  186. $data['city_name'] = $val['leads_area']; //空
  187. $data['address'] = $val['address'];
  188. $data['location'] = $val['address'];
  189. $data['remark'] = ''; //空
  190. $data['remark_dict'] = ''; //空
  191. $data['form_remark'] = $val['bundle'];
  192. $data['app_name'] = $val['leads_type'];
  193. $data['convert_status'] = '';//转化状态-空
  194. $data['req_id'] = '';
  195. $data['clue_state'] = 0;//线索状态值-空
  196. $data['clue_state_name'] = $val['leads_follow_tag'];
  197. $data['clue_owner_name'] = $val['owner_name'];
  198. $data['follow_state_name'] = ''; //空
  199. $data['country_name'] = $val['leads_area'];
  200. $data['system_tags'] = ''; //空
  201. $data['tags'] = $val['leads_tags'];
  202. $data['allocation_status'] = 0; //空
  203. $data['store'] = json(['all_follow_records'=>$val['all_follow_records']]);
  204. $data['extra_info'] = ''; //空
  205. $data['employee_id'] = $info['id'];
  206. $data['org_id'] = $info['org_id'];
  207. $data['root_id'] = $root_id;
  208. $data['type'] = 2;
  209. FishData::insert($data);
  210. }
  211. //检查线索是否被分配
  212. if (isset($fid) && $fid['employee_id'] == 0) {
  213. if (isset($info) && $info) {
  214. $update['clue_state_name'] = $val['leads_follow_tag'];
  215. $update['clue_owner_name'] = $info['name'];
  216. $update['org_id'] = $info['org_id'];
  217. $update['employee_id'] = $info['id'];
  218. FishData::where(['clue_id' => $val['leads_id'], 'type' => 2, 'employee_id' => 0, 'is_allocation' => 0])->update($update);
  219. }
  220. }
  221. if (isset($fid) && empty($fid['clue_owner_name'])){
  222. if (isset($info) && $info) {
  223. $update['clue_state_name'] = $val['leads_follow_tag'];
  224. $update['clue_owner_name'] = $info['name'];
  225. $update['org_id'] = $info['org_id'];
  226. $update['employee_id'] = $info['id'];
  227. FishData::where(['clue_id' => $val['leads_id'], 'type' => 2, 'is_allocation' => 0])->update($update);
  228. }
  229. }
  230. }
  231. }
  232. /**
  233. * 刷新token
  234. */
  235. public function refreshToken($fish, $tmp_data, $root_id)
  236. {
  237. $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
  238. if ($fish_refresh_time + 80000 < time()) {
  239. $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
  240. if ($token['code'] == 0) {
  241. $tmp_data['fish_access_token'] = $token['data']['access_token'];
  242. $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
  243. $tmp_data['fish_refresh_time'] = time();
  244. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
  245. } else {
  246. $tmp_data['fish_access_token'] = '';
  247. $tmp_data['fish_refresh_token'] = '';
  248. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($tmp_data)]);
  249. }
  250. }
  251. return $tmp_data;
  252. }
  253. /**
  254. * 刷新token
  255. */
  256. public function refreshTokenV2($fish, $tmp_data, $root_id)
  257. {
  258. $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
  259. if ($fish_refresh_time + 80000 < time()) {
  260. $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
  261. if ($token['code'] == 0) {
  262. $tmp_data['fish_access_token'] = $token['data']['access_token'];
  263. $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
  264. $tmp_data['fish_refresh_time'] = time();
  265. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  266. } else {
  267. $tmp_data['fish_access_token'] = '';
  268. $tmp_data['fish_refresh_token'] = '';
  269. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
  270. }
  271. }
  272. return $tmp_data;
  273. }
  274. }