110 行
3.1 KiB
PHP
110 行
3.1 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\controller;
|
|
|
|
use addons\shopro\exception\Exception;
|
|
use addons\shopro\model\UserAddress;
|
|
use addons\shopro\model\Area;
|
|
|
|
class Address extends Base
|
|
{
|
|
|
|
protected $noNeedLogin = ['area'];
|
|
protected $noNeedRight = ['*'];
|
|
|
|
public function index()
|
|
{
|
|
$this->success('收货地址', UserAddress::getUserAddress());
|
|
}
|
|
|
|
public function defaults()
|
|
{
|
|
$this->success('默认收货地址', UserAddress::getUserDefaultAddress());
|
|
}
|
|
|
|
public function area()
|
|
{
|
|
$cacheKey = 'shopro_area_tree_data';
|
|
$data = cache($cacheKey);
|
|
if (!$data) {
|
|
// 一次性查出全部省市区数据(仅 1 次查询),并在内存中按 pid 构建树形,消除 350+ 次嵌套 SQL 击穿
|
|
$allAreas = Area::where('level', 'in', [1, 2, 3])
|
|
->field('id as value, name as label, pid, level')
|
|
->order('id asc')
|
|
->select();
|
|
|
|
$provinceData = [];
|
|
$cityMap = [];
|
|
$areaMap = [];
|
|
foreach ($allAreas as $item) {
|
|
if ($item['level'] == 1) {
|
|
$provinceData[] = $item;
|
|
} elseif ($item['level'] == 2) {
|
|
$cityMap[$item['pid']][] = $item;
|
|
} elseif ($item['level'] == 3) {
|
|
$areaMap[$item['pid']][] = $item;
|
|
}
|
|
}
|
|
|
|
$cityData = [];
|
|
$areaData = [];
|
|
foreach ($provinceData as $k => $p) {
|
|
$cities = $cityMap[$p['value']] ?? [];
|
|
$cityData[$k] = $cities;
|
|
foreach ($cities as $i => $c) {
|
|
$areaData[$k][$i] = $areaMap[$c['value']] ?? [];
|
|
}
|
|
}
|
|
|
|
$data = [
|
|
'provinceData' => $provinceData,
|
|
'cityData' => $cityData,
|
|
'areaData' => $areaData
|
|
];
|
|
cache($cacheKey, $data, 604800);
|
|
}
|
|
|
|
$this->success('省市区', $data);
|
|
}
|
|
|
|
public function edit()
|
|
{
|
|
$params = $this->request->post();
|
|
|
|
// 表单验证
|
|
$this->shoproValidate($params, get_class(), 'edit');
|
|
|
|
$this->success('编辑地址', UserAddress::edit($params));
|
|
}
|
|
|
|
public function info()
|
|
{
|
|
$params = $this->request->get();
|
|
$this->success('地址详情', UserAddress::info($params));
|
|
}
|
|
|
|
public function del()
|
|
{
|
|
$params = $this->request->post();
|
|
$this->success('地址详情', UserAddress::del($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function regeo()
|
|
{
|
|
$latitude = $this->request->post('latitude');
|
|
$longitude = $this->request->post('longitude');
|
|
if (!is_numeric($latitude) || !is_numeric($longitude) || abs($latitude) < 0.000001 || abs($longitude) < 0.000001) {
|
|
$this->error('请先选择位置');
|
|
}
|
|
try {
|
|
$data = \app\common\library\Tianditu::reverse((float)$longitude, (float)$latitude);
|
|
} catch (\Exception $e) {
|
|
$this->error($e->getMessage() ?: '地址解析失败');
|
|
}
|
|
$this->success('解析成功', $data);
|
|
}
|
|
|
|
} |