Browse Source

Merge branch 'mahdi' of https://gitea.hooradev.ir/mahdihty/liwo into mohammad

mohammad
Mohammad Akbari 4 years ago
parent
commit
21ad27151b
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 115
      app/Channels/Messages/SmsMessage.php
  2. 74
      app/Channels/SmsChannel.php
  3. 12
      app/Enums/sms.php
  4. 8
      app/Listeners/NotifHandler.php
  5. 4
      app/Models/Business.php
  6. 10
      app/Models/User.php
  7. 2
      app/Notifications/DBNotification.php
  8. 17
      app/Notifications/SmsNotification.php
  9. 4
      app/Notifications/SocketNotification.php
  10. 13
      app/Providers/AppServiceProvider.php
  11. 15
      app/Utilities/HelperClass/NotificationHelper.php
  12. 16
      config/smsirlaravel.php
  13. 1
      database/migrations/2021_03_08_114700_create_notifications_table.php
  14. 12
      resources/lang/fa/notification.php
  15. 4
      routes/api.php

115
app/Channels/Messages/SmsMessage.php

@ -0,0 +1,115 @@
<?php
namespace App\Channels\Messages;
class SmsMessage
{
/**
* The devices token to send the message from.
*
* @var array|string
*/
public $to;
/**
* The data of the Sms message.
*
* @var array
*/
public $data;
/**
* The params that we define in sms.ir template
* when we use ultraFastSend method
*
* @var array
*/
public $params;
/**
* The id of template we define in sms.ir
* when we use ultraFastSend method
*
* @var string
*/
public $template_id;
/**
* The generated verification code.
* when we use sendVerification method.
*
* @var string
*/
public $verification_code;
/**
* Set the devices token to send the message from.
*
* @param array|string $to
* @return $this
*/
public function to($to)
{
$this->to = $to;
return $this;
}
/**
* Set the data of the sms message.
*
* @param array $data
* @return $this
*/
public function data(array $data)
{
$this->data = $data;
return $this;
}
/**
* Set the params of the sms message when we use ultraFastSend method
*
* @param array|mixed $params
* @return $this
*/
public function params($params)
{
foreach ($params as $key => $value) {
$this->params[] = ['Parameter' => $key, 'ParameterValue' => $value];
}
return $this;
}
/**
* Set the template_id of the sms message when we use ultraFastSend method
*
* @param string $template_id
* @return $this
*/
public function templateId($template_id)
{
$this->template_id = $template_id;
return $this;
}
/**
* Set verification_code of sms message when we use sendVerification method
*
* @param string $verification_code
* @return $this
*/
public function verificationCode(string $verification_code)
{
$this->verification_code = $verification_code;
return $this;
}
}

74
app/Channels/SmsChannel.php

@ -3,18 +3,33 @@
namespace App\Channels;
use App\Channels\Messages\SmsMessage;
use Illuminate\Notifications\Notification;
use GuzzleHttp\Client as HttpClient;
class SmsChannel
{
/**
* The API URL for Socket.
* The API URL for sms.
*
* @var string
*/
protected $sms_url;
/**
* The api key for sms inside sms.ir panel.
*
* @var string
*/
protected $api_key;
/**
* The secret key for sms inside sms.ir panel.
*
* @var string
*/
protected $secret_key;
/**
* The HTTP client instance.
*
@ -28,10 +43,12 @@ class SmsChannel
* @param \GuzzleHttp\Client $http
* @return void
*/
public function __construct(HttpClient $http, string $sms_url)
public function __construct(HttpClient $http, string $sms_url, string $api_key, string $secret_key)
{
$this->http = $http;
$this->sms_url = $sms_url;
$this->api_key = $api_key;
$this->secret_key = $secret_key;
}
/**
@ -48,21 +65,68 @@ class SmsChannel
$message->to($notifiable->routeNotificationFor('sms', $notification));
if (! $message->to) {
if (! $message->to || (! ($message->params && $message->template_id) && ! $message->verification_code)) {
return;
}
try {
$this->http->post($this->sms_url . 'api/' . $type , [
'headers' => [
'x-sms-ir-secure-token'=>self::getToken()
'x-sms-ir-secure-token'=> $this->getToken()
],
'connect_timeout' => 30,
'json' => ['data' => $message->data],
'json' => $this->buildJsonPayload($message, $type),
]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
report($e);
}
}
/**
* This method used in every request to get the token at first.
*
* @return mixed - the Token for use api
*/
protected function getToken()
{
$body = [
'UserApiKey' => $this->api_key,
'SecretKey' => $this->secret_key,
'System' => 'laravel_v_1_4'
];
try {
$result = $this->http->post( $this->sms_url . 'api/Token',['json'=>$body,'connect_timeout'=>30]);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
report($e);
return;
}
return json_decode($result->getBody(),true)['TokenKey'];
}
/**
* Create sms json payload based on type
*
* @param SmsMessage $message
* @param $type
* @return array
*/
protected function buildJsonPayload(SmsMessage $message, $type) {
if ($type === 'UltraFastSend') {
return[
'ParameterArray' => $message->params,
'TemplateId' => $message->template_id,
'Mobile' => $message->to
];
}
if ($type === 'VerificationCode') {
return[
'Code' => $message->data,
'MobileNumber' => $message->to
];
}
}
}

12
app/Enums/sms.php

@ -0,0 +1,12 @@
<?php
return [
'types' => [
'verification_code' => [
'name' => 'VerificationCode'
],
'ultra_fast_send' => [
'name' => 'ultraFastSend'
]
],
];

8
app/Listeners/NotifHandler.php

@ -3,8 +3,10 @@
namespace App\Listeners;
use App\Events\ModelSaved;
use App\Notifications\SocketNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
class NotifHandler
{
@ -29,8 +31,12 @@ class NotifHandler
$message = json_decode($event->message);
$event_class = 'App\Events\\'.enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name');
if (class_exists($event_class)) {
// event(new ('App\Events\\'.$event_class($message)));
$event_class::dispatch($message);
}
Notification::send(auth()->user(), new SocketNotification(
[
'message' => enum('tables.'.$message->data->table_name.'.singular_name').enum('cruds.inverse.'.$message->data->crud_id.'.name'),
'payload'=>$business_info
]));
}
}

4
app/Models/Business.php

@ -5,6 +5,8 @@ namespace App\Models;
use App\Models\File;
use App\Models\Model;
use App\Models\SoftDeletes;
use App\Notifications\SocketNotification;
use Illuminate\Support\Facades\Notification;
use Illuminate\Validation\Rule;
use Illuminate\Http\UploadedFile;
use Spatie\MediaLibrary\HasMedia;
@ -234,6 +236,8 @@ class Business extends Model implements HasMedia
Cache::put('business_info'.$businessId , $business_info, config('app.cache_ttl'));
Notification::send(auth()->user(), new SocketNotification(['message' => 'business info update','payload'=>$business_info]));
return $business_info;
}

10
app/Models/User.php

@ -60,6 +60,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
return request('_business_info')['id'] ?? null;
}
/**
* Specifies the user's phone number
*
* @return string
*/
public function routeNotificationForSms()
{
return $this->mobile;
}
public function updateRelations()
{
// projects relations

2
app/Notifications/DBNotification.php

@ -29,7 +29,7 @@ class DBNotification extends Notification
*/
public function via($notifiable)
{
return ['db'];
return ['database'];
}
/**

17
app/Notifications/SmsNotification.php

@ -2,6 +2,7 @@
namespace App\Notifications;
use App\Channels\Messages\SmsMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
@ -14,6 +15,12 @@ class SmsNotification extends Notification
public $message;
/**
* Show type of sms notification.
* [ultraFastSend or sendVerification ]
*
* @var mixed|string
*/
public $type;
/**
@ -21,10 +28,10 @@ class SmsNotification extends Notification
*
* @return void
*/
public function __construct($message, $type = 'MessageSend')
public function __construct($message, $type = null)
{
$this->message = $message;
$this->type = $type;
$this->type = $type ?? enum('sms.types.verification_code.name');
}
/**
@ -46,9 +53,11 @@ class SmsNotification extends Notification
*/
public function toSms($notifiable)
{
return [
return (new SmsMessage())
->params($this->message['params'] ?? [])
->templateId($this->message['template_id'] ?? null)
->verificationCode($this->message['verification_code'] ?? null);
];
}
public function getType()

4
app/Notifications/SocketNotification.php

@ -45,8 +45,8 @@ class SocketNotification extends Notification
{
return (new SocketMessage())
->data([
'title' => $this->message['title'],
'body' => $this->message['body'],
'message' => $this->message['message'],
'payload' => $this->message['payload'],
]);
}
}

13
app/Providers/AppServiceProvider.php

@ -4,6 +4,7 @@ namespace App\Providers;
use App\Channels\DBChannel;
use App\Channels\FcmChannel;
use App\Channels\SmsChannel;
use App\Channels\SocketChannel;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
@ -26,8 +27,16 @@ class AppServiceProvider extends ServiceProvider
$service->extend('socket', function ($app) {
return new SocketChannel(new HttpClient, config('socket.url'));
});
$service->extend('db', function ($app) {
return new DBChannel();
// $service->extend('db', function ($app) {
// return new DBChannel();
// });
$service->extend('sms', function ($app) {
return new SmsChannel(
new HttpClient,
config('smsirlaravel.webservice-url'),
config('smsirlaravel.api-key'),
config('smsirlaravel.secret-key'),
);
});
});
}

15
app/Utilities/HelperClass/NotificationHelper.php

@ -8,11 +8,14 @@ use App\Notifications\DBNotification;
use App\Notifications\FcmNotification;
use App\Notifications\MailNotification;
use App\Notifications\SocketNotification;
use App\Notifications\SmsNotification;
use Illuminate\Support\Facades\Notification;
class NotificationHelper
{
protected $notif;
protected $sms_notif;
/**
* Make notification object
*
@ -33,6 +36,12 @@ class NotificationHelper
return $this;
}
public function makeSmsNotif($template_name, $options = [])
{
$this->sms_notif = __('notification.sms.templates.'.$template_name, $options);
return $this;
}
/**
* Fetch message from notifications lang file
*
@ -56,7 +65,7 @@ class NotificationHelper
public function sendNotifications($users, $level = null) {
switch ($level) {
case "emergency":
// Notification::send($users, new SmsNotification($notif));
Notification::send($users, new SmsNotification($this->sms_notif, enum('sms.types.ultra_fast_send.name')));
case "critical":
Notification::send($users, new MailNotification($this->notif));
case "high":
@ -64,10 +73,10 @@ class NotificationHelper
case "medium":
Notification::send($users, new FcmNotification($this->notif));
case "low":
Notification::send($users, new SocketNotification($this->notif));
// Notification::send($users, new SocketNotification($this->notif));
break;
default:
Notification::send($users, new SocketNotification($this->notif));
// Notification::send($users, new SocketNotification($this->notif));
}
}

16
config/smsirlaravel.php

@ -0,0 +1,16 @@
<?php
return [
/* Important Settings */
// ======================================================================
'webservice-url' => env('SMSIR_WEBSERVICE_URL','https://ws.sms.ir/'),
// SMS.ir Api Key
'api-key' => env('SMSIR_API_KEY',null),
// SMS.ir Secret Key
'secret-key' => env('SMSIR_SECRET_KEY',null),
// Your sms.ir line number
'line-number' => env('SMSIR_LINE_NUMBER',null),
// ======================================================================
];

1
database/migrations/2021_03_08_114700_create_notifications_table.php

@ -17,7 +17,6 @@ class CreateNotificationsTable extends Migration
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->unsignedBigInteger('business_id');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();

12
resources/lang/fa/notification.php

@ -85,4 +85,16 @@ return [
'suspended' => 'حساب مسدود شد.',
],
'sms' => [
'templates' => [
'template_name' => [
'template_id' => 'asdasd',
'params' => [
'user' => ':user',
'business' => ':business',
]
]
]
]
];

4
routes/api.php

@ -8,7 +8,9 @@ $router->get('/lab', function () {
$router->get('/ntest', function () {
$user = \App\Models\User::find(1);
\Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SocketNotification(['title' => "hello!!!", 'body' => 'sss']));
\Illuminate\Support\Facades\Notification::send($user, new \App\Notifications\SmsNotification(['verification_code' => "1234"]));
// (new \App\Utilities\HelperClass\NotificationHelper())
// ->makeSmsNotif('template_name', ['user' => 'myUser', 'business' => 'myBusiness']);
})->middleware('bindBusiness');
$router->group(['prefix' => 'actions'], function () use ($router) {
$router->group(['prefix' => 'businesses'], function () use ($router) {

Loading…
Cancel
Save