AssetServerService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. return [true, $result];
  53. }
  54. public function pdUpdate($data,$param){
  55. if(! isset($data['tourism_inventory_a'])) return [false, '盘点单抬头数据不能为空'];
  56. if(! isset($data['tourism_inventory_a']['id'])) return [false, '盘点单抬头数据ID不能为空'];
  57. if(! isset($data['tourism_inventory_dtl_b'])) return [false, '盘点单明细数据不能为空'];
  58. $url = config("asset.updatePd");
  59. $post = [
  60. 'tourism_inventory_a' => $data['tourism_inventory_a'],
  61. 'tourism_inventory_dtl_b' => $data['tourism_inventory_dtl_b'],
  62. ];
  63. list($status,$result) = $this->post_helper($url,$post,$param['header']);
  64. if(! $status) return [$status, $result];
  65. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  66. if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
  67. return [true, ''];
  68. }
  69. public function post_helper($url, $data, $header = [], $timeout = 20){
  70. Log::channel('apiLog')->info('资产盘点POST', ["api" => $url , "param" => $data ,"header" => $header]);
  71. $ch = curl_init();
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  74. curl_setopt($ch, CURLOPT_ENCODING, '');
  75. curl_setopt($ch, CURLOPT_POST, 1);
  76. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  77. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  78. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  79. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  80. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  81. $r = curl_exec($ch);
  82. if ($r === false) {
  83. // 获取错误号
  84. $errorNumber = curl_errno($ch);
  85. // 获取错误信息
  86. $errorMessage = curl_error($ch);
  87. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  88. Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $message]);
  89. return [false, $message];
  90. }
  91. curl_close($ch);
  92. $return = json_decode($r, true);
  93. Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $return]);
  94. if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正确'];
  95. return [true, $return];
  96. }
  97. public function get_helper($url,$header=[],$timeout = 20){
  98. Log::channel('apiLog')->info('资产盘点GET', ["api" => $url ,"header" => $header]);
  99. $ch = curl_init();
  100. curl_setopt_array($ch, array(
  101. CURLOPT_URL => $url,
  102. CURLOPT_RETURNTRANSFER => true,
  103. CURLOPT_ENCODING => '',
  104. CURLOPT_MAXREDIRS => 10,
  105. CURLOPT_TIMEOUT => $timeout,
  106. CURLOPT_FOLLOWLOCATION => true,
  107. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  108. CURLOPT_CUSTOMREQUEST => 'GET',
  109. CURLOPT_SSL_VERIFYPEER => false,
  110. CURLOPT_HTTPHEADER => $header,
  111. ));
  112. $r = curl_exec($ch);
  113. if ($r === false) {
  114. // 获取错误号
  115. $errorNumber = curl_errno($ch);
  116. // 获取错误信息
  117. $errorMessage = curl_error($ch);
  118. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  119. Log::channel('apiLog')->info('资产盘点GET', ["message" => $message]);
  120. return [false, $message];
  121. }
  122. curl_close($ch);
  123. $return = json_decode($r, true);
  124. Log::channel('apiLog')->info('资产盘点GET', ["message" => $return]);
  125. if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正常'];
  126. return [true, $return];
  127. }
  128. }