News.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service;
  10. use mall\utils\Tool;
  11. use app\common\models\Archives as ArchivesModel;
  12. use app\common\models\Category as CategoryModel;
  13. use think\facade\Config;
  14. use app\common\exception\BaseException;
  15. class News extends Service {
  16. public static function getList($data){
  17. $condition = [
  18. ["status","=",0]
  19. ];
  20. $condition[] = ["pid","=",$data["cat_id"]??"72"];
  21. $size = Config::get("website.pageSize");
  22. $page = $data["page"]??1;
  23. $count = ArchivesModel::where($condition)->count();
  24. $result = ArchivesModel::field("id,title,create_time,photo")->where($condition)->order("id","desc")->page($page,$size)->select()->toArray();
  25. foreach($result as $key=>$value){
  26. $result[$key]["photo"] = Tool::thumb($value["photo"],"medium",true);
  27. }
  28. $array = [ "list"=>$result??[], "page"=>$page, "total"=>0, "size"=>$size ];
  29. $total = ceil($count / $size);
  30. $array["total"] = $total;
  31. if($total == $page -1){
  32. throw new BaseException("没有数据了哦!",-1,$array);
  33. }
  34. return $array;
  35. }
  36. /**
  37. * 新闻详情
  38. * @param $id
  39. * @return array|\think\Model|null
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public static function detail($id){
  45. if(!$row = ArchivesModel::field("pid,title,hits,content,create_time")->where(["id"=>$id,"status"=>0])->find()){
  46. throw new \Exception("内容不存在",0);
  47. }
  48. ArchivesModel::where("id",$id)->where("status",0)->inc("hits",1)->update();
  49. $row["content"] = Tool::replaceContentImage(Tool::removeContentAttr($row["content"]));
  50. $row["cat_name"] = CategoryModel::where("id",$row["pid"])->value("title");
  51. return $row;
  52. }
  53. }