Browse Source

Merge branch 'develop'

xielin 5 years ago
parent
commit
71c58042a9

+ 1 - 0
app/Http/Controllers/ConfigController.php

@@ -38,6 +38,7 @@ class ConfigController extends Controller
             //日志类型
             //日志类型
             'log_type' => [
             'log_type' => [
                 'add_data' => '增加数据',
                 'add_data' => '增加数据',
+                'delete' => '删除内容',
             ],
             ],
             //是否推荐
             //是否推荐
             'is_suggest' => [
             'is_suggest' => [

+ 202 - 0
app/Http/Controllers/MusicController.php

@@ -0,0 +1,202 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/7/8
+ * Time: 下午10:37
+ */
+namespace App\Http\Controllers;
+use App\Models\PostMusicCategoryRel;
+use App\Repositories\MusicRepository;
+use App\Transformers\MusicCategoryTransformer;
+use App\Transformers\MusicTranformer;
+use App\Transformers\UserMusicTranformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rule;
+use League\Fractal\Manager;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+use League\Fractal\Resource\Collection;
+
+class MusicController extends Controller
+{
+    public function __construct(MusicRepository $musicRepository) {
+        $this->musicRepository = $musicRepository;
+    }
+    //音乐分类列表
+    public function categoryList(Request $request)
+    {
+        $categoryList = $this->musicRepository->category_list($request->all());
+        if (count($categoryList)>0) {
+            foreach ($categoryList as $k => $v) {
+                $v->music_count = PostMusicCategoryRel::where('music_category_id', $v['id'])->count();
+            }
+        }
+        $fractal = new Manager();
+        $resource = new Collection($categoryList, new MusicCategoryTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($categoryList));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'id'
+            ],
+            'columns' => [
+                'id',
+                'name',
+                'music_count',
+                'is_open'
+            ]
+        ];
+        return $data;
+    }
+
+    //新增音乐分类
+    public function categoryCreate(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'name' => 'required|string|max:8',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->musicRepository->categoryCreate($request->all());
+
+    }
+
+    //编辑音乐分类
+    public function categoryEdit(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:post_music_category',
+            'name' => 'required|string|max:8',
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->musicRepository->categoryEdit($request->all());
+
+    }
+
+    //修改分类状态
+    public function editStatus(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:post_music_category',
+            'is_open' => ['required', Rule::in(0,1)],
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->musicRepository->editStatus($request->all());
+    }
+
+    //删除音乐分类
+    public function categoryDelete(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:post_music_category'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return $this->musicRepository->categoryDelete($request->only('id'));
+
+    }
+
+    //音乐列表
+    public function musicList(Request $request)
+    {
+        $musicList = $this->musicRepository->musicList($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($musicList, new MusicTranformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($musicList));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+                'id',
+                'name',
+                'category_id',
+            ],
+            'columns' => [
+                'id',
+                'name',
+                'category_name',
+                'music_duration',
+                'created_at'
+            ]
+        ];
+        return $data;
+
+    }
+
+    //新增音乐
+    public function musicCreate(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'name' => 'required|string|max:50',
+            'url' => 'required|string',
+            'category_id' => 'required|integer',
+            'music_duration' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->musicRepository->musicCreate($request->all());
+
+    }
+
+    //修改音乐
+    public function musicEdit(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:post_music_category_rel',
+            'name' => 'required|string|max:50',
+            'category_id' => 'required|integer',
+            'music_duration' => 'required|integer',
+            'url' => 'required|string'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+
+        return  $this->musicRepository->musicEdit($request->all());
+    }
+
+    //删除音乐
+    public function musicDelete(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:post_music_category_rel'
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return $this->musicRepository->musicDelete($request->only('id'));
+
+    }
+
+    //用户上传音乐列表
+    public function userMusic(Request $request)
+    {
+        $musicList = $this->musicRepository->userMusic($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($musicList, new UserMusicTranformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($musicList));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'columns' => [
+                'id',
+                'created_at',
+                'username',
+                'name',
+                'url'
+            ]
+        ];
+        return $data;
+    }
+
+}

+ 17 - 0
app/Models/PostMusicUser.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-07-09
+ * Time: 15:47
+ */
+
+namespace App\Models;
+
+
+use Illuminate\Database\Eloquent\Model;
+
+class PostMusicUser extends Model {
+    protected $table = 'post_music_user';
+    protected $guarded = [];
+}

+ 241 - 0
app/Repositories/MusicRepository.php

@@ -0,0 +1,241 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/7/8
+ * Time: 下午23:50
+ */
+namespace App\Repositories;
+use Acekyd\LaravelMP3\LaravelMP3;
+use App\Models\PostMusic;
+use App\Models\PostMusicCategory;
+use App\Models\PostMusicCategoryRel;
+use App\Models\PostMusicUser;
+use Illuminate\Support\Facades\DB;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+use Dingo\Api\Http\Response;
+use Illuminate\Database\QueryException;
+
+
+class MusicRepository
+{
+    public function __construct(PostMusicCategoryRel $postMusicCategoryRel,PostMusicCategory $postMusicCategory,PostMusic $postMusic,PostMusicUser $postMusicUser)
+    {
+        $this->postMusicCategoryRel = $postMusicCategoryRel;
+        $this->postMusicCategory = $postMusicCategory;
+        $this->postMusic = $postMusic;
+        $this->postMusicUser = $postMusicUser;
+    }
+
+    public function category_list($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+        $where = [];
+        if(isset($request['id'])){
+            $where[] = ['id', '=', $request['id']];
+        }
+
+        return $this->postMusicCategory->where($where)->orderBy('id', 'asc')->paginate($perPage);
+    }
+
+    public function categoryCreate($request)
+    {
+        if($this->postMusicCategory->where('name', $request['name'])->exists()){
+            throw new HttpException(500, '该分类已经存在');
+        }
+
+        $data = [
+            'name' => $request['name'],
+            'is_open' => $request['is_open'] ?? 0,
+        ];
+
+        if (!$this->postMusicCategory->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+    }
+
+    public function categoryEdit($request)
+    {
+        if($this->postMusicCategory->where('name', $request['name'])->exists()){
+            throw new HttpException(500, '该分类已经存在');
+        }
+
+        $post_music_category = $this->postMusicCategory->where('id', $request['id'])->first();
+        $post_music_category->name = $request['name'];
+        $post_music_category->is_open = $request['is_open'] ?? 0;
+        $post_music_category->updated_at = date('Y-m-d H:i:s');
+
+        $res = $post_music_category->save();
+        if (!$res) {
+            throw new HttpException(500, '音乐分类更新失败');
+        }
+    }
+
+    public function editStatus($request)
+    {
+        $post_music_category = $this->postMusicCategory->find($request['id']);
+        $post_music_category->is_open = $request['is_open'];
+        $post_music_category->updated_at = date('Y-m-d H:i:s');
+
+        $res = $post_music_category->save();
+
+        if (!$res) {
+            throw new HttpException(500, '修改状态失败');
+        }
+    }
+
+    public function categoryDelete($request)
+    {
+         if ($this->postMusicCategoryRel->where('music_category_id',$request['id'])->exists()) {
+             throw new HttpException(500, '当前分类下含有音乐,不能删除');
+         }
+
+        $post_music_category = $this->postMusicCategory->where('id', $request['id'])->first();
+        $res = $post_music_category->delete();
+        if (!$res){
+            return Response::create([
+                'message'  => '删除失败,请重试',
+                'status_code'   => 500
+            ]);
+        }
+    }
+
+    public function musicList($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+        $where = [];
+        if(isset($request['id'])){
+            $where[] = ['post_music_category_rel.id', '=', $request['id']];
+        }
+        if(isset($request['name'])){
+            $where[] = ['post_music.name', 'like', "%{$request['name']}%"];
+        }
+        if(isset($request['category_id'])){
+            $where[] = ['post_music_category.id', '=', $request['category_id']];
+        }
+
+        $postMusicList = $this->postMusicCategoryRel
+            ->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
+            ->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
+            ->select('post_music_category_rel.id','post_music.name','post_music.url','post_music.music_duration','post_music.created_at','post_music_category.name as category_name','post_music_category.id as category_id')
+            ->where($where)
+            ->orderBy('id', 'desc')
+            ->paginate($perPage);
+
+        return $postMusicList;
+
+    }
+
+    public function musicCreate($request)
+    {
+        $url = $request['url'];
+        $data = [
+            'name' => $request['name'],
+            'url' => $url,
+            'music_duration' => $request['music_duration'],
+        ];
+        $date = date('Y-m-d H:i:s');
+        DB::beginTransaction();
+        try {
+            $res = $this->postMusic->create($data);
+            if ($res) {
+                $mid = $res->id;
+                $create_category_rel = [
+                    'mid' => $mid,
+                    'music_category_id' => $request['category_id'],
+                    'created_at' => $date,
+                    'updated_at' => $date
+                ];
+                $result = $this->postMusicCategoryRel->insert($create_category_rel);
+                if (!$result) {
+                    throw new HttpException(500, '音乐与分类关联-添加失败');
+                }
+            }
+
+            DB::commit();
+            return Response::create();
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            return Response::create([
+                'message' => '添加失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code' => 500
+            ]);
+        }
+    }
+
+    public function musicEdit($request)
+    {
+        $post_music_rel = $this->postMusicCategoryRel->select('mid')->where('id', $request['id'])->first();
+        $post_music = $this->postMusic->where('id',$post_music_rel->mid)->first();
+        $date = date('Y-m-d H:i:s');
+        $post_music->name = $request['name'];
+        $post_music->url = $request['url'];
+        $post_music->music_duration = $request['music_duration'];
+        $post_music->updated_at = $date;
+        DB::beginTransaction();
+        try{
+                $res = $post_music->save();
+                if ($res) {
+                    $create_category_rel = [
+                        'mid' => $post_music->id,
+                        'music_category_id' => $request['category_id'],
+                        'created_at' => $date,
+                        'updated_at' => $date,
+                    ];
+                    $result = $this->postMusicCategoryRel->where('id',$request['id'])->update($create_category_rel);
+                    if (!$result){
+                        throw new HttpException(500, '音乐与分类关联-修改失败');
+                    }
+                }
+
+            DB::commit();
+            return Response::create();
+
+        }catch (QueryException $exception){
+            DB::rollBack();
+            return Response::create([
+                'message'  => '修改失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code'   => 500
+            ]);
+        }
+
+    }
+
+    public function musicDelete($request)
+    {
+        $post_music_rel = $this->postMusicCategoryRel->where('id', $request['id'])->first();
+        DB::beginTransaction();
+        try {
+            $res = $post_music_rel->delete();
+            if ($res) {
+                $post_music = $this->postMusic->where('id',$post_music_rel->mid)->delete();
+                if (!$post_music){
+                    throw new HttpException(500, '音乐删除失败');
+                }
+            }
+            DB::commit();
+            return Response::create();
+
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            return Response::create([
+                'message' => '删除失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code' => 500
+            ]);
+        }
+    }
+
+    public function userMusic($request)
+    {
+        $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+
+        return $this->postMusicUser->orderBy('id', 'asc')->paginate($perPage);
+
+    }
+
+
+}

+ 23 - 0
app/Transformers/MusicCategoryTransformer.php

@@ -0,0 +1,23 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/7/8
+ * Time: 下午9:53
+ */
+namespace App\Transformers;
+use App\Models\PostMusicCategory;
+use League\Fractal\TransformerAbstract;
+
+class MusicCategoryTransformer extends TransformerAbstract {
+
+    public function transform(PostMusicCategory $categoryList)
+    {
+        return [
+            'id' => $categoryList['id'],
+            'name' => $categoryList['name'],
+            'music_count' => $categoryList['music_count'],
+            'is_open' => $categoryList['is_open'],
+        ];
+    }
+}

+ 27 - 0
app/Transformers/MusicTranformer.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/7/4
+ * Time: 上午9:53
+ */
+namespace App\Transformers;
+
+use App\Models\PostMusicCategoryRel;
+use League\Fractal\TransformerAbstract;
+
+class MusicTranformer extends TransformerAbstract {
+
+    public function transform(PostMusicCategoryRel $musicList)
+    {
+        return [
+            'id' => $musicList['id'],
+            'name' => $musicList['name'],
+            'music_duration' => date('i:s',$musicList['music_duration']),
+            'url' => $musicList['url'],
+            'created_at' => date($musicList['created_at']),
+            'category_id' => $musicList['category_id'],
+            'category_name' => $musicList['category_name'],
+        ];
+    }
+}

+ 1 - 0
app/Transformers/Post/LogTransformer.php

@@ -20,6 +20,7 @@ class LogTransformer extends TransformerAbstract
             'add_praise_count' => '增加点赞数:',
             'add_praise_count' => '增加点赞数:',
             'add_collect_count' => '增加收藏数:',
             'add_collect_count' => '增加收藏数:',
             'add_share_count' => '增加分享数:',
             'add_share_count' => '增加分享数:',
+            'delete' => '删除内容:',
         ];
         ];
         $data = json_decode($postLog['content']);
         $data = json_decode($postLog['content']);
         $content = '';
         $content = '';

+ 25 - 0
app/Transformers/UserMusicTranformer.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/7/10
+ * Time: 下午14:53
+ */
+namespace App\Transformers;
+
+use App\Models\PostMusicUser;
+use League\Fractal\TransformerAbstract;
+
+class UserMusicTranformer extends TransformerAbstract {
+
+    public function transform(PostMusicUser $musicList)
+    {
+        return [
+            'id' => $musicList['id'],
+            'name' => $musicList['name'],
+            'url' => $musicList['url'],
+            'created_at' => date($musicList['created_at']),
+            'username' => $musicList['username'],
+        ];
+    }
+}

+ 3 - 2
composer.json

@@ -17,9 +17,10 @@
         "multilinguals/apollo-client": "^0.1.2",
         "multilinguals/apollo-client": "^0.1.2",
         "php-amqplib/php-amqplib": "^2.9",
         "php-amqplib/php-amqplib": "^2.9",
         "predis/predis": "^1.1",
         "predis/predis": "^1.1",
-        "tymon/jwt-auth": "1.0.0-rc.4.1",
         "shaozeming/aliyun-vod": "^2.0",
         "shaozeming/aliyun-vod": "^2.0",
-        "vlucas/phpdotenv": "^3.3"
+        "tymon/jwt-auth": "1.0.0-rc.4.1",
+        "vlucas/phpdotenv": "^3.3",
+        "wapmorgan/mp3info": "^0.0.4"
     },
     },
     "require-dev": {
     "require-dev": {
         "fzaninotto/faker": "^1.4",
         "fzaninotto/faker": "^1.4",

+ 2 - 2
config/constants.php

@@ -5,7 +5,7 @@
  */
  */
 
 
 return [
 return [
-    'VIRUS_APP_ID' => env('VIRUS_APP_ID', '5cfdcf97249e6a00082639d3'),
-    'VIRUS_APP_SECRET' => env('VIRUS_APP_SECRET', '4f2afc9c4099ee1f39c9f551123e54bd'),
+    'VIRUS_APP_ID' => env('VIRUS_APP_ID', '5d26a98d88fa5b000752c374'),
+    'VIRUS_APP_SECRET' => env('VIRUS_APP_SECRET', 'd433cf381abdf4d2a6a842a0a467f73f'),
     'VIRUS_URL' => env('VIRUS_URL', 'https://api.dev.caihongxingqiu.com/virus/'),
     'VIRUS_URL' => env('VIRUS_URL', 'https://api.dev.caihongxingqiu.com/virus/'),
 ];
 ];

+ 35 - 0
database/migrations/2019_07_08_173223_create_post_music_user.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreatePostMusicUser extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('post_music_user', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('url')->default('')->comment('音乐地址');
+            $table->string('name',100)->comment('音乐名称');
+            $table->string('username',100)->default('')->comment('上传用户');
+            $table->integer('uid')->comment('上传用户ID');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('table_post_music_user');
+    }
+}

+ 34 - 0
database/migrations/2019_07_09_142246_add_is_open_to_post_music_category_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddIsOpenToPostMusicCategoryTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('post_music_category', function (Blueprint $table) {
+            $table->tinyInteger('is_open')
+                ->default(0)
+                ->comment('是否开启:0.否,1.是');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('post_music_category', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 35 - 0
database/migrations/2019_07_09_145014_add_operator_type_to_post_log.php

@@ -0,0 +1,35 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddOperatorTypeToPostLog extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('post_log', function (Blueprint $table) {
+            $table->string('operator_type',16)
+                ->default('admin')
+                ->after('uid')
+                ->comment('操作人类型(user用户,admin后台人员,system 系统)');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('post_log', function (Blueprint $table) {
+            //
+        });
+    }
+}

+ 23 - 1
routes/api.php

@@ -123,6 +123,28 @@ $api->version('v1', [
             //阅读/分享/收藏等普通行为账单
             //阅读/分享/收藏等普通行为账单
             $api->get('generalRecord/list', 'GeneralRecordController@index');
             $api->get('generalRecord/list', 'GeneralRecordController@index');
         });
         });
+
+            //音乐分类列表
+            $api->get('music/category/list', 'MusicController@categoryList');
+            //新增音乐分类
+            $api->post('music/category/create', 'MusicController@categoryCreate');
+            //编辑音乐分类
+            $api->put('music/category/create', 'MusicController@categoryEdit');
+            //修改音乐分类状态
+            $api->put('music/category/status', 'MusicController@editStatus');
+            //删除音乐分类
+            $api->delete('music/category/delete', 'MusicController@categoryDelete');
+
+            //音乐列表
+            $api->get('music/list', 'MusicController@musicList');
+            //新建音乐
+            $api->post('music/create', 'MusicController@musicCreate');
+            //修改音乐
+            $api->put('music/create', 'MusicController@musicEdit');
+            //删除音乐
+            $api->delete('music/delete', 'MusicController@musicDelete');
+            //用户上传音乐列表
+            $api->get('music/upload/list', 'MusicController@userMusic');
     });
     });
 
 
-});
+});