Comment.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\users;
  10. use app\admin\service\Service;
  11. use app\admin\model\users\UsersComment as UsersCommentModel;
  12. use app\common\models\goods\Goods as GoodsModel;
  13. use app\common\models\users\Users as UsersModel;
  14. use think\facade\Session;
  15. class Comment extends Service {
  16. /**
  17. * 获取评论数据
  18. * @param $data
  19. * @return array
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\DbException
  22. * @throws \think\db\exception\ModelNotFoundException
  23. */
  24. public static function getList($data){
  25. $condition = [];
  26. $key = $data["key"]??[];
  27. if(isset($key["cat_id"]) && $key["cat_id"] != '-1'){
  28. $filed = $key["cat_id"] == 0 ? "users.username" : "goods.title";
  29. $condition[] = [$filed,"like",'%'.$key["title"].'%'];
  30. }
  31. $count = UsersCommentModel::withJoin(['goods','users'])->where($condition)->count();
  32. $result = UsersCommentModel::withJoin(['goods','users'])->where($condition)->page($data["page"]??1,$data["limit"]??10)->order("users_comment.id","desc")->select()->toArray();
  33. return [ "count"=>$count, "data"=>array_map(function ($res){
  34. $res["username"] = getUserName($res);
  35. return $res;
  36. },$result) ];
  37. }
  38. /**
  39. * 详情
  40. * @param $id
  41. * @return array
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public static function detail($id){
  47. if(!$row=UsersCommentModel::where("id",$id)->find()){
  48. throw new \Exception("您要查找的内容不存在!");
  49. }
  50. $row["goods_name"] = GoodsModel::where("id",$row["goods_id"])->value("title");
  51. $users = UsersModel::where("id",$row["user_id"])->find();
  52. $row["username"] = getUserName($users);
  53. return [ "data"=>$row ];
  54. }
  55. /**
  56. * 保存数据
  57. * @param $data
  58. * @return bool
  59. * @throws \Exception
  60. */
  61. public static function save($data){
  62. if(empty($data["reply_content"])){
  63. throw new \Exception("请填写回复内容",0);
  64. }
  65. $data["admin_id"] = Session::get("system_user_id");
  66. $data["reply_time"] = time();
  67. $data["status"] = 1;
  68. UsersCommentModel::where("id",$data["id"])->save($data);
  69. return true;
  70. }
  71. /**
  72. * 删除
  73. * @param $id
  74. * @return bool
  75. */
  76. public static function delete($id){
  77. return UsersCommentModel::where("id",$id)->delete();
  78. }
  79. }