| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- namespace addons\Coupon\backend\controllers;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- use addons\Coupon\common\logic\CouponSend;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use addons\Coupon\common\test\test;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use PHPUnit\Util\Exception;
- /**
- * 优惠卷发放给指定会员
- *
- * Class UserCouponController
- * @package addons\Coupon\backend\controllers
- */
- class UserSendController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['coupon_num', 'user_id_list'], 'required'],
- [['coupon_id'], 'required'],
- [['user_id_list'], 'safe'],
- [['all_send'], 'string'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if(!$params['user_id_list']&&$params['all_send']==2) throw new \Exception("请选择发送对象");
- if($params['all_send'] == 1){ //发送给全部会员则走队列
- $params['mall_id'] = $this->mall_id;
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $params, CouponSend::class, 'index');
- }else{
- $t = \Yii::$app->db->beginTransaction();
- try {
- if($params['user_id_list']){
- foreach ($params['user_id_list'] as $k=>$v){
- $params['user_id'] = $v;
- $params['mall_id'] = $this->mall_id;
- $params['num'] = $params['coupon_num'];//数量
- $params['extra'] = '操作后台发放';//额外信息
- $params['from'] = "1";
- $return = AddonsCouponUserRelate::setData($params);
- if($return!==true){
- $t->rollBack();
- return $this->error(AddonsCouponUserRelate::$static_error);
- }
- }
- }
- }catch (Exception $e) {
- $t->rollBack();
- return $this->error($e->getMessage());
- }
- $t->commit();
- return $this->success('操作成功', $return);
- }
- }else{
- $params = \Yii::$app->request->get();
- $list = AddonsCoupon::getOne([['id' => $params['id']]]);
- $res = \Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 搜索用户
- * @return \yii\web\Response
- */
- public function actionSearchUser()
- {
- if (\Yii::$app->request->isAjax) {
- $params = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($params, [
- [['keyword_name','keyword_id','keyword_mobile'], 'string','message' => '请输入发放对象搜索关键字'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if(!empty($params['keyword_name']))$where[] = ['like','nickname',$params['keyword_name']];
- if(!empty($params['keyword_id']))$where[] = ['=','id',$params['keyword_id']];
- if(!empty($params['keyword_mobile']))$where[] = ['=','mobile',$params['keyword_mobile']];
- $cond = ['where' => $where, 'limit' => 10,'order'=>"id desc"];
- $field = "nickname,id,avatar_url,mobile";
- $list = User::listPage($cond);
- return $this->success('操作成功', $list);
- }
- }
- }
|