TestService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace App\Service;
  3. use Illuminate\Support\Facades\Config;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. class TestService extends Service
  7. {
  8. public function testdwy($data){
  9. if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  10. $url = $data['url'];
  11. $post = $data['post'];
  12. $header = $data['header'];
  13. $json = json_encode($post);
  14. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',$json);
  15. $json = str_replace('"loginBindingParameters":[]','"loginBindingParameters":{}',$json);
  16. list($status, $result) = $this->post_helper($url,$json, $header, 40);
  17. if(! $status) return [false, $result];
  18. return [true, $result];
  19. }
  20. public function testdwyget($data){
  21. if(empty($data['url']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  22. $url = $data['url'];
  23. $header = $data['header'];
  24. list($status,$result) = $this->get_helper($url,$header);
  25. if(! $status) return [false, $result];
  26. return [true, $result];
  27. }
  28. public function testdwyput($data){
  29. if(empty($data['url']) || empty($data['post']) || empty($data['header'])) return [false,'API请求参数不能为空'];
  30. $url = $data['url'];
  31. $post = $data['post'];
  32. $header = $data['header'];
  33. $json = json_encode($post);
  34. $json = str_replace('"workflowSearchBean":{}','"workflowSearchBean":[]',json_encode($post));
  35. $json = str_replace('"workflowSearchBean":[]','"workflowSearchBean":{}',json_encode($post));
  36. list($status, $result) = $this->put_helper($url,$json, $header,40);
  37. if(! $status) return [false, $result];
  38. return [true, $result];
  39. }
  40. public function updateTopStock($data){
  41. list($status, $msg) = $this->rule();
  42. if(! $status) return [false, 'IP未入白名单'];
  43. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getStock") return [false,'API请求参数不能为空'];
  44. if(empty($data['code'])) return [false,'API请求参数不能为空'];
  45. if(empty($data['warehouse'])) return [false,'API请求参数不能为空'];
  46. list($status, $msg) = $this->connectYy();
  47. if(! $status) return [false, $msg];
  48. //数据库
  49. $db = $msg[0];
  50. $u8 = $msg[1];
  51. list($status, $msg) = $this->getStock($db, $u8,$data['code'],$data['warehouse']);
  52. return [$status, $msg];
  53. }
  54. public function rule(){
  55. // 获取用户的IP地址
  56. $userIP = $_SERVER['REMOTE_ADDR'];
  57. // 获取设置的IP地址
  58. $ip = env("AliYUN");
  59. $allowedIPs = [$ip];
  60. $allowedIPs = array_filter($allowedIPs);
  61. if(empty($allowedIPs)) return [false, $userIP];
  62. // 校验用户IP是否在允许的范围内
  63. $isValidIP = false;
  64. foreach ($allowedIPs as $allowedIP) {
  65. if (strpos($allowedIP, '/') !== false) {
  66. // IP段表示法校验
  67. list($subnet, $mask) = explode('/', $allowedIP);
  68. if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  69. $isValidIP = true;
  70. break;
  71. }
  72. } else {
  73. // 单个IP地址校验
  74. if ($allowedIP === $userIP) {
  75. $isValidIP = true;
  76. break;
  77. }
  78. }
  79. }
  80. return [$isValidIP, $userIP];
  81. }
  82. public function connectYy(){
  83. $model_box = DB::connection("mysqlT9");
  84. $u8 = $model_box->table('setting')
  85. ->where('setting_name','u8')
  86. ->where('setting_value','<>','')
  87. ->first();
  88. if(empty($u8)) return [false, 'u8配置参数不存在!'];
  89. $u8 = $u8->setting_value;
  90. // 使用 eval() 函数执行字符串并转换为数组
  91. $u8 = eval("return {$u8};");
  92. if(empty($u8['domain'])) return [false, '外部域名不能为空!'];
  93. if(empty($u8['u8_api_port'])) return [false, 'u8程序API端口不能为空!'];
  94. if(empty($u8['u8_database_port'])) return [false, 'u8程序数据库端口不能为空!'];
  95. if(empty($u8['database'])) return [false, 'u8程序数据库不能为空!'];
  96. if(empty($u8['database_account'])) return [false, 'u8程序数据库登录账号不能为空!'];
  97. if(empty($u8['database_password'])) return [false, 'u8程序数据库登录密码不能为空!'];
  98. if(empty($u8['sAccID'])) return [false, 'u8程序sAccID不能为空!'];
  99. if(empty($u8['sServer'])) return [false, 'u8程序sServer不能为空!'];
  100. if(empty($u8['sUserID'])) return [false, 'u8程序sUserID不能为空!'];
  101. if(empty($u8['sPassword'])) return [false, 'u8程序sPassword不能为空!'];
  102. $config = [
  103. 'driver' => 'sqlsrv',
  104. 'host' => $u8['domain'],
  105. 'port' => $u8['u8_database_port'],
  106. 'database' => $u8['database'],
  107. 'username' => $u8['database_account'],
  108. 'password' => $u8['database_password'],
  109. ];
  110. // 数据库配置设置
  111. Config::set('database.connections.sqlsrvs', $config);
  112. // 连接
  113. try {
  114. $pdo = DB::connection('sqlsrvs')->getPdo();
  115. if ($pdo instanceof \PDO) {
  116. // 连接成功的逻辑代码
  117. $db = DB::connection('sqlsrvs');
  118. return [true, [$db, $u8]];
  119. } else {
  120. return [false, '连接失败!'];
  121. }
  122. } catch (\Throwable $e) {
  123. return [false, $e->getMessage()];
  124. }
  125. }
  126. public function getStock($db, $u8, $code = [], $warehouse = ""){
  127. if(empty($code) || empty($warehouse)) return [false, '存货以及仓库不能为空'];
  128. //映射ip是否通畅
  129. $bool = $this->isDomainAvailable($u8['domain']);
  130. if(! $bool) return [false, 'U8程序外部域名不可达'];
  131. $result = $db->table('CurrentStock')
  132. ->where("cWhCode", $warehouse)
  133. ->whereIn("cInvCode", $code)
  134. ->select('iQuantity as number', 'cInvCode as product_no')
  135. ->get()->toArray();
  136. $return = [];
  137. foreach ($result as $value){
  138. $return[] = [
  139. 'number' => floatval($value->number),
  140. 'product_no' => $value->product_no
  141. ];
  142. }
  143. return [true, $return];
  144. }
  145. public function post_helper($url, $data, $header = [], $timeout = 20){
  146. Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
  147. $ch = curl_init();
  148. curl_setopt($ch, CURLOPT_URL, $url);
  149. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  150. curl_setopt($ch, CURLOPT_ENCODING, '');
  151. curl_setopt($ch, CURLOPT_POST, 1);
  152. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  153. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  154. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  155. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  156. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  157. $r = curl_exec($ch);
  158. if ($r === false) {
  159. // 获取错误号
  160. $errorNumber = curl_errno($ch);
  161. // 获取错误信息
  162. $errorMessage = curl_error($ch);
  163. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  164. Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);
  165. return [false, $message];
  166. }
  167. curl_close($ch);
  168. Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);
  169. return [true, json_decode($r, true)];
  170. }
  171. public function get_helper($url,$header=[],$timeout = 20){
  172. $ch = curl_init();
  173. curl_setopt_array($ch, array(
  174. CURLOPT_URL => $url,
  175. CURLOPT_RETURNTRANSFER => true,
  176. CURLOPT_ENCODING => '',
  177. CURLOPT_MAXREDIRS => 10,
  178. CURLOPT_TIMEOUT => $timeout,
  179. CURLOPT_FOLLOWLOCATION => true,
  180. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  181. CURLOPT_CUSTOMREQUEST => 'GET',
  182. CURLOPT_SSL_VERIFYPEER => false,
  183. CURLOPT_HTTPHEADER => $header,
  184. ));
  185. $r = curl_exec($ch);
  186. if ($r === false) {
  187. // 获取错误号
  188. $errorNumber = curl_errno($ch);
  189. // 获取错误信息
  190. $errorMessage = curl_error($ch);
  191. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  192. Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);
  193. return [false, $message];
  194. }
  195. curl_close($ch);
  196. Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);
  197. return [true, json_decode($r, true)];
  198. }
  199. public function put_helper($url, $data, $header = [], $timeout = 20){
  200. Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);
  201. $ch = curl_init();
  202. curl_setopt($ch, CURLOPT_URL, $url);
  203. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  204. curl_setopt($ch, CURLOPT_ENCODING, '');
  205. curl_setopt($ch, CURLOPT_POST, 1);
  206. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  207. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  208. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  209. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  210. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  211. $r = curl_exec($ch);
  212. if ($r === false) {
  213. // 获取错误号
  214. $errorNumber = curl_errno($ch);
  215. // 获取错误信息
  216. $errorMessage = curl_error($ch);
  217. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  218. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);
  219. return [false, $message];
  220. }
  221. curl_close($ch);
  222. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);
  223. return [true, json_decode($r, true)];
  224. }
  225. }