Selaa lähdekoodia

wesmiler 报恩寺项目提交

wesmiler 4 vuotta sitten
vanhempi
commit
7888d454b0

+ 75 - 0
app/Http/Controllers/Api/v1/DynamicController.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Http\Controllers\Api\v1;
+
+use App\Http\Controllers\Api\BaseController;
+use App\Http\Validator\ArticleValidator;
+use App\Http\Validator\DynamicValidator;
+use App\Services\ArticleBooksService;
+use App\Services\ArticleCatesService;
+use App\Services\ArticleService;
+use App\Services\ConfigService;
+use Illuminate\Http\Request;
+
+/**
+ * 动态控制器类
+ * @author wesmiler
+ * @since 2020/11/10
+ * Class DynamicController
+ * @package App\Http\Controllers
+ */
+class DynamicController extends BaseController
+{
+    /**
+     * 构造函数
+     * @author wesmiler
+     * @since 2020/11/11
+     * ArticleController constructor.
+     */
+    public function __construct()
+    {
+        parent::__construct();
+
+        $this->service = new ArticleService();
+        $this->cateService = new ArticleCatesService();
+        $this->bookService = new ArticleBooksService();
+    }
+
+    /**
+     * 列表
+     * @return array
+     */
+    public function index(){
+        $params = request()->all();
+        return $this->service->getDataList($params);
+    }
+
+    /**
+     * 详情
+     * @return array|mixed
+     */
+    public function info(){
+        $id = request()->get('id',0);
+        if($id<=0){
+            return message(1006, false);
+        }
+
+        $info = $this->service->getDetail($id);
+        return message(1005, true, $info);
+    }
+
+    /**
+     * 发布动态
+     * @param Request $request
+     * @param DynamicValidator $validate
+     * @return array
+     */
+    public function publish(Request $request, DynamicValidator $validate){
+        $params = $validate->check($request->all(),'publish');
+        if(!is_array($params)){
+            return message($params, false);
+        }
+
+        return $this->service->publish($this->userId);
+    }
+}

+ 43 - 0
app/Http/Validator/DynamicValidator.php

@@ -0,0 +1,43 @@
+<?php
+namespace App\Http\Validator;
+class DynamicValidator extends BaseValidator
+{
+    // 当前模型所有验证规则
+    public static $rules = [
+        'id' => 'required',
+        'content' => 'required|string|min:1|max:500',
+    ];
+
+    // 当前模型所有错误提示信息
+    public static $msgs = [
+        'required' => ':attribute不能为空',
+        'string' => ':attribute必须是字符串',
+        'min' => ':attribute长度不能小于:min位',
+        'max' => ':attribute长度不能大于:max位',
+        'exists' => ':attribute不存在',
+        'rule' => ':attribute格式不正确',
+    ];
+
+    // 当前模型所有验证字段
+    public static $fields = [
+        'id' => 'ID',
+        'content' => '动态内容',
+    ];
+
+    // 当前模型所有验证场景
+    public static $scenes = [
+        'info'=> ['id'],
+        'publish'=> ['content'],
+    ];
+
+    /**
+     * 验证
+     * @param $request
+     * @param string $scene
+     * @return int|mixed
+     */
+    public static function check($request, $scene=''){
+        $validator = new BaseValidator(self::$rules, self::$msgs, self::$fields, self::$scenes);
+        return $validator->checkParams($request, $scene);
+    }
+}

+ 39 - 0
app/Models/DynamicCommentModel.php

@@ -0,0 +1,39 @@
+<?php
+// +----------------------------------------------------------------------
+// | Laravel框架 [ Laravel ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 Laravel研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: wesmiler <12345678@qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 动态评论管理-模型
+ * @author wesmiler
+ * @since 2020/11/11
+ * Class DynamicCommentModel
+ * @package App\Models
+ */
+class DynamicCommentModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'dynamic_comment';
+
+    /**
+     * 获取记录信息
+     * @param int $id 记录ID
+     * @return array|string
+     * @author wesmiler
+     * @since 2020/11/11
+     */
+    public function getInfo($id)
+    {
+        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
+        return $info;
+    }
+
+}

+ 45 - 0
app/Models/DynamicModel.php

@@ -0,0 +1,45 @@
+<?php
+// +----------------------------------------------------------------------
+// | Laravel框架 [ Laravel ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 Laravel研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: wesmiler <12345678@qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 动态管理-模型
+ * @author wesmiler
+ * @since 2020/11/11
+ * Class DynamicModel
+ * @package App\Models
+ */
+class DynamicModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'dynamic';
+
+    /**
+     * 获取记录信息
+     * @param int $id 记录ID
+     * @return array|string
+     * @author wesmiler
+     * @since 2020/11/11
+     */
+    public function getInfo($id)
+    {
+        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
+        if ($info) {
+            // 图片
+            if ($info['albums']) {
+                $info['albums'] = json_decode($info['albums'], true);
+            }
+        }
+        return $info;
+    }
+
+}

+ 42 - 0
app/Services/ArticleService.php

@@ -181,6 +181,7 @@ class ArticleService extends BaseService
                 $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
                 $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
                 $item['publish_at'] = $item['publish_at'] ? $item['publish_at'] : $item['create_at'];
+                $item['content'] = $item['content']? htmlspecialchars_decode($item['content']) : '';
             }
             unset($item);
         }
@@ -206,6 +207,7 @@ class ArticleService extends BaseService
             ->first();
         $info = $info? $info->toArray() : [];
         if($info){
+            $info['content'] = $info['content']? htmlspecialchars_decode($info['content']) : '';
             $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
             $info['publish_at'] = $info['publish_at']? $info['publish_at'] : datetime( $info['create_time'],'Y-m-d H:i:s');
 
@@ -255,4 +257,44 @@ class ArticleService extends BaseService
         return parent::edit($data); // TODO: Change the autogenerated stub
     }
 
+    /**
+     * 发布动态
+     * @param $userId
+     * @return array
+     */
+    public function publish($userId){
+        $params = request()->all();
+        $content = isset($params['content'])? htmlspecialchars($params['content']) : '';
+        $albums = isset($params['fileList'])? $params['fileList'] : [];
+        $sourceId = isset($params['source_id'])? intval($params['source_id']) : 0;
+        if($sourceId && !ArticleModel::where(['id'=> $sourceId, 'mark'=> 1,'status'=> 1])->value('id')){
+            return message('抱歉,当前文章状态不可分享到动态', false);
+        }
+
+        // 验证用户
+        $memberInfo = MemberModel::where(['id'=> $userId, 'mark'=> 1,'status'=> 1])
+            ->select(['id','openid','nickname'])
+            ->first();
+        if(!$memberInfo){
+            return message('账户不可操作或已冻结,请联系客服', false);
+        }
+
+        $confirm = ConfigService::make()->getConfigByCode('dynamic_confirm');
+        $confirm = intval($confirm)? intval($confirm) : 2;
+        $data = [
+            'user_id'=> $userId,
+            'source_id'=> $sourceId,
+            'comment_close'=> isset($params['comment_close']) && $params['comment_close']? 1 : 2,
+            'content'=> $content,
+            'albums'=> $albums? json_encode($albums, 256) : '',
+            'create_time'=> time(),
+            'status'=> $confirm,
+        ];
+
+        if($id = $this->model::insertGetId($data)){
+            return message('发布成功', true, ['id'=> $id]);
+        }
+
+        return message('发布失败', false);
+    }
 }

+ 114 - 0
app/Services/DynamicCommentService.php

@@ -0,0 +1,114 @@
+<?php
+// +----------------------------------------------------------------------
+// | Laravel框架 [ Laravel ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 Laravel研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: wesmiler <12345678@qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services;
+
+use App\Models\DynamicCommentModel;
+use App\Models\MemberModel;
+
+/**
+ * 动态评论管理-服务类
+ * @author wesmiler
+ * @since 2020/11/11
+ * Class DynamicCommentService
+ * @package App\Services
+ */
+class DynamicCommentService extends BaseService
+{
+    protected static $instance = null;
+    /**
+     * 构造函数
+     * @author wesmiler
+     * @since 2020/11/11
+     * DynamicCommentService constructor.
+     */
+    public function __construct()
+    {
+        $this->model = new DynamicCommentModel();
+    }
+
+    /**
+     * 静态入口
+     * @return DynamicService|null
+     */
+    public static function make(){
+        if(!self::$instance){
+            self::$instance = new DynamicService();
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * 获取列表
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function getList()
+    {
+        $params = request()->all();
+        return parent::getList();
+    }
+
+    /**
+     * 获取列表
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function getDataList($params)
+    {
+        $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
+        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
+
+        $dataList = $this->model::from('dynamic_comment as a')
+            ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
+            ->where(['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1])
+            ->where('m.user_id','>',0)
+            ->select(['a.id', 'a.user_id', 'm.nickname','m.avatar', 'a.source_id', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
+            ->orderBy('a.create_time', 'desc')
+            ->paginate($pageSize);
+
+        $dataList = $dataList ? $dataList->toArray() : [];
+        if ($dataList) {
+            foreach ($dataList['data'] as &$item) {
+                $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
+                $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
+            }
+            unset($item);
+        }
+
+        return [
+            'code' => 0,
+            'success'=> true,
+            'msg' => '操作成功',
+            'count' => isset($dataList['total']) ? $dataList['total'] : 0,
+            'data' => isset($dataList['data']) ? $dataList['data'] : 0,
+        ];
+    }
+
+
+    /**
+     * 添加或编辑
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function edit()
+    {
+        $data = request()->all();
+
+        $data['update_time'] = time();
+        return parent::edit($data); // TODO: Change the autogenerated stub
+    }
+
+}

+ 222 - 0
app/Services/DynamicService.php

@@ -0,0 +1,222 @@
+<?php
+// +----------------------------------------------------------------------
+// | Laravel框架 [ Laravel ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 Laravel研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: wesmiler <12345678@qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services;
+
+use App\Models\CollectModel;
+use App\Models\DynamicCommentModel;
+use App\Models\DynamicModel;
+use App\Models\FollowModel;
+use App\Models\MemberModel;
+
+/**
+ * 动态管理-服务类
+ * @author wesmiler
+ * @since 2020/11/11
+ * Class DynamicService
+ * @package App\Services
+ */
+class DynamicService extends BaseService
+{
+    protected static $instance = null;
+    /**
+     * 构造函数
+     * @author wesmiler
+     * @since 2020/11/11
+     * DynamicService constructor.
+     */
+    public function __construct()
+    {
+        $this->model = new DynamicModel();
+    }
+
+    /**
+     * 静态入口
+     * @return DynamicService|null
+     */
+    public static function make(){
+        if(!self::$instance){
+            self::$instance = new DynamicService();
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * 获取列表
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function getList()
+    {
+        $params = request()->all();
+        $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
+        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
+
+        $dataList = $this->model::from('dynamic as a')
+            ->where(function ($query) use ($params) {
+                $query->where('a.mark', 1);
+
+                $isRecommand = isset($params['is_recommand']) ? intval($params['is_recommand']) : 0;
+                if ($isRecommand > 0) {
+                    $query->where('a.is_recommand', $isRecommand);
+                }
+
+                $status = isset($params['status']) ? $params['status'] : 0;
+                if ($status > 0) {
+                    $query->where('a.status', $status);
+                } else {
+                    $query->whereIn('a.status', [1, 2]);
+                }
+
+            })
+            ->select(['a.id', 'a.user_id', 'a.comment_close', 'a.source_id', 'a.is_recommand', 'a.content', 'a.albums', 'a.status', 'a.create_time', 'a.update_time'])
+            ->orderBy('a.update_time', 'desc')
+            ->paginate($pageSize);
+
+        $dataList = $dataList ? $dataList->toArray() : [];
+        if ($dataList) {
+            foreach ($dataList['data'] as &$item) {
+                $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
+            }
+            unset($item);
+        }
+
+        return [
+            'code' => 0,
+            'success'=> true,
+            'msg' => '操作成功',
+            'count' => isset($dataList['total']) ? $dataList['total'] : 0,
+            'data' => isset($dataList['data']) ? $dataList['data'] : 0,
+        ];
+    }
+
+    /**
+     * 获取列表
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function getDataList($params)
+    {
+        $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
+        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
+
+        $dataList = $this->model::from('dynamic as a')
+            ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
+            ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
+            ->where(['a.mark'=>1,'a.status'=> 1,'m.mark'=> 1,'m.status'=> 1])
+            ->where('m.user_id','>',0)
+            ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a,albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
+            ->orderBy('a.update_time', 'desc')
+            ->paginate($pageSize);
+
+        $dataList = $dataList ? $dataList->toArray() : [];
+        if ($dataList) {
+            foreach ($dataList['data'] as &$item) {
+                $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
+                $item['avatar'] = $item['avatar'] ? get_image_url($item['avatar']) : '';
+                $item['albums'] = $item['albums'] ? json_decode($item['albums'], true) : [];
+                $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
+
+                // 关注数量
+                $item['collect'] = 0;
+                if($item['id']){
+                    $count = CollectModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
+                    $item['collect'] = $count? $count : 0;
+                }
+
+                // 评论数量
+                $item['comment'] = 0;
+                if($item['id']){
+                    $count = DynamicCommentModel::where(['source_id'=> $item['id'],'mark'=> 1,'status'=> 1])->count('id');
+                    $item['comment'] = $count? $count : 0;
+                }
+            }
+            unset($item);
+        }
+
+        return [
+            'code' => 0,
+            'success'=> true,
+            'msg' => '操作成功',
+            'count' => isset($dataList['total']) ? $dataList['total'] : 0,
+            'data' => isset($dataList['data']) ? $dataList['data'] : 0,
+        ];
+    }
+
+    /**
+     * 获取详情
+     * @param $id
+     */
+    public function getDetail($id, $userId=0){
+        $info = $this->model::from('dynamic as a')
+            ->leftJoin('article as ar', 'ar.id', '=', 'a.source_id')
+            ->leftJoin('member as m', 'm.id', '=', 'a.user_id')
+            ->where(['a.mark'=> 1,'a.status'=> 1,'a.id'=> $id,'m.mark'=> 1,'m.status'=> 1])
+            ->where('m.id','>',0)
+            ->select(['a.id', 'a.user_id', 'ar.title as title','m.nickname','m.avatar', 'a.source_id','ar.thumb', 'a.is_recommand', 'a.comment_close', 'a,albums', 'a.content', 'a.status', 'a.create_time', 'a.update_time'])
+            ->first();
+        $info = $info? $info->toArray() : [];
+        if($info){
+            $info['thumb'] = $info['thumb']? get_image_url($info['thumb']) : '';
+            $info['avatar'] = $info['avatar']? get_image_url($info['avatar']) : '';
+            $info['create_time'] = $info['create_time']? datetime( $info['create_time'],'Y-m-d H:i:s') : '';
+
+            $info['is_follow'] = 0;
+            $info[''] = 0;
+            if($info['user_id']){
+                $author = MemberModel::where(['id'=> $info['user_id']])->value('nickname');
+                $info['author'] = $author? $author : '报恩寺';
+            }
+
+            // 是否已收藏
+            $info['is_collect'] = 0;
+            if($userId){
+                if(CollectModel::where(['user_id'=> $userId,'source_id'=> $id,'type'=> 1,'mark'=> 1,'status'=> 1])->value('id')){
+                    $info['is_collect'] = 1;
+                }
+            }
+        }
+        return $info;
+    }
+
+    /**
+     * 添加或编辑
+     * @return array
+     * @since 2020/11/11
+     * @author wesmiler
+     */
+    public function edit()
+    {
+        $data = request()->all();
+
+        // 图片处理
+        $type = isset($data['type'])? intval($data['type']) : 0;
+
+        $image = $data['thumb']? trim($data['thumb']) : '';
+        $id = isset($data['id']) ? $data['id'] : 0;
+        if (!$id && !$image && $type == 1) {
+            return message('请上传封面图片', false);
+        }
+        if (strpos($image, "temp")) {
+            $data['thumb'] = save_image($image, 'item');
+        } else {
+            $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
+        }
+
+        $data['update_time'] = time();
+        $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
+        return parent::edit($data); // TODO: Change the autogenerated stub
+    }
+
+}

+ 6 - 0
routes/api.php

@@ -118,3 +118,9 @@ Route::post('/address/default', [\App\Http\Controllers\Api\v1\AddressController:
 Route::post('/trades/list', [\App\Http\Controllers\Api\v1\TradeController::class, 'index']);
 Route::post('/trades/slary', [\App\Http\Controllers\Api\v1\TradeController::class, 'slary']);
 Route::post('/trades/info', [\App\Http\Controllers\Api\v1\TradeController::class, 'info']);
+
+
+// 动态
+Route::post('/dynamic/list', [\App\Http\Controllers\Api\v1\DynamicController::class, 'index']);
+Route::post('/dynamic/info', [\App\Http\Controllers\Api\v1\DynamicController::class, 'info']);
+Route::post('/dynamic/publish', [\App\Http\Controllers\Api\v1\DynamicController::class, 'publish']);