CityController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/4/26
  6. * Time: 上午11:13
  7. */
  8. namespace frontend\controllers;
  9. use frontend\base\CommonController;
  10. use Yii;
  11. class CityController extends CommonController
  12. {
  13. public function actionIndex()
  14. {
  15. /**
  16. * 所有城市
  17. */
  18. $cityModel = new \common\models\CityDetails();
  19. $cityAll = $cityModel->CityInfo();
  20. /**
  21. * 获取传递过来的ID 显示相应的城市信息
  22. */
  23. $rowCity = $cityModel->FingByName(Yii::$app->request->get('cid'));
  24. if(!empty($rowCity))
  25. {
  26. $rowCity['img'] = json_decode($rowCity['img']);
  27. }
  28. /**
  29. * 传递区域ID 返回相关楼盘信息
  30. */
  31. $houseModel = new \common\models\House();
  32. if(!empty($rowCity['city_id']))
  33. {
  34. $houseModel->city = $rowCity['city_id'];
  35. $house = $houseModel->QgetList(['pfg_category_city.city_name','pfg_house.id','pfg_house.city','pfg_house.price','pfg_house.name','pfg_house_detail.address','pfg_house_detail.main_units','pfg_house_detail.price_unit','pfg_house.thumb','pfg_house.sale_price','pfg_house.characteristic','pfg_house_detail.longitude_latitude']);
  36. if($house['data'] != null)
  37. {
  38. $characteristic = new \common\models\Characteristic();
  39. $charList = $characteristic->getList([]);
  40. $characteristicId = array_column($charList,'name','id');
  41. $chara = array_column($house['data'],'characteristic','id');
  42. $charaArrs = [];
  43. //处理特色主题
  44. foreach ($chara as $key=>$val)
  45. {
  46. $arr = json_decode($val,true);
  47. if(is_array($arr) && !empty($arr))
  48. {
  49. foreach ($arr as $k=>$v)
  50. {
  51. if(isset($characteristicId[$v]))
  52. {
  53. $charaArrs[$key][$k] = $characteristicId[$v];
  54. }
  55. }
  56. }
  57. }
  58. $Tel = new \common\models\CategoryTelCity();
  59. $telRow = $Tel->QgetAll();
  60. $cityTel = [];
  61. foreach ($telRow as &$v)
  62. {
  63. $cityTel[$v['cid']] = explode(',',$v['tel']);
  64. }
  65. $randArr = [];
  66. foreach ($house['data'] as &$val)
  67. {
  68. if(isset($charaArrs[$val['id']]))
  69. {
  70. //随机选择3个特色主题
  71. if(count($charaArrs[$val['id']]) > 3)
  72. {
  73. $ranChar = array_rand($charaArrs[$val['id']],3);
  74. $randArr[] = $charaArrs[$val['id']][$ranChar[0]];
  75. $randArr[] = $charaArrs[$val['id']][$ranChar[1]];
  76. $randArr[] = $charaArrs[$val['id']][$ranChar[2]];
  77. $val['characteristic'] = $randArr;
  78. $randArr= [];
  79. }
  80. else
  81. {
  82. $val['characteristic'] = $charaArrs[$val['id']];
  83. }
  84. }
  85. if(!empty($cityTel[$val['city']][0]))
  86. {
  87. $rand = array_rand($cityTel[$val['city']],1);
  88. $val['citytel'] =$cityTel[$val['city']][$rand];
  89. }
  90. else
  91. {
  92. $val['citytel'] = 1111;
  93. }
  94. }
  95. }
  96. }
  97. return $this->render('index',['city'=>$cityAll,'citydetails'=>$rowCity,'house'=>$house]);
  98. }
  99. }