SignService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace App\Services\User;
  3. use App\Exceptions\AlertException;
  4. use App\Jobs\GrowingIO\SignFlowerReportJob;
  5. use App\Jobs\GrowingIO\SignReportJob;
  6. use App\Managers\CouponManager;
  7. use App\Models\FlowerLogModel;
  8. use App\Models\Log\SignRewardLogModel;
  9. use App\Models\User\SignModel;
  10. use App\Models\User\UserModel;
  11. use App\Models\User\UserStateModel;
  12. use App\Services\Service;
  13. use Illuminate\Contracts\Cache\LockTimeoutException;
  14. use Illuminate\Database\Eloquent\ModelNotFoundException;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 签到-领奖励
  20. * Class SignService
  21. * @package App\Services\User
  22. */
  23. class SignService extends Service
  24. {
  25. public static $SIGN = array(
  26. 1 => [
  27. "is_big_reward" => 0,
  28. "reward" => ['flower', 'popularity', 'app_lock'],
  29. ],
  30. 2 => [
  31. "is_big_reward" => 0,
  32. "reward" => ['flower', 'popularity', 'app_lock'],
  33. ],
  34. 3 => [
  35. "is_big_reward" => 1,
  36. "reward" => ['flower', 'popularity', 'app_lock', 'couponsvip'] // 超级会员满减券抵扣9元
  37. ],
  38. 4 => [
  39. "is_big_reward" => 0,
  40. "reward" => ['flower', 'popularity', 'app_lock'],
  41. ],
  42. 5 => [
  43. "is_big_reward" => 1,
  44. "reward" => ['flower', 'popularity', 'app_lock', 'coupon72dis'] // 72小时折扣券
  45. ],
  46. 6 => [
  47. "is_big_reward" => 0,
  48. "reward" => ['flower', 'popularity', 'app_lock'],
  49. ],
  50. 7 => [
  51. "is_big_reward" => 1,
  52. "reward" => ['flower', 'popularity', 'app_lock', 'coupon72pair'] // 72小时报名券
  53. ],
  54. );
  55. /**
  56. * 今日签到情况
  57. * @param int $uid
  58. * @return array
  59. */
  60. public function today(int $uid): array
  61. {
  62. $be_continuous_day = 0; // 连续签到几天
  63. $is_big_reward = 0; // 是否有礼包
  64. $is_sign = false; // 今日是否签到
  65. $arrReward = array(); // 签到奖励
  66. try {
  67. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  68. if ($sign->sign_at->isToday()) {
  69. $is_sign = true;
  70. $be_continuous_day = $sign->be_continuous_day;
  71. $is_big_reward = self::$SIGN[$be_continuous_day]['is_big_reward'];
  72. foreach (self::$SIGN[$be_continuous_day]['reward'] as $reward) {
  73. if (
  74. SignRewardLogModel::where([['uid', $uid], ['reward', $reward]])
  75. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
  76. ) {
  77. $arrReward[$reward] = true;
  78. } else {
  79. $arrReward[$reward] = false;
  80. }
  81. }
  82. } elseif ($sign->sign_at->isYesterday()) {
  83. $be_continuous_day = $sign->be_continuous_day;
  84. $is_big_reward = self::$SIGN[($be_continuous_day % 7) + 1]['is_big_reward'];
  85. if (7 == $be_continuous_day) {
  86. $be_continuous_day = 0;
  87. }
  88. }
  89. } catch (ModelNotFoundException $exception) {
  90. $be_continuous_day = 0;
  91. }
  92. return array(
  93. 'be_continuous_day' => $be_continuous_day,
  94. 'is_big_reward' => $is_big_reward,
  95. 'is_sign' => $is_sign,
  96. 'reward' => $arrReward,
  97. );
  98. }
  99. /**
  100. * 签到
  101. * @param int $uid
  102. * @param $platform
  103. * @return bool
  104. * @throws AlertException
  105. */
  106. public function sign(int $uid, $platform): bool
  107. {
  108. $be_continuous_day = 1; // 连续签到几天
  109. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->first();
  110. if (collect($sign)->isNotEmpty()) {
  111. if ($sign->sign_at->isToday()) {
  112. throw new AlertException("已经签到了");
  113. } elseif ($sign->sign_at->isYesterday()) {
  114. $be_continuous_day = $sign->be_continuous_day % 7 + 1;
  115. }
  116. }
  117. $lock = Cache::lock("sign:{$uid}", 60);
  118. try {
  119. $lock->get(function () use ($uid, $be_continuous_day, $platform) {
  120. $sign = SignModel::create([
  121. 'uid' => $uid,
  122. 'be_continuous_day' => $be_continuous_day,
  123. 'sign_date' => date('Y-m-d'),
  124. ]);
  125. dispatch_now(new SignReportJob($sign, $platform));
  126. });
  127. } catch (LockTimeoutException $exception) {
  128. } finally {
  129. optional($lock)->release();
  130. }
  131. return true;
  132. }
  133. /**
  134. * 领取小花奖励
  135. * @param int $uid
  136. * @return float|int|string
  137. * @throws AlertException*@throws \Exception
  138. * @throws \Exception
  139. */
  140. public function rewardFlower(int $uid)
  141. {
  142. if (
  143. SignRewardLogModel::where([['uid', $uid], ['reward', 'flower']])
  144. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
  145. ) {
  146. throw new AlertException("已领取过");
  147. }
  148. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  149. if ($sign->sign_at->isToday()) {
  150. if (!in_array("flower", self::$SIGN[$sign->be_continuous_day]['reward'])) {
  151. throw new AlertException("无这个奖励");
  152. }
  153. $rdCnt = sprintf("%.2f", 1.0 + mt_rand() / mt_getrandmax() * (3.0 - 1.0));
  154. $rdCnt = (self::$SIGN[$sign->be_continuous_day]['is_big_reward'] + 1) * $rdCnt;
  155. try {
  156. DB::beginTransaction();
  157. SignRewardLogModel::create([
  158. 'uid' => $uid,
  159. 'reward' => 'flower',
  160. 'data' => $rdCnt,
  161. ]);
  162. $user = UserModel::where('uid', $uid)->first();
  163. if ($user) {
  164. $user->increment("red_flower", $rdCnt);
  165. }
  166. $flowerLog = FlowerLogModel::create([
  167. 'uid' => $uid,
  168. 'type' => 7,
  169. 'red_flower' => $rdCnt,
  170. 'remark' => '签到奖励',
  171. ]);
  172. dispatch_now(new SignFlowerReportJob($flowerLog, $user));
  173. DB::commit();
  174. return $rdCnt;
  175. } catch (\Exception $exception) {
  176. DB::rollBack();
  177. throw $exception;
  178. }
  179. } else {
  180. throw new AlertException("还未签到签到了");
  181. }
  182. }
  183. /**
  184. * 领取人气值奖励
  185. * @param int $uid
  186. * @return float|int|string
  187. * @throws AlertException
  188. */
  189. public function rewardPopularity(int $uid)
  190. {
  191. if (
  192. SignRewardLogModel::where('uid', $uid)
  193. ->where('reward', 'popularity')
  194. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())
  195. ->exists()
  196. ) {
  197. throw new AlertException("已领取过");
  198. }
  199. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  200. if ($sign->sign_at->isToday()) {
  201. if (!in_array("popularity", self::$SIGN[$sign->be_continuous_day]['reward'])) {
  202. throw new AlertException("无这个奖励");
  203. }
  204. $long = (self::$SIGN[$sign->be_continuous_day]['is_big_reward'] + 1) * 5 * 3600;
  205. try {
  206. DB::beginTransaction();
  207. SignRewardLogModel::create([
  208. 'uid' => $uid,
  209. 'reward' => 'popularity',
  210. 'data' => $long,
  211. ]);
  212. UserStateModel::inc($uid, "popularity_sign_end_at", $long, true);
  213. DB::commit();
  214. return intval($long / 3600);
  215. } catch (\Exception $exception) {
  216. DB::rollBack();
  217. throw $exception;
  218. }
  219. } else {
  220. throw new AlertException("还未签到签到了");
  221. }
  222. }
  223. /**
  224. * 领取解锁次数
  225. * @param int $uid
  226. * @throws AlertException
  227. */
  228. public function rewardUnLookCard(int $uid)
  229. {
  230. if (!$this->isSign($uid)) {
  231. throw new AlertException("还未签到");
  232. }
  233. if (
  234. SignRewardLogModel::where([['uid', $uid], ['reward', 'app_lock']])
  235. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
  236. ) {
  237. throw new AlertException("已领取过");
  238. }
  239. SignRewardLogModel::create([
  240. 'uid' => $uid,
  241. 'reward' => 'app_lock',
  242. 'data' => 1,
  243. ]);
  244. UserModel::where('uid', $uid)->increment('app_like_unlock_count');
  245. }
  246. /**
  247. * 判断是否签到
  248. * @param $uid
  249. * @return mixed
  250. */
  251. public function isSign($uid)
  252. {
  253. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  254. return $sign->sign_at->isToday();
  255. }
  256. /**
  257. * 领取72h卡券满减券
  258. * @param int $uid
  259. * @return float|int|string
  260. * @throws AlertException
  261. */
  262. public function rewardCouponSuperVip(int $uid)
  263. {
  264. if (
  265. SignRewardLogModel::where([['uid', $uid], ['reward', 'couponsvip']])
  266. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
  267. ) {
  268. throw new AlertException("已领取过");
  269. }
  270. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  271. if ($sign->sign_at->isToday()) {
  272. if (!in_array("couponsvip", self::$SIGN[$sign->be_continuous_day]['reward'])) {
  273. throw new AlertException("无这个奖励");
  274. }
  275. DB::beginTransaction();
  276. try {
  277. SignRewardLogModel::create([
  278. 'uid' => $uid,
  279. 'reward' => 'couponsvip',
  280. 'data' => 1,
  281. ]);
  282. $couponManager = new CouponManager();
  283. $couponManager->generateSignSuperVipCoupons($uid);
  284. DB::commit();
  285. } catch (\Exception $exception) {
  286. DB::rollBack();
  287. throw $exception;
  288. }
  289. return 1;
  290. } else {
  291. throw new AlertException("还未签到签到了");
  292. }
  293. }
  294. /**
  295. * 72小时折扣券
  296. * @param int $uid
  297. * @return float|int|string
  298. * @throws AlertException
  299. */
  300. public function rewardCoupon72Dis(int $uid)
  301. {
  302. if (
  303. SignRewardLogModel::where([['uid', $uid], ['reward', 'coupon72dis']])
  304. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
  305. ) {
  306. throw new AlertException("已领取过");
  307. }
  308. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  309. if ($sign->sign_at->isToday()) {
  310. if (!in_array("coupon72dis", self::$SIGN[$sign->be_continuous_day]['reward'])) {
  311. throw new AlertException("无这个奖励");
  312. }
  313. DB::beginTransaction();
  314. try {
  315. SignRewardLogModel::create([
  316. 'uid' => $uid,
  317. 'reward' => 'coupon72dis',
  318. 'data' => 1,
  319. ]);
  320. $couponManager = new CouponManager();
  321. $couponManager->generateSign72DiscountCoupons($uid);
  322. DB::commit();
  323. } catch (\Exception $exception) {
  324. DB::rollBack();
  325. throw $exception;
  326. }
  327. return 1;
  328. } else {
  329. throw new AlertException("还未签到签到了");
  330. }
  331. }
  332. /**
  333. * 72小时入场券
  334. * @param int $uid
  335. * @return float|int|string
  336. * @throws AlertException
  337. */
  338. public function rewardCoupon72Pair(int $uid)
  339. {
  340. if (
  341. SignRewardLogModel::where([['uid', $uid], ['reward', 'coupon72pair']])
  342. ->where('created_at', '>', Carbon::today()->toDateTimeLocalString())->exists()
  343. ) {
  344. throw new AlertException("已领取过");
  345. }
  346. $sign = SignModel::where("uid", $uid)->orderBy("id", 'desc')->firstOrFail();
  347. if ($sign->sign_at->isToday()) {
  348. if (!in_array("coupon72pair", self::$SIGN[$sign->be_continuous_day]['reward'])) {
  349. throw new AlertException("无这个奖励");
  350. }
  351. DB::beginTransaction();
  352. try {
  353. SignRewardLogModel::create([
  354. 'uid' => $uid,
  355. 'reward' => 'coupon72pair',
  356. 'data' => 1,
  357. ]);
  358. $couponManager = new CouponManager();
  359. $couponManager->generateSuperVip72FreeCoupons($uid);
  360. DB::commit();
  361. } catch (\Exception $exception) {
  362. DB::rollBack();
  363. throw $exception;
  364. }
  365. return 1;
  366. } else {
  367. throw new AlertException("还未签到签到了");
  368. }
  369. }
  370. }