DefaultController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace addons\Recommen\api\modules\v1\controllers;
  3. use addons\Recommen\common\enums\RecommenEnum;
  4. use addons\Recommen\common\models\Recommen;
  5. use addons\Recommen\common\models\RecommenLog;
  6. use addons\Recommen\common\models\RecommenReward;
  7. use common\components\export\CsvTool;
  8. use common\enums\DiyEnum;
  9. use common\enums\StatusEnum;
  10. use common\models\common\Attachment;
  11. use common\models\common\PosterCode;
  12. use common\models\user\User;
  13. use Yii;
  14. use api\controllers\OnAuthController;
  15. /**
  16. * 默认控制器
  17. *
  18. * Class DefaultController
  19. * @package addons\Recommen\api\modules\v1\controllers
  20. */
  21. class DefaultController extends OnAuthController
  22. {
  23. public $modelClass = '';
  24. /**
  25. * 不用进行登录验证的方法
  26. *
  27. * 例如: ['index', 'update', 'create', 'view', 'delete']
  28. * 默认全部需要验证
  29. *
  30. * @var array
  31. */
  32. protected $authOptional = ['index','detail','list','ranking'];
  33. /**
  34. * 活动列表
  35. * @Author: ltm
  36. * @DateTime: 2022/5/17 0021 11:29
  37. * @Copyright: copyright (c) 2021 广东七件事集团
  38. * @return mixed|string|void
  39. */
  40. public function actionList()
  41. {
  42. $retuest = Yii::$app->request;
  43. if ($retuest->isGet) {
  44. $params = Yii::$app->request->get();
  45. // 检查提交的参数
  46. $where = [];
  47. $time = time();
  48. $where[] = ['<', 'start_time', $time];
  49. $where[] = ['>', 'end_time', $time];
  50. $where[] = ['=', 'status', StatusEnum::ENABLED];
  51. $where[] = ['=', 'mall_id', $this->mall_id];
  52. $order = 'id desc';
  53. $params = array('where' => $where, 'order'=>$order, 'limit' => 5);
  54. $list = Recommen::listPage($params, false, true);
  55. if($list){
  56. foreach($list['list'] as $k=>$v){
  57. $list['list'][$k]['recommen_reward'] = json_decode($v['recommen_reward']);
  58. $list['list'][$k]['open_time'] = date("Y-m-d H:i",$v['start_time']).'-'.date("Y-m-d H:i",$v['end_time']);
  59. }
  60. }
  61. $this->success('获取成功', $list);
  62. }
  63. }
  64. /**
  65. * 活动列表
  66. * @Author: ltm
  67. * @DateTime: 2022/5/17 0021 11:29
  68. * @Copyright: copyright (c) 2021 广东七件事集团
  69. * @return mixed|string|void
  70. */
  71. public function actionRanking(){
  72. $retuest = Yii::$app->request;
  73. if ($retuest->isGet) {
  74. $get = Yii::$app->request->get();
  75. $where = [];
  76. $with = ['user'];
  77. //筛选出正在进行的活动
  78. $active = Recommen::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['>', 'start_time', time()])->andWhere(['<', 'end_time', time()])->asArray()->column();
  79. if($active) { $where[] = ['in', 'active_id', $active]; }
  80. $where[] = ['=', 'mall_id', $this->mall_id];
  81. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  82. $order = 'new_num desc';
  83. $params = array('where' => $where,'with' => $with,'group'=>'user_id','order'=>$order, 'limit' => $get['limit']);
  84. $params['select'] = 'user_id,max(new_num) as new_num';
  85. $list = RecommenLog::listPage($params, false, true);
  86. //获取奖励
  87. if($list['list'] && $get['page'] ==1){
  88. if($list['list']){
  89. $user_ids = array_column($list['list'],'user_id');
  90. $user_ids = array_unique($user_ids);
  91. $reward_data = ['where' => [['in','user_id',$user_ids]],'order'=>'id desc', 'limit' => 5];
  92. $reward = RecommenReward::lists($reward_data,true);
  93. $reward_data = [];
  94. if($reward){
  95. foreach($reward as $v){
  96. if($v['reward'] && $v['reward'] != "null" && $v['reward'] != null){
  97. $reward_data[] = implode(',',json_decode($v['reward'],true));
  98. }
  99. }
  100. }
  101. $list['reward'] = $reward_data;
  102. }
  103. }
  104. $this->success('获取成功', $list);
  105. }
  106. }
  107. /**
  108. * 活动详情
  109. * @Author: ltm
  110. * @DateTime: 2022/5/17 0021 11:29
  111. * @Copyright: copyright (c) 2021 广东七件事集团
  112. * @return mixed|string|void
  113. */
  114. public function actionDetail()
  115. {
  116. $retuest = Yii::$app->request;
  117. if ($retuest->isGet) {
  118. $params = Yii::$app->request->get();
  119. // 检查提交的参数
  120. $res = Yii::$app->services->validate->validate($params,[
  121. [['scene','id'], 'safe'],
  122. ]);
  123. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  124. // 检查提交的参数
  125. if($params['scene']){
  126. $where[] = ['=', 'mall_id', $this->mall_id];
  127. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  128. $where[] = ['=', 'code', $params['scene']];
  129. $qrode = PosterCode::getOne($where,true);
  130. if($qrode){
  131. $params['id'] = $qrode['item_id'];
  132. }
  133. }
  134. $where = [];
  135. $where[] = ['=', 'id', $params['id']];
  136. $where[] = ['=', 'status', StatusEnum::ENABLED];
  137. $where[] = ['=', 'mall_id', $this->mall_id];
  138. $detail = Recommen::getOne($where, true);
  139. if($detail){
  140. $detail['recommen_reward'] = json_decode($detail['recommen_reward']);
  141. $detail['be_recommen_reward'] = json_decode($detail['be_recommen_reward']);
  142. $detail['type_name'] = RecommenEnum::getRewardType()[$detail['type']];
  143. $detail['open_time'] = date("Y-m-d H:i",$detail['start_time']).'-'.date("Y-m-d H:i",$detail['end_time']);
  144. }
  145. $data['detail'] = $detail;
  146. $sign = $this->mall['mall_sign'];
  147. $data['qrcode'] = '/plugins/recommen/detail?active_id='.$params['id']."?sign={$sign}&pid=".$this->user_id."&share_type=1";
  148. $data['qrcode_id'] = RecommenEnum::SHARE_TYPE;
  149. $this->success('获取成功', $data);
  150. }
  151. }
  152. }