123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace api\modules\v1\controllers;
- use common\models\Building;
- use common\models\Comment;
- use common\models\Designer;
- use common\models\Feedback;
- use common\models\ImageSource;
- use common\models\Manager;
- use common\models\MaterType;
- use common\models\Reply;
- use common\models\UserCompany;
- use common\models\UserInfo;
- use yii\data\Pagination;
- use yii\rest\ActiveController;
- use common\library\Apireturn;
- use yii\helpers\ArrayHelper;
- use yii\filters\auth\QueryParamAuth;
- use yii;
- class CommentController extends ActiveController
- {
- const DISPLAY = 10;//显示条数
- public $modelClass = 'common\models';
- public function behaviors()
- {
- return ArrayHelper::merge(parent::behaviors(), [
- 'authenticator' => [
- 'class' => QueryParamAuth::className(),
- 'tokenParam' => "token",//access-token修改为token
- 'optional' => [//不需要认证方法名 array
- ],
- ]
- ]);
- }
- // public function actionCommentlist(){
- // $page = Yii::$app->request->POST('page', 1);
- // $tempcomment = Comment::find()
- // ->joinWith('houseinfo')->with(['userinfo','reply.from','reply.to'])
- // ->where('bd_building.uid=:uid',[':uid'=>Yii::$app->user->id])
- // ->orderBy('{{%comment}}.updated_at desc');
- // $tempcomment=$tempcomment->offset(($page-1)*self::DISPLAY)->limit(self::DISPLAY)->asArray()->all();
- // foreach ($tempcomment as $key =>$each){
- // $tempcomment[$key]['created_at']=Comment::time_tran($each['created_at']);
- // if(count($each['reply'])>0){
- // foreach ($each['reply'] as $eh_rl=> $eachrelay){
- // $tempcomment[$key]['reply'][$eh_rl]['created_at']=Comment::time_tran($eachrelay['created_at']);
- // }
- // }
- // }
- // $data['commentlist']=$tempcomment;
- // $data['companyname']=UserCompany::findOne(['uid'=>Yii::$app->user->id])->getAttribute('company');
- // return Apireturn::sent(1,'评论列表成功',200,$data);
- // }
- /**
- * 我的评价列表 --装修公司
- * @return array
- */
- public function actionCommentlist(){
- $page = Yii::$app->request->POST('page', 1);//分页页数
- $display = Yii::$app->request->POST('display', self::DISPLAY);//分页页数
- $comment = Comment::find()->select("id,pid,star,uid,content,created_at")->where('status = :status AND reply_uid = :uid ',[':status'=>Comment::STATUS_ACTIVE,':uid'=>Yii::$app->user->id])->offset(($page - 1) * $display)->limit($display)->orderBy("created_at DESC")->all();
- $data['list'] = array();
- $company = UserCompany::find()->where(['uid'=>Yii::$app->user->id])->select('company')->one();
- $data['company_name'] = !empty($company) ? $company->company : "";
- if(!empty($comment))
- {
- foreach ($comment as $com_key => $com_val)
- {
- $data['list'][$com_key]['id'] = $com_val->id;
- $data['list'][$com_key]['star'] = $com_val->star;
- $data['list'][$com_key]['content'] = $com_val->content;
- $data['list'][$com_key]['created_at'] = Comment::time_tran($com_val->created_at);
- $data['list'][$com_key]['imgs'] = ImageSource::find()->select('pic')->where(['topid'=>$com_val->id,'type'=>ImageSource::TYPE_COMMENT,'status'=>ImageSource::STATUS_YES])->asArray()->all();
- if(!empty($com_val->userinfo))
- {
- $data['list'][$com_key]['portrait'] = !empty($com_val->userinfo->portrait) ? UserInfo::imagesUrl($com_val->userinfo->portrait):"";;
- $data['list'][$com_key]['nickname'] = $com_val->userinfo->nickname;
- }
- $data['list'][$com_key]['building'] =array();
- $building = Building::find()->where(['id'=>$com_val->pid])->select("id,name,layout,acreage,style")->one();
- if(!empty($building))
- {
- foreach ( $building->getOldAttributes() as $key =>$value)
- {
- $data['list'][$com_key]['building'][$key] = $value;
- }
- $data['list'][$com_key]['building']['backimg'] = empty($building->all) ? "" : $building->all->pic;
- }
- $data['list'][$com_key]['replys'] = array();
- $replys = Reply::find()->where(['cid'=>$com_val->id,'status'=>Reply::STATUS_ACTIVE])->select('id,from_userid,to_userid,content,created_at')->orderBy('created_at ASC')->all();
- if(!empty($replys))
- {
- foreach ($replys as $rep_key => $rep_val)
- {
- $data['list'][$com_key]['replys'][$rep_key]['own'] = $rep_val->from_userid == Yii::$app->user->id ? 1 : 0 ;
- $data['list'][$com_key]['replys'][$rep_key]['content'] = $rep_val->content;
- $data['list'][$com_key]['replys'][$rep_key]['created_at'] = Comment::time_tran($rep_val->created_at);
- }
- }
- }
- }
- return Apireturn::sent(1,'success',200,$data);
- }
- /**
- * 我的评价列表 --用户
- * @return array
- */
- public function actionUcommentlist(){
- $page = Yii::$app->request->POST('page', 1);
- $tempcomment = Comment::find()
- ->joinWith('houseinfo')->with(['userinfo','houseinfo.all','reply.fro','reply.too'])
- ->where('{{%comment}}.uid=:uid',[':uid'=>Yii::$app->user->id])
- ->orderBy('{{%comment}}.updated_at desc');
- $tempcomment=$tempcomment->offset(($page-1)*self::DISPLAY)->limit(self::DISPLAY)->asArray()->all();
- foreach ($tempcomment as $key =>$each){
- if(!empty($each['userinfo'])) {
- $tempcomment[$key]['userinfo']['portrait'] = !empty($each['userinfo']['portrait']) ? UserInfo::imagesUrl($each['userinfo']['portrait']) : "";
- }
- $tempcomment[$key]['created_at']=Comment::time_tran($each['created_at']);
- $tempcomment[$key]['imgs']=ImageSource::find()->select('pic')->where(['topid'=>$each['id'],'type'=>ImageSource::TYPE_COMMENT,'status'=>ImageSource::STATUS_YES])->asArray()->all();
- if(count($each['reply'])>0){
- foreach ($each['reply'] as $eh_rl=> $eachrelay){
- $tempcomment[$key]['reply'][$eh_rl]['created_at']=Comment::time_tran($eachrelay['created_at']);
- }
- }
- }
- $data['commentlist']=$tempcomment;
- $data['name']=UserInfo::findOne(['uid'=>Yii::$app->user->id])->getAttribute('nickname');
- return Apireturn::sent(1,'评论列表成功',200,$data);
- }
- /**
- * 发表评论
- * @return array
- */
- public function actionComment(){
- $pic = Yii::$app->request->post('pic');
- $building = Building::find()->select('id,uid')->where(['id'=>Yii::$app->request->post('pid')])->one();
- if(empty($building))
- return Apireturn::sent(0,"找不到该工地/样板房",200);
- $picinfo = json_decode($pic,true);
- $model =new Comment();
- $model->load(Yii::$app->request->post(),'');
- $model->uid=Yii::$app->user->id;
- $model->reply_uid=$building->uid;
- $model->created_at=time();
- $model->updated_at=time();
- $model->status=Comment::STATUS_ACTIVE;
- if( $model->save()){
- if(count($picinfo)>0)
- {
- foreach ($picinfo as $eachinfo)
- {
- //新上传的图片
- if(empty($eachinfo['id']))
- {
- $newpic=new ImageSource();
- $newpic->pic=$eachinfo['imgurl'];
- $newpic->thumbnail=$eachinfo['imgurl'];
- $newpic->updated_at = time();
- $newpic->created_at = time();
- $newpic->topid = $model->id;
- $newpic->type = ImageSource::TYPE_COMMENT;
- $newpic->status=Comment::STATUS_ACTIVE;
- if(!($newpic->save()))
- {
- return Apireturn::sent(1, 'pic save fail', 200);
- }
- }
- }
- return Apireturn::sent(1, '图片保存成功', 200,$picinfo);
- }
- else
- {
- return Apireturn::sent(1, '图片保存成功', 200,$picinfo);
- }
- }
- return Apireturn::sent(0,$model->firstErrors,200);
- }
- }
|