| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2021/9/22
- * Time: 13:37
- */
- namespace business\admin\controllers;
- use app\common\models\Address;
- use business\common\controllers\components\BaseController;
- use Illuminate\Support\Collection;
- class AddressController extends BaseController
- {
- public function getAddressList()
- {
- $separate = DIRECTORY_SEPARATOR;
- $path = "business{$separate}asset{$separate}json{$separate}";
- if (file_exists($path . 'address.json')) {
- $list = file_get_contents($path . 'address.json');
- return $this->successJson('成功', json_decode($list, true));
- }
- $list = Address::get();
- $group = $list->groupBy('level');
- $level_1 = $group['1'];
- foreach ($level_1 as $address) {
- $address['childs'] = $this->recursion($address, $group);
- }
- $list = json_encode($level_1);
- if (!file_exists($path)) {
- mkdir($path);
- }
- file_put_contents($path . 'address.json', $list);
- return $this->successJson('成功', $level_1);
- }
- private function recursion(Address $address, Collection $collection)
- {
- if (!$group = $collection[$address->level + 1]) {
- return [];
- }
- $childs = array();
- foreach ($group as $key => $item) {
- if ($address->id == $item->parentid) {
- unset($group[$key]);
- $child = $this->recursion($item, $collection);
- if ($child) {
- $item['childs'] = $child;
- }
- $childs[] = $item;
- }
- }
- return $childs;
- }
- }
|