BuildingController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace frontend\controllers;
  3. use api\modules\v1\controllers\ImageUploadController;
  4. use common\library\LMUploadFile;
  5. use common\library\Sms;
  6. use common\library\ZM_Geohash;
  7. use common\models\ActivityReceive;
  8. use common\models\Appointment;
  9. use common\models\Building;
  10. use common\models\Comment;
  11. use common\models\Designer;
  12. use common\models\ImageSource;
  13. use common\models\Manager;
  14. use common\models\User;
  15. use common\models\UserInfo;
  16. use Yii;
  17. use yii\db\Connection;
  18. use yii\web\Cookie;
  19. use yii\web\UploadedFile;
  20. class BuildingController extends BasewechatController
  21. {
  22. public $imgtype = array('bmp','jpg','png','gif','jpeg');
  23. public function actionIndex()
  24. {
  25. return $this->render('index');
  26. }
  27. //工地与样板房详情
  28. public function actionDetail()
  29. {
  30. $type = Yii::$app->request->get('type');//用户分享,有值显示返回图标
  31. $cookie = Yii::$app->request->cookies;
  32. $userid = YII::$app->user->id;
  33. $user = User::find()->select('tel')->where('id=:id and status=:status',[':id'=>$userid,':status'=>User::STATUS_ACTIVE])->one();
  34. $usertel = '';
  35. if(!empty($user)){
  36. $usertel = $user->tel;
  37. }
  38. $latitude = "";
  39. $longitude = "";
  40. if($cookie->has('localtion'))
  41. {
  42. $lcookie = $cookie->getValue('localtion');
  43. $latitude=$lcookie['lat'];
  44. $longitude=$lcookie['lng'];
  45. }
  46. $id = \Yii::$app->request->get('id');
  47. $datas = Building::find()->where('id=:id and posted=:posted',[':id'=>$id,'posted'=>Building::POSTED_YES])->one();
  48. if(!empty($datas)){
  49. $geohash = new ZM_Geohash();
  50. $distance="";
  51. $nearlist =[];
  52. if(!empty($latitude)&&!empty($longitude))
  53. {
  54. if(!empty($datas->latitude)&&!empty($datas->longitude)){
  55. $distance = $geohash->getDistance($latitude,$longitude,$datas->latitude,$datas->longitude);
  56. }
  57. $nearlist = Building::findBySql("select id,name,layout,acreage,get_Distance(latitude,longitude,:latitude,:longitude) as distance from bd_building where posted=1 and opening_begin < :time and opening_end > :time AND id != :id HAVING distance < :distance ORDER BY distance ASC LIMIT 0,2", [':latitude' => $latitude, ':longitude' => $longitude, ':time' => time(), ':distance' => 10000,':id'=>$id])->joinWith('image')->asArray()->all();
  58. if(!empty($nearlist)){
  59. for($i=0;$i<count($nearlist);$i++) {
  60. $buildid = $nearlist[$i]['id'];
  61. $images = ImageSource::find()->select('pic')->where('topid=:id and status=:status', [':id' => $buildid, ':status' => ImageSource::STATUS_YES])->andWhere(['type' => [20,30,40,50]])->asArray()->all();
  62. $nearlist[$i]['image'] = $images;
  63. }
  64. }
  65. }
  66. $comments = Comment::find()->where('pid=:pid and status=:status',[':pid'=>$id,':status'=>Comment::STATUS_ACTIVE])->orderBy('created_at DESC')->all();
  67. $arr = Appointment::find()->where('uid=:uid and pid=:pid and status=:status',[':uid'=>$userid,':pid'=>$id,':status'=>Appointment::STATUS_DEAL])->andWhere(['>', 'booking_time', time()])->orderBy('updated_at desc')->one();
  68. $datas->view += 1;
  69. $datas->save();
  70. $wechat = new \common\library\WeChat();
  71. $ticket = $wechat->getTicket();
  72. $jssdk = new \common\library\Jssdk(Yii::$app->params['wechat']['appID'],Yii::$app->params['wechat']['appsecret'],$ticket);
  73. $signPackage = $jssdk->getSignPackage();
  74. return $this->render('site',['datas'=>$datas,'arr'=>$arr,'comments'=>$comments,'distance'=>$distance,'nearlist'=>$nearlist,'usertel'=>$usertel,'signPackage'=>$signPackage,'type'=>$type]);
  75. }else{
  76. var_dump('该房子未通过审核');exit;
  77. Yii::$app->getSession()->setFlash('error', '该房子未通过审核!');
  78. return $this->render('index');
  79. }
  80. }
  81. //设计师详情
  82. public function actionDesigner()
  83. {
  84. $cookie = Yii::$app->request->cookies;
  85. $latitude = "";
  86. $longitude = "";
  87. if($cookie->has('localtion'))
  88. {
  89. $lcookie = $cookie->getValue('localtion');
  90. $latitude=$lcookie['lat'];
  91. $longitude=$lcookie['lng'];
  92. }
  93. $id = \Yii::$app->request->get('id');
  94. $data = Designer::find()->where('id=:id and status=:status',[':id'=>$id,':status'=>Designer::STATUS_YES])->one();
  95. // $arrs = Building::findBySql("SELECT *,get_Distance(latitude,longitude,:latitude,:longitude) as distance FROM {{%building}} WHERE designer_id = :designer_id AND posted = :posted ORDER BY created_at desc", [':designer_id'=>$id,':latitude' => $latitude, ':longitude' => $longitude,':posted'=>Building::POSTED_YES])->asArray()->all();
  96. $arrs = Building::find()->where("designer_id = :designer_id AND posted = :posted",[':designer_id'=>$id,':posted'=>Building::POSTED_YES])->orderBy('created_at DESC')->all();
  97. $geohash = new ZM_Geohash();
  98. foreach ($arrs as $list)
  99. {
  100. if(!empty($latitude)&&!empty($longitude)&&!empty($list->latitude)&&!empty($list->longitude))
  101. {
  102. $distance = $geohash->getDistance($latitude,$longitude,$list->latitude,$list->longitude);
  103. $list->distance = $distance;
  104. }
  105. }
  106. //
  107. return $this->render('designer',['data'=>$data,'arrs'=>$arrs]);
  108. }
  109. //项目经理详情
  110. public function actionManager()
  111. {
  112. $cookie = Yii::$app->request->cookies;
  113. $latitude = "";
  114. $longitude = "";
  115. if($cookie->has('localtion'))
  116. {
  117. $lcookie = $cookie->getValue('localtion');
  118. $latitude=$lcookie['lat'];
  119. $longitude=$lcookie['lng'];
  120. }
  121. $id = \Yii::$app->request->get('id');
  122. $data = Manager::find()->where('id=:id and status=:status',[':id'=>$id,':status'=>Manager::STATUS_YES])->one();
  123. // $arrs = Building::findBySql("SELECT *,get_Distance(latitude,longitude,:latitude,:longitude) as distance FROM {{%building}} WHERE manager_id = :manager_id AND posted = :posted ORDER BY created_at desc", [':manager_id'=>$id,':latitude' => $latitude, ':longitude' => $longitude,':posted'=>Building::POSTED_YES])->asArray()->all();
  124. $arrs = Building::find()->where("manager_id = :manager_id AND posted = :posted",[':manager_id'=>$id,':posted'=>Building::POSTED_YES])->orderBy('created_at DESC')->all();
  125. $geohash = new ZM_Geohash();
  126. foreach ($arrs as $list)
  127. {
  128. if(!empty($latitude)&&!empty($longitude)&&!empty($list->latitude)&&!empty($list->longitude))
  129. {
  130. $distance = $geohash->getDistance($latitude,$longitude,$list->latitude,$list->longitude);
  131. $list->distance = $distance;
  132. }
  133. }
  134. return $this->render('manager',['data'=>$data,'arrs'=>$arrs]);
  135. }
  136. //评价页面
  137. public function actionEvaluate()
  138. {
  139. $id = \Yii::$app->request->get('id');
  140. $type= \Yii::$app->request->get('type');
  141. $userid = YII::$app->user->id;
  142. $data = UserInfo::find()->where('uid=:uid',[':uid'=>$userid])->one();
  143. $picture_form = new LMUploadFile();
  144. return $this->render('evaluate',['data'=>$data,'pid'=>$id,'type'=>$type,"picture_form" => $picture_form]);
  145. }
  146. //评价
  147. public function actionEvaluate1()
  148. {
  149. $star = Yii::$app->request->post('star');
  150. $content = Yii::$app->request->post('content');
  151. $pid = Yii::$app->request->post('pid');
  152. $type = Yii::$app->request->post('type');
  153. if(!empty($pid)){
  154. if(!empty($star)&&!empty($content)){
  155. $data = new Comment();
  156. $data->pid = $pid;
  157. $data->type = $type;
  158. $data->star = $star;
  159. $data->content = $content;
  160. $data->uid = Yii::$app->user->id;
  161. $data->created_at = time();
  162. $data->updated_at = time();
  163. $data->status = Comment::STATUS_ACTIVE;
  164. if($data->validate() && $data->save()){
  165. $imgs =json_decode(Yii::$app->request->post('imgs'),true);//上传的评论图片
  166. if(!empty($imgs))
  167. {
  168. foreach ($imgs as $key=>$val)
  169. {
  170. $imageSource = new ImageSource();
  171. $imageSource->type = ImageSource::TYPE_COMMENT;
  172. $imageSource->topid = $data->id;
  173. $imageSource->pic = $val;
  174. $imageSource->created_at = time();
  175. $imageSource->status = ImageSource::STATUS_YES;
  176. $imageSource->updated_at = time();
  177. $imageSource->save();
  178. }
  179. }
  180. $result=['sign'=>1,'msg'=>'评论成功'];
  181. }else{
  182. $result=['sign'=>0,'msg'=>'提交失败,请重新提交'];
  183. }
  184. }else{
  185. $result=['sign'=>0,'msg'=>'评价内容和评星不能为空'];
  186. }
  187. }else{
  188. $result=['sign'=>0,'msg'=>'房产pid数据有误'];
  189. }
  190. return json_encode($result);
  191. }
  192. /**
  193. * 上传评论图片
  194. * @return string
  195. */
  196. public function actionEvaluateimg()
  197. {
  198. if (Yii::$app->request->isPost) {
  199. $arr = array('pic'=>"","sign"=>1,"error"=>"");
  200. $file = $_FILES['mypic'] ;
  201. $files['name']=$file['name'];
  202. $files['type']=$file['type'];
  203. $files['tmp_name']=$file['tmp_name'];
  204. $files['error']=$file['error'];
  205. $files['size']=$file['size'];
  206. $folder = "/evaluate/".date("Ym")."/".date("d")."/";
  207. $file_path=\Yii::getAlias('@common/allupload').$folder;
  208. $result = $this->uploadFile($files,$file_path,2*1024*1024,$this->imgtype);
  209. if(!empty($result['dest']))
  210. $arr['pic']= $folder.$result['dest'];
  211. else
  212. $arr = ['sign'=>0,'error'=>$result['mes']];
  213. return json_encode($arr);
  214. }
  215. }
  216. /**
  217. * 上传图片
  218. * @param $file
  219. * @param string $path
  220. * @param $max_size
  221. * @param $allowExt
  222. * @return mixed
  223. */
  224. public function uploadFile($file,$path='./uploads',$max_size,$allowExt){
  225. $filename=$file['name'];
  226. $type=$file['type'];
  227. $temp_name=$file['tmp_name'];
  228. $error=$file['error'];
  229. $size=$file['size'];
  230. if ($error==UPLOAD_ERR_OK) {
  231. if ($size>$max_size) {
  232. $res['mes']=$filename."文件超过规定上传大小,最多2M";
  233. }
  234. $ext=$this->getExt($filename);
  235. if (!in_array($ext, $allowExt)) {
  236. $res['mes']=$filename.'文件名不合乎规范';
  237. }
  238. // if (!is_uploaded_file($temp_name)) {
  239. // $res['mes']=$filename."文件不是通过HTTP POST 方法上传上传过来的";
  240. // }
  241. if (@$res) {
  242. return $res;
  243. }
  244. if (!file_exists($path)) {
  245. mkdir($path,0777,true);
  246. chmod($path, 0777);
  247. }
  248. $fname=$this->getUniName();
  249. $url =$fname.'.'.$ext;
  250. $destination=$path.'/'.$fname.'.'.$ext;
  251. if (move_uploaded_file($temp_name, $destination)) {
  252. $res['mes']=$filename.'上传成功';
  253. $res['dest']=$url;
  254. }else{
  255. $res['mes']=$filename."文件上传失败";
  256. }
  257. }else{
  258. switch ($error) {
  259. case '1':
  260. $res['mes']="超过了配置文件上传文件的大小";
  261. break;
  262. case '2':
  263. $res['mes']="超过表单设置上传文件文件的大小";
  264. break;
  265. case '3':
  266. $res['mes']="文件部分被上传";
  267. break;
  268. case '4':
  269. $res['mes']="没有文件被上传";
  270. break;
  271. case '6':
  272. $res['mes']="没有找到临时目录";
  273. break;
  274. case '7':
  275. $res['mes']="文件不可写";
  276. break;
  277. default:
  278. $res['mes']="上传文件失败";
  279. break;
  280. }
  281. }
  282. return $res;
  283. }
  284. public function getExt($filename){
  285. $arr=explode('.', basename($filename));
  286. return end($arr);
  287. }
  288. public function getUniName(){
  289. return md5(uniqid(microtime(true),true));
  290. }
  291. //添加预约时间
  292. public function actionAppointment()
  293. {
  294. $userid = YII::$app->user->id;
  295. $time = Yii::$app->request->post('time');
  296. $pid = Yii::$app->request->post('pid');
  297. $type = Yii::$app->request->post('type');
  298. if(!empty($time)&&!empty($pid)){
  299. $model = new Appointment();
  300. $model->uid = Yii::$app->user->id;
  301. $arr = Appointment::find()->where('uid=:uid and pid=:pid and status=:status',[':uid'=>$userid,':pid'=>$pid,':status'=>Appointment::STATUS_DEAL])->andWhere(['>', 'updated_at', time()])->orderBy('updated_at desc')->one();
  302. if(empty($arr)){
  303. $user = User::find()->where('id=:id',[':id'=>$userid])->one();
  304. $userinfo = UserInfo::find()->where('uid=:uid',[':uid'=>$userid])->one();
  305. if(!empty($userinfo)&&!empty($user)){
  306. $model->pid = $pid;
  307. $model->type = $type;
  308. $model->applicant_tel = $user->tel;
  309. $model->applicant_name = $userinfo->nickname;
  310. $model->created_at = time();
  311. $model->booking_time = intval($time+60);
  312. $model->updated_at = intval($time+60);
  313. $model->status = Appointment::STATUS_DEAL;
  314. $model->state = Appointment::STATE_YTAY;
  315. if($model->validate() && $model->save()){
  316. //发送短信
  317. $this->appSms($pid);
  318. //红包发放
  319. $activiModel = new ActivityReceive();
  320. $activiModel->sentRed($pid);
  321. $result=['sign'=>1,'msg'=>'预约成功'];
  322. }else{
  323. $result=['sign'=>0,'msg'=>'预约失败'];
  324. }
  325. }else{
  326. $result=['sign'=>0,'msg'=>'用户详细信息有误'];
  327. }
  328. }else{
  329. $result=['sign'=>0,'msg'=>'您已经有预约'];
  330. }
  331. }else{
  332. $result=['sign'=>0,'msg'=>'时间或房屋id不能为空'];
  333. }
  334. return json_encode($result);
  335. }
  336. //取消预约
  337. public function actionCancelappointment()
  338. {
  339. $userid = YII::$app->user->id;
  340. $pid = Yii::$app->request->post('pid');
  341. $model = Appointment::find()->where('uid=:uid and pid=:pid and status=:status',[':uid'=>$userid,':pid'=>$pid,':status'=>Appointment::STATUS_DEAL])->orderBy('booking_time desc')->one();
  342. if(!empty($model)){
  343. $model->status = Appointment::STATUS_DELETE;
  344. $model->updated_at = time();
  345. if($model->validate() && $model->save()){
  346. $result=['sign'=>1,'msg'=>'已取消'];
  347. }else{
  348. $result=['sign'=>0,'msg'=>'取消失败'];
  349. }
  350. }else{
  351. $result=['sign'=>0,'msg'=>'不存在预约'];
  352. }
  353. return json_encode($result);
  354. }
  355. public function actionBuildingposition()
  356. {
  357. $lat = Yii::$app->request->get('lat');
  358. $lng = Yii::$app->request->get('lng');
  359. $type = Yii::$app->request->get('type');
  360. return $this->render('buildingPosition',['lat'=>$lat,'lng'=>$lng,'type'=>$type]);
  361. }
  362. //预约发送短信
  363. public function appSms($id)
  364. {
  365. $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();
  366. if(!empty($data)){
  367. $sms = new Sms();
  368. //用户
  369. if(!empty(Yii::$app->user->identity->tel)){
  370. if($data['type']==Building::TYPE_HOUSE)//样板房
  371. {
  372. $content = "业主您好!您已成功预约".$data['address']."的房子进行参观,具体参观的时间请联络平台客服热线400-0592-018!";
  373. }else{
  374. $content = "您已成功预约".$data['address'].",有问题请及时联系我们的客服400-0592-018";
  375. }
  376. $sms->SendMessage(Yii::$app->user->identity->tel,$content,false);
  377. }
  378. //装修公司
  379. if(!empty($data['tel']))
  380. {
  381. $content = "您好,你的".$data['address'].",有业主预约参观,请保持好现场形象管理,业主的信息请在".Yii::$app->params['sitetitle']."后台查看。";
  382. $sms->SendMessage($data['tel'],$content,false);
  383. }
  384. }
  385. }
  386. }