SearchController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use common\library\ZM_Geohash;
  4. use common\models\Building;
  5. use common\models\MaterType;
  6. use common\models\UserMater;
  7. use yii\rest\ActiveController;
  8. use common\library\Apireturn;
  9. use yii\helpers\ArrayHelper;
  10. use yii\filters\auth\QueryParamAuth;
  11. use yii;
  12. class SearchController extends ActiveController
  13. {
  14. public $modelClass = 'common\models';
  15. const DISPLAY = 10;//显示条数
  16. public function behaviors()
  17. {
  18. return ArrayHelper::merge(parent::behaviors(), [
  19. 'authenticator' => [
  20. 'class' => QueryParamAuth::className(),
  21. 'tokenParam' => "token",//access-token修改为token
  22. 'optional' => [//不需要认证方法名 array
  23. ],
  24. ]
  25. ]);
  26. }
  27. /**
  28. * 搜索页面
  29. * @return array
  30. */
  31. public function actionPage()
  32. {
  33. $type = Yii::$app->request->post('type');
  34. if(in_array($type,array(Building::TYPE_SITE,Building::TYPE_HOUSE)))//工地/样板房
  35. {
  36. $list = array_merge(CompanyBuildingController::$layout_arr,CompanyBuildingController::$style_arr);
  37. }else{
  38. $list = MaterType::find()->select('id,name')->where(['status'=>MaterType::STATUS_YES])->asArray()->all();
  39. }
  40. return Apireturn::sent(1,'success',200,$list);
  41. }
  42. /**
  43. * 搜索结果
  44. */
  45. public function actionList()
  46. {
  47. $latitude = Yii::$app->request->post('latitude');
  48. $longitude = Yii::$app->request->post('longitude');
  49. $type = Yii::$app->request->post('type');
  50. $page = Yii::$app->request->POST('page', 1);//分页页数
  51. $display = Yii::$app->request->POST('display', self::DISPLAY);//分页页数
  52. $search = Yii::$app->request->post('name');
  53. switch ($type)
  54. {
  55. case Building::TYPE_SITE :
  56. $count = Building::find()->where(['posted'=>Building::POSTED_YES,'type'=>Building::TYPE_SITE])->andWhere('name like :search or address like :search or layout like :search or stage like :search or pattern like :search or style like :search ',[':search'=>'%'.$search.'%'])->count();
  57. $list = Building::findBySql("select *,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE posted = :posted AND type = :type AND ( name like :search or address like :search or layout like :search or stage like :search or pattern like :search or style like :search) ORDER BY distance ASC LIMIT ".($page-1) * $display.",".$display, [':latitude' => $latitude, ':longitude' => $longitude,':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_SITE,':search'=>'%'.$search.'%'])->all();
  58. break;
  59. case Building::TYPE_HOUSE :
  60. $count = Building::find()->where(['posted'=>Building::POSTED_YES,'type'=>Building::TYPE_HOUSE])->andWhere('name like :search or address like :search or layout like :search or stage like :search or pattern like :search or style like :search ',[':search'=>'%'.$search.'%'])->count();
  61. $list = Building::findBySql("select *,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%building}} WHERE posted = :posted AND type = :type AND ( name like :search or address like :search or layout like :search or stage like :search or pattern like :search or style like :search) ORDER BY distance ASC LIMIT ".($page-1) * $display.",".$display, [':latitude' => $latitude, ':longitude' => $longitude,':posted'=>Building::POSTED_YES,':type'=>Building::TYPE_HOUSE,':search'=>'%'.$search.'%'])->all();
  62. break;
  63. case 10 :
  64. $count = UserMater::find()->where(['{{%user_mater}}.member'=>UserMater::MEMBER_YES])->andWhere('{{%user_mater}}.company like :search OR {{%mater_type}}.name like :search ',[':search'=>'%'.$search.'%'])->joinWith('type')->count();
  65. $list = UserMater::findBySql("select a.id,a.company,a.backpic,a.age,a.address,b.name as type_name,get_Distance(latitude,longitude,:latitude,:longitude) as distance from {{%user_mater}} a LEFT JOIN {{%mater_type}} b ON a.type_id = b.id WHERE a.member = :member AND (a.company like :search OR b.name like :search ) ORDER BY distance ASC LIMIT ".($page-1) * $display.",".$display, [':latitude' => $latitude, ':longitude' => $longitude,':member'=>UserMater::MEMBER_YES,':search'=>'%'.$search.'%'])->asArray()->all();
  66. break;
  67. default :
  68. break;
  69. }
  70. $lists = array();
  71. if(!empty($list))
  72. {
  73. if(in_array($type,array(Building::TYPE_SITE,Building::TYPE_HOUSE)))
  74. {
  75. foreach ($list as $key => $value)
  76. {
  77. $lists[$key]['id'] = $value->id;
  78. $lists[$key]['name'] = $value->name;
  79. $lists[$key]['pic'] = $value->all ? $value->all->pic : "";
  80. $lists[$key]['address'] = $value->address;
  81. $lists[$key]['layout'] = $value->layout;
  82. $lists[$key]['style'] = $value->style;
  83. $lists[$key]['acreage'] = $value->acreage;
  84. $lists[$key]['type'] = $value->type;
  85. $lists[$key]['distance'] = $value->distance >= 0 ? sprintf("%.1f",$value->distance/1000) : "";
  86. }
  87. }else{
  88. $lists = $list;
  89. foreach ($lists as $key => $value)
  90. {
  91. $lists[$key]['distance'] = $value['distance'] >= 0 ? sprintf("%.1f",$value['distance']/1000) : "";
  92. }
  93. }
  94. }
  95. return Apireturn::sent(1,'success',200,array('count'=>$count,'list'=>$lists));
  96. }
  97. }