🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 接口情况 作者:ncgis 时间:20150422 作用:获取商品评论 提交方法:GET 提交参数 ~~~ good_id:商品id limit:每页数量默认10[可选] page:当前页 默认1 [可选] ~~~ 所在文件:product 接口地址:http://test.lingsq.com/index.php/api?method=lingsqapi.api.product.commentdetail&good_id=[good\_id]&limit=[limit]&page=[page] 返回数据样式: ~~~ { "status":false, "comment_count":1, "data": [ { "author":"\u4e70\u5bb6\uff1ab****6", "time":"2015-04-22 08:56:42", "comment":"\u5f88\u597d\u6211\u6536\u5230\u8d27\u4e86", "items": [ {"author":"b****6","time":"2015-04-22 08:57:35","comment":"\u4e0d\u9519\u54e6"} ] } ], "goods_point": { "avg_num":"5.00", "avg":5, "count":"1" } } ~~~ 数据说明 ~~~ status:用户登录状态 comment_count:评论数量 author:评论用户 time:评论时间 comment:评论 items:追加评论项 avg_num:平均数 avg:(不晓得和上面的有什么区别) count:评论总数 ~~~ ## 实现代码 ~~~ public function commentdetail() { if(empty($_GET["good_id"])) { $back["errNo"]=20005; $back["errMsg"]=$this->errMsg($back["errNo"]); return $back; } $page=empty($_GET["page"])?1:$_GET["page"]; $limit=$_GET["limit"]; $page = intval($page); $gid = intval($_GET["good_id"]); $limit = $limit?intval($limit):10; $offset = ($page-1)*$limit; $aData = array(); $aData['status'] = $this->member_id?true:false; $objComment = &app::get('b2c')->model('member_comments'); $filter = array('type_id'=>$gid,'object_type'=>'discuss','for_comment_id'=>0,'display'=>'true','comments_type'=>'1','comment|noequal'=>'','comment|noequal'=>null); $aData['comment_count'] = $objComment->count($filter); $sql = " select c.comment_id,c.author,c.time,c.comment,c.addon,c.author_id,m.name as author_alias from sdb_b2c_member_comments as c left join sdb_b2c_members as m on c.author_id=m.member_id left join sdb_b2c_order_items as i on c.order_id=i.order_id and c.type_id=i.goods_id where c.object_type='discuss' and c.for_comment_id=0 and c.type_id=".intval($gid)." and c.display='true' and c.comments_type='1' and c.comment is not null and c.comment != '' group by comment_id,i.goods_id order by time desc limit {$offset},{$limit}"; $aData['data'] = array(); $aId = array(); foreach((array)$objComment->db->select($sql) as $val){ $val['addon'] = unserialize($val['addon']); if(!empty($val['author_alias'])) $val['author'] = $val['author_alias']; if(isset($val['addon']['hidden_name']) && $val['addon']['hidden_name'] == 'YES' && ($val['author_id'] !=0 || $val['author_id'] !=1)){ $val['author'] = mb_substr($val['author'], 0, 1, 'UTF-8').'****'.mb_substr($val['author'], mb_strlen($string, 'UTF-8')-1, 1, 'UTF-8'); } $val['author'] = '买家:'.$val['author']; $val['time'] = date('Y-m-d H:i:s',$val['time']); $aId[] = $val['comment_id']; unset($val['author_alias'],$val['author_id'],$val['addon']); $aData['data'][] = $val; } if(!empty($aId)){ $addition = array(); $temp = $objComment->getList('comment_id,for_comment_id',array('for_comment_id' => $aId,'comments_type'=>'3','display' => 'true')); foreach((array)$temp as $rows){ $aId[] = $rows['comment_id']; $addition[$rows['for_comment_id']][] = $rows['comment_id']; } $aReply = (array)kernel::single('business_message_disask')->getCommentsReply($aId, true); foreach((array)$aData['data'] as $key => $rows){ foreach($aReply as $rkey => $rrows){ $condition1 = $rows['comment_id'] == $rrows['for_comment_id']; $condition2 = !empty($addition) && isset($addition[$rows['comment_id']]) && in_array($rrows['for_comment_id'],$addition[$rows['comment_id']]); if($condition1 || $condition2){ $aData['data'][$key]['items'][] = array( 'author' => ($aReply[$rkey]['comments_type']==0&&$aReply[$rkey]['author']!='管理员')?'掌柜':$aReply[$rkey]['author'], 'time' => date('Y-m-d H:i:s',$aReply[$rkey]['time']), 'comment' => $aReply[$rkey]['comment'], ); } } unset($aData['data'][$key]['comment_id']); } } $args = array($gid,$page); $objGoods = app::get('b2c')->model('goods'); $objPoint = app::get('business')->model('comment_goods_point'); $aGoods_list = $objGoods->getRow('avg_point,comments_count',array('goods_id'=>$gid,'store_id|than'=>0)); $aData['goods_point'] = array('avg_num'=>$aGoods_list['avg_point'],'avg'=>$objPoint->star_class($aGoods_list['avg_point']),'count'=>$aGoods_list['comments_count']); return $aData; } ~~~