123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- <?php
- namespace api\modules\v1\controllers;
- use common\library\Sms;
- use common\library\ZM_Geohash;
- use common\models\Appointment;
- use common\models\Building;
- use common\models\Comment;
- use common\models\ImageSource;
- use common\models\MaterType;
- use common\models\Reply;
- use common\models\UserCompany;
- use common\models\UserInfo;
- use common\models\UserMater;
- use yii\rest\ActiveController;
- use common\library\Apireturn;
- use yii\helpers\ArrayHelper;
- use yii\filters\auth\QueryParamAuth;
- use yii;
- class BuildingController extends ActiveController
- {
- public $modelClass = 'common\models';
- const DISPLAY = 10;//显示条数
- const DISTANCE = 5000; //距离
- public function behaviors()
- {
- return ArrayHelper::merge(parent::behaviors(), [
- 'authenticator' => [
- 'class' => QueryParamAuth::className(),
- 'tokenParam' => "token",//access-token修改为token
- 'optional' => [//不需要认证方法名 array
- 'list'
- ],
- ]
- ]);
- }
- /**
- * 首页列表
- * @return array
- */
- public function actionList()
- {
- $latitude = Yii::$app->request->post('latitude'); //纬度范围为-90~90
- $longitude = Yii::$app->request->post('longitude'); //经度范围为-180~180
- $type = Yii::$app->request->post('type');//类型
- $materType = Yii::$app->request->post('materType');//建材商类型id
- $page = Yii::$app->request->post('page', 1);//分页页数
- $display = Yii::$app->request->post('display', self::DISPLAY);//分页页数
- $count = 0;
- $list = array();
- switch ($type)
- {
- case Building::TYPE_SITE :
- //$list = Building::findBySql("select id,name,latitude,longitude,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE type = :type AND posted = :posted AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ORDER BY distance ASC LIMIT ".($page-1)*$display.",".$display, [':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_SITE,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE])->asArray()->all();
- $list = Building::findBySql("select id,name,latitude,longitude,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE type = :type AND posted = :posted AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ", [':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_SITE,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE])->asArray()->all();
- $count = Building::findBySql("select id,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE type = :type AND posted = :posted AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ", [':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_SITE,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE])->count();
- break;
- case Building::TYPE_HOUSE :
- //$list = Building::findBySql("select id,name,latitude,longitude,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE type = :type AND posted = :posted AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ORDER BY distance ASC LIMIT ".($page-1)*$display.",".$display, [':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_HOUSE,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE])->asArray()->all();
- $list = Building::findBySql("select id,name,latitude,longitude,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE type = :type AND posted = :posted AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ", [':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_HOUSE,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE])->asArray()->all();
- $count = Building::findBySql("select id,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE type = :type AND posted = :posted AND opening_begin < :time AND opening_end > :time HAVING distance < :distance ", [':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_HOUSE,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE])->count();
- break;
- case 10 : //建材
- $where = "";
- if(!empty($materType) && is_numeric($materType))
- $where = " AND type_id = ".$materType;
- //$list = UserMater::findBySql("select id,company,latitude,longitude,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%user_mater}} WHERE member = :member ".$where." HAVING distance <= :distance ORDER BY distance ASC LIMIT ".($page-1)*$display.",".$display, [':latitude' => $latitude, ':longitude' => $longitude, ':member' => UserMater::MEMBER_YES, ':distance' => self::DISTANCE])->asArray()->all();
- $list = UserMater::findBySql("select id,company,latitude,longitude,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%user_mater}} WHERE member = :member ".$where." HAVING distance <= :distance", [':latitude' => $latitude, ':longitude' => $longitude, ':member' => UserMater::MEMBER_YES, ':distance' => self::DISTANCE])->asArray()->all();
- $count = UserMater::findBySql("select id,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%user_mater}} WHERE member = :member ".$where." HAVING distance <= :distance ", [':latitude' => $latitude, ':longitude' => $longitude, ':member' => UserMater::MEMBER_YES, ':distance' => self::DISTANCE])->count();
- break;
- default :
- break;
- }
- return Apireturn::sent(1,'success',200,array('count'=>$count,'list'=>$list));
- }
- /**
- * 建材商分类
- * @return array
- */
- public function actionMaterType()
- {
- $list = MaterType::find()->select('id,name')->where(['status'=>MaterType::STATUS_YES])->orderBy("sort ASC,id DESC")->asArray()->all();
- return Apireturn::sent(1,'success',200,$list);
- }
- /**
- * 工地/样板房详情
- */
- public function actionInfo()
- {
- $id = Yii::$app->request->post('id');
- $latitude = Yii::$app->request->post('latitude');
- $longitude = Yii::$app->request->post('longitude');
- $model = Building::find()->where(['id'=>$id,'posted'=>Building::POSTED_YES])->select('id,name,type,latitude,longitude,layout,budget,stage,style,acreage,pattern,address,opening_begin,opening_end,manager_id,designer_id,view')->one();
- if(empty($model))
- return Apireturn::sent(0,'找不到该工地',200);
- $data = array();
- foreach ($model->getOldAttributes() as $key => $value)
- {
- $data[$key] = $value;
- }
- //公司名称
- $data['company_name'] = "";
- $userCompany = UserCompany::find()->select('company')->where(['uid'=>Yii::$app->user->id])->one();
- if(!empty($userCompany))
- $data['company_name'] = $userCompany->company;
- //距离
- $data['distance'] = "";
- $data['nearlist'] = array();
- if(!empty($latitude)&&!empty($longitude))
- {
- $geohash = new ZM_Geohash();
- if(!empty($model->latitude) && !empty($model->longitude) )
- {
- $data['distance'] = sprintf("%.1f",$geohash->getDistance($latitude,$longitude,$model->latitude,$model->longitude)/1000);
- }
- $nearlist = Building::findBySql("select id,name,layout,acreage,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} where posted = :posted AND type = :type and opening_begin < :time and opening_end > :time AND id != :id HAVING distance < :distance ORDER BY distance ASC LIMIT 0,2", [':posted'=>Building::POSTED_YES,':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => self::DISTANCE,':id'=>$id,':type'=>$data['type']])->all();
- foreach ($nearlist as $near_key => $near_val)
- {
- $data['nearlist'][$near_key]['id'] = $near_val->id;
- $data['nearlist'][$near_key]['name'] = $near_val->name;
- $data['nearlist'][$near_key]['layout'] = $near_val->layout;
- $data['nearlist'][$near_key]['acreage'] = $near_val->acreage;
- $data['nearlist'][$near_key]['distance'] = sprintf("%.1f",$near_val->distance/1000);
- $data['nearlist'][$near_key]['pic'] = !empty($near_val->all) ? $near_val->all->pic : "";
- }
- }
- //户型图
- $data['flat']="";
- if(!empty($model->flat->pic))
- $data['flat'] = $model->flat->pic;
- //设计师详情
- $data['designer'] = array();
- if(!empty($model->designer))
- {
- $data['designer']['id'] = $model->designer->id;
- $data['designer']['pic'] = !empty($model->designer->image->pic) ? $model->designer->image->pic : "";
- $data['designer']['realname'] = $model->designer->realname;
- }
- //项目经理详情
- $data['manager'] = array();
- if(!empty($model->manager))
- {
- $data['manager']['id'] = $model->manager->id;
- $data['manager']['pic'] = !empty($model->manager->image->pic) ? $model->manager->image->pic : "";
- $data['manager']['realname'] = $model->manager->realname;
- }
- //轮播图列表
- $data['imgs'] = array();
- if(!empty($model->allimg))
- {
- foreach ($model->allimg as $img_val)
- {
- $data['imgs'][] = $img_val->pic;
- }
- }
- //预约人列表
- $data['appoints'] = array();
- if(!empty($model->appointment))
- {
- foreach ($model->appointment as $user)
- {
- $data['appoints'][] = !empty($user->user->userinfo->portrait) ? UserInfo::imagesUrl($user->user->userinfo->portrait):"";
- }
- }
- $appointment = Appointment::find()->where('uid=:uid and pid=:pid and status=:status',[':uid'=>Yii::$app->user->id,':pid'=>$id,':status'=>Appointment::STATUS_DEAL])->andWhere(['>', 'booking_time', time()])->orderBy('updated_at desc')->one();
- if(!empty($appointment))
- $data['appointment'] = 1 ;
- else
- $data['appointment'] = 0 ;
- //浏览记录+1
- Yii::$app->db->createCommand()->update("{{%building}}",['view'=>$model->view+1],'id = :id',[':id'=>$id])->execute();
- return Apireturn::sent(1,'success',200,$data);
- }
- /**
- * 工地/样板房评论
- */
- public function actionCommentList()
- {
- $id = Yii::$app->request->post('id');
- $page = Yii::$app->request->POST('page', 1);//分页页数
- $display = Yii::$app->request->POST('display', self::DISPLAY);//分页页数
- $comment = Comment::find()->select("id,star,uid,content,created_at")->where('pid=:id and status = :status ',[':id'=>$id,':status'=>Comment::STATUS_ACTIVE])->offset(($page - 1) * $display)->limit($display)->orderBy("created_at DESC")->all();
- $data['list'] = array();
- 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]['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);
- }
- /**
- * 取消预约
- */
- public function actionCancelAppoint()
- {
- $id = Yii::$app->request->post('id');
- $model = Appointment::find()->where('uid=:uid and pid=:pid and status=:status', [':uid' => Yii::$app->user->id, ':pid' => $id, ':status' => Appointment::STATUS_DEAL])->orderBy('booking_time desc')->one();
- if (empty($model))
- return Apireturn::sent(0, '找不到记录');
- $model->status = Appointment::STATUS_DELETE;
- $model->updated_at = time();
- if ($model->validate() && $model->save())
- return Apireturn::sent(1, '取消成功');
- else
- return Apireturn::sent(0, '取消失败');
- }
- /**
- * 预约
- */
- public function actionAppointment()
- {
- $time = Yii::$app->request->post('time');
- $id = Yii::$app->request->post('id');
- if(empty($id)||empty($time))
- return Apireturn::sent(0, '缺少参数');
- if(empty(Yii::$app->user->identity->tel))
- return Apireturn::sent(0, '请先绑定手机号');
- $building = Building::find()->select('type,opening_begin,opening_end')->where(['id'=>$id,'posted'=>Building::POSTED_YES])->one();
- if(empty($building))
- return Apireturn::sent(0, '找不到记录');
- $time = strtotime($time);
- if($building->opening_begin > $time || $building->opening_end < $time)
- return Apireturn::sent(0, '不在预约时间内');
- $userinfo = UserInfo::find()->select('nickname')->where(['uid'=>Yii::$app->user->id])->one();
- $model = new Appointment();
- $model->uid = Yii::$app->user->id;
- $model->pid = $id;
- $model->type = $building->type;
- $model->applicant_tel = Yii::$app->user->identity->tel;
- $model->created_at = time();
- $model->updated_at = time();
- $model->booking_time = $time;
- $model->applicant_name = $userinfo->nickname;
- $model->status = Appointment::STATUS_DEAL;
- $model->state = Appointment::STATE_YTAY;
- if($model->validate() && $model->save()){
- //红包发放
- //$activiModel = new ActivityReceive();
- //$activiModel->sentRed($pid);
- $this->sendSms($id);
- return Apireturn::sent(1, '预约成功');
- }else{
- return Apireturn::sent(0, '预约失败');
- }
- return Apireturn::sent(1, 'success');
- }
- //预约发送短信
- public function sendSms($id)
- {
- $data = Building::findBySql("SELECT a.address,a.type,b.tel FROM {{%building}} AS a LEFT JOIN {{%user_company}} AS b ON a.uid = b.uid WHERE a.id = :id",[':id'=>$id])->asArray()->one();
- if(!empty($data)){
- $sms = new Sms();
- //用户
- if(!empty(Yii::$app->user->identity->tel)){
- if($data['type']==Building::TYPE_HOUSE)//样板房
- {
- $content = "业主您好!您已成功预约".$data['address']."的房子进行参观,具体参观的时间请联络平台客服热线400-0592-018!";
- }else{
- $content = "您已成功预约".$data['address'].",有问题请及时联系我们的客服400-0592-018";
- }
- $sms->SendMessage(Yii::$app->user->identity->tel,$content,false);
- }
- //装修公司
- if(!empty($data['tel']))
- {
- $content = "您好,你的".$data['address'].",有业主预约参观,请保持好现场形象管理,业主的信息请在".Yii::$app->params['sitetitle']."后台查看。";
- $sms->SendMessage($data['tel'],$content,false);
- }
- }
- }
- /**
- * 用户评论
- * @return array
- */
- public function actionComment()
- {
- $post = Yii::$app->request->post();
- if(empty($post['id']))
- return Apireturn::sent(0, '缺少id');
- $model = Building::find()->where(['id'=>$post['id']])->select('id,type,uid')->one();
- if(empty($model))
- return Apireturn::sent(0, '找不到记录');
- if(empty($post['star']))
- return Apireturn::sent(0, '选择星级');
- if(empty($post['content']))
- return Apireturn::sent(0, '评论内容不能为空');
- if(intval($post['star'])<=0 || intval($post['star'])>5)
- return Apireturn::sent(0, '星级错误');
- $common = new Comment();
- $common->pid = $post['id'];
- $common->type = $model->type;
- $common->uid = Yii::$app->user->id;
- $common->star = $post['star'];
- $common->content =$post['content'];
- $common->created_at = time();
- $common->updated_at = time();
- $common->status = Comment::STATUS_ACTIVE;
- $common->reply_uid = $model->uid;
- if($common->save()){
- $imgs =json_decode($post['imgs'],true);//上传的评论图片
- if(!empty($imgs))
- {
- foreach ($imgs as $key=>$val)
- {
- $imageSource = new ImageSource();
- $imageSource->type = ImageSource::TYPE_COMMENT;
- $imageSource->topid = $common->id;
- $imageSource->pic = $val;
- $imageSource->created_at = time();
- $imageSource->status = ImageSource::STATUS_YES;
- $imageSource->updated_at = time();
- $imageSource->save();
- }
- }
- return Apireturn::sent(1, '评论成功');
- }else{
- return Apireturn::sent(0, '评论失败');
- }
- }
- }
|