UserSendController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace addons\Coupon\backend\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\logic\CouponSend;
  5. use addons\Coupon\common\models\AddonsCoupon;
  6. use addons\Coupon\common\models\AddonsCouponUserRelate;
  7. use addons\Coupon\common\test\test;
  8. use common\enums\RabbitMqEnum;
  9. use common\enums\StatusEnum;
  10. use common\models\user\User;
  11. use PHPUnit\Util\Exception;
  12. /**
  13. * 优惠卷发放给指定会员
  14. *
  15. * Class UserCouponController
  16. * @package addons\Coupon\backend\controllers
  17. */
  18. class UserSendController extends BaseController
  19. {
  20. public $enableCsrfValidation = false;
  21. /**
  22. * 首页
  23. *
  24. * @return string
  25. */
  26. public function actionIndex()
  27. {
  28. if (\Yii::$app->request->isAjax) {
  29. if (\Yii::$app->request->isPost) {
  30. $params = \Yii::$app->request->post();
  31. $res = \Yii::$app->services->validate->validate($params, [
  32. [['coupon_num', 'user_id_list'], 'required'],
  33. [['coupon_id'], 'required'],
  34. [['user_id_list'], 'safe'],
  35. [['all_send'], 'string'],
  36. ]);
  37. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  38. if(!$params['user_id_list']&&$params['all_send']==2) throw new \Exception("请选择发送对象");
  39. if($params['all_send'] == 1){ //发送给全部会员则走队列
  40. $params['mall_id'] = $this->mall_id;
  41. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $params, CouponSend::class, 'index');
  42. }else{
  43. $t = \Yii::$app->db->beginTransaction();
  44. try {
  45. if($params['user_id_list']){
  46. foreach ($params['user_id_list'] as $k=>$v){
  47. $params['user_id'] = $v;
  48. $params['mall_id'] = $this->mall_id;
  49. $params['num'] = $params['coupon_num'];//数量
  50. $params['extra'] = '操作后台发放';//额外信息
  51. $params['from'] = "1";
  52. $return = AddonsCouponUserRelate::setData($params);
  53. if($return!==true){
  54. $t->rollBack();
  55. return $this->error(AddonsCouponUserRelate::$static_error);
  56. }
  57. }
  58. }
  59. }catch (Exception $e) {
  60. $t->rollBack();
  61. return $this->error($e->getMessage());
  62. }
  63. $t->commit();
  64. return $this->success('操作成功', $return);
  65. }
  66. }else{
  67. $params = \Yii::$app->request->get();
  68. $list = AddonsCoupon::getOne([['id' => $params['id']]]);
  69. $res = \Yii::$app->services->validate->validate($params, [
  70. [['id'], 'required'],
  71. ]);
  72. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  73. return $this->success('操作成功', $list);
  74. }
  75. }
  76. return $this->render($this->action->id);
  77. }
  78. /**
  79. * 搜索用户
  80. * @return \yii\web\Response
  81. */
  82. public function actionSearchUser()
  83. {
  84. if (\Yii::$app->request->isAjax) {
  85. $params = \Yii::$app->request->get();
  86. $res = \Yii::$app->services->validate->validate($params, [
  87. [['keyword_name','keyword_id','keyword_mobile'], 'string','message' => '请输入发放对象搜索关键字'],
  88. ]);
  89. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  90. if(!empty($params['keyword_name']))$where[] = ['like','nickname',$params['keyword_name']];
  91. if(!empty($params['keyword_id']))$where[] = ['=','id',$params['keyword_id']];
  92. if(!empty($params['keyword_mobile']))$where[] = ['=','mobile',$params['keyword_mobile']];
  93. $cond = ['where' => $where, 'limit' => 10,'order'=>"id desc"];
  94. $field = "nickname,id,avatar_url,mobile";
  95. $list = User::listPage($cond);
  96. return $this->success('操作成功', $list);
  97. }
  98. }
  99. }