UPDATE API REGISTER

main
nocode 1 year ago
parent e5c4e6b6e5
commit 5b3fcc0eff
  1. 40
      app/Http/Controllers/Api/AuthApiController.php
  2. 1
      app/Models/Agent.php
  3. 1
      app/Models/AgentUser.php

@ -14,6 +14,10 @@ use App\Models\AgentUser;
class AuthApiController extends BaseAuthApiController class AuthApiController extends BaseAuthApiController
{ {
public $_default_usertype = 'agents'; public $_default_usertype = 'agents';
public $_default_grant_two = 'agents';
const AGENTS = 'agents';
const GENERAL = 'general';
// //
public function login(Request $request) public function login(Request $request)
@ -45,12 +49,14 @@ class AuthApiController extends BaseAuthApiController
'phone' => $user->phone, 'phone' => $user->phone,
'last_login' => $user->last_login, 'last_login' => $user->last_login,
]; ];
//Find Agent USer
$agentUser = AgentUser::where('user_id', $user->id)->latest()->first(); $agentUser = AgentUser::where('user_id', $user->id)->latest()->first();
if (!empty($agentUser->agent_root_id)) { if (!empty($agentUser->agent_root_id)) {
// Agent User Root
$agentRootInfo = Agent::find($agentUser->agent_root_id); $agentRootInfo = Agent::find($agentUser->agent_root_id);
} }
if (!empty($agentUser->agent_id)) { if (!empty($agentUser->agent_id)) {
// Agent User Current
$myAgent = Agent::find($agentUser->agent_id); $myAgent = Agent::find($agentUser->agent_id);
} }
@ -60,6 +66,7 @@ class AuthApiController extends BaseAuthApiController
$data['downline_register'] = $this->getLinkParam('downline_register', $phone); $data['downline_register'] = $this->getLinkParam('downline_register', $phone);
$data['promotional_link'] = $this->getLinkParam('promotional_link', $phone); $data['promotional_link'] = $this->getLinkParam('promotional_link', $phone);
$data['role'] = $user->user_type; $data['role'] = $user->user_type;
// Creating a token without scopes... // Creating a token without scopes...
$token = $user->createToken($user->id . ' token ' . time(), ['*'])->accessToken; $token = $user->createToken($user->id . ' token ' . time(), ['*'])->accessToken;
@ -80,19 +87,41 @@ class AuthApiController extends BaseAuthApiController
if ($validator->fails()) { if ($validator->fails()) {
return $this->sendError('Validation Error.', $validator->errors()); return $this->sendError('Validation Error.', $validator->errors());
} }
if (empty($request->agents_code)) {
return response()->json(['status' => true, 'message' => 'Đăng ký thất bại, vui lòng gửi lên mã đại lý.']);
}
$checkUserAgent = AgentUser::where('code', '=', $request->agents_code)->first();
if (empty($checkUserAgent)) {
return response()->json(['status' => true, 'message' => 'Đăng ký thất bại, Không tìm thấy đại lý.']);
}
$dataInsert = $this->transformRegisterData($request->all()); $dataInsert = $this->transformRegisterData($request->all());
if (!empty($dataInsert)) { if (!empty($dataInsert)) {
$user = \App\Models\User::insert($dataInsert); $userId = \App\Models\User::insertGetId($dataInsert);
if ($userId) {
$dataAgentInsert = [
'name' => $dataInsert['name']??'',
'type' => self::AGENTS,
'grant' => $this->_default_grant_two
];
$agentInsertId = Agent::insertGetId($dataAgentInsert);
if($agentInsertId){
AgentUser::insert(['user_id' => $userId, 'agent_id' => $agentInsertId, 'status' => 1, 'code' => $dataInsert['phone']]);
};
}
return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]); return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]);
} }
return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]); return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]);
} }
public function logout(Request $request){ public function logout(Request $request)
{
$token = $request->user()->token(); $token = $request->user()->token();
// expried token // expried token
$token->revoke(); $token->revoke();
// response // Response
return response()->json([ return response()->json([
'status' => true, 'status' => true,
'message' => 'Đăng xuất thành công.' 'message' => 'Đăng xuất thành công.'
@ -147,7 +176,8 @@ class AuthApiController extends BaseAuthApiController
} }
return null; return null;
} }
function getLinkParam($type, $phone){ function getLinkParam($type, $phone)
{
$arr = [ $arr = [
'downline_register' => 'daisu.sundayenglish.com/dk', 'downline_register' => 'daisu.sundayenglish.com/dk',
'promotional_link' => 'thongtin.sundayenglish.com' 'promotional_link' => 'thongtin.sundayenglish.com'

@ -14,7 +14,6 @@ class Agent extends Model
'id', 'id',
'name', 'name',
'root_path', 'root_path',
'code',
'grant', 'grant',
'type', 'type',
'phone', 'phone',

@ -15,6 +15,7 @@ class AgentUser extends Model
'user_id', 'user_id',
'agent_root_id', 'agent_root_id',
'root_id', 'root_id',
'code',
'status', 'status',
'created_by', 'created_by',
'updated_by', 'updated_by',

Loading…
Cancel
Save