CommonController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace app\controller;
  3. require_once app()->getRootPath().'vendor/autoload.php';
  4. use app\BaseController;
  5. use Neoxygen\NeoClient\ClientBuilder;
  6. use think\facade\Cache;
  7. use think\facade\Request;
  8. use think\facade\Log;
  9. class CommonController extends BaseController
  10. {
  11. // checkIsExistSession
  12. public function checkIslogin()
  13. {
  14. return Request::session('name') ?? false;
  15. }
  16. /**
  17. * Connect Neo4j
  18. * return Object
  19. */
  20. private function connectNeo4j()
  21. {
  22. $client = ClientBuilder::create()
  23. //->addConnection('default', 'http', '59.110.24.172', 7474, true, 'neo4j', 'jm@123')
  24. ->addConnection('default', 'http', '120.46.136.62', 7474, true, 'neo4j', 'neo4j-pwd')
  25. ->setAutoFormatResponse(true)
  26. ->setDefaultTimeout(200)
  27. ->build();
  28. return $client;
  29. }
  30. private function connectNeo4jWesternMedicine()
  31. {
  32. $client = ClientBuilder::create()
  33. ->addConnection('default', 'http', '124.70.100.133', 7474, true, 'neo4j', 'neo4j-pwd')
  34. ->setAutoFormatResponse(true)
  35. ->setDefaultTimeout(200)
  36. ->build();
  37. return $client;
  38. }
  39. /**
  40. * Create Node
  41. * @param $node string
  42. * @param $label string
  43. * @param $data array
  44. */
  45. public function createNode($node , $label , $data)
  46. {
  47. $client = $this->connectNeo4jWesternMedicine();
  48. $str = json_encode($data , JSON_UNESCAPED_UNICODE);
  49. $singleData = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9]*)":/' ,'$1:' , $str); // 替换key引号
  50. $query = "CREATE (". $node . ":" . $label . $singleData . ") RETURN $node";
  51. $query = stripslashes($query);
  52. return $client->sendCypherQuery($query);
  53. }
  54. public function getRelationshipsByLable()
  55. {
  56. $client = $this->connectNeo4jWesternMedicine();
  57. $query = "MATCH p=()-[r:SubordinateDepartment]->() RETURN p";
  58. $result = $client->sendCypherQuery($query)->getResult();
  59. return $result;
  60. }
  61. /**
  62. * Select SingleNode
  63. * @param $node string
  64. * @param $label string
  65. * @param $field string
  66. * @param $value string
  67. * return array
  68. */
  69. public function getNodes($tag , $node , $label , $skip , $limit , $field = '' , $value = '' , $fields = [])
  70. {
  71. $client = $tag == 'zy' ? $this->connectNeo4j() : $this->connectNeo4jWesternMedicine();
  72. if($field) {
  73. $query = "match (". $node . ":" . $label .")"."where $node . $field =~ '.*".$value.".*' ". " RETURN $node";
  74. // $result = $client->sendCypherQuery($query)->getResult()->getSingleNode();
  75. // return $result->getProperties($fields);
  76. $data = $client->sendCypherQuery($query)->getResult();
  77. } else {
  78. $query = "match (". $node . ":" . $label .") RETURN $node skip $skip limit $limit";
  79. $data = $client->sendCypherQuery($query)->getResult();
  80. }
  81. foreach ($data->getNodes() as $key => $node){
  82. $datas[] = ['id' => $node->getId()];
  83. }
  84. $list = array_column($p = $data->getTableFormat() , 'n');
  85. foreach ($list as $k => $value) {
  86. $list[$k]['id'] = $datas[$k]['id'];
  87. }
  88. $query1 = "MATCH (n:".$label.") RETURN count(n) as count";
  89. $count = $client->sendCypherQuery($query1)->getResult()->getTableFormat();
  90. $list = ['list' => $list , 'count' => $count[0]['count']];
  91. return $list;
  92. }
  93. // getNodeById
  94. public function getNodeById($zsk , $id , $tag = '')
  95. {
  96. $client = $zsk == 'zy' ? $this->connectNeo4j() : $this->connectNeo4jWesternMedicine();
  97. $query = "MATCH (n) WHERE id(n)=$id RETURN n";
  98. $result = $client->sendCypherQuery($query)->getResult()->getSingleNode();
  99. if($tag) {
  100. return $result->getLabels();
  101. }
  102. $detail = $result->getProperties();
  103. $detail['id'] = $id;
  104. return $detail;
  105. }
  106. // set node
  107. // public function setNode($node , $label , $filed , $value , $whereField = '', $whereValue = '', $tag = 2)
  108. // {
  109. // // match(userd:Userd) where userd.name = "中文4" set userd.name = "修改测试" return userd;
  110. // $client = $this->connectNeo4j();
  111. //
  112. // if(1 == $tag) {
  113. // $query = "match (". $node . ":" . $label .") where ".$node.".".$whereField."='$whereValue' set $node.$filed='$value' RETURN $node";
  114. // } else {
  115. //
  116. // $query = "match (". $node . ":" . $label .") set $node.$filed='$value' RETURN $node";
  117. // }
  118. // return $client->sendCypherQuery($query);
  119. // }
  120. //MATCH (r)
  121. //WHERE id(r) = 501
  122. //SET r.test = "testtest"
  123. public function setNode($query , $tag = 'zy')
  124. {
  125. $client = $tag == 'zy' ? $this->connectNeo4j() : $this->connectNeo4jWesternMedicine();
  126. $result = $client->sendCypherQuery($query);
  127. return $result;
  128. }
  129. // delete node
  130. public function deleteNode()
  131. {
  132. }
  133. // remove node
  134. public function removeNode()
  135. {
  136. }
  137. public function checkPhone($phone)
  138. {
  139. return preg_match("/^1[3456789]{1}\d{9}$/", $phone) ?? false;
  140. }
  141. /**
  142. * Json Success
  143. */
  144. public function _json_succ($data = [] , $code = 0 , $msg = 'Success')
  145. {
  146. return json(
  147. [
  148. 'code' => $code ,
  149. 'msg' => $msg ,
  150. 'data' => $data
  151. ]
  152. );
  153. }
  154. /**
  155. * Json error
  156. */
  157. public function _json_error($msg = '' , $code = 1 , $data = null)
  158. {
  159. return json(
  160. [
  161. 'code' => $code ,
  162. 'msg' => $msg ,
  163. 'data' => $data
  164. ]
  165. );
  166. }
  167. /**
  168. * 发送短信
  169. */
  170. public function curlSmsSend($mobile , $code)
  171. {
  172. $ch = curl_init();
  173. curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");
  174. curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
  175. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
  176. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  177. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  178. curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
  179. curl_setopt($ch, CURLOPT_USERPWD , 'api:key-194cc0004de341090bbbce1c14a27c8f');
  180. curl_setopt($ch, CURLOPT_POST, TRUE);
  181. curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $mobile,'message' => '验证码:'.$code.'【铁壳测试】'));
  182. $res = curl_exec( $ch );
  183. curl_close( $ch );
  184. return $res;
  185. }
  186. /**
  187. * 订单号
  188. */
  189. protected function createOrderNum()
  190. {
  191. return date('Ymd') . str_pad(random_int(1 , 99999), 5 , '0' , STR_PAD_LEFT);
  192. }
  193. // ----------------------------------------------- 翻译模块 -----------------------------------------
  194. /**
  195. * 获取api调取验证token
  196. * 返回格式:string(93) "{"errcode":0,"msg":"success","data":{"authtoken":"03364a56aef9fbf63e0479d670e7fc4bef3f6dd9"}}"
  197. */
  198. protected function getAuthToken()
  199. {
  200. $authtoken = Cache::get('authtoken') ?? '';
  201. if( ! $authtoken ) {
  202. $params = config('translate.user');
  203. $url = config('translate.request_url.get_authtoken_url');
  204. $response = $this->curlPostRequest($url , $params);
  205. if( ! $response) {
  206. return $this->_json_error('请求失败!');
  207. }
  208. $result = json_decode($response , true);
  209. $authtoken = $result['data']['authtoken'];
  210. /**
  211. * 设置缓存
  212. */
  213. Cache::set('authtoken' , $authtoken , config('translate.expire.authtoken_expire'));
  214. return $authtoken;
  215. }
  216. return $authtoken;
  217. }
  218. /**
  219. * 获取企业/个人信息
  220. */
  221. protected function profile()
  222. {
  223. $url = config('translate.request_url.get_profile_url');
  224. $response = $this->curlGetRequest($url);
  225. Log::info(__FUNCTION__.': ' . $response);
  226. if( ! $response) {
  227. return false;
  228. }
  229. $result = json_decode($response , true);
  230. return $result ?? '';
  231. }
  232. /**
  233. * curl get 请求
  234. */
  235. protected function curlGetRequest($url)
  236. {
  237. $authtoken = $this->getAuthToken();
  238. $curl_con = curl_init();
  239. curl_setopt($curl_con, CURLOPT_URL , $url);
  240. curl_setopt($curl_con, CURLOPT_HEADER, false);
  241. curl_setopt($curl_con, CURLOPT_CUSTOMREQUEST, 'GET');
  242. curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, 1);
  243. curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
  244. curl_setopt($curl_con, CURLOPT_FOLLOWLOCATION, true); // 解决curl返回301
  245. curl_setopt($curl_con, CURLOPT_HTTPHEADER , array(
  246. "Content-Type: application/json;charset=utf-8",
  247. "Authorization: Token $authtoken"
  248. )
  249. );
  250. $res = curl_exec($curl_con);
  251. $status = curl_getinfo($curl_con);
  252. curl_close($curl_con);
  253. if (isset($status['http_code']) && 200 == $status['http_code']) {
  254. return $res;
  255. } else {
  256. return false;
  257. }
  258. }
  259. /**
  260. * curl post 请求
  261. */
  262. protected function curlPostRequest($url , $params , $authtoken = '')
  263. {
  264. $data = json_encode($params , JSON_UNESCAPED_UNICODE);
  265. $curl_con = curl_init();
  266. curl_setopt($curl_con, CURLOPT_URL , $url);
  267. curl_setopt($curl_con, CURLOPT_HEADER, false);
  268. curl_setopt($curl_con, CURLOPT_CUSTOMREQUEST, 'POST');
  269. curl_setopt($curl_con, CURLOPT_POSTFIELDS, $data);
  270. curl_setopt($curl_con, CURLOPT_RETURNTRANSFER, 1);
  271. curl_setopt($curl_con, CURLOPT_CONNECTTIMEOUT, 5);
  272. curl_setopt($curl_con, CURLOPT_FOLLOWLOCATION, true); // 解决curl返回301
  273. if($authtoken) {
  274. curl_setopt($curl_con, CURLOPT_HTTPHEADER , array(
  275. "Content-Type: application/json;charset=utf-8" ,
  276. "Authorization: Token $authtoken"
  277. )
  278. );
  279. } else {
  280. curl_setopt($curl_con, CURLOPT_HTTPHEADER , array(
  281. "Content-Type: application/json;charset=utf-8"
  282. )
  283. );
  284. }
  285. $res = curl_exec($curl_con);
  286. $status = curl_getinfo($curl_con);
  287. curl_close($curl_con);
  288. if (isset($status['http_code']) && 200 == $status['http_code']) {
  289. return $res;
  290. } else {
  291. return false;
  292. }
  293. }
  294. }