AreaController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022-05-27
  8. * Time: 11:17
  9. */
  10. namespace business\admin\controllers;
  11. use app\common\models\Area;
  12. use app\common\models\Street;
  13. use business\common\controllers\components\BaseController;
  14. use Illuminate\Database\Eloquent\Collection;
  15. class AreaController extends BaseController
  16. {
  17. public function openStreet()
  18. {
  19. $is_street = \Setting::get("shop.trade")['is_street']?1:0;
  20. return $this->successJson('street', ['is_street' => $is_street]);
  21. }
  22. public function index()
  23. {
  24. if ($this->isStreet(request('parent_id'))) {
  25. $areas = Street::where('parentid',request('parent_id'))->get();
  26. } else {
  27. $areas = Area::where('parentid',request('parent_id'))->get();
  28. }
  29. return $this->successJson('成功', $this->formatAreas($areas));
  30. }
  31. public function init()
  32. {
  33. list($provinceId,$cityId,$districtId,$street_id) = explode(',',request('area_ids'));
  34. $province = Area::where('parentid',0)->get();
  35. $cities = Area::where('parentid',$provinceId)->get();
  36. $districts = Area::where('parentid',$cityId)->get();
  37. $streets = Street::where('parentid',$districtId)->get();
  38. $province = $this->formatAreas($province);
  39. $province->where('id',$provinceId)->first()->children = $cities;
  40. $cities->where('id',$cityId)->first()->children = $districts;
  41. $districts->where('id',$districtId)->first()->children = $streets;
  42. return $this->successJson('成功', $province);
  43. }
  44. protected function isStreet($areaId)
  45. {
  46. if ($areaId == 0) {
  47. return false;
  48. }
  49. $area = Area::find(request('parent_id'));
  50. if (!$area) {
  51. return false;
  52. }
  53. return $area->level == 3;
  54. }
  55. protected function formatAreas(Collection $areas)
  56. {
  57. if ($areas->isEmpty()) {
  58. return $areas;
  59. }
  60. // 不是子节点
  61. if (!$areas->first()->isLeaf()) {
  62. $areas->each(function (Area $area) {
  63. $area['children'] = [];
  64. });
  65. }
  66. return $areas;
  67. }
  68. }