Cabinet.php 7.0 KB

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