You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

202 lines
7.0 KiB

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Api\BaseAuthApiController as BaseAuthApiController;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Models\Agent;
use App\Models\AgentUser;
class AuthApiController extends BaseAuthApiController
{
public $_default_usertype = 'agents';
public $_default_grant_two = '2';
const AGENTS = 'agents';
const GENERAL = 'general';
//
public function login(Request $request)
{
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
'username' => 'required|email',
'password' => 'required'
]);
if ($validator->fails()) {
return $this->sendError('Validation Error.', $validator->errors());
}
if (!Auth::attempt(['email' => $request->username, 'password' => $request->password])) {
return $this->sendError('Unauthorised.', ['error' => 'Unauthorised']);
}
$user = User::where('email', $request->username)->first();
if (empty($user)) {
$this->sendError('Error: ', ['error' => 'Email invalid']);
}
$data = [
'id' => $user->id,
'address' => $user->address,
'avatar' => $user->avatar,
'birthday' => $user->birthday,
'email' => $user->email,
'fullname' => $user->name,
'gender' => $this->getGenderText($user->gender),
'phone' => $user->phone,
'last_login' => $user->last_login,
];
//Find Agent USer
$agentUser = AgentUser::where('user_id', $user->id)->latest()->first();
if (!empty($agentUser->agent_root_id)) {
// Agent User Root
$agentRootInfo = Agent::find($agentUser->agent_root_id);
}
if (!empty($agentUser->agent_id)) {
// Agent User Current
$myAgent = Agent::find($agentUser->agent_id);
}
$phone = $user->phone ?? null;
$data['general_agents_code'] = $agentRootInfo->code ?? '';
$data['general_agents_name'] = $agentRootInfo->name ?? '';
$data['downline_register'] = $this->getLinkParam('downline_register', $phone);
$data['promotional_link'] = $this->getLinkParam('promotional_link', $phone);
$data['role'] = $user->user_type;
// Creating a token without scopes...
$token = $user->createToken($user->id . ' token ' . time(), ['*'])->accessToken;
$data['access_token'] = $token;
$data['token_type'] = 'Bearer';
$data['expires_in'] = 1296000;
$user->update(['last_login' => new \DateTime()]);
return response()->json($data, 200);
}
public function register(Request $request)
{
$validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
'email' => 'required|email',
'fullname' => 'required'
]);
if ($validator->fails()) {
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());
if (!empty($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'],
'agent_root_id' => $checkUserAgent->agent_root_id
]);
}
;
}
return response()->json(['status' => true, 'msg' => $this->getMessageReponse(1)]);
}
return response()->json(['status' => false, 'msg' => $this->getMessageReponse(2)]);
}
public function logout(Request $request)
{
$token = $request->user()->token();
// expried token
$token->revoke();
// Response
return response()->json([
'status' => true,
'message' => 'Đăng xuất thành công.'
]);
}
function transformRegisterData($data)
{
$finalData = [];
if (!empty($data['email'])) {
$finalData['email'] = $data['email'];
}
if (!empty($data['address'])) {
$finalData['address'] = $data['address'];
}
if (!empty($data['birthday'])) {
$finalData['birthday'] = $data['birthday'];
}
if (!empty($data['fullname'])) {
$finalData['name'] = $data['fullname'];
}
if (!empty($data['phone'])) {
$finalData['phone'] = $data['phone'];
}
$finalData['password'] = !empty($data['password']) ? Hash::make($data['password']) : Hash::make('1qaz2wsxA@');
if (!empty($data['gender'])) {
$finalData['gender'] = !empty($data['gender']) && $data['gender'] == 'male' ? 1 : 0;
}
$finalData['user_type'] = !empty($data['role']) ? $data['role'] : $this->_default_usertype;
return $finalData;
}
function getMessageReponse($key)
{
$msg = [
1 => "Tạo tài khoản thành công.",
2 => "Tạo tài khoản thất bại.",
3 => "Email đã tồn tại.",
4 => "Email không hợp lệ.",
5 => "Mật khẩu phải có ít nhất 8 ký tự, bao gồm chữ cái và số.",
6 => "Tên đăng nhập phải có ít nhất 6 ký"
];
return $msg[$key] ?? '';
}
function getGenderText($gender)
{
if ($gender == 1) {
return 'male';
}
if ($gender == 2) {
return 'female';
}
return null;
}
function getLinkParam($type, $phone)
{
$arr = [
'downline_register' => 'daisu.sundayenglish.com/dk',
'promotional_link' => 'thongtin.sundayenglish.com'
];
$domain = $arr[$type] ?? '';
if (empty($phone)) {
return $domain;
} else {
$params = http_build_query(['mds' => $phone]);
return $domain . '?' . $params;
}
}
}