Cabinet.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 think\Session;
  11. use QueueClient;
  12. use QueueServer;
  13. use Exception;
  14. class Cabinet extends Base
  15. {
  16. /**
  17. * 检查取件码
  18. */
  19. public function check_fcode(){
  20. $code = input('param.code');
  21. $OrderModel = new OrderModel();
  22. $result = $OrderModel->check_fcode($code);
  23. if($result['code'] != 1){
  24. return json(json_error_exception('1009'));
  25. }
  26. json_success($result['data']);
  27. }
  28. /**
  29. * 自提柜向服务器上报
  30. */
  31. public function record_store(){
  32. $order_sn = input('param.order_sn');
  33. $trunk = input('param.trunk');
  34. $cabinet = input('param.cabinet');
  35. $state = input('param.state');
  36. }
  37. /**
  38. * 自提柜向服务器上报用户取走商品。
  39. */
  40. public function record_fetch(){
  41. $trunk = input('param.trunk');
  42. $cabinet = input('param.cabinet');
  43. $state = input('param.state');
  44. $result = $this->change_status($cabinet , $trunk , $state);
  45. if($result['code'] != 1){
  46. return json(json_error_exception('1006'));
  47. }
  48. json_success('');
  49. }
  50. /**
  51. * 自提柜批量生产
  52. */
  53. public function add_cabinet(){
  54. $param = input('param.');
  55. $url = urldecode($param['url']);
  56. $alias = $param['alias'];
  57. $count = intval($param['count']);
  58. if($count < 0 || empty($url)) {
  59. return json(json_error_exception('1006','参数不正确'));
  60. }
  61. $CabinetModel = new CabinetModel();
  62. $flag = $CabinetModel->insertCabinet($url,$alias,$count);
  63. if($flag['code'] != 1){
  64. return json(json_error_exception('1006',$flag['msg']));
  65. }
  66. json_success([],'success');
  67. }
  68. /**
  69. * 开启箱门
  70. */
  71. public function OpenBox()
  72. {
  73. session('start','1');
  74. $cabinet_number = input('param.cabinet_number');
  75. $box_number = input('param.box_number');
  76. $sid = session_id();
  77. $params = [
  78. 'cabinet_number' => $cabinet_number,
  79. 'box_number' => $box_number,
  80. 'ret_queue' => $sid
  81. ];
  82. $queue_name = 'access_wait_open';
  83. $this->push_queue('access_wait_open','OpenBox',$params);
  84. $content = $this->wait_ret($sid,'lredis',6379);
  85. if($content['value'] == 'ok'){
  86. $this->box_action_record($cabinet_number , $box_number , 1);
  87. }
  88. }
  89. /**
  90. * 关闭箱门
  91. */
  92. public function LockBox()
  93. {
  94. session('start','1');
  95. $cabinet_number = input('param.cabinet_number');
  96. $box_number = input('param.box_number');
  97. $sid = session_id();
  98. $params = [
  99. 'cabinet_number' => $cabinet_number,
  100. 'box_number' => $box_number,
  101. 'ret_queue' => $sid
  102. ];
  103. $this->push_queue('access_wait_close','close_box',$params);
  104. $content = $this->wait_ret($sid,'lredis',6379);
  105. if($content['value'] == 'ok'){
  106. $this->box_action_record($cabinet_number , $box_number , 2);
  107. }
  108. }
  109. private function testSend($queue_name,$value) {
  110. QueueClient::push($queue_name,'lredis',6379,'open_box_return',['value' => 'ok']);
  111. }
  112. /**
  113. * queue推送
  114. */
  115. private function push_queue($queue_name,$key,$params){
  116. QueueClient::push($queue_name,'lredis',6379,$key,$params);
  117. }
  118. /**
  119. * queue监听队列等待
  120. */
  121. private function wait_ret($queue_name,$host,$name)
  122. {
  123. $worker = new QueueServer($queue_name,$host,$name);
  124. $queues = $worker->scan();
  125. $empty_times = 0;
  126. while (true)
  127. {
  128. pcntl_signal_dispatch();
  129. try
  130. {
  131. $content = $worker->pop($queues, 0);
  132. return $content;
  133. }
  134. catch (Exception $e)
  135. {
  136. $err = $e->getMessage();
  137. $code = $e->getCode();
  138. break;
  139. }
  140. }
  141. }
  142. /**
  143. * 更新箱子时间
  144. */
  145. /**
  146. * 自提柜箱子修改状态
  147. */
  148. private function change_status($cabinet_number , $box_number , $status){
  149. $BoxModel = new BoxModel();
  150. $box = $BoxModel->getOneCabinetBox($cabinet_number,$box_number);
  151. if(empty($cabinet)){
  152. return json(json_error_exception(1008));
  153. }
  154. if($status == $cabinet['status']){
  155. return msg(1, '', 'success');
  156. }
  157. $where['cabinet_number'] = $cabinet_number;
  158. $where['box_number'] = $box_number;
  159. $update['box_status'] = $status;
  160. return $BoxModel->editData($where,$update);
  161. }
  162. public function test(){
  163. pre(1);
  164. $cabinet_number = input('param.cabinet_number');
  165. $box_number = input('param.box_number');
  166. $order_sn = input('param.order_sn');
  167. }
  168. /**
  169. * 自提柜箱子绑定订单号
  170. */
  171. private function box_bind_order($cabinet_number , $box_number , $order_sn){
  172. $where['cabinet_number'] = $cabinet_number;
  173. $where['box_number'] = $box_number;
  174. $update['order_sn'] = $order_sn;
  175. $update['box_status'] = 2;
  176. $BoxModel = new BoxModel();
  177. return $BoxModel->editData($where,$update);
  178. }
  179. /**
  180. * 记录箱子开启/关闭
  181. */
  182. private function box_action_record($cabinet_number , $box_number , $type){
  183. $BoxActionModel = new BoxActionModel();
  184. $params['cabinet_number'] = $cabinet_number;
  185. $params['box_number'] = $box_number;
  186. $params['type'] = $type;
  187. $params['datetime'] = date("Y-m-d H:i:s");
  188. $BoxActionModel->save($params);
  189. }
  190. }