| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace addons\SignIn\backend\controllers;
- use addons\SignIn\common\models\SignInAwardConfig;
- use addons\SignIn\common\models\UserSignInAward;
- use addons\SignIn\common\models\UserSignInAwardRecord;
- use addons\SignIn\common\models\UserSignInRecord;
- use addons\Coupon\common\models\AddonsCoupon;
- use common\enums\RedisKeyEnum;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use common\helpers\csv\CsvExportHelper;
- use common\enums\DownloadEnum;
- use common\models\mall\MallAddons;
- use common\enums\AddonsEnum;
- use Yii;
- use function GuzzleHttp\json_decode;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\SignIn\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- //setting reward
- $group = Yii::$app->request->get('group','');
- $group = $group ?: 'reward';
- $config = Yii::$app->services->setting->addons->get($this->mall_id, 'SignIn', $group);
- $list = [];
- if ($group == 'setting') {
- $status = $config['status'] ?? 1;
- $rule = $config['rule'] ?? '';
- $list = ['status' => $status, 'rule' => $rule];
- } else {
- $reward_coupon = $config['reward_coupon'] ? json_decode($config['reward_coupon']) : [];
- $reward_score = $config['reward_score'] ? json_decode($config['reward_score'],true) : [];
- $today_coupon = $config['today_coupon'] ? json_decode($config['today_coupon'], true) : [];
- $today_score = $config['today_score'] ? json_decode($config['today_score'],true) : [];
- $is_coupon_sign_repeat = $config['is_coupon_sign_repeat'] ?? 0;
- $is_score_sign_repeat = $config['is_score_sign_repeat'] ?? 0;
- if(!isset($today_score['give_type'])) $today_score['give_type'] = '';
- foreach ($reward_score as &$item){
- if(!isset($item['give_type'])) $item['give_type'] = '';
- }
- $coupon_list = [];
- if(MallAddons::isInstall($this->mall_id,'Coupon')){
- $coupon_list = AddonsCoupon::getMallCoupon($this->mall_id);
- if (!empty($reward_coupon)) {
- //过滤过期,数量不足,删除的优惠券
- foreach ($reward_coupon as $key => $val) {
- if (!isset($coupon_list[$val->coupon_id])) {
- unset($reward_coupon[$key]);
- continue;
- }
- $reward_coupon[$key]->coupon_name = $coupon_list[$val->coupon_id]['name'];
- }
- }
- if (!empty($today_coupon)) {
- if (!isset($coupon_list[$today_coupon['coupon_id']])) {
- $today_coupon['coupon_id'] = 0;
- $today_coupon['num'] = 0;
- }
- }
- }
- $list = [
- 'reward_score' => $reward_score,
- 'reward_coupon' => array_values($reward_coupon),
- 'coupon_list' => $coupon_list,
- 'today_coupon' => $today_coupon,
- 'today_score' => $today_score,
- 'is_coupon_sign_repeat' => $is_coupon_sign_repeat,
- 'is_score_sign_repeat' => $is_score_sign_repeat,
- ];
- }
- $list['isShow'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_SIGNIN);
- $give_type = [
- [
- 'name'=>'score','title'=>Yii::$app->services->setting->mall->get($this->mall_id, 'score.score_name') ?? '积分',
- ],
- [
- 'name'=>'balance','title'=>Yii::$app->services->setting->mall->get($this->mall_id, 'balance.balance_name') ?? '余额',
- ],
- ];
- if(MallAddons::isInstall($this->mall_id,'Redpack')){
- $config = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_REDPACK, 'basic');
- $name = !empty($config['redpack_name'])?$config['redpack_name']:'红包';
- $give_type[]=['name'=>'RedpackFrozen','title'=>'冻结'.$name,];
- $give_type[]=['name'=>'Redpack','title'=>'激活'.$name,];
- }
- $list['give_type'] = $give_type;
- return $this->success('操作成功', $list);
- } else {
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post, [
- [['status','is_coupon_sign_repeat', 'is_score_sign_repeat'], 'in', 'range' => [0, 1]],
- [['score', 'coupon', 'today_score', 'today_coupon', 'status', 'rule', 'group'], 'safe']
- ]);
- if ($post['group'] == 'setting') {
- $setting['setting'] = ['rule' => $post['rule'], 'status' => $post['status']];
- Yii::$app->services->setting->addons->set($this->mall_id, 'SignIn', $setting);
- } else {
- if (!empty($post['coupon'])) {
- array_walk($post['coupon'], function(&$val, $index) {
- unset($val['coupon_name']);
- });
- }
- if ($post['today_coupon']['coupon_id'] == 0) $post['today_coupon'] = ['coupon_id' => 0, 'num' => 0];
- $data['reward'] = [
- 'reward_score' => $post['score'],
- 'reward_coupon' => $post['coupon'],
- 'today_score' => $post['today_score'],
- 'today_coupon' => $post['today_coupon'],
- 'is_coupon_sign_repeat' => $post['is_coupon_sign_repeat'],
- 'is_score_sign_repeat' => $post['is_score_sign_repeat'],
- ];
- Yii::$app->services->setting->addons->set($this->mall_id, 'SignIn', $data);
- }
- return $this->success('设置成功');
- }
- }
- return $this->render('index');
- }
- /**
- * 签到记录
- */
- public function actionRecord() {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $filename = '签到列表-' . date('YmdHis').'_'.rand(10000,99999);
- $data = \Yii::$app->request->post();
- $data['table'] = 'score';
- $data['mall_id'] = $this->mall_id;
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_MEMBER,UserSignInRecord::class,'exportSignIn',$data);
- if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- $where[] = ['=', 'mall_id', $this->mall_id];
- $get = Yii::$app->request->get();
- $keyword = $get['keyword'] ?? '';
- if (!empty($level)) $userPraams['where'] = ['=', 'level', $level];
- if (!empty($keyword)) {
- $model = User::find();
- $userPraams['where'] = ['=', 'mall_id', $this->mall_id];
- $userPraams['where'] = [['or',
- ['=', 'id', $keyword],
- ['=', 'mobile', $keyword]]];
- $userPraams['select'] = ['id', 'nickname', 'username', 'avatar_url', 'mobile', 'level'];
- $userPraams['index'] = ['id'];
- User::paramPack($userPraams, $model);
- $userList = $model->asArray()->all();
- }
- $userLevel = UserLevel::getListIndexByLevelColumn($this->mall_id);
- //筛选
- if ($get['start_date'] != '') {
- $where[] = ['>', 'created_at', strtotime($get['start_date'])];
- }
- if ($get['end_date'] != '') {
- $where[] = ['<', 'created_at', strtotime($get['end_date'])];
- }
- if ($keyword && empty($userList)) {
- $list['list'] = [];
- } else {
- if (!empty($userList)) $where[] = ['in', 'user_id', array_column($userList, 'id')];
- $params['where'] = $where;
- $params['limit'] = 10;
- $params['select'] = ['user_id', 'max(id) id', 'max(created_at) as created_at'];
- $params['order'] = 'id desc';
- $params['group'] = 'user_id';
- $list = UserSignInRecord::listPage($params, false);
- if (!empty($list['list'])) {
- if (empty($userList)) {
- $user_ids = array_column($list['list'], 'user_id');
- $userList = User::find()
- ->select(['id', 'nickname', 'username', 'avatar_url', 'mobile', 'level'])
- ->where(['id' => $user_ids])->indexBy('id')->all();
- }
- foreach ($list['list'] as &$user) {
- if($user['created_at']>=strtotime(date("Y-m-d"))){ //只查今日奖励
- $award = UserSignInAwardRecord::find()->select(['num', 'type'])->where(['sign_id' => $user['id']])->all();
- $user['score_num'] = 0;
- $user['coupon_num'] = 0;
- if (!empty($award)) {
- foreach ($award as $val) {
- if ($val['type'] == 1) $user['score_num'] += $val['num'];
- if ($val['type'] == 2) $user['coupon_num'] += $val['num'];
- }
- }
- }
- $user['nickname'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['nickname'] : '';
- $user['username'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['username'] : '';
- $user['avatar_url'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['avatar_url'] : '';
- $user['mobile'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['mobile'] : '';
- $user['level'] = isset($userList[$user['user_id']]) ? $userList[$user['user_id']]['level'] : '';
- }
- }
- }
- $list['list'] = !empty($list['list']) ? $list['list'] : [];
- $list['export_list'] = UserSignInRecord::exportFieldsList();
- $list['user_level'] = $userLevel;
- return $this->success('操作成功', $list);
- }
- return $this->render('record');
- }
- /**
- * 用户签到
- */
- public function actionUserSignIn() {
- ini_set('memory_limit', '256M'); // 将内存限制设置为256M
- if (Yii::$app->request->isAjax) {
- $user_id = Yii::$app->request->get('user_id');
- $user_id = $user_id ?? 0;
- $user = User::findOne(['id' => $user_id]);
- if (empty($user)) throw new \Exception('用户不存在');
- $where = [['=', 'mall_id', $this->mall_id], ['=', 'user_id', $user_id]];
- $params['where'] = $where;
- $params['limit'] = 10;
- $params['order'] = 'created_at desc';
- $params['select'] = ['num', 'coupon_id', 'type', 'describe', 'created_at'];
- $list = UserSignInAwardRecord::listPage($params);
- return $this->success('操作成功', $list);
- }
- return $this->render('user_sign_in');
- }
- }
|