Explorar el Código

Wesmiler OTC 提交更新 0729

APPLE hace 3 años
padre
commit
db69a57202

+ 47 - 0
app/Http/Controllers/Admin/ArticleController.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Http\Controllers\Admin;
+use App\Services\Common\ArticleService;
+
+/**
+ * 文章
+ * Class ArticleController
+ * @package App\Http\Controllers\Admin
+ */
+class ArticleController extends Backend
+{
+    public function __construct()
+    {
+        parent::__construct();
+        $this->service = new ArticleService();
+    }
+
+    /**
+     * 获取列表
+     * @return array|mixed
+     */
+    public function index()
+    {
+        $pageSize = request()->post('limit', 15);
+        $params = request()->all();
+        $list = ArticleService::make()->getDataList($params, $pageSize);
+        $message = array(
+            "msg" => '操作成功',
+            "code" => 0,
+            "data" => isset($list['list'])? $list['list']:[],
+            "count" => isset($list['total'])? $list['total']:0,
+        );
+        return $message;
+    }
+
+    /**
+     * 发布广告
+     * @return array|mixed
+     */
+    public function edit()
+    {
+        return ArticleService::make()->edit();
+    }
+
+
+}

+ 68 - 0
app/Http/Controllers/Api/ArticleController.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Services\Common\ArticleService;
+
+/**
+ * 文章
+ * Class ArticleController
+ * @package App\Http\Controllers\Api
+ */
+class ArticleController extends webApp
+{
+    public function __construct()
+    {
+        parent::__construct();
+        $this->service = new ArticleService();
+    }
+
+    /**
+     * 获取列表
+     * @return array|mixed
+     */
+    public function help()
+    {
+        $pageSize = request()->post('limit', 15);
+        $params = request()->all();
+        $params['status'] = 1;
+        $params['time_type'] = 1;
+        $params['type'] = 1;
+        $list = ArticleService::make()->getDataList($params, $pageSize);
+        return message(1002, true, $list);
+    }
+
+    /**
+     * 详情
+     * @return array|mixed
+     */
+    public function info()
+    {
+        $id = request()->post('id', 0);
+        if($id<=0){
+            return message(1003, false);
+        }
+
+        $info = ArticleService::make()->getInfo(['id'=> $id,'mark'=> 1]);
+        if($info){
+            ArticleService::make()->updateViews($id);
+        }
+        return message(1002, true, $info);
+    }
+
+    /**
+     * 单页数据
+     * @return array
+     */
+    public function page()
+    {
+        $type = request()->post('type', 0);
+        if($type<=0){
+            return message(1003, false);
+        }
+
+        $info = ArticleService::make()->getInfo(['type'=> $type,'mark'=> 1]);
+
+        return message(1002, true, $info);
+    }
+}

+ 161 - 0
app/Services/Common/ArticleService.php

@@ -0,0 +1,161 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services\Common;
+
+use App\Models\ArticleModel;
+use App\Services\BaseService;
+
+/**
+ * 文章管理-服务类
+ * Class ArticleService
+ * @package App\Services\Common
+ */
+class ArticleService extends BaseService
+{
+    protected static  $instance = null;
+
+    /**
+     * 构造函数
+     * ArticleService constructor.
+     */
+    public function __construct()
+    {
+        $this->model = new ArticleModel();
+    }
+
+    /**
+     * 静态入口
+     * @return static|null
+     */
+    public static function make()
+    {
+        if (!self::$instance) {
+            self::$instance = (new static());
+        }
+        return self::$instance;
+    }
+
+    /**
+     * 获取挂单广告列表
+     * @param $params 参数
+     * @param int $pageSize 分页大小:默认 15
+     * @return array
+     */
+    public function getDataList($params, $pageSize = 15, $field=[])
+    {
+        $where = ['a.mark' => 1];
+        $type = isset($params['type'])? $params['type'] : 1;
+        $status = isset($params['status'])? $params['status'] : 0;
+        $attrType = isset($params['attr_type'])? $params['attr_type'] : 1;
+        if($type>0){
+            $where['a.type'] = $type;
+        }
+        if($status>0){
+            $where['a.status'] = $status;
+        }
+        if($attrType>0){
+            $where['a.attr_type'] = $attrType;
+        }
+
+        $list = $this->model->from('advert as a')
+            ->where($where)
+            ->where(function($query) use($params){
+                $title = isset($params['title'])? trim($params['title']) : '';
+                if($title){
+                    $query->where('a.title','like',"%{$title}%");
+                }
+
+                $timeType = isset($params['time_type'])? $params['time_type'] : 0;
+                if($timeType == 1){
+                    $query->where('a.post_time','>=', time());
+                }
+            })
+            ->select($field? $field : ['a.*'])
+            ->paginate($pageSize > 0 ? $pageSize : 9999999);
+        $list = $list? $list->toArray() :[];
+        if($list){
+            $siteName = \App\Services\ConfigService::make()->getConfigByCode('site_name');
+
+            foreach($list['data'] as &$item){
+                $item['create_time_text'] = $item['create_time']? datetime($item['create_time']):'';
+                $item['post_time_text'] = $item['post_time']? datetime($item['post_time'], 'm-d H:i'):'';
+                $item['cover'] = $item['cover']? get_image_url($item['cover']):'';
+                $item['author'] = $item['author']? $item['author'] : $siteName;
+            }
+        }
+
+
+        return [
+            'pageSize'=> $pageSize,
+            'total'=>isset($list['total'])? $list['total'] : 0,
+            'list'=> isset($list['data'])? $list['data'] : []
+        ];
+    }
+
+    /**
+     * 获取资料详情
+     * @param $where
+     * @param array $field
+     */
+    public function getInfo($where, array $field = [])
+    {
+        $field = $field ? $field : ['*'];
+        if (is_array($where)) {
+            $info = $this->model->where($where)->select($field)->first();
+        } else {
+            $info = $this->model->where(['id' => (int)$where])->select($field)->first();
+        }
+
+        $info = $info ? $info->toArray() : [];
+        if($info){
+            $info['cover'] = $info['cover']? get_image_url($info['cover']):'';
+            $info['post_time_text'] = $info['post_time']? datetime($info['post_time']):'';
+        }
+        return $info;
+    }
+
+    /**
+     * 更新浏览
+     * @param $id
+     * @return mixed
+     */
+    public function updateViews($id){
+        return $this->model->where(['id'=> $id])->increment('view_num', 1);
+    }
+
+
+    /**
+     * 添加或编辑
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function edit()
+    {
+        $data = request()->all();
+        // 图片处理
+        $cover = trim($data['cover']);
+        if (strpos($cover, "temp")) {
+            $data['cover'] = save_image($cover, 'ad');
+        } else {
+            $data['cover'] = str_replace(IMG_URL, "", $data['cover']);
+        }
+
+        // 开始时间
+        if ($data['post_time']) {
+            $data['post_time'] = strtotime($data['post_time']);
+        }
+
+        return parent::edit($data); // TODO: Change the autogenerated stub
+    }
+
+}