AssetServerService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. if(! isset($result['success'])) {
  68. $error = $result[0]['message'] ?? "操作失败,请刷新页面";
  69. return [false, $error];
  70. }
  71. return [true, ''];
  72. }
  73. public function createFixedBorrow($data,$param){
  74. if(empty($data['id'])) return [false, 'ID不能为空'];
  75. if(empty($data['ssbm']) || empty($data['ssbm_show'])) return [false, '部门不能为空'];
  76. if(empty($data['ssfb']) || empty($data['ssfb_show'])) return [false, '分部不能为空'];
  77. if(empty($data['borrow_start_date']) || empty($data['borrow_start_date_lt'])) return [false, '借用开始不能为空'];
  78. if(empty($data['borrow_end_date']) || empty($data['borrow_end_date_lt'])) return [false, '借用结束不能为空'];
  79. if(empty($data['borrow_person']) || empty($data['borrow_person_show'])) return [false, '借用人不能为空'];
  80. $url = config("asset.create_fixed_borrow");
  81. $post = [
  82. 'id' => $data['id'],
  83. 'ssbm' => $data['ssbm'],
  84. 'ssbm_show' => $data['ssbm_show'],
  85. 'ssfb' => $data['ssfb'],
  86. 'ssfb_show' => $data['ssfb_show'],
  87. 'borrow_start_date' => $data['borrow_start_date'],
  88. 'borrow_start_date_lt' => $data['borrow_start_date_lt'],
  89. 'borrow_end_date' => $data['borrow_end_date'],
  90. 'borrow_end_date_lt' => $data['borrow_end_date_lt'],
  91. 'borrow_person' => $data['borrow_person'],
  92. 'borrow_person_show' => $data['borrow_person_show'],
  93. ];
  94. list($status,$result) = $this->post_helper($url,$post,$param['header']);
  95. if(! $status) return [$status, $result];
  96. if(! empty($result['status']) && $result['status'] == 'error') return [false, $result['msg']];
  97. if(! empty($result['type']) && $result['type'] == 'errorVm') return [false, $result['message']];
  98. if(! isset($result['success'])) {
  99. $error = $result[0]['message'] ?? "操作失败,请刷新页面";
  100. return [false, $error];
  101. }
  102. return [true, ''];
  103. }
  104. public function post_helper($url, $data, $header = [], $timeout = 20){
  105. Log::channel('apiLog')->info('资产盘点POST', ["api" => $url , "param" => $data ,"header" => $header]);
  106. $ch = curl_init();
  107. curl_setopt($ch, CURLOPT_URL, $url);
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  109. curl_setopt($ch, CURLOPT_ENCODING, '');
  110. curl_setopt($ch, CURLOPT_POST, 1);
  111. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  112. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  113. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  114. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  115. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  116. $r = curl_exec($ch);
  117. if ($r === false) {
  118. // 获取错误号
  119. $errorNumber = curl_errno($ch);
  120. // 获取错误信息
  121. $errorMessage = curl_error($ch);
  122. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  123. Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $message]);
  124. return [false, $message];
  125. }
  126. curl_close($ch);
  127. $return = json_decode($r, true);
  128. Log::channel('apiLog')->info('资产盘点POST结果', ["message" => $return]);
  129. if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正确'];
  130. return [true, $return];
  131. }
  132. public function get_helper($url,$header=[],$timeout = 20){
  133. Log::channel('apiLog')->info('资产盘点GET', ["api" => $url ,"header" => $header]);
  134. $ch = curl_init();
  135. curl_setopt_array($ch, array(
  136. CURLOPT_URL => $url,
  137. CURLOPT_RETURNTRANSFER => true,
  138. CURLOPT_ENCODING => '',
  139. CURLOPT_MAXREDIRS => 10,
  140. CURLOPT_TIMEOUT => $timeout,
  141. CURLOPT_FOLLOWLOCATION => true,
  142. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  143. CURLOPT_CUSTOMREQUEST => 'GET',
  144. CURLOPT_SSL_VERIFYPEER => false,
  145. CURLOPT_HTTPHEADER => $header,
  146. ));
  147. $r = curl_exec($ch);
  148. if ($r === false) {
  149. // 获取错误号
  150. $errorNumber = curl_errno($ch);
  151. // 获取错误信息
  152. $errorMessage = curl_error($ch);
  153. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  154. Log::channel('apiLog')->info('资产盘点GET', ["message" => $message]);
  155. return [false, $message];
  156. }
  157. curl_close($ch);
  158. $return = json_decode($r, true);
  159. Log::channel('apiLog')->info('资产盘点GET', ["message" => $return]);
  160. if(! empty($return['message']) && $return['message'] == 'error.unAuthorized') return [0, '登录凭证已失效或者不正常'];
  161. return [true, $return];
  162. }
  163. }