207 lines
8.1 KiB
PHP
207 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Cnt;
|
|
use App\Models\User;
|
|
use App\Models\Customer;
|
|
use App\Models\Setting;
|
|
use App\Models\Message;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
|
use Illuminate\Routing\Controller as BaseController;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
|
|
|
|
class Controller extends BaseController
|
|
{
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
public function resultCreate($id){
|
|
return response()->json(['id'=>$id] , 201);
|
|
}
|
|
/**
|
|
* add paginate to list
|
|
* @param array $list
|
|
* @param object $table
|
|
* @return array mixed
|
|
*/
|
|
protected function addPaginate($table,$list){
|
|
$list = $this->addLimitPage($list);
|
|
$list['count'] = $table->count();
|
|
$list['rows'] = $table->offset( $list['page']*$list['limit'])
|
|
->limit($list['limit'])
|
|
->get();
|
|
$list['page'] = (int)$list['page'];
|
|
$list['limit'] = (int)$list['limit'];
|
|
return $list;
|
|
}
|
|
/**
|
|
* add page and limit to list
|
|
* @param $list
|
|
* @return mixed
|
|
*/
|
|
protected function addLimitPage($list){
|
|
if (!isset($list['page'])){
|
|
$list['page'] = 0;
|
|
}
|
|
if (!isset($list['limit'])){
|
|
$list['limit'] = 5;
|
|
}
|
|
if ($list['limit']>50){
|
|
$list['limit'] = 50;
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
public static function Send($to , $msg, $is_customer = true)
|
|
{
|
|
$r = self::SendMessage($to , $msg, $is_customer);
|
|
// if ($r == 200){
|
|
// Sms::set($to,$msg , $is_customer);
|
|
// }
|
|
}
|
|
|
|
public static function SendMessage($to, $msg ,$is_send = false,$force_method ='null')
|
|
{
|
|
$config = config('message', false);
|
|
$credentials = [
|
|
"telegram" => [
|
|
"token" => self::Config('telegram_bot_token')
|
|
],
|
|
"bale" => [
|
|
"token" => self::Config('bale_bot_token')
|
|
// "token" => Setting::where('key', 'bale_bot_token')->where('is_active', true)->first()->value ?? null
|
|
],
|
|
"sms" => [
|
|
'user_name' => 'ysms1362',
|
|
'password' => 'tL4w0PfNy2()',
|
|
'sender' => '3000164545',
|
|
],
|
|
];
|
|
$platforms = [];
|
|
$send_sms = true;
|
|
$user = User::where('mobile', $to)->first() ?: Customer::where('mobile', $to)->first();
|
|
if ($user) {
|
|
//
|
|
// // Telegram
|
|
// if (isset($credentials['telegram']) && $user->telegram) {
|
|
// try {
|
|
// $res = Http::get("https://api.wwwapp.ir/telegran_send.php", [
|
|
// 'botToken' => $credentials['telegram']['token'],
|
|
// 'chatId' => $user->telegram,
|
|
// 'message' => "جناب {$user->name} {$msg}",
|
|
// ]);
|
|
// $data = $res->json();
|
|
// if (isset($data['success']) && $data['success'] === true) {
|
|
// $platforms[] = "telegram:{$user->telegram}";
|
|
// $send_sms = false;
|
|
// }
|
|
// } catch (\Exception $e) {
|
|
// }
|
|
// }
|
|
//
|
|
// Bale
|
|
if (isset($credentials['bale']) && $user->bale) {
|
|
try {
|
|
Http::withoutVerifying()->get("https://tapi.bale.ai/{$credentials['bale']['token']}/sendMessage", [
|
|
'chat_id' => $user->bale,
|
|
'text' => "جناب {$user->name} \n{$msg}",
|
|
]);
|
|
$platforms[] = "bale:{$user->bale}";
|
|
$send_sms = false;
|
|
} catch (\Exception $e) {
|
|
return $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
//SMS
|
|
if ($send_sms or $force_method=="sms") {
|
|
$endpoint = "https://www.0098sms.com/sendsmslink.aspx";
|
|
$response = Http::withOptions([
|
|
'curl' => [
|
|
// اجبار به استفاده از IPv4 و جلوگیری از تاخیر IPv6
|
|
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
|
// افزایش سرعت برقراری ارتباط TCP
|
|
CURLOPT_TCP_FASTOPEN => 1,
|
|
]
|
|
])->withoutVerifying()->get($endpoint, [
|
|
'FROM' => $credentials['sms']['sender'],
|
|
'TO' => $to,
|
|
'TEXT' => trim($msg),
|
|
'USERNAME' => $credentials['sms']['user_name'],
|
|
'PASSWORD' => $credentials['sms']['password'],
|
|
'DOMAIN' => '0098',
|
|
]);
|
|
$platforms[] = "0098:{$to}";
|
|
|
|
}
|
|
|
|
Message::create([
|
|
'sms' => $msg,
|
|
'receiver' => $to,
|
|
'platforms' => implode(', ', $platforms), // ذخیره لیست سامانهها
|
|
]);
|
|
}
|
|
public static function SendTelegram($to , $msg)
|
|
{
|
|
// "chatId" => 504951108,
|
|
// "chatId" => 5870340227, fathi
|
|
$response = Http::withHeaders([
|
|
'x-secret-key' => 'PvQM36mgk2FUELHjcm4YG49Nu56uQxot',
|
|
])->post('https://tele.ygram.ir/send-message', ["message" => $msg, "chatId" => $to, "botToken" => 'Paramisgold_bot']);
|
|
return $response->json();
|
|
}
|
|
public static function Config($name)
|
|
{
|
|
return Setting::where('key',$name)->first()->value ?? false;
|
|
}
|
|
|
|
public static function jibit($method, $p1 = null, $p2 = null, $p3 = null)
|
|
{
|
|
$counter = Cnt::where('key', 'jibit_counter')->first();
|
|
if(empty($counter)){
|
|
$counter = new Cnt();
|
|
$counter->key = "jibit_counter";
|
|
$counter->value = 0;
|
|
$counter->save();
|
|
}
|
|
|
|
$apiData['refresh'] = Http::withoutVerifying()->withoutVerifying()->withToken(Storage::get('refreshTokenGibit'))
|
|
->post("https://napi.jibit.ir/ide/v1/tokens/generate", ['apiKey' => "J5WxrPxPOg", 'secretKey' => "7dgeE70xQXsQL7r36j3wif6X9",])->json();
|
|
Storage::put('accessTokenGibit', $apiData['refresh']['accessToken']);
|
|
|
|
if (!Storage::exists('accessTokenGibit') || !Storage::exists('refreshTokenGibit')) {
|
|
Storage::put('accessTokenGibit', 'توکن اولیه');
|
|
Storage::put('refreshTokenGibit', 'توکن رفرش اولیه');
|
|
}
|
|
|
|
$apiData['cartMatching'] = Http::withoutVerifying()->withToken(Storage::get('accessTokenGibit'))->get("https://napi.jibit.ir/ide/v1/services/matching?cardNumber=$p1&nationalCode=$p2&birthDate=$p3")->json();
|
|
if ((isset($apiData['cartMatching']['code'])) and $apiData['cartMatching']['code'] == "forbidden") {
|
|
$apiData['refresh'] = Http::withoutVerifying()->withToken(Storage::get('refreshTokenGibit'))
|
|
->post("https://napi.jibit.ir/ide/v1/tokens/generate", ['apiKey' => "J5WxrPxPOg", 'secretKey' => "7dgeE70xQXsQL7r36j3wif6X9",])->json();
|
|
Storage::put('accessTokenGibit', $apiData['refresh']['accessToken']);
|
|
Storage::put('refreshTokenGibit', $apiData['refresh']['refreshToken']);
|
|
$apiData['cartMatching'] = Http::withoutVerifying()->withToken(Storage::get('accessTokenGibit'))->get("https://napi.jibit.ir/ide/v1/services/matching?cardNumber=$p1&nationalCode=$p2&birthDate=$p3")->json();
|
|
}
|
|
//update jibit counter
|
|
$counter->value = (int)$counter->value + 1;
|
|
$counter->save();
|
|
|
|
if (isset( $apiData['cartMatching']['matched']) and $apiData['cartMatching']['matched'] == true) {
|
|
$apiData['cartData'] = Http::withoutVerifying()->withToken(Storage::get('accessTokenGibit'))->get("https://napi.jibit.ir/ide/v1/cards?number=$p1&iban=true")->json();
|
|
return $apiData;
|
|
}elseif(isset( $apiData['cartMatching']['matched']) and $apiData['cartMatching']['matched'] == false){
|
|
return $apiData;
|
|
} else {
|
|
throw new UnprocessableEntityHttpException($apiData['cartMatching']['message']);
|
|
}
|
|
}
|
|
|
|
}
|