Enterprise.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\AdminConfig;
  4. use addons\qingdongams\model\Customer as CustomerModel;
  5. use addons\qingdongams\model\CustomerEnterprise;
  6. use addons\qingdongams\model\CustomerEnterpriseDetail;
  7. use fast\Http;
  8. use think\Db;
  9. use think\Exception;
  10. use think\Log;
  11. /**
  12. * 公司信息公共接口
  13. */
  14. class Enterprise extends StaffApi {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = '*';
  17. /**
  18. * @var \addons\qingdongams\model\CustomerEnterprise;
  19. */
  20. protected $enterpriseModel = null;
  21. /**
  22. * @var \addons\qingdongams\model\CustomerEnterpriseDetail;
  23. */
  24. protected $detailModel = null;
  25. protected $customerModel = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->enterpriseModel = new CustomerEnterprise();
  30. $this->detailModel = new CustomerEnterpriseDetail();
  31. $this->customerModel = new \addons\qingdongams\model\Customer();
  32. $this->enterpriseCode = AdminConfig::where(array('type'=>'juhe','field'=>'juhe_api'))->value('value');
  33. $this->detailCode = AdminConfig::where(array('type'=>'juhe','field'=>'juhe_secret'))->value('value');
  34. }
  35. /**
  36. * 根据关键字获取 列表
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. * @throws \think\exception\DbException
  40. */
  41. public function getEnterprise(){
  42. $keywords = input('keywords');
  43. if(!$keywords){
  44. $this->error('请输入要查询的企业名称');
  45. }
  46. $where['keywords'] = ['like','%'.$keywords.'%'];
  47. $list = $this->enterpriseModel->where($where)->select();
  48. if(count($list)==0) {
  49. $list = $this->curl_enterprise($keywords);
  50. if(!is_array($list) || !$list){
  51. $this->error($list);
  52. }
  53. $this->success('',$list);
  54. }else{
  55. $data = [];
  56. $list = collection($list)->toArray();
  57. foreach($list as $k=>$v){
  58. $data = array_merge($data,json_decode($v['content'],true));
  59. }
  60. $this->success('',$data);
  61. }
  62. }
  63. /**
  64. * 根据公司名字查询
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. */
  69. public function getDetail(){
  70. $name = input('name');
  71. if(!$name){
  72. $this->error('请输入要查询的公司名字');
  73. }
  74. $res = $this->detailModel->where(['name'=>$name])->find();
  75. if(!$res) {
  76. $res = $this->curl_enterprise_detail($name);
  77. if(!$res){
  78. $this->error('未查询到此公司');
  79. }
  80. }else{
  81. $res = json_decode($res['content'],true);
  82. }
  83. if(!$res){
  84. $this->error('暂无此公司信息!');
  85. }
  86. // 工商信息
  87. $enterprise = [
  88. 'basic' => $res,// 基本信息
  89. 'employees_total' => count($res['employees']),
  90. 'employees' => $res['employees'],// 高管
  91. 'partners_total' => count($res['partners']),
  92. 'partners' => $res['partners'],// 股东
  93. 'branches_total' => count($res['branches']),
  94. 'branches' => $res['branches'],// 分支结构
  95. 'oupay_total' => 0,
  96. 'oupay' => [],// 对外投资
  97. 'changerecords_total' => count($res['changerecords']),
  98. 'changerecords' => $res['changerecords'],// 变更信息
  99. ];
  100. // 风险信息
  101. $risk = [
  102. 'abnormal_total' => isset($res['abnormal_items']) ? count($res['abnormal_items']) : 0,
  103. 'abnormal' => isset($res['abnormal_items']) ? $res['abnormal_items'] : [],// 经营异常
  104. ];
  105. $data = [
  106. 'name' => $res['name'],// 公司名字
  107. 'econ_kind' => $res['econ_kind'],// 注册资本
  108. 'oper_name' => $res['oper_name'],// 法人代表
  109. 'start_date'=> $res['start_date'],// 成立日期
  110. 'address' => $res['address'],// 地址
  111. 'money' => $res['regist_capi_new'],// 注册资本金额
  112. 'scope' => $res['scope'],// 经营范围
  113. 'belong_org'=> $res['belong_org'],// 发政机关(所属局)
  114. 'status' => $res['status'],// 经营情况?
  115. 'province' => $res['province'],// 省份缩写
  116. 'enterprise'=>$enterprise,// 工商信息
  117. 'risk' => $risk // 风险
  118. ];
  119. $this->success('',$data);
  120. }
  121. /**
  122. * 获取企业信息
  123. * @param $customerId
  124. * @throws \think\exception\DbException
  125. */
  126. public function curl_enterprise($keywords){
  127. $name = urlencode($keywords);
  128. if(empty($this->enterpriseCode)){
  129. $this->error('后台未设置聚合appid,请联系管理员');
  130. }
  131. $url = "http://japi.juhe.cn/enterprise/simpleList?key=".$this->enterpriseCode."&keyword=".$name;
  132. $res = Http::get($url);
  133. Log::write('查询信息返回-'.$keywords.'---'.$url.'====='.$res);
  134. $res = json_decode($res,true);
  135. if(empty($res) || $res['error_code'] != 0){
  136. $this->error($res['reason']);
  137. }
  138. $save = [
  139. 'keywords' => $keywords,
  140. 'total' => $res['result']['data']['total'],
  141. 'num' => $res['result']['data']['num'],
  142. 'content' => is_array($res['result']['data']['items']) ? json_encode($res['result']['data']['items']) : $res['result']['data']['items'],
  143. 'author_id' => $this->auth->id,
  144. 'author_' => $this->auth->name,
  145. 'res_status' => 0,
  146. ];
  147. try {
  148. Db::startTrans();
  149. $this->enterpriseModel->allowField(true)->save($save);
  150. Db::commit();
  151. } catch (Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. return $res['result']['data']['items'];
  156. }
  157. /**
  158. * 获取精准查询结果
  159. * @param $customerId
  160. * @throws \think\exception\DbException
  161. */
  162. public function curl_enterprise_detail($keywords){
  163. $name = urlencode($keywords);
  164. $url = "http://japi.juhe.cn/enterprise/getDetailByName?key=".$this->detailCode."&keyword=".$name;
  165. $res = Http::get($url);
  166. Log::write('查询精准信息返回-'.$keywords.'---'.$url.'===='.$res);
  167. $res = json_decode($res,true);
  168. if($res['error_code'] != 0){
  169. $this->error($res['reason']);
  170. }
  171. $save = [
  172. 'name' => $keywords,
  173. 'content' => is_array($res['result']['data']) ? json_encode($res['result']['data'],JSON_UNESCAPED_UNICODE) : $res['result']['data'],
  174. 'author_id' => $this->auth->id,
  175. 'author_' => $this->auth->name,
  176. 'res_status' => 0,
  177. ];
  178. try {
  179. Db::startTrans();
  180. $this->detailModel->allowField(true)->save($save);
  181. Db::commit();
  182. } catch (Exception $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. }
  186. return $res['result']['data'];
  187. }
  188. // 获取 上级id
  189. public static function getPid($id,$arr){
  190. $detail = CustomerModel::get($id);
  191. array_push($arr,$detail['parent_id']);
  192. if($detail['parent_id'] != 0){
  193. return ['id'=>$detail['parent_id'],'arr'=>$arr];
  194. }
  195. return self::getPid($detail['parent_id'],$arr);
  196. }
  197. }