TestService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 getSnList($data){
  55. list($status, $msg) = $this->rule();
  56. if(! $status) return [false, 'IP未入白名单'];
  57. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getSnList") return [false,'API请求参数不能为空'];
  58. if(empty($data['sn_type'])) return [false, 'sn码来源依据不能为空'];
  59. list($status, $msg) = $this->connectYy();
  60. if(! $status) return [false, $msg];
  61. //数据库
  62. $db = $msg[0];
  63. $u8 = $msg[1];
  64. if($data['sn_type'] == 1){
  65. list($status, $msg) = $this->getSnListFormU8One($db, $u8,$data);
  66. }else{
  67. //发货出库单 sn码
  68. list($status, $msg) = $this->getSnListFormU8Two($db, $u8,$data);
  69. }
  70. return [$status, $msg];
  71. }
  72. public function getSnforMap($data){
  73. list($status, $msg) = $this->rule();
  74. if(! $status) return [false, 'IP未入白名单'];
  75. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getSnMap") return [false,'API请求参数不能为空'];
  76. if(empty($data['sn'])) return [false, 'sn码不能为空'];
  77. if(empty($data['code'])) return [false, '产品编码不能为空'];
  78. list($status, $msg) = $this->connectYy();
  79. if(! $status) return [false, $msg];
  80. //数据库
  81. $db = $msg[0];
  82. $u8 = $msg[1];
  83. list($status, $msg) = $this->getSnListFormU8ForMap($db, $u8,$data);
  84. return [$status, $msg];
  85. }
  86. public function getSnForWarranty($data){
  87. list($status, $msg) = $this->rule();
  88. if(! $status) return [false, 'IP未入白名单'];
  89. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "getSnForWarranty") return [false,'API请求参数不能为空'];
  90. if(empty($data['sn'])) return [false, 'sn码不能为空'];
  91. list($status, $msg) = $this->connectYy();
  92. if(! $status) return [false, $msg];
  93. //数据库
  94. $db = $msg[0];
  95. $u8 = $msg[1];
  96. list($status, $msg) = $this->getSnForWarrantyData($db, $u8,$data);
  97. return [$status, $msg];
  98. }
  99. public function rule(){
  100. // 获取用户的IP地址
  101. $userIP = $_SERVER['REMOTE_ADDR'];
  102. // 获取设置的IP地址
  103. $ip = env("AliYUN");
  104. $allowedIPs = [$ip];
  105. $allowedIPs = array_filter($allowedIPs);
  106. if(empty($allowedIPs)) return [false, $userIP];
  107. // 校验用户IP是否在允许的范围内
  108. $isValidIP = false;
  109. foreach ($allowedIPs as $allowedIP) {
  110. if (strpos($allowedIP, '/') !== false) {
  111. // IP段表示法校验
  112. list($subnet, $mask) = explode('/', $allowedIP);
  113. if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  114. $isValidIP = true;
  115. break;
  116. }
  117. } else {
  118. // 单个IP地址校验
  119. if ($allowedIP === $userIP) {
  120. $isValidIP = true;
  121. break;
  122. }
  123. }
  124. }
  125. return [$isValidIP, $userIP];
  126. }
  127. public function connectYy(){
  128. $model_box = DB::connection("mysqlT9");
  129. $u8 = $model_box->table('setting')
  130. ->where('setting_name','u8')
  131. ->where('setting_value','<>','')
  132. ->first();
  133. if(empty($u8)) return [false, 'u8配置参数不存在!'];
  134. $u8 = $u8->setting_value;
  135. // 使用 eval() 函数执行字符串并转换为数组
  136. $u8 = eval("return {$u8};");
  137. if(empty($u8['domain'])) return [false, '外部域名不能为空!'];
  138. if(empty($u8['u8_api_port'])) return [false, 'u8程序API端口不能为空!'];
  139. if(empty($u8['u8_database_port'])) return [false, 'u8程序数据库端口不能为空!'];
  140. if(empty($u8['database'])) return [false, 'u8程序数据库不能为空!'];
  141. if(empty($u8['database_account'])) return [false, 'u8程序数据库登录账号不能为空!'];
  142. if(empty($u8['database_password'])) return [false, 'u8程序数据库登录密码不能为空!'];
  143. if(empty($u8['sAccID'])) return [false, 'u8程序sAccID不能为空!'];
  144. if(empty($u8['sServer'])) return [false, 'u8程序sServer不能为空!'];
  145. if(empty($u8['sUserID'])) return [false, 'u8程序sUserID不能为空!'];
  146. if(empty($u8['sPassword'])) return [false, 'u8程序sPassword不能为空!'];
  147. $config = [
  148. 'driver' => 'sqlsrv',
  149. 'host' => $u8['domain'],
  150. 'port' => $u8['u8_database_port'],
  151. 'database' => $u8['database'],
  152. 'username' => $u8['database_account'],
  153. 'password' => $u8['database_password'],
  154. ];
  155. // 数据库配置设置
  156. Config::set('database.connections.sqlsrvs', $config);
  157. // 连接
  158. try {
  159. $pdo = DB::connection('sqlsrvs')->getPdo();
  160. if ($pdo instanceof \PDO) {
  161. // 连接成功的逻辑代码
  162. $db = DB::connection('sqlsrvs');
  163. return [true, [$db, $u8]];
  164. } else {
  165. return [false, '连接失败!'];
  166. }
  167. } catch (\Throwable $e) {
  168. return [false, $e->getMessage()];
  169. }
  170. }
  171. public function getStock($db, $u8, $code = [], $warehouse = ""){
  172. if(empty($code) || empty($warehouse)) return [false, '存货以及仓库不能为空'];
  173. //映射ip是否通畅
  174. $bool = $this->isDomainAvailable($u8['domain']);
  175. if(! $bool) return [false, 'U8程序外部域名不可达'];
  176. $result = $db->table('CurrentStock')
  177. ->where("cWhCode", $warehouse)
  178. ->whereIn("cInvCode", $code)
  179. ->select('iQuantity as number', 'cInvCode as product_no')
  180. ->get()->toArray();
  181. $return = [];
  182. foreach ($result as $value){
  183. $return[] = [
  184. 'number' => floatval($value->number),
  185. 'product_no' => $value->product_no
  186. ];
  187. }
  188. return [true, $return];
  189. }
  190. public function getSnListFormU8One($db, $u8, $data){
  191. if(empty($data['code'])) return [false, '存货不能为空'];
  192. $sn = $data['sn'] ?? "";
  193. $warehouse = [
  194. '001',
  195. '003',
  196. '010',
  197. ];
  198. //映射ip是否通畅
  199. $bool = $this->isDomainAvailable($u8['domain']);
  200. if(! $bool) return [false, 'U8程序外部域名不可达'];
  201. //检索条件 在库的
  202. $model = $db->table('ST_SNState')
  203. ->select('cinvCode as code','cInvSN as sn')
  204. ->whereIn("cWhCode", $warehouse)
  205. ->where("cInvCode", $data['code'])
  206. ->where("iSNState", 2)
  207. ->when(! empty($sn), function ($query) use ($sn) {
  208. return $query->where('cInvSN', 'LIKE', '%'.$sn.'%');
  209. });
  210. $list = $this->limit($model, '', $data);
  211. return [true, $list];
  212. }
  213. public function getSnListFormU8Two($db, $u8, $data){
  214. if(empty($data['code']) || empty($data['depart_title'])) return [false, '存货以及门店信息不能为空'];
  215. $sn = $data['sn'] ?? "";
  216. //映射ip是否通畅
  217. $bool = $this->isDomainAvailable($u8['domain']);
  218. if(! $bool) return [false, 'U8程序外部域名不可达'];
  219. $model = $db->table('rdrecord32 as a')
  220. ->leftJoin('rdrecords32 as b','b.ID','a.ID')
  221. ->leftJoin('ST_SNDetail_SaleOut as c','c.iVouchsID','b.AutoID')
  222. ->select("c.cInvCode as code",'c.cinvSN as sn')
  223. ->whereNotNull('a.cHandler')
  224. ->where("b.cInvCode", $data['code'])
  225. ->where("b.iQuantity", '>', 0)
  226. ->where("b.cDefine31", $data['depart_title'])
  227. ->whereNotNull("c.cinvSN")
  228. ->when(! empty($sn), function ($query) use ($sn) {
  229. return $query->where('c.cInvSN', 'LIKE', '%'.$sn.'%');
  230. })
  231. ->orderBy('a.ID','desc');
  232. $list = $this->limit($model, '', $data);
  233. return [true, $list];
  234. }
  235. public function getSnListFormU8ForMap($db, $u8, $data){
  236. //映射ip是否通畅
  237. $bool = $this->isDomainAvailable($u8['domain']);
  238. if(! $bool) return [false, 'U8程序外部域名不可达'];
  239. $warehouse = [
  240. '001',
  241. '003',
  242. '010',
  243. ];
  244. $list = $db->table('ST_SNState')
  245. ->select("cInvCode as code",'cinvSN as sn')
  246. ->whereIn("cWhCode", $warehouse)
  247. ->whereIn("cInvCode", $data['code'])
  248. ->whereIn('cInvSN', $data['sn'])
  249. ->where("iSNState", 2)
  250. ->get()->toArray();
  251. return [true, $list];
  252. }
  253. public function getSnForWarrantyData($db, $u8, $data){
  254. //映射ip是否通畅
  255. $bool = $this->isDomainAvailable($u8['domain']);
  256. if(! $bool) return [false, 'U8程序外部域名不可达'];
  257. $warehouse = [
  258. '001',
  259. '003',
  260. '010',
  261. ];
  262. $list = $db->table('ST_SNState')
  263. ->select("cInvCode as code",'cinvSN as sn')
  264. ->whereIn("cWhCode", $warehouse)
  265. ->where('cInvSN', $data['sn'])
  266. // ->where("iSNState", 2)
  267. ->first();
  268. return [true, $list];
  269. }
  270. public function post_helper($url, $data, $header = [], $timeout = 20){
  271. Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
  272. $ch = curl_init();
  273. curl_setopt($ch, CURLOPT_URL, $url);
  274. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  275. curl_setopt($ch, CURLOPT_ENCODING, '');
  276. curl_setopt($ch, CURLOPT_POST, 1);
  277. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  278. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  279. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  280. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  281. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  282. $r = curl_exec($ch);
  283. if ($r === false) {
  284. // 获取错误号
  285. $errorNumber = curl_errno($ch);
  286. // 获取错误信息
  287. $errorMessage = curl_error($ch);
  288. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  289. Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);
  290. return [false, $message];
  291. }
  292. curl_close($ch);
  293. Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);
  294. return [true, json_decode($r, true)];
  295. }
  296. public function get_helper($url,$header=[],$timeout = 20){
  297. $ch = curl_init();
  298. curl_setopt_array($ch, array(
  299. CURLOPT_URL => $url,
  300. CURLOPT_RETURNTRANSFER => true,
  301. CURLOPT_ENCODING => '',
  302. CURLOPT_MAXREDIRS => 10,
  303. CURLOPT_TIMEOUT => $timeout,
  304. CURLOPT_FOLLOWLOCATION => true,
  305. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  306. CURLOPT_CUSTOMREQUEST => 'GET',
  307. CURLOPT_SSL_VERIFYPEER => false,
  308. CURLOPT_HTTPHEADER => $header,
  309. ));
  310. $r = curl_exec($ch);
  311. if ($r === false) {
  312. // 获取错误号
  313. $errorNumber = curl_errno($ch);
  314. // 获取错误信息
  315. $errorMessage = curl_error($ch);
  316. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  317. Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);
  318. return [false, $message];
  319. }
  320. curl_close($ch);
  321. Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);
  322. return [true, json_decode($r, true)];
  323. }
  324. public function put_helper($url, $data, $header = [], $timeout = 20){
  325. Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);
  326. $ch = curl_init();
  327. curl_setopt($ch, CURLOPT_URL, $url);
  328. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  329. curl_setopt($ch, CURLOPT_ENCODING, '');
  330. curl_setopt($ch, CURLOPT_POST, 1);
  331. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  332. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  333. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  334. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  335. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  336. $r = curl_exec($ch);
  337. if ($r === false) {
  338. // 获取错误号
  339. $errorNumber = curl_errno($ch);
  340. // 获取错误信息
  341. $errorMessage = curl_error($ch);
  342. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  343. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);
  344. return [false, $message];
  345. }
  346. curl_close($ch);
  347. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);
  348. return [true, json_decode($r, true)];
  349. }
  350. }