Modules_Api_Controllers_InnerController.proxy.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Modules\Api\Controllers;
  3. use App\Jobs\Inner\ReceiveVideosJob;
  4. use App\Model\Mongo\HotelLogModel;
  5. use Hyperf\HttpServer\Request;
  6. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  7. use Psr\SimpleCache\CacheInterface;
  8. class InnerController extends BaseController
  9. {
  10. use \Hyperf\Di\Aop\ProxyTrait;
  11. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  12. function __construct()
  13. {
  14. if (method_exists(parent::class, '__construct')) {
  15. parent::__construct(...func_get_args());
  16. }
  17. $this->__handlePropertyHandler(__CLASS__);
  18. }
  19. /**
  20. * 内部通信token
  21. * @var string
  22. */
  23. private $innerToken = 'VC4Pfzrg9nKpq';
  24. public function receiveVideo(Request $request, ValidatorFactoryInterface $validatorFactory)
  25. {
  26. if (!$this->checkToken()) {
  27. return json_fail('通信失败.');
  28. }
  29. $inputData = $request->all();
  30. $rules = ['source_type' => 'required', 'data' => 'required|array'];
  31. $validator = $validatorFactory->make($inputData, $rules);
  32. if ($validator->fails()) {
  33. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  34. return json_fail($validator->errors()->first());
  35. }
  36. if ($inputData['source_type'] != 'video') {
  37. return json_fail('通信数据类型有误');
  38. }
  39. if (!empty($inputData['data'])) {
  40. // 异步调度.
  41. dispatch_job(new ReceiveVideosJob($inputData['data']));
  42. }
  43. $res = ['result' => 'success'];
  44. return $this->jsonSuccess($res);
  45. }
  46. /**
  47. * 检查token合法性.
  48. * @return bool
  49. */
  50. private function checkToken()
  51. {
  52. $token = request()->header('AUTH-TOKEN', '');
  53. if (empty($token)) {
  54. $token = request()->input('token', '');
  55. }
  56. return $token === $this->innerToken;
  57. }
  58. /**
  59. * 搜索开房记录
  60. * @param Request $request
  61. * @param ValidatorFactoryInterface $validatorFactory
  62. * @return mixed
  63. */
  64. public function searchHotelRecord(Request $request, ValidatorFactoryInterface $validatorFactory)
  65. {
  66. if (!$this->checkToken()) {
  67. return json_fail('通信失败.');
  68. }
  69. $inputData = $request->all();
  70. $rules = ['card_no' => 'required|string|max:18'];
  71. $validator = $validatorFactory->make($inputData, $rules);
  72. if ($validator->fails()) {
  73. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  74. return json_fail($validator->errors()->first());
  75. }
  76. $options = ['skip' => 0, 'limit' => 5, 'sort' => ['check_time' => -1]];
  77. $cache = make(CacheInterface::class);
  78. $hotelRecordData = $cache->remember('check_fid_list:' . $inputData['card_no'], 60 * 24 * 7, function () use($inputData, $options) {
  79. return make(HotelLogModel::class)->db()->find(['ct_type' => 'ID', 'ct_fid' => $inputData['card_no']], $options)->toArray();
  80. });
  81. $data = [];
  82. if ($hotelRecordData) {
  83. foreach ($hotelRecordData as $key => $item) {
  84. $check_time = 'XXXX-XX-XX';
  85. $ct_fid = 'XXXXXXXX';
  86. $birthday = 'XXXX-XX-XX';
  87. // 处理时间
  88. if (strlen($item['check_time']) > 2) {
  89. $check_time = substr($item['check_time'], 0, 4) . '-XX-XX';
  90. }
  91. // 处理出生日期
  92. if (strlen($item['birthday']) > 2) {
  93. $birthday = substr($item['birthday'], 0, 4) . '-XX-XX';
  94. }
  95. // 处理身身份证
  96. if (strlen($item['ct_fid']) > 2) {
  97. $ct_fid = substr($item['ct_fid'], 0, -4) . 'XXXX';
  98. }
  99. $data[] = [
  100. 'check_id' => $item['_id']->__toString(),
  101. 'username' => $item['username'],
  102. 'gender' => $item['gender'] === 1 ? '男' : ($item['gender'] === 2 ? '女' : '未知'),
  103. 'ct_fid' => $ct_fid,
  104. 'birthday' => $birthday,
  105. 'check_address' => 'XXXXXX',
  106. 'check_time' => $check_time,
  107. // 同住性别
  108. 'check_sex' => $item['gender'] === 2 ? '男' : '女',
  109. ];
  110. }
  111. }
  112. return $this->jsonSuccess($data);
  113. }
  114. /**
  115. * @param $data
  116. * @return mixed
  117. */
  118. private function jsonSuccess($data)
  119. {
  120. $result = ['code' => 200, 'message' => '', 'data' => $data, 'handshake' => ''];
  121. return response()->json($result);
  122. }
  123. }