| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace api\modules\v1\controllers\user;
- use addons\SignIn\common\events\SignInEvent;
- use addons\SignIn\common\models\UserSignInRecord;
- use addons\SignIn\common\models\UserSignInAward;
- use addons\SignIn\common\login\SignInReward;
- use api\controllers\OnAuthController;
- use common\enums\AddonsEnum;
- use common\models\mall\MallAddons;
- use common\models\user\UserWallet;
- use Yii;
- class SignInController extends OnAuthController
- {
- public $modelClass = '';
-
- /**
- *
- * 签到规则
- *
- * @author hpei
- * @return array
- */
- public function actionSignInRule() {
- $setting = Yii::$app->services->setting->addons->get($this->mall_id, 'SignIn', 'setting');
- $rule = isset($setting['rule']) ? $setting['rule'] : '';
- $waiiet = UserWallet::getOne([['user_id' => $this->user_id, 'mall_id' => $this->mall_id]]);
- $score = empty($waiiet) ? 0 : ($waiiet['score'] ? $waiiet['score'] : 0);
- return $this->success('操作成功', ['score' => $score, 'rule' => $rule]);
- }
- /**
- * 签到首页
- *
- * @author hpei
- * @return array
- */
- public function actionSignInIndex() {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [[['month'], 'safe']]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $month = $params['month'];
- if (empty($month)) $month = date('Y-m');
- $day = date('t', strtotime($month . '-01'));
- $start_time = strtotime($month . '-01 00:00:00');
- $end_time = strtotime($month . '-' . $day . '23:59:59');
- $params = [['=', 'user_id', $this->user_id], ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]];
- $signDateList = UserSignInRecord::getSignIn($params);
- if (!empty($signDateList)) {
- array_walk($signDateList, function(&$val){
- $val = date('Y-m-j', $val);
- });
- }
- $sign_in_day = [];
- for ($i = 1; $i <= $day; $i++) {
- if (in_array($month . '-' . $i, $signDateList)) {
- $sign_in_day[] = ['date' => $month . '-' . $i, 'is_sign' => true];
- } else {
- $sign_in_day[] = ['date' => $month . '-' . $i, 'is_sign' => false];
- }
- }
- $is_sign_in = in_array(date('Y-m-j'), $signDateList) ? true : false;
- $continue_day = (int)UserSignInAward::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id])->sum('total_sign_in');
- $list = array('is_sign_in' => $is_sign_in, 'continue_day' => $continue_day, 'sign_in_day' => $sign_in_day);
- return $this->success('操作成功', $list);
- }
- /**
- * 签到
- *
- * @author hpei
- * @return array
- */
- public function actionSignIn() {
- $r_key = "sign:in:$this->user_id";
- if (\Yii::$app->redis->get($r_key)) return $this->error('签到中...');
- \Yii::$app->redis->setex($r_key, 30, 1); // 30秒钟内只能请求一次
- $date = date('Y-m-d');
- $start_time = strtotime($date . ' 0:0:0');
- $end_time = strtotime($date . ' 23:59:59');
- $signRecord = UserSignInRecord::find()
- ->where(['user_id' => $this->user_id])
- ->andWhere(['>=', 'created_at', $start_time])
- ->andWhere(['<=', 'created_at', $end_time])
- ->one();
- if ($signRecord) return $this->error('今日已签到');
- $model = new UserSignInRecord();
- $model->mall_id = $this->mall_id;
- $model->user_id = $this->user_id;
- $model->created_at = time();
- if (!$model->save()) return $this->error($model::getStaticError());
- SignInReward::sexecuteReward($this->user_id, $this->mall_id, $model->attributes['id']);//发放奖励
- $event = new SignInEvent($model['mall_id']);
- $order['mall_id'] = $this->mall_id;
- $order['user_id'] = $this->user_id;
- Yii::$app->services->events->dispatch($event, $order, $event->listeners);
- //消费奖励插件签到
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_CONSUME_REWARD);
- if ($is_install) {
- (new \addons\ConsumeReward\common\services\UserService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->setSignIn();
- }
- return $this->success('签到成功');
- }
- }
|