Invite.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace addons\invite;
  3. use addons\batchimg\library\batchimg\image\Exception;
  4. use addons\invite\model\Invite as InviteModel;
  5. use app\common\library\Auth;
  6. use app\common\library\Menu;
  7. use app\common\model\User;
  8. use think\Addons;
  9. use think\Cookie;
  10. use think\Request;
  11. use think\View;
  12. /**
  13. * 邀请插件
  14. */
  15. class Invite extends Addons
  16. {
  17. /**
  18. * 插件安装方法
  19. * @return bool
  20. */
  21. public function install()
  22. {
  23. return true;
  24. }
  25. /**
  26. * 插件卸载方法
  27. * @return bool
  28. */
  29. public function uninstall()
  30. {
  31. return true;
  32. }
  33. /**
  34. * 插件启用方法
  35. * @return bool
  36. */
  37. public function enable()
  38. {
  39. return true;
  40. }
  41. /**
  42. * 插件禁用方法
  43. * @return bool
  44. */
  45. public function disable()
  46. {
  47. return true;
  48. }
  49. /**
  50. * 会员中心边栏后
  51. * @return mixed
  52. * @throws \Exception
  53. */
  54. public function userSidenavAfter()
  55. {
  56. $request = Request::instance();
  57. $controllername = strtolower($request->controller());
  58. $actionname = strtolower($request->action());
  59. $data = [
  60. 'controllername' => $controllername,
  61. 'actionname' => $actionname
  62. ];
  63. return $this->fetch('view/hook/user_sidenav_after', $data);
  64. }
  65. /**
  66. * 会员注册成功
  67. * @param $auth
  68. * @throws \think\exception\DbException
  69. */
  70. public function userRegisterSuccessed($auth)
  71. {
  72. $inviter = (int)Cookie::get("inviter");
  73. if ($inviter) {
  74. $ip = Request::instance()->ip(0, false);
  75. $config = get_addon_config('invite');
  76. $inviterUser = User::get($inviter);
  77. //未找到邀请人,或IP和邀请者相同
  78. if (!$inviterUser || $inviterUser->loginip == $auth->loginip) {
  79. return;
  80. }
  81. //高级过滤模式
  82. if ($config['filtermode'] == 'advanced') {
  83. $ipRegistered = InviteModel::where('user_id', $inviterUser->id)->where('ip', $ip)->find();
  84. if ($ipRegistered) {
  85. return;
  86. }
  87. }
  88. //创建邀请记录
  89. InviteModel::create(['user_id' => $inviterUser->id, 'ip' => $ip, 'invited_user_id' => $auth->id]);
  90. //受邀请者增加积分
  91. if ($config['invitedscore']) {
  92. User::score($config['invitedscore'], $auth->id, '受邀注册赠送');
  93. }
  94. //每日邀请上限
  95. if ($config['dailymaxinvite']) {
  96. $inviteCount = InviteModel::where('user_id', $inviterUser->id)->whereTime('createtime', 'today')->count();
  97. if ($inviteCount > $config['dailymaxinvite']) {
  98. return;
  99. }
  100. }
  101. //增加邀请者积分
  102. User::score($config['rewardscore'], $inviterUser->id, '邀请好友注册');
  103. }
  104. Cookie::delete("inviter");
  105. }
  106. }