CommentController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use common\models\Building;
  4. use common\models\Comment;
  5. use common\models\Designer;
  6. use common\models\Feedback;
  7. use common\models\ImageSource;
  8. use common\models\Manager;
  9. use common\models\MaterType;
  10. use common\models\Reply;
  11. use common\models\UserCompany;
  12. use common\models\UserInfo;
  13. use yii\data\Pagination;
  14. use yii\rest\ActiveController;
  15. use common\library\Apireturn;
  16. use yii\helpers\ArrayHelper;
  17. use yii\filters\auth\QueryParamAuth;
  18. use yii;
  19. class CommentController extends ActiveController
  20. {
  21. const DISPLAY = 10;//显示条数
  22. public $modelClass = 'common\models';
  23. public function behaviors()
  24. {
  25. return ArrayHelper::merge(parent::behaviors(), [
  26. 'authenticator' => [
  27. 'class' => QueryParamAuth::className(),
  28. 'tokenParam' => "token",//access-token修改为token
  29. 'optional' => [//不需要认证方法名 array
  30. ],
  31. ]
  32. ]);
  33. }
  34. // public function actionCommentlist(){
  35. // $page = Yii::$app->request->POST('page', 1);
  36. // $tempcomment = Comment::find()
  37. // ->joinWith('houseinfo')->with(['userinfo','reply.from','reply.to'])
  38. // ->where('bd_building.uid=:uid',[':uid'=>Yii::$app->user->id])
  39. // ->orderBy('{{%comment}}.updated_at desc');
  40. // $tempcomment=$tempcomment->offset(($page-1)*self::DISPLAY)->limit(self::DISPLAY)->asArray()->all();
  41. // foreach ($tempcomment as $key =>$each){
  42. // $tempcomment[$key]['created_at']=Comment::time_tran($each['created_at']);
  43. // if(count($each['reply'])>0){
  44. // foreach ($each['reply'] as $eh_rl=> $eachrelay){
  45. // $tempcomment[$key]['reply'][$eh_rl]['created_at']=Comment::time_tran($eachrelay['created_at']);
  46. // }
  47. // }
  48. // }
  49. // $data['commentlist']=$tempcomment;
  50. // $data['companyname']=UserCompany::findOne(['uid'=>Yii::$app->user->id])->getAttribute('company');
  51. // return Apireturn::sent(1,'评论列表成功',200,$data);
  52. // }
  53. /**
  54. * 我的评价列表 --装修公司
  55. * @return array
  56. */
  57. public function actionCommentlist(){
  58. $page = Yii::$app->request->POST('page', 1);//分页页数
  59. $display = Yii::$app->request->POST('display', self::DISPLAY);//分页页数
  60. $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();
  61. $data['list'] = array();
  62. $company = UserCompany::find()->where(['uid'=>Yii::$app->user->id])->select('company')->one();
  63. $data['company_name'] = !empty($company) ? $company->company : "";
  64. if(!empty($comment))
  65. {
  66. foreach ($comment as $com_key => $com_val)
  67. {
  68. $data['list'][$com_key]['id'] = $com_val->id;
  69. $data['list'][$com_key]['star'] = $com_val->star;
  70. $data['list'][$com_key]['content'] = $com_val->content;
  71. $data['list'][$com_key]['created_at'] = Comment::time_tran($com_val->created_at);
  72. $data['list'][$com_key]['imgs'] = ImageSource::find()->select('pic')->where(['topid'=>$com_val->id,'type'=>ImageSource::TYPE_COMMENT,'status'=>ImageSource::STATUS_YES])->asArray()->all();
  73. if(!empty($com_val->userinfo))
  74. {
  75. $data['list'][$com_key]['portrait'] = !empty($com_val->userinfo->portrait) ? UserInfo::imagesUrl($com_val->userinfo->portrait):"";;
  76. $data['list'][$com_key]['nickname'] = $com_val->userinfo->nickname;
  77. }
  78. $data['list'][$com_key]['building'] =array();
  79. $building = Building::find()->where(['id'=>$com_val->pid])->select("id,name,layout,acreage,style")->one();
  80. if(!empty($building))
  81. {
  82. foreach ( $building->getOldAttributes() as $key =>$value)
  83. {
  84. $data['list'][$com_key]['building'][$key] = $value;
  85. }
  86. $data['list'][$com_key]['building']['backimg'] = empty($building->all) ? "" : $building->all->pic;
  87. }
  88. $data['list'][$com_key]['replys'] = array();
  89. $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();
  90. if(!empty($replys))
  91. {
  92. foreach ($replys as $rep_key => $rep_val)
  93. {
  94. $data['list'][$com_key]['replys'][$rep_key]['own'] = $rep_val->from_userid == Yii::$app->user->id ? 1 : 0 ;
  95. $data['list'][$com_key]['replys'][$rep_key]['content'] = $rep_val->content;
  96. $data['list'][$com_key]['replys'][$rep_key]['created_at'] = Comment::time_tran($rep_val->created_at);
  97. }
  98. }
  99. }
  100. }
  101. return Apireturn::sent(1,'success',200,$data);
  102. }
  103. /**
  104. * 我的评价列表 --用户
  105. * @return array
  106. */
  107. public function actionUcommentlist(){
  108. $page = Yii::$app->request->POST('page', 1);
  109. $tempcomment = Comment::find()
  110. ->joinWith('houseinfo')->with(['userinfo','houseinfo.all','reply.fro','reply.too'])
  111. ->where('{{%comment}}.uid=:uid',[':uid'=>Yii::$app->user->id])
  112. ->orderBy('{{%comment}}.updated_at desc');
  113. $tempcomment=$tempcomment->offset(($page-1)*self::DISPLAY)->limit(self::DISPLAY)->asArray()->all();
  114. foreach ($tempcomment as $key =>$each){
  115. if(!empty($each['userinfo'])) {
  116. $tempcomment[$key]['userinfo']['portrait'] = !empty($each['userinfo']['portrait']) ? UserInfo::imagesUrl($each['userinfo']['portrait']) : "";
  117. }
  118. $tempcomment[$key]['created_at']=Comment::time_tran($each['created_at']);
  119. $tempcomment[$key]['imgs']=ImageSource::find()->select('pic')->where(['topid'=>$each['id'],'type'=>ImageSource::TYPE_COMMENT,'status'=>ImageSource::STATUS_YES])->asArray()->all();
  120. if(count($each['reply'])>0){
  121. foreach ($each['reply'] as $eh_rl=> $eachrelay){
  122. $tempcomment[$key]['reply'][$eh_rl]['created_at']=Comment::time_tran($eachrelay['created_at']);
  123. }
  124. }
  125. }
  126. $data['commentlist']=$tempcomment;
  127. $data['name']=UserInfo::findOne(['uid'=>Yii::$app->user->id])->getAttribute('nickname');
  128. return Apireturn::sent(1,'评论列表成功',200,$data);
  129. }
  130. /**
  131. * 发表评论
  132. * @return array
  133. */
  134. public function actionComment(){
  135. $pic = Yii::$app->request->post('pic');
  136. $building = Building::find()->select('id,uid')->where(['id'=>Yii::$app->request->post('pid')])->one();
  137. if(empty($building))
  138. return Apireturn::sent(0,"找不到该工地/样板房",200);
  139. $picinfo = json_decode($pic,true);
  140. $model =new Comment();
  141. $model->load(Yii::$app->request->post(),'');
  142. $model->uid=Yii::$app->user->id;
  143. $model->reply_uid=$building->uid;
  144. $model->created_at=time();
  145. $model->updated_at=time();
  146. $model->status=Comment::STATUS_ACTIVE;
  147. if( $model->save()){
  148. if(count($picinfo)>0)
  149. {
  150. foreach ($picinfo as $eachinfo)
  151. {
  152. //新上传的图片
  153. if(empty($eachinfo['id']))
  154. {
  155. $newpic=new ImageSource();
  156. $newpic->pic=$eachinfo['imgurl'];
  157. $newpic->thumbnail=$eachinfo['imgurl'];
  158. $newpic->updated_at = time();
  159. $newpic->created_at = time();
  160. $newpic->topid = $model->id;
  161. $newpic->type = ImageSource::TYPE_COMMENT;
  162. $newpic->status=Comment::STATUS_ACTIVE;
  163. if(!($newpic->save()))
  164. {
  165. return Apireturn::sent(1, 'pic save fail', 200);
  166. }
  167. }
  168. }
  169. return Apireturn::sent(1, '图片保存成功', 200,$picinfo);
  170. }
  171. else
  172. {
  173. return Apireturn::sent(1, '图片保存成功', 200,$picinfo);
  174. }
  175. }
  176. return Apireturn::sent(0,$model->firstErrors,200);
  177. }
  178. }