RegisterController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Models\User;
  4. use App\Models\Customer;
  5. use App\Models\Cart;
  6. use App\Models\BusinessSetting;
  7. use App\Models\AffiliateLog;
  8. use App\OtpConfiguration;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Controllers\OTPVerificationController;
  11. use App\Notifications\EmailVerificationNotification;
  12. use Illuminate\Auth\Events\Registered;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Hash;
  15. use Illuminate\Support\Facades\Validator;
  16. use Illuminate\Foundation\Auth\RegistersUsers;
  17. use Cookie;
  18. use Session;
  19. use Nexmo;
  20. use Twilio\Rest\Client;
  21. use function dd;
  22. class RegisterController extends Controller
  23. {
  24. /*
  25. |--------------------------------------------------------------------------
  26. | Register Controller
  27. |--------------------------------------------------------------------------
  28. |
  29. | This controller handles the registration of new users as well as their
  30. | validation and creation. By default this controller uses a trait to
  31. | provide this functionality without requiring any additional code.
  32. |
  33. */
  34. use RegistersUsers;
  35. /**
  36. * Where to redirect users after registration.
  37. *
  38. * @var string
  39. */
  40. protected $redirectTo = '/';
  41. /**
  42. * Create a new controller instance.
  43. *
  44. * @return void
  45. */
  46. public function __construct()
  47. {
  48. $this->middleware('guest');
  49. }
  50. /**
  51. * Get a validator for an incoming registration request.
  52. *
  53. * @param array $data
  54. * @return \Illuminate\Contracts\Validation\Validator
  55. */
  56. protected function validator(array $data)
  57. {
  58. return Validator::make($data, [
  59. 'name' => 'required|string|max:255',
  60. 'password' => 'required|string|min:6|confirmed',
  61. ]);
  62. }
  63. /**
  64. * Create a new user instance after a valid registration.
  65. *
  66. * @param array $data
  67. * @return \App\Models\User
  68. */
  69. protected function create(array $data)
  70. {
  71. $userlist=User::where('id',$data['invit'])->get();
  72. $li=[];
  73. if($userlist){ //查询上级ID是否存在
  74. $userlist=$userlist[0];
  75. $li=[
  76. 'leader_id'=>$userlist['id'],
  77. 'tpath'=>!empty($userlist['tpath']) ? $userlist['tpath'].','.$userlist['id'] : $userlist['id'],
  78. 'invit_1'=>$userlist['id'],
  79. 'invit_2'=>!empty($userlist['invit_1']) ? $userlist['invit_1'] : '',
  80. 'invit_3'=>!empty($userlist['invit_2']) ? $userlist['invit_2'] : '',
  81. 'pid'=>$userlist['id'],
  82. ];
  83. }
  84. if (filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { //邮箱注册
  85. $lis=[
  86. 'name' => $data['name'],
  87. 'email' => $data['email'],
  88. 'password' => Hash::make($data['password']),
  89. ];
  90. $l=array_merge($lis,$li);
  91. $user = User::create($l);
  92. }else { //手机注册
  93. if (addon_is_activated('otp_system')){
  94. $lis=[
  95. 'name' => $data['name'],
  96. 'phone' => '+'.$data['country_code'].$data['phone'],
  97. 'password' => Hash::make($data['password']),
  98. 'verification_code' => rand(100000, 999999)
  99. ];
  100. $l=array_merge($lis,$li);
  101. $user = User::create($l);
  102. $otpController = new OTPVerificationController;
  103. $otpController->send_code($user);
  104. }
  105. }
  106. if(session('temp_user_id') != null){
  107. Cart::where('temp_user_id', session('temp_user_id'))
  108. ->update([
  109. 'user_id' => $user->id,
  110. 'temp_user_id' => null
  111. ]);
  112. Session::forget('temp_user_id');
  113. }
  114. if(Cookie::has('referral_code')){
  115. $referral_code = Cookie::get('referral_code');
  116. $referred_by_user = User::where('referral_code', $referral_code)->first();
  117. if($referred_by_user != null){
  118. $user->referred_by = $referred_by_user->id;
  119. $user->pid = $referred_by_user->id;
  120. $user->save();
  121. }
  122. }
  123. return $user;
  124. }
  125. public function register(Request $request)
  126. {
  127. if (filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
  128. if(User::where('email', $request->email)->first() != null){
  129. flash(translate('Email or Phone already exists.'));
  130. return back();
  131. }
  132. }
  133. elseif (User::where('phone', '+'.$request->country_code.$request->phone)->first() != null) {
  134. flash(translate('Phone already exists.'));
  135. return back();
  136. }
  137. if($request->invit==null){
  138. flash(translate('The invitation code must not be empty'));
  139. return back();
  140. }
  141. $this->validator($request->all())->validate();
  142. $user = $this->create($request->all());
  143. $this->guard()->login($user);
  144. $osn = 'YQ'.date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  145. if($user->invit_1 != null){ //一级分佣
  146. $invit_1=BusinessSetting::where('type', 'commission_people_level_1')->first()->value;
  147. User::where('id', $user->invit_1)->increment('balance', $invit_1);
  148. //写入日志
  149. $affiliate_log = new AffiliateLog;
  150. $affiliate_log->user_id = $user->id;
  151. $affiliate_log->referred_by_user = $user->invit_1;
  152. $affiliate_log->amount = $invit_1;
  153. $affiliate_log->order_id = $osn;
  154. $affiliate_log->note = 'UserID:'.$user->invit_1.'Invite new users:'.$user->name.'('.$user->email.') Register,First level commission bonus $'.$invit_1;
  155. $affiliate_log->affiliate_type = 'user_registration_first';
  156. $affiliate_log->save();
  157. if($user->invit_2 != null){ //二级分佣
  158. $invit_2=BusinessSetting::where('type', 'commission_people_level_2')->first()->value;
  159. User::where('id', $user->invit_2)->increment('balance', $invit_2);
  160. //写入日志
  161. $affiliate_log = new AffiliateLog;
  162. $affiliate_log->user_id = $user->id;
  163. $affiliate_log->referred_by_user = $user->invit_2;
  164. $affiliate_log->amount = $invit_2;
  165. $affiliate_log->order_id = $osn;
  166. $affiliate_log->note = 'UserID:'.$user->invit_1.'Invite new users:'.$user->name.'('.$user->email.') Register,Second level commission bonus $'.$invit_2;
  167. $affiliate_log->affiliate_type = 'user_registration_first';
  168. $affiliate_log->save();
  169. if($user->invit_3 != null){ //三级分佣
  170. $invit_3=BusinessSetting::where('type', 'commission_people_level_3')->first()->value;
  171. User::where('id', $user->invit_3)->increment('balance', $invit_3);
  172. //写入日志
  173. $affiliate_log = new AffiliateLog;
  174. $affiliate_log->user_id = $user->id;
  175. $affiliate_log->referred_by_user = $user->invit_3;
  176. $affiliate_log->amount = $invit_3;
  177. $affiliate_log->order_id = $osn;
  178. $affiliate_log->note = 'UserID:'.$user->invit_1.'Invite new users:'.$user->name.'('.$user->email.') Register,Three level commission bonus $'.$invit_3;
  179. $affiliate_log->affiliate_type = 'user_registration_first';
  180. $affiliate_log->save();
  181. }
  182. }
  183. }
  184. if($user->email != null){
  185. if(BusinessSetting::where('type', 'email_verification')->first()->value != 1){
  186. $user->email_verified_at = date('Y-m-d H:m:s');
  187. $user->save();
  188. flash(translate('Registration successful.'))->success();
  189. }
  190. else {
  191. try {
  192. $user->sendEmailVerificationNotification();
  193. flash(translate('Registration successful. Please verify your email.'))->success();
  194. } catch (\Throwable $th) {
  195. $user->delete();
  196. flash(translate('Registration failed. Please try again later.'))->error();
  197. }
  198. }
  199. }
  200. return $this->registered($request, $user)
  201. ?: redirect($this->redirectPath());
  202. }
  203. protected function registered(Request $request, $user)
  204. {
  205. if ($user->email == null) {
  206. return redirect()->route('verification');
  207. }elseif(session('link') != null){
  208. return redirect(session('link'));
  209. }else {
  210. return redirect()->route('home');
  211. }
  212. }
  213. }