AddressController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/22
  8. * Time: 13:37
  9. */
  10. namespace business\admin\controllers;
  11. use app\common\models\Address;
  12. use business\common\controllers\components\BaseController;
  13. use Illuminate\Support\Collection;
  14. class AddressController extends BaseController
  15. {
  16. public function getAddressList()
  17. {
  18. $separate = DIRECTORY_SEPARATOR;
  19. $path = "business{$separate}asset{$separate}json{$separate}";
  20. if (file_exists($path . 'address.json')) {
  21. $list = file_get_contents($path . 'address.json');
  22. return $this->successJson('成功', json_decode($list, true));
  23. }
  24. $list = Address::get();
  25. $group = $list->groupBy('level');
  26. $level_1 = $group['1'];
  27. foreach ($level_1 as $address) {
  28. $address['childs'] = $this->recursion($address, $group);
  29. }
  30. $list = json_encode($level_1);
  31. if (!file_exists($path)) {
  32. mkdir($path);
  33. }
  34. file_put_contents($path . 'address.json', $list);
  35. return $this->successJson('成功', $level_1);
  36. }
  37. private function recursion(Address $address, Collection $collection)
  38. {
  39. if (!$group = $collection[$address->level + 1]) {
  40. return [];
  41. }
  42. $childs = array();
  43. foreach ($group as $key => $item) {
  44. if ($address->id == $item->parentid) {
  45. unset($group[$key]);
  46. $child = $this->recursion($item, $collection);
  47. if ($child) {
  48. $item['childs'] = $child;
  49. }
  50. $childs[] = $item;
  51. }
  52. }
  53. return $childs;
  54. }
  55. }