Archives.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\admin\service\platform;
  10. use app\admin\service\Service;
  11. use app\admin\service\platform\Category as CategoryService;
  12. use app\admin\model\Archives as ArchivesModel;
  13. use mall\utils\Tool;
  14. class Archives extends Service {
  15. /**
  16. * 获取文章列表数据
  17. * @param $data
  18. * @return array
  19. * @throws \think\db\exception\DataNotFoundException
  20. * @throws \think\db\exception\DbException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. */
  23. public static function getList($data){
  24. $count = ArchivesModel::withSearch(["pid","title"],[
  25. 'pid'=>$data['key']["cat_id"]??'',
  26. 'title'=>$data['key']["title"]??''
  27. ])->withJoin("category")->count();
  28. $result = array_map(function ($res){
  29. $res["photo"] = Tool::thumb($res["photo"]);
  30. return $res;
  31. },ArchivesModel::withSearch(["pid","title"],[
  32. 'pid'=>$data['key']["cat_id"]??'',
  33. 'title'=>$data['key']["title"]??''
  34. ])->withJoin("category")->order("archives.id","desc")->page($data["page"]??1,$data["limit"]??10)->select()->toArray());
  35. return ["count"=>$count, "data"=>$result];
  36. }
  37. /**
  38. * 获取单篇文章数据
  39. * @param $id
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public static function detail($id){
  46. return [
  47. "data"=>ArchivesModel::where(["id"=>$id])->find(),
  48. "cat"=>CategoryService::getTree(["status"=>0,"module"=>"article"])
  49. ];
  50. }
  51. /**
  52. * 保存
  53. * @param $data
  54. * @return bool
  55. */
  56. public static function save($data){
  57. if(ArchivesModel::where("id",$data["id"])->count()){
  58. ArchivesModel::where("id",$data["id"])->save($data);
  59. }else{
  60. ArchivesModel::create($data);
  61. }
  62. return true;
  63. }
  64. /**
  65. * 删除文章
  66. * @param $id
  67. * @return bool
  68. * @throws \Exception
  69. */
  70. public static function delete($id){
  71. if(!ArchivesModel::where("id",$id)->count()){
  72. throw new \Exception("您删除的内容不存在",0);
  73. }
  74. ArchivesModel::where("id",$id)->delete();
  75. return true;
  76. }
  77. /**
  78. * 更新字段值
  79. * @return ArchivesModel
  80. */
  81. public static function setFields(){
  82. $data = self::getFields();
  83. return ArchivesModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  84. }
  85. }