127 lines
3.2 KiB
PHP
127 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\Admin\Layout\Menu;
|
|
use App\Services\SettingService;
|
|
use EasyWeChat\Factory as EasyWeChatFactory;
|
|
use EasyWeChat\Payment\Application as EasyWeChatPaymentApplication;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Overtrue\EasySms\EasySms;
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* All of the container singletons that should be registered.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $singletons = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
// Schema::defaultStringLength(191);
|
|
$this->registerEasySms();
|
|
$this->registerEasyWeChat();
|
|
$this->registerRequestRealIp();
|
|
$this->registerSettings();
|
|
$this->registerAdminMenu();
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
Relation::enforceMorphMap([
|
|
'user' => \App\Models\User::class,
|
|
'order' => \App\Models\Order::class,
|
|
'order_refund_log' => \App\Models\OrderRefundLog::class,
|
|
'after_sale' => \App\Models\AfterSale::class,
|
|
'wallet_to_bank_log' => \App\Models\WalletToBankLog::class,
|
|
'balance_log' => \App\Models\BalanceLog::class,
|
|
'admin_users' => \App\Models\Admin\Administrator::class,
|
|
'user_vip' => \App\Models\UserVip::class,
|
|
]);
|
|
|
|
JsonResource::withoutWrapping();
|
|
}
|
|
|
|
protected function registerAdminMenu()
|
|
{
|
|
$this->app->singleton('admin.menu', Menu::class);
|
|
}
|
|
|
|
/**
|
|
* 注册微信服务
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function registerEasyWeChat()
|
|
{
|
|
$this->app->singleton(EasyWeChatPaymentApplication::class, function ($app) {
|
|
return EasyWeChatFactory::payment($app['config']->get('wechat.payment.default'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 注册短信发送服务
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function registerEasySms()
|
|
{
|
|
$this->app->singleton(EasySms::class, function ($app) {
|
|
return new EasySms($app['config']->get('easysms'));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 在请求上注册 realIp 宏
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function registerRequestRealIp()
|
|
{
|
|
Request::macro('realIp', function () {
|
|
if ($forwarded = $this->header('X-Forwarded-For')) {
|
|
$segments = HeaderUtils::split($forwarded, ',');
|
|
|
|
foreach ($segments as $segment) {
|
|
if (filter_var($segment, \FILTER_VALIDATE_IP)) {
|
|
return $segment;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->ip();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 注册应用设置服务
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function registerSettings()
|
|
{
|
|
$this->app->singleton(SettingService::class, function ($app) {
|
|
return new SettingService($app['cache.store']);
|
|
});
|
|
}
|
|
}
|