main
nocode 1 year ago
parent f5b15c6e69
commit 44d13d58da
  1. 32
      app/Http/Controllers/Api/AuthApiController.php

@ -29,11 +29,11 @@ class AuthApiController extends BaseAuthApiController
} else { } else {
$user = User::where('phone', $request->username)->first(); $user = User::where('phone', $request->username)->first();
} }
if(empty($user)){ if (empty($user)) {
return response()->json(['status' =>false, 'msg' => 'Email hoặc SĐT chưa được đăng ký.'], parent::HTTP_OK); return response()->json(['status' => false, 'msg' => 'Email hoặc SĐT chưa được đăng ký.'], parent::HTTP_OK);
} }
if (!Auth::attempt(['id' => $user->id, 'password' => $request->password])) { if (!Auth::attempt(['id' => $user->id, 'password' => $request->password])) {
return response()->json(['status' =>false, 'msg' =>'Tài khoản hoặc mật khẩu không chính xác. Vui lòng thử lại.'], parent::HTTP_OK); return response()->json(['status' => false, 'msg' => 'Tài khoản hoặc mật khẩu không chính xác. Vui lòng thử lại.'], parent::HTTP_OK);
} }
$user = User::find($user->id); $user = User::find($user->id);
@ -56,6 +56,9 @@ class AuthApiController extends BaseAuthApiController
if (!empty($agentUser->agent_root_id)) { if (!empty($agentUser->agent_root_id)) {
// Agent User Root // Agent User Root
$agentRootInfo = Agent::find($agentUser->agent_root_id); $agentRootInfo = Agent::find($agentUser->agent_root_id);
$agentUserRoot = AgentUser::where('agent_id', $agentUser->agent_root_id)->where('agent_root_id', $agentUser->agent_root_id)->latest()->first();
} }
if (!empty($agentUser->agent_id)) { if (!empty($agentUser->agent_id)) {
// Agent User Current // Agent User Current
@ -64,7 +67,7 @@ class AuthApiController extends BaseAuthApiController
$phone = $user->phone ?? null; $phone = $user->phone ?? null;
$data['general_agents_code'] = $agentUser->code ?? ''; $data['general_agents_code'] = $agentUserRoot->code ?? '';
$data['general_agents_name'] = $agentRootInfo->name ?? ''; $data['general_agents_name'] = $agentRootInfo->name ?? '';
$data['downline_register'] = $this->getLinkParam('downline_register', $agentUser->code); $data['downline_register'] = $this->getLinkParam('downline_register', $agentUser->code);
$data['promotional_link'] = $this->getLinkParam('promotional_link', $agentUser->code); $data['promotional_link'] = $this->getLinkParam('promotional_link', $agentUser->code);
@ -95,19 +98,19 @@ class AuthApiController extends BaseAuthApiController
// } // }
$checkUserAgent = AgentUser::where('code', '=', $request->agents_code)->first(); $checkUserAgent = AgentUser::where('code', '=', $request->agents_code)->first();
if (empty($checkUserAgent)) { if (empty($checkUserAgent)) {
return response()->json(['status' => false, 'msg' => 'Đăng ký thất bại, Không tìm thấy đại lý.']); return response()->json(['status' => false, 'msg' => 'Đăng ký thất bại, Không tìm thấy đại lý.']);
} }
$lastIndex = AgentUser::latest()->first(); $lastIndex = AgentUser::latest()->first();
$lastCode = $lastIndex->code??null; $lastCode = $lastIndex->code ?? null;
if(empty($lastCode)){ if (empty($lastCode)) {
$lastCode = "A99"; $lastCode = "A99";
} }
$agentCodeNew = $this->generateNewCode($lastCode); $agentCodeNew = $this->generateNewCode($lastCode);
$dataInsert = $this->transformRegisterData($request->all()); $dataInsert = $this->transformRegisterData($request->all());
if (!empty($dataInsert)) { if (!empty($dataInsert)) {
@ -126,7 +129,7 @@ class AuthApiController extends BaseAuthApiController
'user_id' => $userId, 'user_id' => $userId,
'agent_id' => $agentInsertId, 'agent_id' => $agentInsertId,
'status' => 1, 'status' => 1,
'code' => $agentCodeNew??'', 'code' => $agentCodeNew ?? '',
'agent_root_id' => $checkUserAgent->agent_root_id 'agent_root_id' => $checkUserAgent->agent_root_id
]); ]);
} }
@ -137,14 +140,15 @@ class AuthApiController extends BaseAuthApiController
return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]); return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]);
} }
public function generateNewCode($code){ public function generateNewCode($code)
{
$newCode = ''; $newCode = '';
if(!empty($code)){ if (!empty($code)) {
$latestCodeNumber = (int) substr($code, 1); $latestCodeNumber = (int) substr($code, 1);
$newCodeNumber = $latestCodeNumber + 1; $newCodeNumber = $latestCodeNumber + 1;
$newCode = 'A' . str_pad($newCodeNumber, 3, '0', STR_PAD_LEFT); $newCode = 'A' . str_pad($newCodeNumber, 3, '0', STR_PAD_LEFT);
} }
return $newCode; return $newCode;
} }
public function logout(Request $request) public function logout(Request $request)
{ {

Loading…
Cancel
Save