SignInController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace api\modules\v1\controllers\user;
  3. use addons\SignIn\common\events\SignInEvent;
  4. use addons\SignIn\common\models\UserSignInRecord;
  5. use addons\SignIn\common\models\UserSignInAward;
  6. use addons\SignIn\common\login\SignInReward;
  7. use api\controllers\OnAuthController;
  8. use common\enums\AddonsEnum;
  9. use common\models\mall\MallAddons;
  10. use common\models\user\UserWallet;
  11. use Yii;
  12. class SignInController extends OnAuthController
  13. {
  14. public $modelClass = '';
  15. /**
  16. *
  17. * 签到规则
  18. *
  19. * @author hpei
  20. * @return array
  21. */
  22. public function actionSignInRule() {
  23. $setting = Yii::$app->services->setting->addons->get($this->mall_id, 'SignIn', 'setting');
  24. $rule = isset($setting['rule']) ? $setting['rule'] : '';
  25. $waiiet = UserWallet::getOne([['user_id' => $this->user_id, 'mall_id' => $this->mall_id]]);
  26. $score = empty($waiiet) ? 0 : ($waiiet['score'] ? $waiiet['score'] : 0);
  27. return $this->success('操作成功', ['score' => $score, 'rule' => $rule]);
  28. }
  29. /**
  30. * 签到首页
  31. *
  32. * @author hpei
  33. * @return array
  34. */
  35. public function actionSignInIndex() {
  36. $params = Yii::$app->request->get();
  37. $res = Yii::$app->services->validate->validate($params, [[['month'], 'safe']]);
  38. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  39. $month = $params['month'];
  40. if (empty($month)) $month = date('Y-m');
  41. $day = date('t', strtotime($month . '-01'));
  42. $start_time = strtotime($month . '-01 00:00:00');
  43. $end_time = strtotime($month . '-' . $day . '23:59:59');
  44. $params = [['=', 'user_id', $this->user_id], ['>=', 'created_at', $start_time], ['<=', 'created_at', $end_time]];
  45. $signDateList = UserSignInRecord::getSignIn($params);
  46. if (!empty($signDateList)) {
  47. array_walk($signDateList, function(&$val){
  48. $val = date('Y-m-j', $val);
  49. });
  50. }
  51. $sign_in_day = [];
  52. for ($i = 1; $i <= $day; $i++) {
  53. if (in_array($month . '-' . $i, $signDateList)) {
  54. $sign_in_day[] = ['date' => $month . '-' . $i, 'is_sign' => true];
  55. } else {
  56. $sign_in_day[] = ['date' => $month . '-' . $i, 'is_sign' => false];
  57. }
  58. }
  59. $is_sign_in = in_array(date('Y-m-j'), $signDateList) ? true : false;
  60. $continue_day = (int)UserSignInAward::find()->where(['user_id' => $this->user_id, 'mall_id' => $this->mall_id])->sum('total_sign_in');
  61. $list = array('is_sign_in' => $is_sign_in, 'continue_day' => $continue_day, 'sign_in_day' => $sign_in_day);
  62. return $this->success('操作成功', $list);
  63. }
  64. /**
  65. * 签到
  66. *
  67. * @author hpei
  68. * @return array
  69. */
  70. public function actionSignIn() {
  71. $r_key = "sign:in:$this->user_id";
  72. if (\Yii::$app->redis->get($r_key)) return $this->error('签到中...');
  73. \Yii::$app->redis->setex($r_key, 30, 1); // 30秒钟内只能请求一次
  74. $date = date('Y-m-d');
  75. $start_time = strtotime($date . ' 0:0:0');
  76. $end_time = strtotime($date . ' 23:59:59');
  77. $signRecord = UserSignInRecord::find()
  78. ->where(['user_id' => $this->user_id])
  79. ->andWhere(['>=', 'created_at', $start_time])
  80. ->andWhere(['<=', 'created_at', $end_time])
  81. ->one();
  82. if ($signRecord) return $this->error('今日已签到');
  83. $model = new UserSignInRecord();
  84. $model->mall_id = $this->mall_id;
  85. $model->user_id = $this->user_id;
  86. $model->created_at = time();
  87. if (!$model->save()) return $this->error($model::getStaticError());
  88. SignInReward::sexecuteReward($this->user_id, $this->mall_id, $model->attributes['id']);//发放奖励
  89. $event = new SignInEvent($model['mall_id']);
  90. $order['mall_id'] = $this->mall_id;
  91. $order['user_id'] = $this->user_id;
  92. Yii::$app->services->events->dispatch($event, $order, $event->listeners);
  93. //消费奖励插件签到
  94. $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_CONSUME_REWARD);
  95. if ($is_install) {
  96. (new \addons\ConsumeReward\common\services\UserService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]))->setSignIn();
  97. }
  98. return $this->success('签到成功');
  99. }
  100. }