Cabinet.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace app\index\controller;
  3. define('BASE_ROOT_PATH',str_replace('/application/index/controller','',dirname(__FILE__)));
  4. require_once(BASE_ROOT_PATH . '/extend/queue.php');
  5. require_once(BASE_ROOT_PATH . '/extend/queue.logic.php');
  6. use app\index\model\BoxActionModel;
  7. use app\index\model\CabinetModel;
  8. use app\index\model\BoxModel;
  9. use app\index\model\OrderModel;
  10. use app\index\model\UserModel;
  11. use think\App;
  12. use think\facade\Log;
  13. use think\Session;
  14. use QueueClient;
  15. use QueueServer;
  16. use Exception;
  17. class Cabinet extends Base
  18. {
  19. public function __construct(App $app = null)
  20. {
  21. parent::__construct($app);
  22. session('start','1');
  23. }
  24. /**
  25. * 检查取件码
  26. */
  27. public function CheckFcode(){
  28. $code = input('param.code');
  29. $OrderModel = new OrderModel();
  30. $result = $OrderModel->check_fcode($code);
  31. if($result['code'] != 1){
  32. return json(json_error_exception('1009'));
  33. }
  34. json_success($result['data']);
  35. }
  36. /**
  37. * 自提柜向服务器上报用户取走商品。
  38. */
  39. public function record_fetch(){
  40. $trunk = input('param.trunk');
  41. $cabinet = input('param.cabinet');
  42. $state = input('param.state');
  43. $result = $this->change_status($cabinet , $trunk , $state);
  44. if($result['code'] != 1){
  45. json_error(1006);
  46. }
  47. json_success('');
  48. }
  49. /**
  50. * 自提柜批量生产
  51. */
  52. public function AddCabinet(){
  53. $param = input('param.');
  54. $url = urldecode($param['url']);
  55. $alias = $param['alias'];
  56. $count = intval($param['count']);
  57. if($count < 0 || empty($url)) {
  58. return json(json_error_exception('1006','参数不正确'));
  59. }
  60. $CabinetModel = new CabinetModel();
  61. $flag = $CabinetModel->insertCabinet($url,$alias,$count);
  62. if($flag['code'] != 1){
  63. return json(json_error_exception('1006',$flag['msg']));
  64. }
  65. json_success([]);
  66. }
  67. public function OpenBox()
  68. {
  69. $params['id'] = $this->getid();
  70. $cabinet_code = input('param.cabinet_number');
  71. $box_number = input('param.box_number');
  72. $box_position_list[] = $box_number;
  73. $params['box_position_list'] = $box_position_list;
  74. $params['delay_time'] = 0;
  75. $cabinet = $this->get_cabinet($cabinet_code);
  76. if($cabinet == false){
  77. return json(json_error_exception('1008'));
  78. }
  79. $params['ret_queue'] = 'RET' . session_id();
  80. $params['req_url'] = $cabinet['req_url'];
  81. $params['key'] = $cabinet['login_key'];
  82. $params['cabinet_code'] = $cabinet['number'];
  83. $params['method'] = 'OpenBoxR';
  84. $result = $this->proc_request(self::queue_name,'OpenBox',$params);
  85. if($result == false) {
  86. json_error(2000);
  87. } else {
  88. json_success($result);
  89. }
  90. }
  91. public function LockCabinet()
  92. {
  93. $params['id'] = $this->getid();
  94. $cabinet_code = input('param.cabinet_number');
  95. $cabinet = $this->get_cabinet($cabinet_code);
  96. $params['delay_time'] = 0;
  97. if($cabinet == false){
  98. return json(json_error_exception('1008'));
  99. }
  100. $params['ret_queue'] = 'RET' . session_id();
  101. $params['req_url'] = $cabinet['req_url'];
  102. $params['key'] = $cabinet['login_key'];
  103. $params['cabinet_code'] = $cabinet['number'];
  104. $params['method'] = 'LockCabinetR';
  105. $result = $this->proc_request(self::queue_name,'LockCabinet',$params);
  106. if($result == false) {
  107. json_error(2000);
  108. } else {
  109. json_success($result);
  110. }
  111. }
  112. public function UnLockCabinet()
  113. {
  114. $params['id'] = $this->getid();
  115. $cabinet_code = input('param.cabinet_number');
  116. $cabinet = $this->get_cabinet($cabinet_code);
  117. $params['delay_time'] = 0;
  118. if($cabinet == false){
  119. return json(json_error_exception('1008'));
  120. }
  121. $params['ret_queue'] = 'RET' . session_id();
  122. $params['req_url'] = $cabinet['req_url'];
  123. $params['key'] = $cabinet['login_key'];
  124. $params['cabinet_code'] = $cabinet['number'];
  125. $params['method'] = 'UnLockCabinetR';
  126. $result = $this->proc_request(self::queue_name,'UnLockCabinet',$params);
  127. if($result == false) {
  128. json_error(2000);
  129. } else {
  130. json_success($result);
  131. }
  132. }
  133. public function ReleaseAlarm()
  134. {
  135. $params['id'] = $this->getid();
  136. $cabinet_code = input('param.cabinet_number');
  137. $params['delay_time'] = 0;
  138. $cabinet = $this->get_cabinet($cabinet_code);
  139. if($cabinet == false){
  140. return json(json_error_exception('1008'));
  141. }
  142. $params['ret_queue'] = 'RET' . session_id();
  143. $params['req_url'] = $cabinet['req_url'];
  144. $params['key'] = $cabinet['login_key'];
  145. $params['cabinet_code'] = $cabinet['number'];
  146. $params['method'] = 'ReleaseAlarmR';
  147. $result = $this->proc_request(self::queue_name,'ReleaseAlarm',$params);
  148. if($result == false) {
  149. json_error(2000);
  150. } else {
  151. json_success($result);
  152. }
  153. }
  154. public function Reboot()
  155. {
  156. $params['id'] = $this->getid();
  157. $cabinet_code = input('param.cabinet_number');
  158. $params['delay_time'] = 0;
  159. $cabinet = $this->get_cabinet($cabinet_code);
  160. if($cabinet == false){
  161. return json(json_error_exception('1008'));
  162. }
  163. $params['ret_queue'] = 'RET' . session_id();
  164. $params['req_url'] = $cabinet['req_url'];
  165. $params['key'] = $cabinet['login_key'];
  166. $params['cabinet_code'] = $cabinet['number'];
  167. $params['method'] = 'RebootR';
  168. $result = $this->proc_request(self::queue_name,'Reboot',$params);
  169. if($result == false) {
  170. json_error(2000);
  171. } else {
  172. json_success($result);
  173. }
  174. }
  175. public function QueryState()
  176. {
  177. $params['id'] = $this->getid();
  178. $cabinet_code = input('param.cabinet_number');
  179. $params['delay_time'] = 0;
  180. $cabinet = $this->get_cabinet($cabinet_code);
  181. if($cabinet == false){
  182. return json(json_error_exception('1008'));
  183. }
  184. $params['ret_queue'] = 'RET' . session_id();
  185. $params['req_url'] = $cabinet['req_url'];
  186. $params['key'] = $cabinet['login_key'];
  187. $params['cabinet_code'] = $cabinet['number'];
  188. $params['method'] = 'RebootR';
  189. $result = $this->proc_request(self::queue_name,'QueryState',$params);
  190. if($result == false) {
  191. json_error(2000);
  192. } else {
  193. json_success($result);
  194. }
  195. }
  196. public function CabinetList(){
  197. $param = input('param.');
  198. $CabinetModel = new CabinetModel();
  199. $selectResult = $CabinetModel->getCabinetsByWhere();
  200. $cabinet_status = ['1' => '运行中' , '2' => '已锁定'];
  201. // 拼装参数
  202. foreach($selectResult as $key=>$vo){
  203. $selectResult[$key]['status_text'] = $cabinet_status[$vo['cabinet_status']];
  204. }
  205. $return['total'] = $CabinetModel->getAllCabinets(); //总数量
  206. $return['rows'] = $selectResult;
  207. json_success($return);
  208. }
  209. public function BoxList(){
  210. $cabinet_number = input('cabinet_number');
  211. if(empty($cabinet_number)){
  212. json_error(1007 , '柜子号码不能为空');
  213. }
  214. $BoxModel = new BoxModel();
  215. $where['sc_box.cabinet_number'] = $cabinet_number;
  216. $selectResult = $BoxModel->getBoxsByWhere($where);
  217. $box_status = ['1' => '空闲' , '2' => '已使用' , '3' => '锁定中'];
  218. // 拼装参数
  219. foreach($selectResult as $key=>$vo){
  220. $selectResult[$key]['status_text'] = $box_status[$vo['box_status']];
  221. }
  222. $return['total'] = $BoxModel->getAllCounts(); //总数量
  223. $return['rows'] = $selectResult;
  224. json_success($return);
  225. }
  226. public function BoxActionList(){
  227. $param = input('param.');
  228. if(empty($param['cabinet_number']) || empty($param['box_number'])){
  229. json_error(1007 , '柜子号码或箱子号码不能为空');
  230. }
  231. $where['cabinet_number'] = $param['cabinet_number'];
  232. $where['box_number'] = $param['box_number'];
  233. $limit = isset($param['pageSize']) ? $param['pageSize'] : 10;
  234. $param['pageNumber'] = isset($param['pageNumber']) ? $param['pageNumber'] : 1;
  235. $offset = ($param['pageNumber'] - 1) * $limit;
  236. $BoxActionModel = new BoxActionModel();
  237. $selectResult = $BoxActionModel->getBoxActionsByWhere($where , $offset, $limit);
  238. $status = ['1' => '开箱' , '2' => '关箱'];
  239. // 拼装参数
  240. foreach($selectResult as $key=>$vo){
  241. $selectResult[$key]['status_text'] = $status[$vo['type']];
  242. }
  243. $return['total'] = $BoxActionModel->getAllCounts($where); //总数量
  244. $return['rows'] = $selectResult;
  245. json_success($return);
  246. }
  247. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  248. private function proc_request($queue_name, $key, $params){
  249. QueueClient::push($queue_name,self::redis_host , self::redis_port ,$key,$params);
  250. [$method,$args] = $this->wait_result($params['ret_queue']);
  251. Log::record($args);
  252. return $args;
  253. }
  254. private function getid(){
  255. return md5(rand(1000000,9999999));
  256. }
  257. private function wait_result($queue_name)
  258. {
  259. $times = 10;
  260. while ($times--)
  261. {
  262. try
  263. {
  264. $worker = new QueueServer($queue_name,self::redis_host , self::redis_port);
  265. $queues = $worker->scan();
  266. $content = $worker->pop($queues, 1);
  267. if(is_array($content)) {
  268. $method = key($content);
  269. $arg = current($content);
  270. return [$method,$arg];
  271. }
  272. }
  273. catch (Exception $e)
  274. {
  275. $err = $e->getMessage();
  276. $code = $e->getCode();
  277. }
  278. }
  279. return [false,false];
  280. }
  281. /**
  282. * 自提柜箱子修改状态
  283. */
  284. private function change_status($cabinet_number , $box_number , $status){
  285. $BoxModel = new BoxModel();
  286. $box = $BoxModel->getOneCabinetBox($cabinet_number,$box_number);
  287. if(empty($cabinet)){
  288. json_error(1008);
  289. }
  290. if($status == $cabinet['status']){
  291. return msg(1, '', 'success');
  292. }
  293. $where['cabinet_number'] = $cabinet_number;
  294. $where['box_number'] = $box_number;
  295. $update['box_status'] = $status;
  296. return $BoxModel->editData($where,$update);
  297. }
  298. public function test(){
  299. pre(1);
  300. }
  301. /**
  302. * 自提柜箱子绑定订单号
  303. */
  304. private function box_bind_order($cabinet_number , $box_number , $order_sn){
  305. $where['cabinet_number'] = $cabinet_number;
  306. $where['box_number'] = $box_number;
  307. $update['order_sn'] = $order_sn;
  308. $update['box_status'] = 2;
  309. $BoxModel = new BoxModel();
  310. return $BoxModel->editData($where,$update);
  311. }
  312. /**
  313. * 记录箱子开启/关闭
  314. */
  315. private function box_action_record($cabinet_number , $box_number , $type){
  316. $BoxActionModel = new BoxActionModel();
  317. $params['cabinet_number'] = $cabinet_number;
  318. $params['box_number'] = $box_number;
  319. $params['type'] = $type;
  320. $params['datetime'] = date("Y-m-d H:i:s");
  321. $BoxActionModel->save($params);
  322. }
  323. /**
  324. * 获取柜子信息
  325. */
  326. private function get_cabinet($cabinet_number){
  327. $cabinet = CabinetModel::find($cabinet_number);
  328. if(empty($cabinet)){
  329. return false;
  330. }
  331. $cabinet = $cabinet->toArray();
  332. return $cabinet;
  333. }
  334. }