DwyController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Model\BigKingTbj;
  4. use App\Service\EmployeeService;
  5. use App\Service\SysMenuService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Redis;
  8. use Illuminate\Support\Str;
  9. class DwyController extends BaseController
  10. {
  11. public function advertisement()
  12. {
  13. return ['status' => 200, 'data' => [
  14. 'video' => ['https://clouddevice.qingyaokeji.com/1.mp4',
  15. 'https://clouddevice.qingyaokeji.com/2.mp4'],
  16. 'img' => [
  17. 'https://clouddevice.qingyaokeji.com/3.jpeg',
  18. 'https://clouddevice.qingyaokeji.com/3.jpeg',
  19. ],
  20. 'limit_time' => 10
  21. ]];
  22. }
  23. public function openCommand(Request $request)
  24. {
  25. $data = $request->all();
  26. file_put_contents('dwy1.txt', json_encode($data) . PHP_EOL, 8);
  27. $device_id = $data['device_code'];
  28. $box_code = $data['box_code'];
  29. $key = $data['type'];
  30. if ($key === 'UP') {
  31. $num = Redis::get($device_id . 'status_num');
  32. if (empty($num)) $num = 0;
  33. $num++;
  34. if ($num === 1) {
  35. Redis::set($device_id . 'status_num', $num);
  36. Redis::expire($device_id . 'status_num', 300);
  37. Redis::set($device_id . 'status', 201);
  38. Redis::expire($device_id . 'status', 300);
  39. Redis::set($device_id, json_encode([$box_code]));
  40. Redis::expire($device_id, 300);
  41. } else {
  42. $old = Redis::get($device_id);
  43. if (empty($old)) $old = [];
  44. else $old = json_decode($old, true);
  45. $data = array_merge($old, [$box_code]);
  46. Redis::set($device_id, json_encode($data));
  47. Redis::expire($device_id, 300);
  48. Redis::del($device_id . 'status');
  49. Redis::del($device_id . 'status_num');
  50. }
  51. } else {
  52. Redis::set($device_id, json_encode([$box_code]));
  53. Redis::expire($device_id, 300);
  54. }
  55. return ['status' => 200];
  56. }
  57. public function openDoor(Request $request)
  58. {
  59. $data = $request->all();
  60. file_put_contents('dwy1.txt', json_encode($data) . PHP_EOL, 8);
  61. $data = $data['device_code'];
  62. $code = Redis::get($data);
  63. Redis::del($data);
  64. // return ['data'=>['CK00001','CK00018'],'status'=>200];
  65. if (empty($code)) return ['data' => [], 'status' => 201];
  66. $status = Redis::get($data . 'status');
  67. if (empty($status)) $status = 200;
  68. return ['data' => json_decode($code, true), 'status' => $status];
  69. }
  70. public function getOpenCommand(Request $request)
  71. {
  72. $data = $request->all();
  73. file_put_contents('dwy1.txt', json_encode($data) . PHP_EOL, 8);
  74. $device_id = $data['device_code'];
  75. $box_code = $data['box_code'];
  76. $key = $device_id . '_' . $box_code;
  77. $return = 201;
  78. $status = Redis::get($key);
  79. if ($status) {
  80. $return = 200;
  81. Redis::del($key);
  82. }
  83. return ['status' => $return];
  84. }
  85. public function boxList(Request $request)
  86. {
  87. $param = $request->all();
  88. $token = $param['token'];
  89. $device_code = $param['device_code'];
  90. //商标仓列表
  91. $url = 'https://tm.dwycloud.com/jbl/api/module-data/device/device/diy/device_code';
  92. $header = [
  93. 'Content-Type:application/json',
  94. 'Authorization: ' . $token,
  95. ];
  96. $box_data = $this->post_helper($url, '{"device_code":"' . $device_code . '"}', $header);
  97. $box_data = json_decode($box_data, true);
  98. if (isset($box_data['status']) && $box_data['status'] === 'success') {
  99. //库存查询
  100. $url = 'https://tm.dwycloud.com/jbl/api/module-data/brand_in_stock/brand_in_stock/diy/box_inventory';
  101. $stock_data = $this->post_helper($url, '{"device_code":"' . $device_code . '"}', $header);
  102. $stock_data = json_decode($stock_data, true);
  103. if ($stock_data) {
  104. $list = [];
  105. $material_list = [];
  106. foreach ($box_data['data'] as $v) {
  107. $list[$v['box_code']] = [
  108. 'material_code' => $v['material_code'],
  109. 'material_code_show' => $v['material_code_show'],
  110. 'box_code' => $v['box_code'],
  111. 'box_code_show' => $v['box_code_show'],
  112. 'min_stock_qty' => $v['min_stock_qty'] ?? 0,
  113. 'safe_stock_qty' => $v['safe_stock_qty'] ?? 0,
  114. 'status' => 1, //0设备仓中没有对应库存,1有对应库存,2对应商标仓中还有数量不允许上标
  115. 'qty' => 0,
  116. ];
  117. $material_list[$v['material_code']][] = $v['box_code'];
  118. }
  119. $return = [];
  120. foreach ($stock_data as $v) {
  121. if (isset($material_list[$v['material_code']]) && $v['box_code'] == 'CK00001' && $v['qty'] > 0) {
  122. foreach ($material_list[$v['material_code']] as $vv) {
  123. $list[$vv]['status'] = 1;
  124. }
  125. }
  126. if (isset($list[$v['box_code']]) && $v['qty'] > 0) {
  127. $list[$v['box_code']]['qty'] = $v['qty'];
  128. }
  129. }
  130. foreach ($list as $v) {
  131. if ($v['qty'] > 0) {
  132. $v['status'] = 2;
  133. }
  134. $return[] = $v;
  135. }
  136. return ['status' => 'success', 'data' => $return];
  137. }
  138. }
  139. return $box_data;
  140. }
  141. public function setLb(Request $request)
  142. {
  143. $param = $request->all();
  144. file_put_contents('dwy.txt', json_encode($param) . PHP_EOL, 8);
  145. // $param = [
  146. // 'token'=>'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyNzM2OTUyNTA3NTI5NTQzNzMiLCJhdXRoIjoiUk9MRV9JTk5FUl9VU0VSLFJPTEVfQlJBTkRfQVJSRVNULFJPTEVfQURNSU4iLCJ0b2tlbklkIjoiOTUwIiwiZXhwIjoxNzA3OTcxMTcxfQ.2VQ9Wqmk4rvKAcTsQPw59llBbeXjalKDGIZA4kesnuOJ0POmPVOVlRLbTtUDxl0XiFdPDUrKzuEFl2DXSgQdJg',
  147. // 'list'=>[
  148. // [
  149. // 'title'=>'优选A7家具板ENF小标',
  150. // 'num'=>'20',
  151. // ],[
  152. // 'title'=>'植萃除醛圆标',
  153. // 'num'=>'10',
  154. // ],
  155. // ],
  156. // ];
  157. // dd(json_encode($param));
  158. $token = $param['token'];
  159. $list = $param['list'];
  160. $url = 'https://tm.dwycloud.com/jbl/api/module-data/brand_in_stock/brand_in_stock/diy/box_inventory';
  161. $header = [
  162. 'Content-Type:application/json',
  163. 'Authorization: ' . $token,
  164. ];
  165. $data = $this->post_helper($url, '{"device_code":"DV00001"}', $header);
  166. $data = json_decode($data, true);
  167. foreach ($data as $k => &$v) {
  168. if (!isset($v['product_list'])) $v['product_list'] = [];
  169. if (!isset($v['box_code_show']) || $v['box_code_show'] == '设备仓') {
  170. unset($data[$k]);
  171. continue;
  172. }
  173. $v['use_qty'] = $v['qty'];
  174. if ($v['qty'] <= 0) {
  175. unset($data[$k]);
  176. continue;
  177. }
  178. // $key_list = [];
  179. if (!isset($v['send_qty'])) $v['send_qty'] = 0;
  180. foreach ($list as $kk => $vv) {
  181. if ($vv['num'] == 0) continue;
  182. if ($v['material_code_show'] === $vv['title']) {
  183. $v['restore_qty'] = isset($vv['restock_qty']) && $vv['restock_qty'] > 0 ? $vv['restock_qty'] : 0;
  184. if (($v['qty'] - $v['send_qty']) > $vv['num']) {
  185. $list[$kk]['num'] -= $vv['num'];
  186. $v['send_qty'] += $vv['num'];
  187. } else {
  188. $list[$kk]['num'] -= ($v['qty'] - $v['send_qty']);
  189. $v['send_qty'] += ($v['qty'] - $v['send_qty']);
  190. }
  191. //把产品相关参数进行封装
  192. $key_arr = [
  193. 'product_code' => $vv['product_code'] ?? '',
  194. 'product_code_show' => $vv['product_code_show'] ?? '',
  195. 'order_item_id' => $vv['order_item_id'] ?? '',
  196. 'order_item_id_show' => $vv['order_item_id_show'] ?? '',
  197. 'send_qty' => $vv['num'],
  198. 'restock_qty' => $vv['restock_qty'] ?? 0,
  199. ];
  200. $v['product_list'][] = $key_arr;
  201. }
  202. }
  203. }
  204. sort($data);
  205. return ['status' => 200, 'data' => $data];
  206. }
  207. public function zjlb(Request $request)
  208. {
  209. $param = $request->all();
  210. file_put_contents('dwy1.txt', json_encode($param) . PHP_EOL, 8);
  211. // $param = [
  212. // 'token'=>'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyNzM2OTUyNTA3NTI5NTQzNzMiLCJhdXRoIjoiUk9MRV9JTk5FUl9VU0VSLFJPTEVfQlJBTkRfQVJSRVNULFJPTEVfQURNSU4iLCJ0b2tlbklkIjoiOTUwIiwiZXhwIjoxNzA3OTcxMTcxfQ.2VQ9Wqmk4rvKAcTsQPw59llBbeXjalKDGIZA4kesnuOJ0POmPVOVlRLbTtUDxl0XiFdPDUrKzuEFl2DXSgQdJg',
  213. // 'list'=>[
  214. // [
  215. // 'title'=>'优选A7家具板ENF小标',
  216. // 'num'=>'20',
  217. // ],[
  218. // 'title'=>'植萃除醛圆标',
  219. // 'num'=>'10',
  220. // ],
  221. // ],
  222. // ];
  223. // dd(json_encode($param));
  224. $url = 'https://tm.dwycloud.com/jbl/api/module-data/brand_in_stock/brand_in_stock/diy/box_inventory';
  225. $header = [
  226. 'Content-Type:application/json',
  227. 'Authorization: ' . $request->header('Authorization'),
  228. ];
  229. $data = $this->post_helper($url, json_encode($param), $header);
  230. $data = json_decode($data, true);
  231. foreach ($data as $k => $v) {
  232. if($v['box_code'] !== 'CK00001') unset($data[$k]);
  233. if($v['qty'] == 0) unset($data[$k]);
  234. }
  235. sort($data);
  236. return json_encode($data);
  237. }
  238. public function boxOut(Request $request)
  239. {
  240. $param = $request->all();
  241. //商标绑定
  242. $url = 'https://tm.dwycloud.com/jbl/api/module-data/brand_in_stock/brand_in_stock/diy/lead_bind';
  243. $header = [
  244. 'Content-Type:application/json',
  245. 'Authorization: ' . $request->header('Authorization'),
  246. ];
  247. $lead_bind = $param['lead_bind'];
  248. $this->post_helper($url, json_encode($lead_bind), $header);
  249. //商标出库
  250. $url = 'https://tm.dwycloud.com/jbl/api/module-data/brand_in_stock/brand_in_stock/diy/lead_bind_out_stock';
  251. $lead_bind = $param['lead_out'];
  252. $this->post_helper($url, json_encode($lead_bind), $header);
  253. return ['status'=>200,'success'];
  254. }
  255. public function post_helper($url, $data, $header)
  256. {
  257. $ch = curl_init();
  258. curl_setopt($ch, CURLOPT_POST, 1);
  259. curl_setopt($ch, CURLOPT_URL, $url);
  260. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  261. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  262. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  263. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  264. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  265. $r = curl_exec($ch);
  266. curl_close($ch);
  267. return $r;
  268. }
  269. public function setTbData(Request $request){
  270. $data = $request->all();
  271. if(empty($data['id']) && empty($data['data'])) return [201, ''];
  272. BigKingTbj::updateOrCreate(
  273. ['id' => $data['id']], //查询条件
  274. [
  275. "id" => $data['id'],
  276. "data" => json_encode($data['data'])
  277. ] //添加或者修改的数据
  278. );
  279. return [200, ''];
  280. }
  281. public function getTbData(Request $request){
  282. $data = $request->all();
  283. if(empty($data['id'])) return [201, ''];
  284. $result = BigKingTbj::where('id',$data['id'])->first();
  285. if(empty($result)) return [200,'data' => []];
  286. $result = $result->toArray();
  287. return [200, 'data' => json_decode($result['data'],true)];
  288. }
  289. }