123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- namespace App\Services\User;
- use App\Exceptions\AlertException;
- use App\Jobs\GrowingIO\SignFlowerReportJob;
- use App\Jobs\GrowingIO\SignReportJob;
- use App\Managers\CouponManager;
- use App\Models\FlowerLogModel;
- use App\Models\Log\SignRewardLogModel;
- use App\Models\User\SignModel;
- use App\Models\User\UserModel;
- use App\Models\User\UserStateModel;
- use App\Services\Service;
- use Illuminate\Contracts\Cache\LockTimeoutException;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- /**
- * 签到-领奖励
- * Class SignService
- * @package App\Services\User
- */
- class SignService extends Service
- {
- public static $SIGN = array(
- 1 => [
- "is_big_reward" => 0,
- "reward" => ['flower', 'popularity', 'app_lock'],
- ],
- 2 => [
- "is_big_reward" => 0,
- "reward" => ['flower', 'popularity', 'app_lock'],
- ],
- 3 => [
- "is_big_reward" => 1,
- "reward" => ['flower', 'popularity', 'app_lock', 'couponsvip'] // 超级会员满减券抵扣9元
- ],
- 4 => [
- "is_big_reward" => 0,
- "reward" => ['flower', 'popularity', 'app_lock'],
- ],
- 5 => [
- "is_big_reward" => 1,
- "reward" => ['flower', 'popularity', 'app_lock', 'coupon72dis'] // 72小时折扣券
- ],
- 6 => [
- "is_big_reward" => 0,
- "reward" => ['flower', 'popularity', 'app_lock'],
- ],
- 7 => [
- "is_big_reward" => 1,
- "reward" => ['flower', 'popularity', 'app_lock', 'coupon72pair'] // 72小时报名券
- ],
- );
- /**
- * 今日签到情况
- * @param int $uid
- * @return array
- */
- public function today(int $uid): array
- {
- $be_continuous_day = 0; // 连续签到几天
- $is_big_reward = 0; // 是否有礼包
- $is_sign = false; // 今日是否签到
- $arrReward = array(); // 签到奖励
- try {
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- if ($sign->sign_at->isToday()) {
- $is_sign = true;
- $be_continuous_day = $sign->be_continuous_day;
- $is_big_reward = self::$SIGN[$be_continuous_day]['is_big_reward'];
- foreach (self::$SIGN[$be_continuous_day]['reward'] as $reward) {
- if (
- SignRewardLogModel::where([['uid', $uid], ['reward', $reward]])
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
- ) {
- $arrReward[$reward] = true;
- } else {
- $arrReward[$reward] = false;
- }
- }
- } elseif ($sign->sign_at->isYesterday()) {
- $be_continuous_day = $sign->be_continuous_day;
- $is_big_reward = self::$SIGN[($be_continuous_day % 7) + 1]['is_big_reward'];
- if (7 == $be_continuous_day) {
- $be_continuous_day = 0;
- }
- }
- } catch (ModelNotFoundException $exception) {
- $be_continuous_day = 0;
- }
- return array(
- 'be_continuous_day' => $be_continuous_day,
- 'is_big_reward' => $is_big_reward,
- 'is_sign' => $is_sign,
- 'reward' => $arrReward,
- );
- }
- /**
- * 签到
- * @param int $uid
- * @param $platform
- * @return bool
- * @throws AlertException
- */
- public function sign(int $uid, $platform): bool
- {
- $be_continuous_day = 1; // 连续签到几天
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->first();
- if (collect($sign)->isNotEmpty()) {
- if ($sign->sign_at->isToday()) {
- throw new AlertException("已经签到了");
- } elseif ($sign->sign_at->isYesterday()) {
- $be_continuous_day = $sign->be_continuous_day % 7 + 1;
- }
- }
- $lock = Cache::lock("sign:{$uid}", 60);
- try {
- $lock->get(function () use ($uid, $be_continuous_day, $platform) {
- $sign = SignModel::create([
- 'uid' => $uid,
- 'be_continuous_day' => $be_continuous_day,
- 'sign_date' => date('Y-m-d'),
- ]);
- dispatch_now(new SignReportJob($sign, $platform));
- });
- } catch (LockTimeoutException $exception) {
- } finally {
- optional($lock)->release();
- }
- return true;
- }
- /**
- * 领取小花奖励
- * @param int $uid
- * @return float|int|string
- * @throws AlertException*@throws \Exception
- * @throws \Exception
- */
- public function rewardFlower(int $uid)
- {
- if (
- SignRewardLogModel::where([['uid', $uid], ['reward', 'flower']])
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
- ) {
- throw new AlertException("已领取过");
- }
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- if ($sign->sign_at->isToday()) {
- if (!in_array("flower", self::$SIGN[$sign->be_continuous_day]['reward'])) {
- throw new AlertException("无这个奖励");
- }
- $rdCnt = sprintf("%.2f", 1.0 + mt_rand() / mt_getrandmax() * (3.0 - 1.0));
- $rdCnt = (self::$SIGN[$sign->be_continuous_day]['is_big_reward'] + 1) * $rdCnt;
- try {
- DB::beginTransaction();
- SignRewardLogModel::create([
- 'uid' => $uid,
- 'reward' => 'flower',
- 'data' => $rdCnt,
- ]);
- $user = UserModel::where('uid', $uid)->first();
- if ($user) {
- $user->increment("red_flower", $rdCnt);
- }
- $flowerLog = FlowerLogModel::create([
- 'uid' => $uid,
- 'type' => 7,
- 'red_flower' => $rdCnt,
- 'remark' => '签到奖励',
- ]);
- dispatch_now(new SignFlowerReportJob($flowerLog, $user));
- DB::commit();
- return $rdCnt;
- } catch (\Exception $exception) {
- DB::rollBack();
- throw $exception;
- }
- } else {
- throw new AlertException("还未签到签到了");
- }
- }
- /**
- * 领取人气值奖励
- * @param int $uid
- * @return float|int|string
- * @throws AlertException
- */
- public function rewardPopularity(int $uid)
- {
- if (
- SignRewardLogModel::where('uid', $uid)
- ->where('reward', 'popularity')
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())
- ->exists()
- ) {
- throw new AlertException("已领取过");
- }
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- if ($sign->sign_at->isToday()) {
- if (!in_array("popularity", self::$SIGN[$sign->be_continuous_day]['reward'])) {
- throw new AlertException("无这个奖励");
- }
- $long = (self::$SIGN[$sign->be_continuous_day]['is_big_reward'] + 1) * 5 * 3600;
- try {
- DB::beginTransaction();
- SignRewardLogModel::create([
- 'uid' => $uid,
- 'reward' => 'popularity',
- 'data' => $long,
- ]);
- UserStateModel::inc($uid, "popularity_sign_end_at", $long, true);
- DB::commit();
- return intval($long / 3600);
- } catch (\Exception $exception) {
- DB::rollBack();
- throw $exception;
- }
- } else {
- throw new AlertException("还未签到签到了");
- }
- }
- /**
- * 领取解锁次数
- * @param int $uid
- * @throws AlertException
- */
- public function rewardUnLookCard(int $uid)
- {
- if (!$this->isSign($uid)) {
- throw new AlertException("还未签到");
- }
- if (
- SignRewardLogModel::where([['uid', $uid], ['reward', 'app_lock']])
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
- ) {
- throw new AlertException("已领取过");
- }
- SignRewardLogModel::create([
- 'uid' => $uid,
- 'reward' => 'app_lock',
- 'data' => 1,
- ]);
- UserModel::where('uid', $uid)->increment('app_like_unlock_count');
- }
- /**
- * 判断是否签到
- * @param $uid
- * @return mixed
- */
- public function isSign($uid)
- {
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- return $sign->sign_at->isToday();
- }
- /**
- * 领取72h卡券满减券
- * @param int $uid
- * @return float|int|string
- * @throws AlertException
- */
- public function rewardCouponSuperVip(int $uid)
- {
- if (
- SignRewardLogModel::where([['uid', $uid], ['reward', 'couponsvip']])
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
- ) {
- throw new AlertException("已领取过");
- }
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- if ($sign->sign_at->isToday()) {
- if (!in_array("couponsvip", self::$SIGN[$sign->be_continuous_day]['reward'])) {
- throw new AlertException("无这个奖励");
- }
- DB::beginTransaction();
- try {
- SignRewardLogModel::create([
- 'uid' => $uid,
- 'reward' => 'couponsvip',
- 'data' => 1,
- ]);
- $couponManager = new CouponManager();
- $couponManager->generateSignSuperVipCoupons($uid);
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- throw $exception;
- }
- return 1;
- } else {
- throw new AlertException("还未签到签到了");
- }
- }
- /**
- * 72小时折扣券
- * @param int $uid
- * @return float|int|string
- * @throws AlertException
- */
- public function rewardCoupon72Dis(int $uid)
- {
- if (
- SignRewardLogModel::where([['uid', $uid], ['reward', 'coupon72dis']])
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
- ) {
- throw new AlertException("已领取过");
- }
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- if ($sign->sign_at->isToday()) {
- if (!in_array("coupon72dis", self::$SIGN[$sign->be_continuous_day]['reward'])) {
- throw new AlertException("无这个奖励");
- }
- DB::beginTransaction();
- try {
- SignRewardLogModel::create([
- 'uid' => $uid,
- 'reward' => 'coupon72dis',
- 'data' => 1,
- ]);
- $couponManager = new CouponManager();
- $couponManager->generateSign72DiscountCoupons($uid);
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- throw $exception;
- }
- return 1;
- } else {
- throw new AlertException("还未签到签到了");
- }
- }
- /**
- * 72小时入场券
- * @param int $uid
- * @return float|int|string
- * @throws AlertException
- */
- public function rewardCoupon72Pair(int $uid)
- {
- if (
- SignRewardLogModel::where([['uid', $uid], ['reward', 'coupon72pair']])
- ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
- ) {
- throw new AlertException("已领取过");
- }
- $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
- if ($sign->sign_at->isToday()) {
- if (!in_array("coupon72pair", self::$SIGN[$sign->be_continuous_day]['reward'])) {
- throw new AlertException("无这个奖励");
- }
- DB::beginTransaction();
- try {
- SignRewardLogModel::create([
- 'uid' => $uid,
- 'reward' => 'coupon72pair',
- 'data' => 1,
- ]);
- $couponManager = new CouponManager();
- $couponManager->generateSuperVip72FreeCoupons($uid);
- DB::commit();
- } catch (\Exception $exception) {
- DB::rollBack();
- throw $exception;
- }
- return 1;
- } else {
- throw new AlertException("还未签到签到了");
- }
- }
- }
|