| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace backend\controllers;
- use biz\merchant\services\AdminService;
- use biz\tool\models\GuaGua;
- use biz\tool\models\GuaGuaAttend;
- use biz\tool\models\GuaGuaWin;
- use biz\tool\services\GuaGuaAttendService;
- use biz\tool\services\GuaGuaPrizeService;
- use biz\tool\services\GuaGuaService;
- use biz\tool\services\GuaGuaWinService;
- use biz\user\services\UserService;
- use biz\weixin\services\WxSceneService;
- use Yii;
- use yii\data\Pagination;
- use common\components\util;
- use common\components\qrCodeUtil;
- use common\services\xhUserService;
- class GuaGuaController extends BackendController
- {
-
- public $tabList = [
- ['name' => '老带新优惠卷', 'action' => 'all', 'url' => '/coupon/all'],
- ['name' => '刮刮卡', 'action' => 'list', 'url' => '/gua-gua/list'],
- ];
-
- public function actionList()
- {
- $condition = ['merchantId' => $this->merchantId, 'delStatus' => 1];
- $query = GuaGua::find()->where($condition)->orderBy('createTime desc');
- $countQuery = clone $query;
- $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
- $activities = $query->offset($pages->offset)->orderBy('addTime desc')->limit($pages->limit)->asArray()->all();
- $now = time();
- foreach ($activities as $key => $activity) {
- if ($activity['startTime'] > $now) {
- $activities[$key]['status'] = '未开始';
- } else if ($activity['endTime'] < $now) {
- $activities[$key]['status'] = '已结束';
- } else {
- $activities[$key]['status'] = '进行中';
- }
- $visitNum = GuaGuaService::getCurrentVisitNum($activity);
- $visitUser = GuaGuaService::getCurrentVisitUserNum($activity);
- $activities[$key]['visitNum'] = $visitNum;
- $activities[$key]['visitUser'] = $visitUser;
- }
- return $this->render('list', ['activities' => $activities, 'pages' => $pages]);
- }
-
- public function actionAdd()
- {
- return $this->render('add');
- }
-
- public function actionSaveAdd()
- {
- $post = Yii::$app->request->post();
- $awardNames = $post['awardName'];
- $orders = $post['order'];
- $counts = $post['count'];
- $probabilities = $post['probability'];
- //表单校验
- if ($post['startTime'] > $post['endTime']) {
- util::failInfo('结束时间要大于开始时间');
- }
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- $date = date('Y-m-d H:m:i', time());
- $time = time();
- $post['merchantId'] = $this->merchantId;
- $post['startTime'] = strtotime($post['startTime']);
- $post['endTime'] = strtotime($post['endTime']);
- $post['awardIds'] = '';
- $post['actName'] = $post['activityName'];
- $post['createTime'] = $date;
- $post['addTime'] = $time;
-
- if (($post['endTime'] - $post['startTime']) > 86400 * 30) {
- util::failInfo('活动时长不能超过30天');
- }
-
- $result = GuaGuaService::add($post);
- $cardId = $result['id'];
- //奖品保存
- foreach ($awardNames as $key => $awardName) {
- $data = [
- 'prizeName' => $awardName,
- 'order' => $orders[$key],
- 'count' => $counts[$key],
- 'getChance' => $probabilities[$key],
- 'cardId' => $cardId,
- 'merchantId' => $this->merchantId,
- 'addTime' => $time,
- 'createTime' => $date
- ];
- GuaGuaPrizeService::add($data);
- }
- $transaction->commit();
- util::successInfo('添加成功', 'A0001');
- } catch (Exception $e) {
- $transaction->rollBack();
- util::failInfo('操作失败');
- }
- }
-
- public function actionEdit()
- {
- $id = Yii::$app->request->get('id');
- $guaGua = GuaGuaService::getById($id);
- $awards = GuaGuaPrizeService::getAllByCondition(['cardId' => $id], 'order desc', '*');
- return $this->render('edit', ['activity' => $guaGua, 'awards' => $awards]);
- }
-
- public function actionUpdate()
- {
- $post = Yii::$app->request->post();
- $cardId = $post['id'];
- //有用户已经参与不能再修改
- $hasAttend = GuaGuaAttendService::getByCondition(['cardId' => $cardId]);
- if (!empty($hasAttend)) {
- util::failInfo('有客户参与了抽奖活动,不能修改!');
- }
- $awardIds = $post['awardId'];
- $awardNames = $post['awardName'];
- $orders = $post['order'];
- $counts = $post['count'];
- $probabilities = $post['probability'];
-
- $post['startTime'] = strtotime($post['startTime']);
- $post['endTime'] = strtotime($post['endTime']);
- $post['awardIds'] = '';
- $post['createTime'] = date('Y-m-d H:m:i', time());
-
- GuaGuaService::updateById($cardId, $post);
-
- //奖品保存
- foreach ($awardNames as $key => $awardName) {
- $id = isset($awardIds[$key]) ? $awardIds[$key] : 0;
- if ($id) {
- $data = [
- 'prizeName' => $awardName,
- 'order' => $orders[$key],
- 'count' => $counts[$key],
- 'getChance' => $probabilities[$key],
- 'cardId' => $cardId,
- ];
- GuaGuaPrizeService::updateById($id, $data);
- } else {
- $data = [
- 'prizeName' => $awardName,
- 'order' => $orders[$key],
- 'count' => $counts[$key],
- 'getChance' => $probabilities[$key],
- 'cardId' => $cardId,
- ];
- GuaGuaPrizeService::add($data);
- }
- }
- util::successInfo('修改成功', 'A0001');
- }
-
- /**
- * 中奖列表
- * @return string
- */
- public function actionWinList()
- {
- $cardId = Yii::$app->request->get('id');
- $condition = ['cardId' => $cardId];
- $query = GuaGuaWin::find()->groupBy(['userId', 'prizeId'])->where($condition)->orderBy('getTime desc');
- $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 20]);
- $winnerList = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
- $userList = [];
- $prizeList = [];
- if (!empty($winnerList)) {
- $userIdList = array_column($winnerList, 'userId');
- $userIdList = array_unique(array_filter($userIdList));
- $userList = UserService::getAllByCondition(['id' => ['in', $userIdList]], null, 'id,userName,avatar,mobile', 'id');
- $prizeIdList = array_column($winnerList, 'prizeId');
- $prizeIdList = array_unique(array_filter($prizeIdList));
- $prizeList = GuaGuaPrizeService::getAllByCondition(['id' => ['in', $prizeIdList]], null, 'id,prizeName', 'id');
- }
- return $this->render('winList', ['winnerList' => $winnerList, 'pages' => $pages, 'userList' => $userList, 'prizeList' => $prizeList]);
- }
-
- /**
- * 所有参加抽奖活动的用户列表 shish 2019.8.11
- */
- public function actionJoinList()
- {
- $cardId = Yii::$app->request->get('id');
- $condition = ['cardId' => $cardId];
- $query = GuaGuaAttend::find()->where($condition);
- $totalCount = $query->count();
- $pages = new Pagination(['totalCount' => $totalCount, 'pageSize' => 20]);
- $participants = $query->offset($pages->offset)->limit($pages->limit)->orderBy('addTime desc')->asArray()->all();
- $newParticipants = [];
- foreach ($participants as $participant) {
- $uid = $participant['userId'];
- $user = xhUserService::getById($uid);
- $participant['userName'] = $user['userName'];
- $participant['mobile'] = $user['mobile'];
- $participant['avatar'] = $user['avatar'];
- $newParticipants[] = $participant;
- }
- return $this->render('joinList', ['participants' => $newParticipants, 'totalCount' => $totalCount, 'pages' => $pages]);
- }
-
- /**
- * 奖品详情 shish 2019.8.11
- */
- public function actionPrizeDetail()
- {
- $cardId = Yii::$app->request->get('id');
- $card = GuaGuaService::getById($cardId);
- if (empty($card) || $card['merchantId'] != $this->merchantId) {
- util::failInfo('没有活动或者没有权限操作');
- }
- $prize = GuaGuaPrizeService::getPrizeList($cardId);
- return $this->render('awardDetail', ['activityName' => $card['actName'], 'awards' => $prize]);
- }
-
- /**
- * 设置奖品已领取
- */
- public function actionSetPrizeGet()
- {
- $id = Yii::$app->request->post('id');
- $ret = GuaGuaWinService::setPrizeGet($id);
- if ($ret == false) {
- util::failInfo($this->error->getError());
- }
- util::successInfo();
- }
-
- /**
- * 生成活动链接的二维码地址 shish 2019.8.11
- */
- public function actionShowActQrcode()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $activity = GuaGuaService::getById($id);
- if ($activity['merchantId'] != $this->merchantId) {
- util::failInfo();
- }
- if (!empty($activity['urlQrCode'])) {
- util::successInfo('操作成功', 'A0001', ['url' => $activity['urlQrCode']]);
- }
- /**生成微信场景二维码**/
- $url = WxSceneService::generateGuaGuaScene($id);
- //$url = Yii::$app->params['frontUrl'] . '/gua/index?id=' . $id . '&account=' . $this->merchantId;
- $logo = Yii::getAlias("@app") . '/../images' . $this->merchant['logo'];//取商家的logo
- $imgUrl = qrCodeUtil::generate($url, 'backend_active_url_' . $this->merchantId . '_' . $id, $logo);
- GuaGuaService::updateById($id, ['urlQrCode' => $imgUrl]);
- util::successInfo('操作成功', 'A0001', ['url' => $imgUrl]);
- }
-
- //链接发送到手机上 shish 2019.8.11
- public function actionSendActiveUrl()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['activeId']) ? $get['activeId'] : 0;
- $activity = GuaGuaService::getById($id);
- if ($activity['merchantId'] != $this->merchantId) {
- util::failInfo();
- }
- $url = Yii::$app->params['frontUrl'] . '/gua/index?id=' . $id . '&account=' . $this->merchantId;
- $ret = AdminService::sendUrlToMobile($url, $this->merchant);
- if ($ret == false) {
- util::failInfo($this->error->getError());
- }
- util::successInfo('已发送');
- }
-
- }
|