Sfoglia il codice sorgente

Merge branch 'v0.2' into develop

duqinya 5 anni fa
parent
commit
ea01d671c0

+ 77 - 0
app/Http/Controllers/V2/PlatformContentController.php

@@ -0,0 +1,77 @@
+<?php
+namespace App\Http\Controllers\V2;
+use App\Http\Controllers\BaseController;
+use App\Repositories\PlatformContentRepository;
+use App\Transformers\PlatformContentTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rule;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection;
+use League\Fractal\Manager;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午2:04
+ */
+
+class PlatformContentController extends BaseController
+{
+    public function __construct(PlatformContentRepository $platformContentRepository)
+    {
+        $this->platformContentRepository = $platformContentRepository;
+    }
+
+    //平台内容列表
+    public function index(Request $request)
+    {
+        $platformContent = $this->platformContentRepository->index($request->all());
+
+        $fractal = new Manager();
+        $resource = new Collection($platformContent, new PlatformContentTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($platformContent));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'id',
+            ],
+            'columns' => [
+                'id',
+                'title',
+                'content',
+                'updated_at'
+            ]
+        ];
+        return $data;
+    }
+
+    //新增平台内容
+    public function create(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'title' => 'required|string',
+            'content' => 'required|string',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->platformContentRepository->create($request->all());
+
+    }
+
+    //修改平台内容
+    public function edit(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:platform_content',
+            'content' => 'required',
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->platformContentRepository->edit($request->all());
+    }
+}

+ 112 - 0
app/Http/Controllers/V2/StarNewsController.php

@@ -0,0 +1,112 @@
+<?php
+namespace App\Http\Controllers\V2;
+use App\Http\Controllers\BaseController;
+use App\Repositories\StarNewsRepository;
+use App\Transformers\StarNewsTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rule;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection;
+use League\Fractal\Manager;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 上午10:53
+ */
+
+class StarNewsController extends BaseController
+{
+    public function __construct(StarNewsRepository $starNewsRepository)
+    {
+        $this->starNewsRepository = $starNewsRepository;
+    }
+
+    //星球新闻列表
+    public function index(Request $request)
+    {
+        $starNews = $this->starNewsRepository->index($request->all());
+
+        $fractal = new Manager();
+        $resource = new Collection($starNews, new StarNewsTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($starNews));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'id',
+                'sort',
+                'status'
+            ],
+            'columns' => [
+                'id',
+                'title',
+                'content',
+                'cover_img',
+                'status',
+                'sort',
+                'updated_at'
+            ]
+        ];
+        return $data;
+    }
+
+    //新增星球新闻
+    public function create(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'title' => 'required|string',
+            'content' => 'required|string',
+            'cover_img' => 'required',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->starNewsRepository->create($request->all());
+
+    }
+
+    //修改星球新闻
+    public function edit(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:star_news',
+            'title' => 'required|string',
+            'content' => 'required|string',
+            'cover_img' => 'required',
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->starNewsRepository->edit($request->all());
+    }
+
+    //删除星球新闻
+    public function delete(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:star_news'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return $this->starNewsRepository->delete($request->only('id'));
+
+    }
+
+    //修改星球新闻状态
+    public function editStatus(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:star_news',
+            'status' => ['required', Rule::in(0,1)],
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->starNewsRepository->editStatus($request->all());
+    }
+}

+ 20 - 0
app/Models/PlatformContent.php

@@ -0,0 +1,20 @@
+<?php
+namespace App\Models;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午12:11
+ */
+
+class PlatformContent extends BaseModel
+{
+    protected  $table = 'platform_content';
+    /**
+     * 可被批量赋值的字段
+     * @var array
+     */
+    protected $fillable = ['title','content'];
+
+
+}

+ 24 - 0
app/Models/StarNews.php

@@ -0,0 +1,24 @@
+<?php
+namespace App\Models;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午13:11
+ */
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class StarNews extends BaseModel
+{
+    use SoftDeletes;
+
+    protected $dates = ['deleted_at'];
+    protected  $table = 'star_news';
+    /**
+     * 可被批量赋值的字段
+     * @var array
+     */
+    protected $fillable = ['title','content','cover_img','status','sort'];
+
+
+}

+ 63 - 0
app/Repositories/PlatformContentRepository.php

@@ -0,0 +1,63 @@
+<?php
+namespace App\Repositories;
+use App\Models\PlatformContent;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 下午2:06
+ */
+
+class PlatformContentRepository
+{
+    public function __construct(PlatformContent $platformContent)
+    {
+        $this->platformContent = $platformContent;
+    }
+
+    public function index($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
+        $where = [];
+        if(isset($request['id'])){
+            $where[] = ['id', '=', $request['id']];
+        }
+        if (isset($request['title'])){
+            $where[] = ['title', 'like', "%{$request['title']}%"];
+        }
+
+        return $this->platformContent->where($where)->orderBy('updated_at', 'desc')->paginate($perPage);
+
+    }
+
+    public function create($request)
+    {
+        if($this->platformContent->where('title', $request['title'])->exists()){
+            throw new HttpException(500, '该名称已经存在');
+        }
+
+        $data = [
+            'title' => $request['title'],
+            'content' => $request['content'],
+        ];
+
+        if (!$this->platformContent->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+    }
+
+    public function edit($request)
+    {
+        $platforms = $this->platformContent->where('id', $request['id'])->first();
+        $platforms->content = $request['content'];
+        $platforms->updated_at = date('Y-m-d H:i:s');
+
+        $res = $platforms->save();
+        if (!$res) {
+            throw new HttpException(500, '平台内容更新失败');
+        }
+    }
+
+}

+ 106 - 0
app/Repositories/StarNewsRepository.php

@@ -0,0 +1,106 @@
+<?php
+namespace App\Repositories;
+use App\Models\StarNews;
+use Dingo\Api\Http\Response;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/17
+ * Time: 上午10:59
+ */
+
+class StarNewsRepository
+{
+    public function __construct(StarNews $starNews)
+    {
+        $this->starNews = $starNews;
+    }
+
+    public function index($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
+        $where = [];
+        if(isset($request['id'])){
+            $where[] = ['id', '=', $request['id']];
+        }
+        if(isset($request['status'])){
+            $where[] = ['status', '=', $request['status']];
+        }
+        if (isset($request['title'])){
+            $where[] = ['title', 'like', "%{$request['title']}%"];
+        }
+
+        return $this->starNews->where($where)->orderBy('status', 'desc')->orderBy('sort', 'asc')->paginate($perPage);
+
+    }
+
+    public function create($request)
+    {
+        if($this->starNews->where('title', $request['title'])->exists()){
+            throw new HttpException(500, '该新闻标题已经存在');
+        }
+
+        $data = [
+            'title' => $request['title'],
+            'content' => $request['content'],
+            'cover_img' => $request['cover_img'],
+            'sort' => $request['sort'] ?? 0,
+            'status' => $request['status'] ?? 0,
+        ];
+
+        if (!$this->starNews->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+    }
+
+    public function edit($request)
+    {
+        $starNew = $this->starNews->where('id', $request['id'])->first();
+        $starNew->title = $request['title'];
+        $starNew->content = $request['content'];
+        $starNew->cover_img = $request['cover_img'];
+        $starNew->sort = $request['sort'] ?? 0;
+        $starNew->status = $request['status'] ?? 0;
+        $starNew->updated_at = date('Y-m-d H:i:s');
+
+        $open_news = $this->starNews->where('status',1)->count();
+        if ($open_news >= 5 && $request['status'] == 1){
+            throw new HttpException(500, '最多开启5条新闻');
+        }
+        $res = $starNew->save();
+            if (!$res) {
+                throw new HttpException(500, '星球新闻更新失败');
+            }
+        }
+
+
+    public function delete($request)
+    {
+        $starNew = $this->starNews->where('id', $request['id'])->first();
+        $res = $starNew->delete();
+        if (!$res){
+            return Response::create([
+                'message'  => '删除失败,请重试',
+                'status_code'   => 500
+            ]);
+        }
+    }
+
+    public function editStatus($request)
+    {
+        $starNew = $this->starNews->find($request['id']);
+        $starNew->status = $request['status'];
+        $starNew->updated_at = date('Y-m-d H:i:s');
+        $open_news = $this->starNews->where('status',1)->count();
+        if ($open_news >= 5 && $request['status'] == 1){
+            throw new HttpException(500, '最多开启5条新闻');
+        }
+        $res = $starNew->save();
+
+        if (!$res) {
+            throw new HttpException(500, '修改状态失败');
+        }
+    }
+
+}

+ 20 - 0
app/Transformers/PlatformContentTransformer.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Transformers;
+
+use App\Models\PlatformContent;
+use League\Fractal\TransformerAbstract;
+
+class PlatformContentTransformer extends TransformerAbstract
+{
+
+    public function transform(PlatformContent $platformContent)
+    {
+        return [
+            'id'  => $platformContent['id'],
+            'title'    => $platformContent['title'],
+            'content'    => $platformContent['content'],
+            'updated_at'    => date($platformContent['updated_at']),
+        ];
+    }
+}

+ 23 - 0
app/Transformers/StarNewsTransformer.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace App\Transformers;
+
+use App\Models\StarNews;
+use League\Fractal\TransformerAbstract;
+
+class StarNewsTransformer extends TransformerAbstract
+{
+
+    public function transform(StarNews $starNews)
+    {
+        return [
+            'id'  => $starNews['id'],
+            'title'    => $starNews['title'],
+            'content'    => $starNews['content'],
+            'cover_img'    => $starNews['cover_img'],
+            'status'    => $starNews['status'],
+            'sort'    => $starNews['sort'],
+            'updated_at'    => date($starNews['updated_at']),
+        ];
+    }
+}

+ 36 - 0
database/migrations/2019_06_17_020159_create_star_news_table.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateStarNewsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('star_news', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('title', 30)->comment('新闻标题');
+            $table->string('content', 200)->comment('文字说明');
+            $table->string('cover_img')->comment('封面图');
+            $table->tinyInteger('status')->default(0)->comment('状态: 0.禁用;1.启用');
+            $table->integer('sort')->nullable()->comment('排序');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('star_news');
+    }
+}

+ 33 - 0
database/migrations/2019_06_17_020244_create_platform_content_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePlatformContentTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('platform_content', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('title', 30)->comment('内容名称');
+            $table->text('content')->comment('图文内容');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('platform_content');
+    }
+}

+ 32 - 0
database/migrations/2019_06_17_024434_add_deleted_at_to_star_news_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddDeletedAtToStarNewsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('star_news', function (Blueprint $table) {
+            $table->softDeletes();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('star_news', function (Blueprint $table) {
+            //
+        });
+    }
+}