AssetServerService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Log;
  4. class AssetServerService extends Service
  5. {
  6. public function loginRule($data){
  7. if(empty($data['name'])) return [false, '请输入账号!'];
  8. if(empty($data['password'])) return [false, '请输入密码!'];
  9. return [true, ''];
  10. list($status, $msg) = $this->getToken($data);
  11. if(! $status) return [false, $msg];
  12. return [true, ['data' => $msg]];
  13. }
  14. public function getToken($data){
  15. $account = $data['name'];
  16. $password = $data['password'];
  17. $url = config("asset.login");
  18. $post = [
  19. "name" => $account,
  20. "password" => $password,
  21. "rememberMe" => true
  22. ];
  23. $header = ['Content-Type:application/json'];
  24. list($status, $result) = $this->post_helper($url,$post, $header);
  25. if(! $status) return [$status, $result];
  26. //登录失败
  27. if(! empty($result['errorMessage'])) return [false, $result['errorMessage']];
  28. return [true, $result];
  29. }
  30. public function pdList($data,$param){
  31. $url = config("asset.pdList");
  32. $post['size'] = $data['size'] ?? 10;
  33. $post['number'] = ($data['number'] ?? 1) - 1;
  34. list($status,$result) = $this->post_helper($url,$post,$param['header']);
  35. if(! $status) return [$status, $result];
  36. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  37. if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
  38. if(! isset($result['content'])) {
  39. $error = $result[0]['message'] ?? "操作失败,请刷新页面";
  40. return [false, $error];
  41. }
  42. return [true, $result];
  43. }
  44. public function pdDetail($data,$param){
  45. if(empty($data['id'])) return [false, '请选择盘点单!'];
  46. $url = config("asset.pdDetail");
  47. $url .= $data['id'];
  48. list($status,$result) = $this->get_helper($url,$param['header']);
  49. if(! $status) return [$status, $result];
  50. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  51. if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
  52. if(! isset($result['content'])) {
  53. $error = $result[0]['message'] ?? "操作失败,请刷新页面";
  54. return [false, $error];
  55. }
  56. return [true, $result];
  57. }
  58. public function post_helper($url, $data, $header = [], $timeout = 20){
  59. Log::channel('apiLog')->info('资产盘点POST', ["api" => $url , "param" => $data ,"header" => $header]);
  60. $ch = curl_init();
  61. curl_setopt($ch, CURLOPT_URL, $url);
  62. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  63. curl_setopt($ch, CURLOPT_ENCODING, '');
  64. curl_setopt($ch, CURLOPT_POST, 1);
  65. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  68. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  69. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  70. $r = curl_exec($ch);
  71. if ($r === false) {
  72. // 获取错误号
  73. $errorNumber = curl_errno($ch);
  74. // 获取错误信息
  75. $errorMessage = curl_error($ch);
  76. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  77. Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $message]);
  78. return [false, $message];
  79. }
  80. curl_close($ch);
  81. $return = json_decode($r, true);
  82. Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $return]);
  83. if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正确'];
  84. return [true, $return];
  85. }
  86. public function get_helper($url,$header=[],$timeout = 20){
  87. Log::channel('apiLog')->info('资产盘点GET', ["api" => $url ,"header" => $header]);
  88. $ch = curl_init();
  89. curl_setopt_array($ch, array(
  90. CURLOPT_URL => $url,
  91. CURLOPT_RETURNTRANSFER => true,
  92. CURLOPT_ENCODING => '',
  93. CURLOPT_MAXREDIRS => 10,
  94. CURLOPT_TIMEOUT => $timeout,
  95. CURLOPT_FOLLOWLOCATION => true,
  96. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  97. CURLOPT_CUSTOMREQUEST => 'GET',
  98. CURLOPT_SSL_VERIFYPEER => false,
  99. CURLOPT_HTTPHEADER => $header,
  100. ));
  101. $r = curl_exec($ch);
  102. if ($r === false) {
  103. // 获取错误号
  104. $errorNumber = curl_errno($ch);
  105. // 获取错误信息
  106. $errorMessage = curl_error($ch);
  107. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  108. Log::channel('apiLog')->info('资产盘点GET', ["message" => $message]);
  109. return [false, $message];
  110. }
  111. curl_close($ch);
  112. $return = json_decode($r, true);
  113. Log::channel('apiLog')->info('资产盘点GET', ["message" => $return]);
  114. if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正常'];
  115. return [true, $return];
  116. }
  117. }