You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\Token;
  4. use App\Common\ReturnData;
  5. class Comment extends BaseModel
  6. {
  7. //评价
  8. protected $table = 'comment';
  9. public $timestamps = false;
  10. /**
  11. * 不能被批量赋值的属性
  12. *
  13. * @var array
  14. */
  15. protected $guarded = [];
  16. const UNSHOW_COMMENT = 0; //评论未批准显示
  17. const SHOW_COMMENT = 1; //评论批准显示
  18. const GOODS_COMMENT_TYPE = 0; //商品评论
  19. const ARTICLE_COMMENT_TYPE = 1; //文章评论
  20. //获取列表
  21. public static function getList(array $param)
  22. {
  23. extract($param); //参数:limit,offset
  24. $where['user_id'] = $user_id;
  25. $where['status'] = self::SHOW_COMMENT;
  26. $limit = isset($limit) ? $limit : 10;
  27. $offset = isset($offset) ? $offset : 0;
  28. $model = new self;
  29. if(isset($comment_rank)){$where['comment_rank'] = $comment_rank;} //评价分
  30. if(isset($id_value)){$where['id_value'] = $id_value;} //商品的id
  31. if(isset($comment_type)){$where['comment_type'] = $comment_type;} //0商品评价,1文章评价
  32. if(isset($parent_id)){$where['parent_id'] = $parent_id;}
  33. $model = $model->where($where);
  34. $res['count'] = $model->count();
  35. $res['list'] = array();
  36. if($res['count']>0)
  37. {
  38. $res['list'] = $model->skip($offset)->take($limit)->orderBy('id','desc')->get();
  39. }
  40. return $res;
  41. }
  42. public static function getOne($where)
  43. {
  44. return self::where($where)->first();
  45. }
  46. public static function add(array $data)
  47. {
  48. if(!isset($data['comment_type']) || !isset($data['id_value']) || !isset($data['user_id']) || $data['comment_type']===null || $data['id_value']===null || $data['user_id']===null)
  49. {
  50. return ReturnData::create(ReturnData::PARAMS_ERROR);
  51. }
  52. if(!isset($data['content']) && !isset($data['comment_rank']))
  53. {
  54. return ReturnData::create(ReturnData::PARAMS_ERROR);
  55. }
  56. else
  57. {
  58. if($data['content']===null && $data['comment_rank']===null)
  59. {
  60. return ReturnData::create(ReturnData::PARAMS_ERROR);
  61. }
  62. }
  63. if(self::where(array('user_id'=>$data['user_id'],'id_value'=>$data['id_value'],'comment_type'=>$data['comment_type']))->first()){return ReturnData::create(ReturnData::SYSTEM_FAIL,null,'亲,您已经评价啦!');}
  64. if ($id = self::insertGetId($data))
  65. {
  66. return ReturnData::create(ReturnData::SUCCESS,$id);
  67. }
  68. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  69. }
  70. //批量添加
  71. public static function batchAdd(array $data)
  72. {
  73. $res = '';
  74. if($data)
  75. {
  76. foreach($data as $k=>$v)
  77. {
  78. $id = self::add($v);
  79. if($id['code']==0){$res[] = $id['data'];}else{return $id;}
  80. }
  81. return ReturnData::create(ReturnData::SUCCESS,$res);
  82. }
  83. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  84. }
  85. public static function modify($where, array $data)
  86. {
  87. if (self::where($where)->update($data))
  88. {
  89. return true;
  90. }
  91. return false;
  92. }
  93. //删除一条记录
  94. public static function remove(array $data)
  95. {
  96. if(!self::where(array('user_id'=>$data['user_id'],'comment_type'=>$data['comment_type'],'id_value'=>$data['id_value']))->first()){return '商品尚未评价';}
  97. if (self::where(array('user_id'=>$data['user_id'],'comment_type'=>$data['comment_type'],'id_value'=>$data['id_value']))->delete() === false)
  98. {
  99. return false;
  100. }
  101. return true;
  102. }
  103. }