Invite.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Http\Controllers\Miniprogram;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\InviteRelationModel;
  5. use App\Models\User\UserModel;
  6. class Invite extends Controller
  7. {
  8. /**
  9. * 创建uid专属链接
  10. * @return array
  11. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  12. */
  13. public function getLink()
  14. {
  15. $uid = Auth::auth();
  16. return array(
  17. 'code' => 200,
  18. 'message' => 'success',
  19. 'data' => [
  20. 'link' => $uid
  21. ],
  22. );
  23. }
  24. /**
  25. * 通过邀请进入
  26. * (sign时间小于一天且未被建立邀请关系的为新用户)
  27. * @param string $link
  28. * @return array
  29. * @throws \ApiException
  30. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  31. */
  32. public function intoLink(string $link)
  33. {
  34. $uid = Auth::auth();
  35. $inviteModel = new InviteRelationModel();
  36. $auth = \DB::table('kdgx_user_auth_key')->where(array(
  37. ['uid', $uid],
  38. ['auth_type', config('miniprogram.public_id')]
  39. ))->first();
  40. if ($auth->created_at < mktime(0, 0, 0) - 3600) {
  41. return array(
  42. 'code' => 201,
  43. 'message' => '新用户专享福利'
  44. );
  45. }
  46. if ($inviteModel->where('is_invite_uid', $uid)->exists()) {
  47. return array(
  48. 'code' => 201,
  49. 'message' => '新用户专享福利'
  50. );
  51. }
  52. $invite = $inviteModel->fill([
  53. 'uid' => (int)$link,
  54. 'is_invite_uid' => $uid,
  55. 'state' => 1
  56. ]);
  57. if ($invite->save()) {
  58. UserModel::where('uid', $uid)->increment('red_flower', 1);
  59. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  60. 'uid' => $uid,
  61. 'create_at' => time(),
  62. 'type' => 10,
  63. 'gold_flower' => 0,
  64. 'red_flower' => 1,
  65. 'jsk' => 0,
  66. 'type_id' => 0,
  67. 'remark' => "邀请新人奖励"
  68. ]);
  69. return array(
  70. 'code' => 200,
  71. 'message' => 'success'
  72. );
  73. } else {
  74. throw new \ApiException("数据库异常", 505);
  75. }
  76. }
  77. /**
  78. * 新用户完成注册任务奖励
  79. * @param int $uid
  80. * @return bool
  81. */
  82. public function task(int $uid)
  83. {
  84. $rela = InviteRelationModel::where('is_invite_uid', $uid)->first();
  85. if (collect($rela)->isEmpty()) {
  86. return false;
  87. } else {
  88. $rela->state = 2;
  89. $rela->save();
  90. $ticketObj = new Ticket();
  91. $ticketObj->checkTicket(5, $rela->uid);
  92. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  93. 'uid' => $uid,
  94. 'create_at' => time(),
  95. 'type' => 10,
  96. 'remark' => '邀请新人奖励',
  97. 'jsk' => 1,
  98. ]);
  99. return true;
  100. }
  101. }
  102. /**
  103. * 获取邀请统计
  104. * @return array
  105. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  106. */
  107. public function inviteList()
  108. {
  109. $uid = Auth::auth();
  110. $relas = InviteRelationModel::where('uid', $uid)->get();
  111. $cnt = $relas->count();
  112. $users = UserModel::whereIn('uid', $relas->pluck('is_invite_uid'))->get(['headimgurl', 'uid']);
  113. $jsk_relas = $relas->where('state', 2);
  114. $jsk_cnt = $jsk_relas->count();
  115. $jsk_headimgurls = $users->whereIn('uid', $jsk_relas->pluck('is_invite_uid'));
  116. return array(
  117. 'code' => 200,
  118. 'message' => 'success',
  119. 'data' => [
  120. 'flower' => [
  121. 'headimgurl' => $users->pluck('headimgurl'),
  122. 'cnt' => $cnt
  123. ],
  124. 'jsk' => [
  125. 'headimgurl' => $jsk_headimgurls,
  126. 'cnt' => $jsk_cnt
  127. ],
  128. ],
  129. );
  130. }
  131. }