Cabinet.php 6.2 KB

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