Comments.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 app\common\exception\BaseException;
  11. use app\common\service\order\Order as OrderService;
  12. use app\common\models\users\UsersComment as UsersCommentModel;
  13. use mall\utils\CString;
  14. use think\facade\Config;
  15. use app\common\library\utils\Image;
  16. class Comments extends Service {
  17. /**
  18. * 获取商品评论
  19. * @param $data
  20. * @return array
  21. * @throws BaseException
  22. */
  23. public static function getList($data){
  24. $id = $data["id"]??"0";
  25. $type = OrderService::getOrderType($data["type"]=="goods"?"buy":$data["type"]);
  26. $size = Config::get("website.pageSize");
  27. $page = $data["page"]??1;
  28. $condition = [
  29. ["o.type","=",$type],
  30. ["uc.goods_id","=",$id],
  31. ["uc.status","=",1]
  32. ];
  33. $count = UsersCommentModel::alias("uc")->join("order o","uc.order_no=o.order_no","LEFT")->join("users u","uc.user_id=u.id","LEFT")->where($condition)->count();
  34. $result = UsersCommentModel::alias("uc")
  35. ->field("uc.contents,uc.reply_content,u.avatar,uc.comment_time,u.username,u.nickname,u.mobile")
  36. ->join("order o","uc.order_no=o.order_no","LEFT")->join("users u","uc.user_id=u.id","LEFT")
  37. ->where($condition)->order("uc.id","DESC")->page($page,$size)->select()->toArray();
  38. $data = array_map(function ($data){
  39. $array = [];
  40. $username = !empty($data["nickname"]) ? $data["nickname"] : $data["username"];
  41. $array['time'] = date("Y-m-d",$data['comment_time']);
  42. $array['avatar'] = Image::avatar($data['avatar']);
  43. $array['content'] = strip_tags($data['contents']);
  44. $array['reply_content'] = strip_tags($data['reply_content']);
  45. if(!empty($username)){
  46. $array['username'] = CString::msubstr($username,3,false) . "***";
  47. }else{
  48. $array['username'] = preg_replace('/(1[3-9]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$data['mobile']);
  49. }
  50. return $array;
  51. },$result);
  52. $array = [ "list"=>$data??[], "page"=>$page, "total"=>0, "size"=>$size ];
  53. $total = ceil($count / $size);
  54. $array["total"] = $total;
  55. if($total == $page -1){
  56. throw new BaseException("没有数据了哦!",-1,$array);
  57. }
  58. return $array;
  59. }
  60. }