RegionController.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace addons\Community\api\modules\v1\controllers;
  3. use addons\Community\common\service\SettingService;
  4. use api\controllers\OnAuthController;
  5. use common\models\common\Region;
  6. use yii\helpers\Json;
  7. class RegionController extends OnAuthController
  8. {
  9. public $modelClass = '';
  10. /**
  11. * 不用进行登录验证的方法
  12. *
  13. * 例如: ['index', 'update', 'create', 'view', 'delete']
  14. * 默认全部需要验证
  15. *
  16. * @var array
  17. */
  18. protected $authOptional = [];
  19. public function actionIndex()
  20. {
  21. $ids = (new SettingService())->getFieldSetting($this->mall_id, 'area', true);
  22. // 需要先找出pid
  23. $cIds = Region::find()->where(['level' => 3])->andWhere(['id' => Json::decode($ids)])->select('parent_id')->column();
  24. $rows = Region::find()->where(['id' => $cIds])->andWhere(['level' => 2])
  25. ->orderBy('initial asc')->select('id, name, initial, lng, lat')->asArray()->all();
  26. if (!empty($rows)) {
  27. $rows = array_reduce($rows, function (&$rows, $item) {
  28. $rows[!empty($item['initial']) ? $item['initial'] : '#'][] = $item;
  29. return $rows;
  30. });
  31. }
  32. return $this->success('获取成功', $rows);
  33. }
  34. }