Skip to content

Commit 4a425ed

Browse files
committed
设置了辅助函数,优化了setting,修复了图片url的bug
1 parent 13773bf commit 4a425ed

File tree

11 files changed

+88
-62
lines changed

11 files changed

+88
-62
lines changed

app/Common/MyUpload.php

-13
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,4 @@ static public function uploadFile($file)
3838

3939
return $fileName;
4040
}
41-
42-
/**
43-
* 根据设置的存储盘和相对路径生成绝对路径
44-
*/
45-
static public function generateUrl($url)
46-
{
47-
$file_disk = Setting::where('key', 'file_disk')->value('value');
48-
if ($file_disk == 'cos') {
49-
return 'http://images-1253193383.cosbj.myqcloud.com/' . $url;
50-
}else {
51-
return '/storage/images/' . $url;
52-
}
53-
}
5441
}

app/Http/Controllers/Admin/SettingController.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ class SettingController extends Controller
1111

1212
public function index(Request $request)
1313
{
14-
$keys = explode(",", $request->keys);
15-
$settings = Setting::whereIn('key', $keys)->get();
16-
$data = [];
17-
foreach ($settings as $setting) {
18-
$data[$setting->key] = $setting->value;
19-
}
14+
$data = Setting::getSettings($request->keys);
2015
return response()->json([
2116
'data' => $data
2217
]);

app/Http/Controllers/ArticleController.php

+25-29
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class ArticleController extends Controller
2222
*/
2323
public function list()
2424
{
25-
$articles = Article::where('is_hidden', 0)->orderBy('created_at', 'desc')->paginate(10);
26-
for ($i=0; $i < sizeof($articles); $i++) {
27-
$articles[$i]->content = str_limit(strip_tags($articles[$i]->content_html), 150);
28-
$articles[$i]->created_at_date = $articles[$i]->created_at->toDateString();
29-
$articles[$i]->updated_at_diff = $articles[$i]->updated_at->diffForHumans();
30-
}
31-
$tags = Tag::all();
32-
return view('articles.list', compact('articles', 'tags'));
25+
$articles = Article::where('is_hidden', 0)->orderBy('created_at', 'desc')->paginate(10);
26+
foreach ($articles as $article) {
27+
$article->cover = imageURL($article->cover);
28+
$article->content = str_limit(strip_tags($article->content_html), 150);
29+
$article->created_at_date = $article->created_at->toDateString();
30+
$article->updated_at_diff = $article->updated_at->diffForHumans();
31+
}
32+
33+
$tags = Tag::all();
34+
return view('articles.list', compact('articles', 'tags'));
3335
}
3436

3537
/**
@@ -39,17 +41,19 @@ public function list()
3941
*/
4042
public function search(Request $request)
4143
{
42-
$key = $request->key;
43-
$articles = Article::when($key, function($query) use ($key){
44-
return $query->where('title', 'like', '%'.$key.'%');
45-
})->where('is_hidden', 0)->orderBy('created_at', 'desc')->paginate(10);
46-
for ($i=0; $i < sizeof($articles); $i++) {
47-
$articles[$i]->content = str_limit(strip_tags($articles[$i]->content_html), 150);
48-
$articles[$i]->created_at_date = $articles[$i]->created_at->toDateString();
49-
$articles[$i]->updated_at_diff = $articles[$i]->updated_at->diffForHumans();
50-
}
51-
$tags = Tag::all();
52-
return view('articles.list', compact('articles', 'tags'));
44+
$key = $request->key;
45+
$articles = Article::when($key, function($query) use ($key){
46+
return $query->where('title', 'like', '%'.$key.'%');
47+
})->where('is_hidden', 0)->orderBy('created_at', 'desc')->paginate(10);
48+
foreach ($articles as $article) {
49+
$article->cover = imageURL($article->cover);
50+
$article->content = str_limit(strip_tags($article->content_html), 150);
51+
$article->created_at_date = $article->created_at->toDateString();
52+
$article->updated_at_diff = $article->updated_at->diffForHumans();
53+
}
54+
55+
$tags = Tag::all();
56+
return view('articles.list', compact('articles', 'tags'));
5357
}
5458

5559
/**
@@ -76,11 +80,7 @@ public function show(Request $request, $id)
7680
}
7781
if ($comment->user_id == 1) {
7882
$comment->master = User::select('name', 'avatar')->findOrFail(1);
79-
if ($comment->master->avatar) {
80-
$comment->master->avatar = MyUpload::generateUrl($comment->master->avatar);
81-
}else {
82-
$comment->master->avatar = '/images/default-avatar.png';
83-
}
83+
$comment->master->avatar = imageURL($comment->master->avatar);
8484
}
8585

8686
// $comment->replys = $comment->replys;
@@ -96,11 +96,7 @@ public function show(Request $request, $id)
9696
}
9797
if ($reply->user_id == 1) {
9898
$reply->master = User::select('name', 'avatar')->findOrFail(1);
99-
if ($reply->master->avatar) {
100-
$reply->master->avatar = MyUpload::generateUrl($comment->master->avatar);
101-
}else {
102-
$reply->master->avatar = '/images/default-avatar.png';
103-
}
99+
$reply->master->avatar = imageURL($reply->master->avatar);
104100
}
105101
}
106102
}

app/Http/Controllers/HomeController.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class HomeController extends Controller
1515
*/
1616
public function home(Request $request)
1717
{
18-
//Visit::record($request, '首页');
19-
$articles = Article::where('is_hidden', 0)->orderBy('is_top', 'desc')->orderBy('created_at', 'desc')->limit(10)->get();
20-
for ($i=0; $i < sizeof($articles); $i++) {
21-
$articles[$i]->content = str_limit(strip_tags($articles[$i]->content_html), 500);
22-
$articles[$i]->created_at_date = $articles[$i]->created_at->toDateString();
23-
$articles[$i]->updated_at_diff = $articles[$i]->updated_at->diffForHumans();
24-
}
25-
return view('home', compact('articles'));
18+
$articles = Article::where('is_hidden', 0)->orderBy('is_top', 'desc')->orderBy('created_at', 'desc')->limit(10)->get();
19+
foreach ($articles as $article) {
20+
$article->cover = imageURL($article->cover);
21+
$article->content = str_limit(strip_tags($article->content_html), 500);
22+
$article->created_at_date = $article->created_at->toDateString();
23+
$article->updated_at_diff = $article->updated_at->diffForHumans();
24+
}
25+
return view('home', compact('articles'));
2626
}
2727
}

app/Providers/AppServiceProvider.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace App\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Support\Facades\View;
7+
use Illuminate\Support\Facades\Blade;
8+
use App\Setting;
69

710
class AppServiceProvider extends ServiceProvider
811
{
@@ -13,7 +16,7 @@ class AppServiceProvider extends ServiceProvider
1316
*/
1417
public function boot()
1518
{
16-
\Carbon\Carbon::setLocale('zh');
19+
\Carbon\Carbon::setLocale('zh');
1720
}
1821

1922
/**

app/Setting.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,20 @@
66

77
class Setting extends Model
88
{
9-
//
9+
10+
public static function getSetting($key)
11+
{
12+
return Setting::where('key', $key)->value('value');
13+
}
14+
15+
public static function getSettings($keys)
16+
{
17+
$keys = explode(",", $keys);
18+
$settings = Setting::whereIn('key', $keys)->get();
19+
$data = [];
20+
foreach ($settings as $setting) {
21+
$data[$setting->key] = $setting->value;
22+
}
23+
return (object)$data;
24+
}
1025
}

bootstrap/app.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
require __DIR__ . '/helpers.php';
4+
35
/*
46
|--------------------------------------------------------------------------
57
| Create The Application

bootstrap/helpers.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* 根据存储盘生成图片地址
5+
*
6+
*/
7+
function imageURL($url)
8+
{
9+
if(!$url){
10+
return;
11+
}
12+
13+
$file_disk = App\Setting::where('key', 'file_disk')->value('value');
14+
if ($file_disk == 'cos') {
15+
return 'http://images-1253193383.cosbj.myqcloud.com/' . $url;
16+
}else {
17+
return '/storage/images/' . $url;
18+
}
19+
}
20+
21+
/**
22+
* 获取配置值
23+
*
24+
*/
25+
function setting($key, $default=null)
26+
{
27+
return App\Setting::getSetting($key) ?: $default;
28+
}

resources/views/articles/list.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</div>
3030
</div>
3131
<div class="col-xs-4" style="padding-left:0">
32-
<a href="{{ route('articles.show', $article->id) }}"><img src="{{ $article->cover == '' ? '/default.jpg' : $article->cover }}" class="img-responsive z-cover" alt="imax1"></a>
32+
<a href="{{ route('articles.show', $article->id) }}"><img src="{{ $article->cover or '/default.jpg' }}" class="img-responsive z-cover" alt="imax1"></a>
3333
</div>
3434
</div>
3535
</div>

resources/views/articles/show.blade.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
<!-- header -->
3737
@if($comment->user_id == 1)
38-
<img src="{{ $comment->master->avatar }}" class="img-circle z-avatar">
38+
<img src="{{ $comment->master->avatar or '/images/default-avatar.png' }}" class="img-circle z-avatar">
3939
<p class="z-name z-center-vertical">{{ $comment->master->name }}<span class="label label-info z-label">博 主</span></p>
4040
@elseif($comment->website)
4141
<p class="z-avatar-text">{{ $comment->avatar }}</p>
@@ -66,7 +66,7 @@ class="glyphicon glyphicon-share-alt z-reply-btn">
6666
<div id="comment{{ $reply->id }}"></div>
6767
<!-- header -->
6868
@if( $reply->user_id == 1 )
69-
<img src="{{ $reply->master->avatar }}" class="img-circle z-avatar">
69+
<img src="{{ $reply->master->avatar or '/images/default-avatar.png' }}" class="img-circle z-avatar">
7070
<p class="z-name z-center-vertical">{{ $reply->master->name }}<span class="label label-info z-label">博 主</span></p>
7171
@elseif( $reply->website )
7272
<p class="z-avatar-text">{{ $reply->avatar }}</p>

resources/views/layouts/app.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<!-- CSRF Token -->
2020
<meta name="csrf-token" content="{{ csrf_token() }}">
2121

22-
<title>@yield('title', config('app.name', 'Laravel'))</title>
22+
<title>@yield('title', setting('web_name', 'Laravel'))</title>
2323

2424
<!-- Styles -->
2525
<link href="{{ asset('css/app.css') }}" rel="stylesheet">

0 commit comments

Comments
 (0)