123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\index\controller;
- define('BASE_ROOT_PATH',str_replace('/application/index/controller','',dirname(__FILE__)));
- require_once(BASE_ROOT_PATH . '/extend/queue.php');
- require_once(BASE_ROOT_PATH . '/extend/queue.logic.php');
- use app\index\model\BoxActionModel;
- use app\index\model\CabinetModel;
- use app\index\model\BoxModel;
- use app\index\model\OrderModel;
- use think\Session;
- use QueueClient;
- use QueueServer;
- use Exception;
- class Cabinet extends Base
- {
- /**
- * 检查取件码
- */
- public function check_fcode(){
- $code = input('param.code');
- $OrderModel = new OrderModel();
- $result = $OrderModel->check_fcode($code);
- if($result['code'] != 1){
- return json(json_error_exception('1009'));
- }
- json_success($result['data']);
- }
- /**
- * 自提柜向服务器上报
- */
- public function record_store(){
- $order_sn = input('param.order_sn');
- $trunk = input('param.trunk');
- $cabinet = input('param.cabinet');
- $state = input('param.state');
- }
- /**
- * 自提柜向服务器上报用户取走商品。
- */
- public function record_fetch(){
- $trunk = input('param.trunk');
- $cabinet = input('param.cabinet');
- $state = input('param.state');
- $result = $this->change_status($cabinet , $trunk , $state);
- if($result['code'] != 1){
- return json(json_error_exception('1006'));
- }
- json_success('');
- }
- /**
- * 自提柜批量生产
- */
- public function add_cabinet(){
- $param = input('param.');
- $url = urldecode($param['url']);
- $alias = $param['alias'];
- $count = intval($param['count']);
- if($count < 0 || empty($url)) {
- return json(json_error_exception('1006','参数不正确'));
- }
- $CabinetModel = new CabinetModel();
- $flag = $CabinetModel->insertCabinet($url,$alias,$count);
- if($flag['code'] != 1){
- return json(json_error_exception('1006',$flag['msg']));
- }
- json_success([],'success');
- }
- /**
- * 开启箱门
- */
- public function open_box()
- {
- session('start','1');
- $cabinet_number = input('param.cabinet_number');
- $box_number = input('param.box_number');
- $sid = session_id();
- $params = [
- 'cabinet_number' => $cabinet_number,
- 'box_number' => $box_number,
- 'ret_queue' => $sid
- ];
- $queue_name = 'access_wait_open';
- $this->push_queue('access_wait_open','open_box',$params);
- $content = $this->wait_ret($sid,'lredis',6379);
- if($content['value'] == 'ok'){
- $this->box_action_record($cabinet_number , $box_number , 1);
- }
- }
- /**
- * 关闭箱门
- */
- public function close_box()
- {
- session('start','1');
- $cabinet_number = input('param.cabinet_number');
- $box_number = input('param.box_number');
- $sid = session_id();
- $params = [
- 'cabinet_number' => $cabinet_number,
- 'box_number' => $box_number,
- 'ret_queue' => $sid
- ];
- $this->push_queue('access_wait_close','close_box',$params);
- $content = $this->wait_ret($sid,'lredis',6379);
- if($content['value'] == 'ok'){
- $this->box_action_record($cabinet_number , $box_number , 2);
- }
- }
- private function testSend($queue_name,$value) {
- QueueClient::push($queue_name,'lredis',6379,'open_box_return',['value' => 'ok']);
- }
- /**
- * queue推送
- */
- private function push_queue($queue_name,$key,$params){
- QueueClient::push($queue_name,'lredis',6379,$key,$params);
- }
- /**
- * queue监听队列等待
- */
- private function wait_ret($queue_name,$host,$name)
- {
- $worker = new QueueServer($queue_name,$host,$name);
- $queues = $worker->scan();
- $empty_times = 0;
- while (true)
- {
- pcntl_signal_dispatch();
- try
- {
- $content = $worker->pop($queues, 0);
- return $content;
- }
- catch (Exception $e)
- {
- $err = $e->getMessage();
- $code = $e->getCode();
- break;
- }
- }
- }
- /**
- * 更新箱子时间
- */
- /**
- * 自提柜箱子修改状态
- */
- private function change_status($cabinet_number , $box_number , $status){
- $BoxModel = new BoxModel();
- $box = $BoxModel->getOneCabinetBox($cabinet_number,$box_number);
- if(empty($cabinet)){
- return json(json_error_exception(1008));
- }
- if($status == $cabinet['status']){
- return msg(1, '', 'success');
- }
- $where['cabinet_number'] = $cabinet_number;
- $where['box_number'] = $box_number;
- $update['box_status'] = $status;
- return $BoxModel->editData($where,$update);
- }
- public function test(){
- $cabinet_number = input('param.cabinet_number');
- $box_number = input('param.box_number');
- $order_sn = input('param.order_sn');
- $result = $this->box_bind_order($cabinet_number , $box_number , $order_sn);
- }
- /**
- * 自提柜箱子绑定订单号
- */
- private function box_bind_order($cabinet_number , $box_number , $order_sn){
- $where['cabinet_number'] = $cabinet_number;
- $where['box_number'] = $box_number;
- $update['order_sn'] = $order_sn;
- $update['box_status'] = 2;
- $BoxModel = new BoxModel();
- return $BoxModel->editData($where,$update);
- }
- /**
- * 记录箱子开启/关闭
- */
- private function box_action_record($cabinet_number , $box_number , $type){
- $BoxActionModel = new BoxActionModel();
- $params['cabinet_number'] = $cabinet_number;
- $params['box_number'] = $box_number;
- $params['type'] = $type;
- $params['datetime'] = date("Y-m-d H:i:s");
- $BoxActionModel->save($params);
- }
- }
|