Cabinet.php 6.6 KB

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