TestService.php 14 KB

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