RedpackController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * 红包相关
  4. * @author solu
  5. */
  6. class RedpackController extends BaseController {
  7. protected $ajaxLoginActions = [
  8. 'grab',
  9. 'sendList',
  10. ];
  11. public function __construct() {
  12. parent::__construct(true);
  13. }
  14. /**
  15. * 抢红包
  16. * @author solu
  17. * @param $args
  18. * @return array
  19. */
  20. public function actionGrab($args) {
  21. $rules = [
  22. 'trx_id' => ['string', 'desc' => '红包交易id'],
  23. ];
  24. Param::checkParam2($rules, $args);
  25. $userId = User::getUserId();
  26. $trxId = $args['trx_id'];
  27. $objLock = new Lock('grab_redpack', 10);
  28. $objRedpack = new Redpack();
  29. $objLock->lock($userId);
  30. $data = [];
  31. try {
  32. $data = $objRedpack->grab($trxId, $userId);
  33. } catch (Exception $e) {
  34. $objLock->unlock($userId);
  35. Response::error($e->getCode(), $e->getMessage());
  36. }
  37. $objLock->unlock($userId);
  38. return $data;
  39. }
  40. /**
  41. * 红包详情
  42. * @author solu
  43. * @param $args
  44. * @return array
  45. */
  46. public function actionDetail($args) {
  47. $rules = [
  48. 'trx_id' => ['string', 'desc' => '红包交易id'],
  49. ];
  50. Param::checkParam2($rules, $args);
  51. return (new Redpack())->detail($args['trx_id']);
  52. }
  53. /**
  54. * 发送的红包
  55. * @author solu
  56. * @param $args
  57. * @return array
  58. */
  59. public function actionSendList($args) {
  60. $rules = [
  61. 'page' => ['int', 'nullable' => true, 'default' => 1, 'desc' => '页码'],
  62. ];
  63. Param::checkParam2($rules, $args);
  64. $pageSize = 20;
  65. $offset = max(0, $args['page'] - 1) * $pageSize;
  66. $userId = User::getUserId();
  67. list($list, $total_count, $total_quantity) = (new Redpack())->sendList($userId, $offset, $pageSize);
  68. $more = count($list) >= $pageSize;
  69. return compact('list', 'total_count', 'total_quantity', 'more');
  70. }
  71. /**
  72. * 收到的红包
  73. * @author solu
  74. * @param $args
  75. * @return array
  76. */
  77. public function actionReceiveList($args) {
  78. $rules = [
  79. 'page' => ['int', 'nullable' => true, 'default' => 1, 'desc' => '页码'],
  80. ];
  81. Param::checkParam2($rules, $args);
  82. $pageSize = 20;
  83. $offset = max(0, $args['page'] - 1) * $pageSize;
  84. $userId = User::getUserId();
  85. list($list, $total_count, $total_quantity, $best_count) = (new Redpack())->receiveList($userId, $offset, $pageSize);
  86. $more = count($list) >= $pageSize;
  87. return compact('list', 'total_count', 'total_quantity', 'best_count', 'more');
  88. }
  89. }