TestService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. $construction_id = $data['construction_id'] ?? 0;
  194. $warehouse = [
  195. '001',
  196. '003',
  197. '010',
  198. ];
  199. //映射ip是否通畅
  200. $bool = $this->isDomainAvailable($u8['domain']);
  201. if(! $bool) return [false, 'U8程序外部域名不可达'];
  202. // $db->enableQueryLog();
  203. //检索条件 在库的
  204. $model = $db->table('ST_SNState')
  205. ->select('cinvCode as code','cInvSN as sn','AutoID as auto_id')
  206. ->whereIn("cWhCode", $warehouse)
  207. ->where("cInvCode", $data['code'])
  208. ->where("iSNState", 2)
  209. ->when(! empty($sn), function ($query) use ($sn) {
  210. return $query->where('cInvSN', 'LIKE', '%'.$sn.'%');
  211. })
  212. // ->when(! empty($construction_id), function ($query) use ($construction_id) {
  213. // return $query->whereNull('cSNDefine1')->orWhere('cSNDefine1', '')->orWhere('cSNDefine1', $construction_id);
  214. // })
  215. ->when(empty($construction_id), function ($query) {
  216. return $query->where(function ($q) {
  217. $q->whereNull('cSNDefine1')
  218. ->orWhere('cSNDefine1', '');
  219. });
  220. })
  221. ->orderBy('cSNDefine1','desc')
  222. ->orderBy('AutoID','asc');
  223. $list = $this->limit($model, '', $data);
  224. // dd($db->getQueryLog());
  225. return [true, $list];
  226. }
  227. public function getSnListFormU8Two($db, $u8, $data){
  228. if(empty($data['code']) || empty($data['depart_title'])) return [false, '存货以及门店信息不能为空'];
  229. $sn = $data['sn'] ?? "";
  230. $construction_id = $data['construction_id'] ?? 0;
  231. // $db->enableQueryLog();
  232. //映射ip是否通畅
  233. $bool = $this->isDomainAvailable($u8['domain']);
  234. if(! $bool) return [false, 'U8程序外部域名不可达'];
  235. $model = $db->table('rdrecord32 as a')
  236. ->leftJoin('rdrecords32 as b','b.ID','a.ID')
  237. ->leftJoin('ST_SNDetail_SaleOut as c','c.iVouchsID','b.AutoID')
  238. ->select("c.cInvCode as code",'c.cinvSN as sn','c.AutoID as auto_id') //,'c.cSNDefine1','a.ID'
  239. ->whereNotNull('a.cHandler')
  240. ->where("b.cInvCode", $data['code'])
  241. ->where("b.iQuantity", '>', 0)
  242. ->where("b.cDefine31", $data['depart_title'])
  243. ->whereNotNull("c.cinvSN")
  244. ->when(! empty($sn), function ($query) use ($sn) {
  245. return $query->where('c.cInvSN', 'LIKE', '%'.$sn.'%');
  246. })
  247. // ->when(! empty($construction_id), function ($query) use ($construction_id) {
  248. // return $query->whereNull('c.cSNDefine1')->orWhere('c.cSNDefine1', '')->orWhere('c.cSNDefine1', $construction_id);
  249. // })
  250. ->when(empty($construction_id), function ($query) {
  251. return $query->where(function ($q) {
  252. $q->whereNull('c.cSNDefine1')
  253. ->orWhere('c.cSNDefine1', '');
  254. });
  255. })
  256. ->orderBy('c.cSNDefine1','desc')
  257. ->orderBy('a.ID','desc')
  258. ->orderBy('c.AutoID','asc');
  259. $list = $this->limit($model, '', $data);
  260. // $logs = $db->getQueryLog();dd($logs);
  261. return [true, $list];
  262. }
  263. public function getSnListFormU8ForMap($db, $u8, $data){
  264. //映射ip是否通畅
  265. $bool = $this->isDomainAvailable($u8['domain']);
  266. if(! $bool) return [false, 'U8程序外部域名不可达'];
  267. $warehouse = [
  268. '001',
  269. '003',
  270. '010',
  271. ];
  272. $list = $db->table('ST_SNState')
  273. ->select("cInvCode as code",'cinvSN as sn')
  274. ->whereIn("cWhCode", $warehouse)
  275. ->whereIn("cInvCode", $data['code'])
  276. ->whereIn('cInvSN', $data['sn'])
  277. // ->where("iSNState", 2)
  278. ->get()->toArray();
  279. return [true, $list];
  280. }
  281. public function getSnForWarrantyData($db, $u8, $data){
  282. //映射ip是否通畅
  283. $bool = $this->isDomainAvailable($u8['domain']);
  284. if(! $bool) return [false, 'U8程序外部域名不可达'];
  285. $warehouse = [
  286. '001',
  287. '003',
  288. '010',
  289. ];
  290. $list = $db->table('ST_SNState')
  291. ->select("cInvCode as code",'cinvSN as sn','AutoID as auto_id')
  292. ->whereIn("cWhCode", $warehouse)
  293. ->where('cInvSN', $data['sn'])
  294. // ->where("iSNState", 2)
  295. ->first();
  296. return [true, $list];
  297. }
  298. public function saveSnUseData($data){
  299. list($status, $msg) = $this->rule();
  300. if(! $status) return [false, 'IP未入白名单'];
  301. if(empty($data['urlFromT9']) || $data['urlFromT9'] != "saveSnUseData") return [false,'API请求参数不能为空'];
  302. if(empty($data['sn_type'])) return [false,'sn码来源依据不能为空'];
  303. // if(empty($data['sn_for_u8'])) return [false, 'sn码更新信息不能为空'];
  304. if(empty($data['data_id'])) return [false, 'dataID信息不能为空'];
  305. list($status, $msg) = $this->connectYy();
  306. if(! $status) return [false, $msg];
  307. //数据库
  308. $db = $msg[0];
  309. $u8 = $msg[1];
  310. list($status, $msg) = $this->saveSnUseDataDetail($db, $u8,$data);
  311. return [$status, $msg];
  312. }
  313. public function saveSnUseDataDetail($db, $u8, $data){
  314. //映射ip是否通畅
  315. $bool = $this->isDomainAvailable($u8['domain']);
  316. if(! $bool) return [false, 'U8程序外部域名不可达'];
  317. $sn_for_u8 = $data['sn_for_u8'] ?? [];
  318. try {
  319. DB::beginTransaction();
  320. if(empty($sn_for_u8)){//删除
  321. if($data['sn_type'] == 1){
  322. $db->table('ST_SNState')
  323. ->where('cSNDefine1', $data['data_id'])
  324. ->update(['cSNDefine1' => ""]);
  325. }else{
  326. $db->table('ST_SNDetail_SaleOut')
  327. ->where('cSNDefine1', $data['data_id'])
  328. ->update(['cSNDefine1' => ""]);
  329. }
  330. }else{//增加或更新
  331. if($data['sn_type'] == 1){
  332. $db->table('ST_SNState')
  333. ->whereIn('AutoID', $sn_for_u8)
  334. ->update(['cSNDefine1' => $data['data_id']]);
  335. }else{
  336. $db->table('ST_SNDetail_SaleOut')
  337. ->whereIn('AutoID', $sn_for_u8)
  338. ->update(['cSNDefine1' => $data['data_id']]);
  339. }
  340. }
  341. DB::commit();
  342. }catch (\Exception $exception){
  343. DB::rollBack();
  344. return [false, $exception->getMessage()];
  345. }
  346. return [true, ''];
  347. }
  348. public function post_helper($url, $data, $header = [], $timeout = 20){
  349. Log::channel('apiLog')->info('朗峰POST', ["api" => $url , "param" => $data ,"header" => $header]);
  350. $ch = curl_init();
  351. curl_setopt($ch, CURLOPT_URL, $url);
  352. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  353. curl_setopt($ch, CURLOPT_ENCODING, '');
  354. curl_setopt($ch, CURLOPT_POST, 1);
  355. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  356. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  357. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  358. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  359. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  360. $r = curl_exec($ch);
  361. if ($r === false) {
  362. // 获取错误号
  363. $errorNumber = curl_errno($ch);
  364. // 获取错误信息
  365. $errorMessage = curl_error($ch);
  366. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  367. Log::channel('apiLog')->info('朗峰POST结果', ["message" => $message ]);
  368. return [false, $message];
  369. }
  370. curl_close($ch);
  371. Log::channel('apiLog')->info('朗峰POST结果', ["message" => json_decode($r, true) ]);
  372. return [true, json_decode($r, true)];
  373. }
  374. public function get_helper($url,$header=[],$timeout = 20){
  375. $ch = curl_init();
  376. curl_setopt_array($ch, array(
  377. CURLOPT_URL => $url,
  378. CURLOPT_RETURNTRANSFER => true,
  379. CURLOPT_ENCODING => '',
  380. CURLOPT_MAXREDIRS => 10,
  381. CURLOPT_TIMEOUT => $timeout,
  382. CURLOPT_FOLLOWLOCATION => true,
  383. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  384. CURLOPT_CUSTOMREQUEST => 'GET',
  385. CURLOPT_SSL_VERIFYPEER => false,
  386. CURLOPT_HTTPHEADER => $header,
  387. ));
  388. $r = curl_exec($ch);
  389. if ($r === false) {
  390. // 获取错误号
  391. $errorNumber = curl_errno($ch);
  392. // 获取错误信息
  393. $errorMessage = curl_error($ch);
  394. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  395. Log::channel('apiLog')->info('朗峰GET结果', ["message" => $message]);
  396. return [false, $message];
  397. }
  398. curl_close($ch);
  399. Log::channel('apiLog')->info('朗峰GET结果', ["message" => json_decode($r, true)]);
  400. return [true, json_decode($r, true)];
  401. }
  402. public function put_helper($url, $data, $header = [], $timeout = 20){
  403. Log::channel('apiLog')->info('朗峰PUT', ["api" => $url , "param" => $data ,"header" => $header]);
  404. $ch = curl_init();
  405. curl_setopt($ch, CURLOPT_URL, $url);
  406. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  407. curl_setopt($ch, CURLOPT_ENCODING, '');
  408. curl_setopt($ch, CURLOPT_POST, 1);
  409. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  410. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  411. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  412. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  413. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  414. $r = curl_exec($ch);
  415. if ($r === false) {
  416. // 获取错误号
  417. $errorNumber = curl_errno($ch);
  418. // 获取错误信息
  419. $errorMessage = curl_error($ch);
  420. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  421. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => $message]);
  422. return [false, $message];
  423. }
  424. curl_close($ch);
  425. Log::channel('apiLog')->info('朗峰PUT结果', ["message" => json_decode($r, true)]);
  426. return [true, json_decode($r, true)];
  427. }
  428. }