StoreCouponUser.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\marketing\coupon;
  12. use app\controller\admin\AuthController;
  13. use app\jobs\BatchHandleJob;
  14. use app\services\activity\coupon\StoreCouponIssueServices;
  15. use app\services\activity\coupon\StoreCouponUserServices;
  16. use app\services\other\queue\QueueServices;
  17. use think\facade\App;
  18. /**
  19. * 优惠券发放记录控制器
  20. * Class StoreCouponUser
  21. * @package app\controller\admin\v1\narketing\coupon
  22. */
  23. class StoreCouponUser extends AuthController
  24. {
  25. public function __construct(App $app, StoreCouponUserServices $services)
  26. {
  27. parent::__construct($app);
  28. $this->services = $services;
  29. }
  30. /**
  31. * 发放列表
  32. * @return mixed
  33. */
  34. public function index()
  35. {
  36. $where = $this->request->getMore([
  37. ['status', ''],
  38. ['coupon_title', ''],
  39. ['nickname', ''],
  40. ]);
  41. $list = $this->services->systemPage($where);
  42. return $this->success($list);
  43. }
  44. /**
  45. * 发放优惠券到指定个人
  46. * @param $id
  47. * @param $uid
  48. * @return \think\response\Json
  49. */
  50. public function grant1()
  51. {
  52. $data = $this->request->postMore([
  53. ['id', 0],
  54. ['uid', '']
  55. ]);
  56. if (!$data['id']) return $this->fail('数据不存在!');
  57. /** @var StoreCouponIssueServices $issueService */
  58. $issueService = app()->make(StoreCouponIssueServices::class);
  59. $coupon = $issueService->get($data['id']);
  60. if (!$coupon) {
  61. return $this->fail('数据不存在!');
  62. } else {
  63. $coupon = $coupon->toArray();
  64. }
  65. $user = explode(',', $data['uid']);
  66. if (!$issueService->setCoupon($coupon, $user))
  67. return $this->fail('发放失败,请稍候再试!');
  68. else
  69. return $this->success('发放成功!');
  70. }
  71. /**
  72. * 批量发券
  73. * @return mixed
  74. */
  75. public function grant()
  76. {
  77. $data = $this->request->postMore([
  78. ['id', 0],
  79. ['uid', ''],
  80. ['all', 0],
  81. ['where', []],
  82. ]);
  83. if (!$data['id']) return $this->fail('数据不存在!');
  84. if (!$data['uid'] && $data['all'] == 0) return $this->fail('缺少参数');
  85. /** @var StoreCouponIssueServices $issueService */
  86. $issueService = app()->make(StoreCouponIssueServices::class);
  87. $coupon = $issueService->get($data['id']);
  88. if (!$coupon) {
  89. return $this->fail('数据不存在!');
  90. } else {
  91. $coupon = $coupon->toArray();
  92. }
  93. $type = 1;//代表发放优惠券
  94. if ($data['all'] == 0) $user = explode(',', $data['uid']);
  95. if ($data['all'] == 1) $user = [];
  96. /** @var QueueServices $queueService */
  97. $queueService = app()->make(QueueServices::class);
  98. $queueService->setQueueData($data['where'], 'uid', $user, $type);
  99. //加入队列
  100. BatchHandleJob::dispatch([$coupon, $type]);
  101. return $this->success('后台程序已执行发放优惠券任务!');
  102. }
  103. }