123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- <?php
- namespace app\portal\service;
- use app\portal\model\PortalPostModel;
- use app\portal\model\PortalCategoryModel;
- use think\Db;
- use think\db\Query;
- class ApiService
- {
-
- public static function articles($param)
- {
- $portalPostModel = new PortalPostModel();
- $where = [
- 'post.post_status' => 1,
- 'post.post_type' => 1,
- 'post.delete_time' => 0
- ];
- $paramWhere = empty($param['where']) ? '' : $param['where'];
- $limit = empty($param['limit']) ? 10 : $param['limit'];
- $order = empty($param['order']) ? '' : $param['order'];
- $page = isset($param['page']) ? $param['page'] : false;
- $relation = empty($param['relation']) ? '' : $param['relation'];
- $categoryIds = empty($param['category_ids']) ? '' : $param['category_ids'];
- $join = [
-
- ];
- $whereCategoryId = null;
- if (!empty($categoryIds)) {
- $field = !empty($param['field']) ? $param['field'] : 'post.*,min(category_post.category_id) as category_id';
- array_push($join, ['__PORTAL_CATEGORY_POST__ category_post', 'post.id = category_post.post_id']);
- if (!is_array($categoryIds)) {
- $categoryIds = explode(',', $categoryIds);
- }
- if (count($categoryIds) == 1) {
- $whereCategoryId = function (Query $query) use ($categoryIds) {
- $query->where('category_post.category_id', $categoryIds[0]);
- };
- } else {
- $whereCategoryId = function (Query $query) use ($categoryIds) {
- $query->where('category_post.category_id', 'in', $categoryIds);
- };
- }
- } else {
- $field = !empty($param['field']) ? $param['field'] : 'post.*,min(category_post.category_id) as category_id';
- array_push($join, ['__PORTAL_CATEGORY_POST__ category_post', 'post.id = category_post.post_id']);
- }
- $articles = $portalPostModel->alias('post')->field($field)
- ->join($join)
- ->where($where)
- ->where($paramWhere)
- ->where($whereCategoryId)
- ->where('post.published_time', ['> time', 0], ['<', time()], 'and')
- ->order($order)
- ->group('post.id');
- $return = [];
- if (empty($page)) {
- $articles = $articles->limit($limit)->select();
- if (!empty($relation) && !empty($articles['items'])) {
- $articles->load($relation);
- }
- $return['articles'] = $articles;
- } else {
- if (is_array($page)) {
- if (empty($page['list_rows'])) {
- $page['list_rows'] = 10;
- }
- $articles = $articles->paginate($page);
- } else {
- $articles = $articles->paginate(intval($page));
- }
- if (!empty($relation) && !empty($articles['items'])) {
- $articles->load($relation);
- }
- $articles->appends(request()->get());
- $articles->appends(request()->post());
- $return['articles'] = $articles->items();
- $return['page'] = $articles->render();
- $return['total'] = $articles->total();
- $return['total_pages'] = $articles->lastPage();
- }
- return $return;
- }
-
- public static function tagArticles($param)
- {
- $portalPostModel = new PortalPostModel();
- $where = [
- 'post.post_status' => 1,
- 'post.post_type' => 1,
- 'post.delete_time' => 0
- ];
- $paramWhere = empty($param['where']) ? '' : $param['where'];
- $limit = empty($param['limit']) ? 10 : $param['limit'];
- $order = empty($param['order']) ? '' : $param['order'];
- $page = isset($param['page']) ? $param['page'] : false;
- $relation = empty($param['relation']) ? '' : $param['relation'];
- $tagId = empty($param['tag_id']) ? '' : $param['tag_id'];
- $join = [
-
- ];
- if (empty($tagId)) {
- return null;
- } else {
- $field = !empty($param['field']) ? $param['field'] : 'post.*';
- array_push($join, ['__PORTAL_TAG_POST__ tag_post', 'post.id = tag_post.post_id']);
- $where['tag_post.tag_id'] = $tagId;
- }
- $articles = $portalPostModel->alias('post')->field($field)
- ->join($join)
- ->where($where)
- ->where($paramWhere)
- ->where('post.published_time', ['> time', 0], ['<', time()], 'and')
- ->order($order);
- $return = [];
- if (empty($page)) {
- $articles = $articles->limit($limit)->select();
- if (!empty($relation) && !empty($articles['items'])) {
- $articles->load($relation);
- }
- $return['articles'] = $articles;
- } else {
- if (is_array($page)) {
- if (empty($page['list_rows'])) {
- $page['list_rows'] = 10;
- }
- $articles = $articles->paginate($page);
- } else {
- $articles = $articles->paginate(intval($page));
- }
- if (!empty($relation) && !empty($articles['items'])) {
- $articles->load($relation);
- }
- $articles->appends(request()->get());
- $articles->appends(request()->post());
- $return['articles'] = $articles->items();
- $return['page'] = $articles->render();
- $return['total'] = $articles->total();
- $return['total_pages'] = $articles->lastPage();
- }
- return $return;
- }
-
- public static function article($id)
- {
- $portalPostModel = new PortalPostModel();
- $where = [
- 'post_status' => 1,
- 'post_type' => 1,
- 'id' => $id,
- 'delete_time' => 0
- ];
- return $portalPostModel->where($where)
- ->where('published_time', ['> time', 0], ['<', time()], 'and')
- ->find();
- }
-
- public static function pages($param)
- {
- $paramWhere = empty($param['where']) ? '' : $param['where'];
- $order = empty($param['order']) ? '' : $param['order'];
- $portalPostModel = new PortalPostModel();
- $where = [
- 'post_status' => 1,
- 'post_type' => 2,
- 'delete_time' => 0
- ];
- return $portalPostModel
- ->where($where)
- ->where($paramWhere)
- ->where('published_time', [['> time', 0], ['<', time()]], 'and')
- ->order($order)
- ->select();
- }
-
- public static function page($id)
- {
- $portalPostModel = new PortalPostModel();
- $where = [
- 'post_status' => 1,
- 'post_type' => 2,
- 'id' => $id,
- 'delete_time' => 0
- ];
- return $portalPostModel->where($where)
- ->where('published_time', ['> time', 0], ['<', time()], 'and')
- ->find();
- }
-
- public static function category($id)
- {
- $portalCategoryModel = new PortalCategoryModel();
- $where = [
- 'status' => 1,
- 'delete_time' => 0,
- 'id' => $id
- ];
- return $portalCategoryModel->where($where)->find();
- }
-
- public static function subCategories($categoryId, $field = '*')
- {
- $portalCategoryModel = new PortalCategoryModel();
- $where = [
- 'status' => 1,
- 'delete_time' => 0,
- 'parent_id' => $categoryId
- ];
- return $portalCategoryModel->field($field)->where($where)->order('list_order ASC')->select();
- }
-
- public static function allSubCategories($categoryId)
- {
- $portalCategoryModel = new PortalCategoryModel();
- $categoryId = intval($categoryId);
- if ($categoryId !== 0) {
- $category = $portalCategoryModel->field('path')->where('id', $categoryId)->find();
- if (empty($category)) {
- return [];
- }
- $categoryPath = $category['path'];
- } else {
- $categoryPath = 0;
- }
- $where = [
- 'status' => 1,
- 'delete_time' => 0,
- 'path' => ['like', "$categoryPath-%"]
- ];
- return $portalCategoryModel->where($where)->select();
- }
-
- public static function categories($param)
- {
- $paramWhere = empty($param['where']) ? '' : $param['where'];
- $order = empty($param['order']) ? '' : $param['order'];
- $portalCategoryModel = new PortalCategoryModel();
- $where = [
- 'status' => 1,
- 'delete_time' => 0,
- ];
- return $portalCategoryModel
- ->where($where)
- ->where($paramWhere)
- ->order($order)
- ->select();
- }
-
- public static function breadcrumb($categoryId, $withCurrent = false)
- {
- $data = [];
- $portalCategoryModel = new PortalCategoryModel();
- $path = $portalCategoryModel->where(['id' => $categoryId])->value('path');
- if (!empty($path)) {
- $parents = explode('-', $path);
- if (!$withCurrent) {
- array_pop($parents);
- }
- if (!empty($parents)) {
- $data = $portalCategoryModel->where('id', 'in', $parents)->order('path ASC')->select();
- }
- }
- return $data;
- }
- }
|