VipService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Services\User;
  3. use App\Exceptions\AlertException;
  4. use App\Managers\CouponManager;
  5. use App\Models\Log\VipLogModel;
  6. use App\Models\OrderModel;
  7. use App\Models\User\Openid;
  8. use App\Models\User\UserModel;
  9. use App\Models\User\UserStateModel;
  10. use App\Models\User\UserSysTagModel;
  11. use App\Services\Service;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Redis;
  14. /**
  15. * Class VipService
  16. * @package App\Services\User
  17. */
  18. class VipService extends Service
  19. {
  20. /**
  21. * 成为普通VIP
  22. * @param int $uid
  23. * @throws AlertException
  24. */
  25. public function beVip(int $uid)
  26. {
  27. if (
  28. !Openid::where([
  29. ['uid', $uid],
  30. ['public_id', config('wechat.fpdx.public_id')],
  31. ['subscribe', 1],
  32. ])->exists()
  33. ) {
  34. throw new AlertException("未关注", 101);
  35. }
  36. $ps = new ProfileService();
  37. $ps->beVip($uid, true);
  38. }
  39. /**
  40. * 超级VIP支付结果处理
  41. * @param OrderModel $order
  42. * @return bool
  43. * @throws \Exception
  44. */
  45. public function payment(OrderModel $order)
  46. {
  47. try {
  48. DB::beginTransaction();
  49. /** @var UserModel $user */
  50. $user = UserModel::findOrFail($order->uid);
  51. // 1. 判断要给用户增加超级会员天数
  52. $xuvip = false;
  53. $user->supvip_endat > time() && $xuvip = true;
  54. switch ($order->type) {
  55. case 12:
  56. $addday = 31;
  57. break;
  58. case 13:
  59. $addday = 31 * 3;
  60. $xuvip && $addday += 5;
  61. break;
  62. case 14:
  63. $addday = 31 * 12;
  64. $xuvip && $addday += 31;
  65. break;
  66. default:
  67. return false;
  68. }
  69. $xuvip ? $user->supvip_endat += $addday * 86400 : $user->supvip_endat = mktime(
  70. 0,
  71. 0,
  72. 0
  73. ) + (1 + $addday) * 86400;
  74. // 2. 记录kdgx_fpdx_user_record增加天数、修改到期时间
  75. if (UserSysTagModel::where('uid', $user->uid)->exists()) {
  76. $update = array('supvip_endat' => $user->supvip_endat, 'last_supvip_expire_notice_at' => 0);
  77. !$xuvip && $update['last_be_supvip_at'] = time();
  78. UserSysTagModel::where('uid', $user->uid)->increment('supvip_days', $addday, $update);
  79. } else {
  80. UserSysTagModel::create(array(
  81. 'uid' => $user->uid,
  82. 'supvip_days' => $addday,
  83. 'supvip_endat' => $user->supvip_endat,
  84. 'last_be_supvip_at' => time(),
  85. ));
  86. }
  87. $user->save();
  88. // 3. 发放72h入场券
  89. if (0 == UserStateModel::get($order->uid, "pay_open_supvip") || !$xuvip) {
  90. $couponManager = new CouponManager();
  91. $couponManager->generateSuperVip72FreeCoupons($user->uid);
  92. }
  93. DB::commit();
  94. UserStateModel::set($order->uid, "pay_open_supvip", 1);
  95. if ($xuvip) {
  96. VipLogModel::renewalSupvip($user->uid, $addday);
  97. } else {
  98. VipLogModel::openSupvip($user->uid, $addday);
  99. }
  100. } catch (\Exception $exception) {
  101. DB::rollBack();
  102. return false;
  103. }
  104. return true;
  105. }
  106. /**
  107. * 每期发送72小时入场券
  108. */
  109. public function send72hourTicket()
  110. {
  111. $users = UserModel::where([['supvip_endat', '>', time()]])->get();
  112. foreach ($users as $user) {
  113. $couponManager = new CouponManager();
  114. $couponManager->generateSuperVip72FreeCoupons($user->uid);
  115. }
  116. }
  117. /**
  118. * 内测开通超级会员通道
  119. * @param int $uid
  120. * @throws AlertException
  121. */
  122. public function beSuperVip(int $uid)
  123. {
  124. $exists = Redis::sismember("fpdx_supervip_test", $uid);
  125. if (1 == $exists) {
  126. $existsed = Redis::sismember("fpdx_supervip_test_ed", $uid);
  127. if (1 == $existsed) {
  128. throw new AlertException("你已经是领取了", 102);
  129. }
  130. $user = UserModel::findOrFail($uid);
  131. if (1 != $user->task_photo && 1 != $user->task_question) {
  132. throw new AlertException("请设置心动考验", 103);
  133. }
  134. // 1. 判断要给用户增加超级会员天数
  135. $xuvip = false;
  136. $user->supvip_endat > time() && $xuvip = true;
  137. $addday = 7;
  138. $xuvip ? $user->supvip_endat += $addday * 86400 : $user->supvip_endat = mktime(
  139. 0,
  140. 0,
  141. 0
  142. ) + (1 + $addday) * 86400;
  143. // 2. 记录kdgx_fpdx_user_record增加天数、修改到期时间
  144. if (UserSysTagModel::where('uid', $user->uid)->exists()) {
  145. $update = array('supvip_endat' => $user->supvip_endat, 'last_supvip_expire_notice_at' => 0);
  146. !$xuvip && $update['last_be_supvip_at'] = mktime(0, 0, 0);
  147. UserSysTagModel::where('uid', $user->uid)->increment('supvip_days', $addday, $update);
  148. } else {
  149. UserSysTagModel::create(array(
  150. 'uid' => $user->uid,
  151. 'supvip_days' => $addday,
  152. 'supvip_endat' => $user->supvip_endat,
  153. 'last_be_supvip_at' => mktime(0, 0, 0),
  154. ));
  155. }
  156. $user->save();
  157. // 3. 发放72h入场券
  158. $couponManager = new CouponManager();
  159. $couponManager->generateSuperVip72FreeCoupons($user->uid);
  160. Redis::sadd("fpdx_supervip_test_ed", [$uid]);
  161. if ($xuvip) {
  162. VipLogModel::renewalSupvip($uid, $addday);
  163. } else {
  164. VipLogModel::openSupvip($uid, $addday);
  165. }
  166. } else {
  167. throw new AlertException("无内测资格", 101);
  168. }
  169. }
  170. /**
  171. * 创建卡片领取超级会员
  172. * @param int $uid
  173. * @throws AlertException
  174. */
  175. public function sendSuperVipByStorePartner(int $uid)
  176. {
  177. $existsed = Redis::sismember("fpdx:supervip:send:store:partner", $uid);
  178. if (1 == $existsed) {
  179. throw new AlertException("你已经是领取了", 200);
  180. }
  181. $user = UserModel::findOrFail($uid);
  182. // 1. 判断要给用户增加超级会员天数
  183. $xuvip = false;
  184. $user->supvip_endat > time() && $xuvip = true;
  185. $addday = 7;
  186. $xuvip ? $user->supvip_endat += $addday * 86400 : $user->supvip_endat = mktime(0, 0, 0) + (1 + $addday) * 86400;
  187. // 2. 记录kdgx_fpdx_user_record增加天数、修改到期时间
  188. if (UserSysTagModel::where('uid', $user->uid)->exists()) {
  189. $update = array('supvip_endat' => $user->supvip_endat, 'last_supvip_expire_notice_at' => 0);
  190. !$xuvip && $update['last_be_supvip_at'] = mktime(0, 0, 0);
  191. UserSysTagModel::where('uid', $user->uid)->increment('supvip_days', $addday, $update);
  192. } else {
  193. UserSysTagModel::create(array(
  194. 'uid' => $user->uid,
  195. 'supvip_days' => $addday,
  196. 'supvip_endat' => $user->supvip_endat,
  197. 'last_be_supvip_at' => mktime(0, 0, 0),
  198. ));
  199. }
  200. $user->save();
  201. // 3. 发放72h入场券
  202. $couponManager = new CouponManager();
  203. $couponManager->generateSuperVip72FreeCoupons($user->uid);
  204. Redis::sadd("fpdx:supervip:send:store:partner", [$uid]);
  205. if ($xuvip) {
  206. VipLogModel::renewalSupvip($uid, $addday);
  207. } else {
  208. VipLogModel::openSupvip($uid, $addday);
  209. }
  210. }
  211. /**
  212. * 每月提醒会员到期
  213. * 是否到期提醒过
  214. */
  215. public function remindExpire()
  216. {
  217. // 是否到期提醒过
  218. // 明天0点时间 < 到期时间 < 后天0点时间
  219. $tags = UserSysTagModel::whereBetween(
  220. 'supvip_endat',
  221. [mktime(0, 0, 0) + 86410, mktime(0, 0, 0) + 86400 * 2]
  222. )->where(
  223. 'last_supvip_expire_notice_at',
  224. 0
  225. )->limit(10)->get();
  226. $ns = new NoticeService();
  227. foreach ($tags as $tag) {
  228. if ($ns->supvipExpire($tag->uid)) {
  229. UserSysTagModel::where('uid', $tag->uid)->update(['last_supvip_expire_notice_at' => time()]);
  230. }
  231. }
  232. }
  233. }