validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $query = OrderComment::find()->where(['goods_id' => $this->goods_id, 'is_delete' => 0, 'is_hide' => 0]); if ($this->score > 0) { if ($this->score == 1) { $score = [1, 2]; } elseif($this->score == 2) { $score = [3, 4]; } else { $score = [5]; } $query->andWhere([ 'score' => $score ]); } $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]); $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray() ->select('order_detail_id, is_virtual, virtual_user, virtual_avatar, score, content, pic_list, created_at, reply_content, user_id, saas_id , user_type, user_name, user_avatar')->all(); foreach ($list as $i => $item) { if (intval($item['user_type']) === OrderComment::USER_TYPE_CUSTOM) { $list[$i]['nickname'] = $item['user_name']; $list[$i]['avatar_url'] = $item['user_avatar']; } else { if ($item['saas_id']) { $saasUser = SaasUser::findOne($item['saas_id']); $list[$i]['nickname'] = $saasUser->name; $list[$i]['avatar_url'] = $saasUser->avatar; } else { $user = User::findOne($item['user_id']); $list[$i]['nickname'] = $user->nickname; $list[$i]['avatar_url'] = $user->avatar_url; } } $list[$i]['attr'] = Json::decode(OrderDetail::findOne($item['order_detail_id'])->attr); $list[$i]['addtime'] = date('Y-m-d', $item['created_at']); $list[$i]['pic_list'] = json_decode($item['pic_list']); $list[$i]['nickname'] = $this->substr_cut($list[$i]['nickname']); if ($item['is_virtual'] == 1) { $list[$i]['nickname'] = $this->substr_cut($item['virtual_user']); $list[$i]['avatar_url'] = $item['virtual_avatar']; } unset($list[$i]['virtual_avatar']); unset($list[$i]['is_virtual']); unset($list[$i]['virtual_user']); }; $data = [ 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => $list, 'comment_count' => $this->countData(), ]; return [ 'code' =>0, 'data' => $data ]; } public function countData() { if (!$this->validate()) { return $this->errorResponse; } $score_all = OrderComment::find()->alias('oc') ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0,])->count(); $score_3 = OrderComment::find()->alias('oc') ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [5,4]])->count(); $score_2 = OrderComment::find()->alias('oc') ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [3,2]])->count(); $score_1 = OrderComment::find()->alias('oc') ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [1]])->count(); return (object)[ 'score_all' => $score_all ? $score_all : 0, 'score_3' => $score_3 ? $score_3 : 0, 'score_2' => $score_2 ? $score_2 : 0, 'score_1' => $score_1 ? $score_1 : 0, ]; } // 将用户名 做隐藏 private function substr_cut($user_name) { $strlen = mb_strlen($user_name, 'utf-8'); $firstStr = mb_substr($user_name, 0, 1, 'utf-8'); $lastStr = mb_substr($user_name, -1, 1, 'utf-8'); return $strlen <= 2 ? $firstStr . '*' : $firstStr . str_repeat("*", 3) . $lastStr; } }