MaterController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * 建材商
  4. */
  5. namespace api\modules\v1\controllers;
  6. use common\library\ZM_Geohash;
  7. use common\models\UserMater;
  8. use yii\rest\ActiveController;
  9. use common\library\Apireturn;
  10. use yii\helpers\ArrayHelper;
  11. use yii\filters\auth\QueryParamAuth;
  12. use yii;
  13. class MaterController extends ActiveController
  14. {
  15. public $modelClass = 'common\models';
  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 actionInfo()
  32. {
  33. $id = Yii::$app->request->post('id');
  34. $latitude = Yii::$app->request->post('latitude');
  35. $longitude = Yii::$app->request->post('longitude');
  36. $info = UserMater::find()->select('id,uid,backpic,company,name,tel,position,age,intro,type_id,latitude,longitude,address')->where(['id'=>$id,'member'=>UserMater::MEMBER_YES])->one();
  37. if(empty($info))
  38. return Apireturn::sent(0,'找不到记录',200);
  39. $data = array();
  40. foreach ($info->getOldAttributes() as $key => $val)
  41. {
  42. $data[$key] = $val;
  43. }
  44. $data['type'] = !empty($info->type) ? $info->type->name : "";
  45. //距离
  46. $data['distance'] = "";
  47. if(!empty($latitude)&&!empty($longitude)) {
  48. $geohash = new ZM_Geohash();
  49. if (!empty($info->latitude) && !empty($info->longitude)) {
  50. $data['distance'] = sprintf("%.1f", $geohash->getDistance($latitude, $longitude, $info->latitude, $info->longitude) / 1000);
  51. }
  52. }
  53. $data['imgs'] = array();
  54. if(!empty($info->images))
  55. {
  56. foreach ($info->images as $img_val)
  57. $data['imgs'][] = $img_val->pic;
  58. }
  59. return Apireturn::sent(1,'success',200,$data);
  60. }
  61. }