BuildingController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use common\library\Sms;
  4. use common\library\ZM_Geohash;
  5. use common\models\Appointment;
  6. use common\models\Building;
  7. use common\models\Comment;
  8. use common\models\ImageSource;
  9. use common\models\MaterType;
  10. use common\models\Reply;
  11. use common\models\UserCompany;
  12. use common\models\UserInfo;
  13. use common\models\UserMater;
  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 BuildingController extends ActiveController
  20. {
  21. public $modelClass = 'common\models';
  22. const DISPLAY = 10;//显示条数
  23. const DISTANCE = 5000; //距离
  24. public function behaviors()
  25. {
  26. return ArrayHelper::merge(parent::behaviors(), [
  27. 'authenticator' => [
  28. 'class' => QueryParamAuth::className(),
  29. 'tokenParam' => "token",//access-token修改为token
  30. 'optional' => [//不需要认证方法名 array
  31. 'list'
  32. ],
  33. ]
  34. ]);
  35. }
  36. /**
  37. * 首页列表
  38. * @return array
  39. */
  40. public function actionList()
  41. {
  42. $latitude = Yii::$app->request->post('latitude'); //纬度范围为-90~90
  43. $longitude = Yii::$app->request->post('longitude'); //经度范围为-180~180
  44. $type = Yii::$app->request->post('type');//类型
  45. $materType = Yii::$app->request->post('materType');//建材商类型id
  46. $page = Yii::$app->request->post('page', 1);//分页页数
  47. $display = Yii::$app->request->post('display', self::DISPLAY);//分页页数
  48. $count = 0;
  49. $list = array();
  50. switch ($type)
  51. {
  52. case Building::TYPE_SITE :
  53. //$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();
  54. $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();
  55. $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();
  56. break;
  57. case Building::TYPE_HOUSE :
  58. //$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();
  59. $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();
  60. $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();
  61. break;
  62. case 10 : //建材
  63. $where = "";
  64. if(!empty($materType) && is_numeric($materType))
  65. $where = " AND type_id = ".$materType;
  66. //$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();
  67. $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();
  68. $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();
  69. break;
  70. default :
  71. break;
  72. }
  73. return Apireturn::sent(1,'success',200,array('count'=>$count,'list'=>$list));
  74. }
  75. /**
  76. * 建材商分类
  77. * @return array
  78. */
  79. public function actionMaterType()
  80. {
  81. $list = MaterType::find()->select('id,name')->where(['status'=>MaterType::STATUS_YES])->orderBy("sort ASC,id DESC")->asArray()->all();
  82. return Apireturn::sent(1,'success',200,$list);
  83. }
  84. /**
  85. * 工地/样板房详情
  86. */
  87. public function actionInfo()
  88. {
  89. $id = Yii::$app->request->post('id');
  90. $latitude = Yii::$app->request->post('latitude');
  91. $longitude = Yii::$app->request->post('longitude');
  92. $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();
  93. if(empty($model))
  94. return Apireturn::sent(0,'找不到该工地',200);
  95. $data = array();
  96. foreach ($model->getOldAttributes() as $key => $value)
  97. {
  98. $data[$key] = $value;
  99. }
  100. //公司名称
  101. $data['company_name'] = "";
  102. $userCompany = UserCompany::find()->select('company')->where(['uid'=>Yii::$app->user->id])->one();
  103. if(!empty($userCompany))
  104. $data['company_name'] = $userCompany->company;
  105. //距离
  106. $data['distance'] = "";
  107. $data['nearlist'] = array();
  108. if(!empty($latitude)&&!empty($longitude))
  109. {
  110. $geohash = new ZM_Geohash();
  111. if(!empty($model->latitude) && !empty($model->longitude) )
  112. {
  113. $data['distance'] = sprintf("%.1f",$geohash->getDistance($latitude,$longitude,$model->latitude,$model->longitude)/1000);
  114. }
  115. $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();
  116. foreach ($nearlist as $near_key => $near_val)
  117. {
  118. $data['nearlist'][$near_key]['id'] = $near_val->id;
  119. $data['nearlist'][$near_key]['name'] = $near_val->name;
  120. $data['nearlist'][$near_key]['layout'] = $near_val->layout;
  121. $data['nearlist'][$near_key]['acreage'] = $near_val->acreage;
  122. $data['nearlist'][$near_key]['distance'] = sprintf("%.1f",$near_val->distance/1000);
  123. $data['nearlist'][$near_key]['pic'] = !empty($near_val->all) ? $near_val->all->pic : "";
  124. }
  125. }
  126. //户型图
  127. $data['flat']="";
  128. if(!empty($model->flat->pic))
  129. $data['flat'] = $model->flat->pic;
  130. //设计师详情
  131. $data['designer'] = array();
  132. if(!empty($model->designer))
  133. {
  134. $data['designer']['id'] = $model->designer->id;
  135. $data['designer']['pic'] = !empty($model->designer->image->pic) ? $model->designer->image->pic : "";
  136. $data['designer']['realname'] = $model->designer->realname;
  137. }
  138. //项目经理详情
  139. $data['manager'] = array();
  140. if(!empty($model->manager))
  141. {
  142. $data['manager']['id'] = $model->manager->id;
  143. $data['manager']['pic'] = !empty($model->manager->image->pic) ? $model->manager->image->pic : "";
  144. $data['manager']['realname'] = $model->manager->realname;
  145. }
  146. //轮播图列表
  147. $data['imgs'] = array();
  148. if(!empty($model->allimg))
  149. {
  150. foreach ($model->allimg as $img_val)
  151. {
  152. $data['imgs'][] = $img_val->pic;
  153. }
  154. }
  155. //预约人列表
  156. $data['appoints'] = array();
  157. if(!empty($model->appointment))
  158. {
  159. foreach ($model->appointment as $user)
  160. {
  161. $data['appoints'][] = !empty($user->user->userinfo->portrait) ? UserInfo::imagesUrl($user->user->userinfo->portrait):"";
  162. }
  163. }
  164. $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();
  165. if(!empty($appointment))
  166. $data['appointment'] = 1 ;
  167. else
  168. $data['appointment'] = 0 ;
  169. //浏览记录+1
  170. Yii::$app->db->createCommand()->update("{{%building}}",['view'=>$model->view+1],'id = :id',[':id'=>$id])->execute();
  171. return Apireturn::sent(1,'success',200,$data);
  172. }
  173. /**
  174. * 工地/样板房评论
  175. */
  176. public function actionCommentList()
  177. {
  178. $id = Yii::$app->request->post('id');
  179. $page = Yii::$app->request->POST('page', 1);//分页页数
  180. $display = Yii::$app->request->POST('display', self::DISPLAY);//分页页数
  181. $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();
  182. $data['list'] = array();
  183. if(!empty($comment))
  184. {
  185. foreach ($comment as $com_key => $com_val)
  186. {
  187. $data['list'][$com_key]['id'] = $com_val->id;
  188. $data['list'][$com_key]['star'] = $com_val->star;
  189. $data['list'][$com_key]['content'] = $com_val->content;
  190. $data['list'][$com_key]['created_at'] = Comment::time_tran($com_val->created_at);
  191. $data['list'][$com_key]['imgs'] = ImageSource::find()->select('pic')->where(['topid'=>$com_val->id,'type'=>ImageSource::TYPE_COMMENT,'status'=>ImageSource::STATUS_YES])->asArray()->all();
  192. if(!empty($com_val->userinfo))
  193. {
  194. $data['list'][$com_key]['portrait'] = !empty($com_val->userinfo->portrait) ? UserInfo::imagesUrl($com_val->userinfo->portrait):"";;
  195. $data['list'][$com_key]['nickname'] = $com_val->userinfo->nickname;
  196. }
  197. $data['list'][$com_key]['replys'] = array();
  198. $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();
  199. if(!empty($replys))
  200. {
  201. foreach ($replys as $rep_key => $rep_val)
  202. {
  203. $data['list'][$com_key]['replys'][$rep_key]['own'] = $rep_val->from_userid == Yii::$app->user->id ? 1 : 0 ;
  204. $data['list'][$com_key]['replys'][$rep_key]['content'] = $rep_val->content;
  205. $data['list'][$com_key]['replys'][$rep_key]['created_at'] = Comment::time_tran($rep_val->created_at);
  206. }
  207. }
  208. }
  209. }
  210. return Apireturn::sent(1,'success',200,$data);
  211. }
  212. /**
  213. * 取消预约
  214. */
  215. public function actionCancelAppoint()
  216. {
  217. $id = Yii::$app->request->post('id');
  218. $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();
  219. if (empty($model))
  220. return Apireturn::sent(0, '找不到记录');
  221. $model->status = Appointment::STATUS_DELETE;
  222. $model->updated_at = time();
  223. if ($model->validate() && $model->save())
  224. return Apireturn::sent(1, '取消成功');
  225. else
  226. return Apireturn::sent(0, '取消失败');
  227. }
  228. /**
  229. * 预约
  230. */
  231. public function actionAppointment()
  232. {
  233. $time = Yii::$app->request->post('time');
  234. $id = Yii::$app->request->post('id');
  235. if(empty($id)||empty($time))
  236. return Apireturn::sent(0, '缺少参数');
  237. if(empty(Yii::$app->user->identity->tel))
  238. return Apireturn::sent(0, '请先绑定手机号');
  239. $building = Building::find()->select('type,opening_begin,opening_end')->where(['id'=>$id,'posted'=>Building::POSTED_YES])->one();
  240. if(empty($building))
  241. return Apireturn::sent(0, '找不到记录');
  242. $time = strtotime($time);
  243. if($building->opening_begin > $time || $building->opening_end < $time)
  244. return Apireturn::sent(0, '不在预约时间内');
  245. $userinfo = UserInfo::find()->select('nickname')->where(['uid'=>Yii::$app->user->id])->one();
  246. $model = new Appointment();
  247. $model->uid = Yii::$app->user->id;
  248. $model->pid = $id;
  249. $model->type = $building->type;
  250. $model->applicant_tel = Yii::$app->user->identity->tel;
  251. $model->created_at = time();
  252. $model->updated_at = time();
  253. $model->booking_time = $time;
  254. $model->applicant_name = $userinfo->nickname;
  255. $model->status = Appointment::STATUS_DEAL;
  256. $model->state = Appointment::STATE_YTAY;
  257. if($model->validate() && $model->save()){
  258. //红包发放
  259. //$activiModel = new ActivityReceive();
  260. //$activiModel->sentRed($pid);
  261. $this->sendSms($id);
  262. return Apireturn::sent(1, '预约成功');
  263. }else{
  264. return Apireturn::sent(0, '预约失败');
  265. }
  266. return Apireturn::sent(1, 'success');
  267. }
  268. //预约发送短信
  269. public function sendSms($id)
  270. {
  271. $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();
  272. if(!empty($data)){
  273. $sms = new Sms();
  274. //用户
  275. if(!empty(Yii::$app->user->identity->tel)){
  276. if($data['type']==Building::TYPE_HOUSE)//样板房
  277. {
  278. $content = "业主您好!您已成功预约".$data['address']."的房子进行参观,具体参观的时间请联络平台客服热线400-0592-018!";
  279. }else{
  280. $content = "您已成功预约".$data['address'].",有问题请及时联系我们的客服400-0592-018";
  281. }
  282. $sms->SendMessage(Yii::$app->user->identity->tel,$content,false);
  283. }
  284. //装修公司
  285. if(!empty($data['tel']))
  286. {
  287. $content = "您好,你的".$data['address'].",有业主预约参观,请保持好现场形象管理,业主的信息请在".Yii::$app->params['sitetitle']."后台查看。";
  288. $sms->SendMessage($data['tel'],$content,false);
  289. }
  290. }
  291. }
  292. /**
  293. * 用户评论
  294. * @return array
  295. */
  296. public function actionComment()
  297. {
  298. $post = Yii::$app->request->post();
  299. if(empty($post['id']))
  300. return Apireturn::sent(0, '缺少id');
  301. $model = Building::find()->where(['id'=>$post['id']])->select('id,type,uid')->one();
  302. if(empty($model))
  303. return Apireturn::sent(0, '找不到记录');
  304. if(empty($post['star']))
  305. return Apireturn::sent(0, '选择星级');
  306. if(empty($post['content']))
  307. return Apireturn::sent(0, '评论内容不能为空');
  308. if(intval($post['star'])<=0 || intval($post['star'])>5)
  309. return Apireturn::sent(0, '星级错误');
  310. $common = new Comment();
  311. $common->pid = $post['id'];
  312. $common->type = $model->type;
  313. $common->uid = Yii::$app->user->id;
  314. $common->star = $post['star'];
  315. $common->content =$post['content'];
  316. $common->created_at = time();
  317. $common->updated_at = time();
  318. $common->status = Comment::STATUS_ACTIVE;
  319. $common->reply_uid = $model->uid;
  320. if($common->save()){
  321. $imgs =json_decode($post['imgs'],true);//上传的评论图片
  322. if(!empty($imgs))
  323. {
  324. foreach ($imgs as $key=>$val)
  325. {
  326. $imageSource = new ImageSource();
  327. $imageSource->type = ImageSource::TYPE_COMMENT;
  328. $imageSource->topid = $common->id;
  329. $imageSource->pic = $val;
  330. $imageSource->created_at = time();
  331. $imageSource->status = ImageSource::STATUS_YES;
  332. $imageSource->updated_at = time();
  333. $imageSource->save();
  334. }
  335. }
  336. return Apireturn::sent(1, '评论成功');
  337. }else{
  338. return Apireturn::sent(0, '评论失败');
  339. }
  340. }
  341. }