fix(地址): 选点改走服务端天地图,直辖县按所属省匹配
正式版不能直接请求地图域名时,由后端转发逆地理。保存区划时带上省的 id,避免同名直辖县挂错省份。 Co-authored-by: Cursor <cursoragent@cursor.com>
这个提交包含在:
@@ -0,0 +1,79 @@
|
||||
<?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()
|
||||
{
|
||||
$data['provinceData'] = Area::where('level', 1)->order('id asc')->field('id as value, name as label, pid, level')->select();
|
||||
foreach ($data['provinceData'] as $k => $p) {
|
||||
$data['cityData'][$k] = Area::where(['level' => 2, 'pid' => $p->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
|
||||
foreach ($data['cityData'][$k] as $i => $c) {
|
||||
$data['areaData'][$k][$i] = Area::where(['level' => 3, 'pid' => $c->value])->order('id asc')->field('id as value, name as label, pid, level')->select();
|
||||
}
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
}
|
||||
122
addons/shopro/model/UserAddress.php
普通文件
122
addons/shopro/model/UserAddress.php
普通文件
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\model;
|
||||
|
||||
use addons\shopro\model\Area;
|
||||
use think\Model;
|
||||
use addons\shopro\exception\Exception;
|
||||
use traits\model\SoftDelete;
|
||||
|
||||
/**
|
||||
* 用户地址模型
|
||||
*/
|
||||
class UserAddress extends Model
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
protected $name = 'shopro_user_address';
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'int';
|
||||
// 定义时间戳字段名
|
||||
protected $createTime = 'createtime';
|
||||
protected $updateTime = 'updatetime';
|
||||
protected $deleteTime = 'deletetime';
|
||||
protected $hidden = ['createtime', 'updatetime', 'deletetime'];
|
||||
|
||||
// 追加属性
|
||||
protected $append = [];
|
||||
|
||||
public static function getUserAddress()
|
||||
{
|
||||
$user = User::info();
|
||||
return self::all([
|
||||
'user_id' => $user->id,
|
||||
'deletetime' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getUserDefaultAddress()
|
||||
{
|
||||
$user = User::info();
|
||||
$default = self::get([
|
||||
'user_id' => $user->id,
|
||||
'deletetime' => null,
|
||||
'is_default' => '1'
|
||||
]);
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
public static function edit($params)
|
||||
{
|
||||
$user = User::info();
|
||||
extract($params);
|
||||
$areaNameArray = explode("-", $area_text);
|
||||
|
||||
try {
|
||||
$province = Area::get(['name' => $areaNameArray[0], 'level' => 1]);
|
||||
$city = $province ? Area::get(['name' => $areaNameArray[1], 'pid' => $province->id, 'level' => 2]) : null;
|
||||
$area = $city ? Area::get(['name' => $areaNameArray[2], 'pid' => $city->id, 'level' => 3]) : null;
|
||||
|
||||
if (!$province || !$city || !$area) {
|
||||
new Exception('暂不支持,请手动选择行政区');
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
new Exception('暂不支持,请手动选择行政区');
|
||||
}
|
||||
$is_default = (isset($is_default) && $is_default) ? '1' : '0';
|
||||
if ($is_default) {
|
||||
self::where(['user_id' => $user->id, 'is_default' => '1'])->update(['is_default' => '0']);
|
||||
}
|
||||
|
||||
if (!isset($id) || $id == 0) {
|
||||
$edit = self::create([
|
||||
'consignee' => $consignee,
|
||||
'phone' => $phone,
|
||||
'province_id' => $province->id,
|
||||
'province_name' => $province->name,
|
||||
'city_id' => $city->id,
|
||||
'city_name' => $city->name,
|
||||
'area_id' => $area->id,
|
||||
'area_name' => $area->name,
|
||||
'is_default' => $is_default,
|
||||
'latitude' => (isset($latitude) && $latitude) ? $latitude : null,
|
||||
'longitude' => (isset($longitude) && $longitude) ? $longitude : null,
|
||||
'user_id' => $user->id,
|
||||
'address' => $address
|
||||
]);
|
||||
} else {
|
||||
$edit = self::update([
|
||||
'consignee' => $consignee,
|
||||
'phone' => $phone,
|
||||
'province_id' => $province->id,
|
||||
'province_name' => $province->name,
|
||||
'city_id' => $city->id,
|
||||
'city_name' => $city->name,
|
||||
'area_id' => $area->id,
|
||||
'area_name' => $area->name,
|
||||
'is_default' => $is_default,
|
||||
'latitude' => (isset($latitude) && $latitude) ? $latitude : null,
|
||||
'longitude' => (isset($longitude) && $longitude) ? $longitude : null,
|
||||
'user_id' => $user->id,
|
||||
'address' => $address,
|
||||
'id' => $id
|
||||
]);
|
||||
}
|
||||
return $edit;
|
||||
}
|
||||
|
||||
public static function info($params)
|
||||
{
|
||||
$user = User::info();
|
||||
extract($params);
|
||||
return self::get(['id' => $id, 'user_id' => $user->id]);
|
||||
}
|
||||
|
||||
public static function del($params)
|
||||
{
|
||||
$user = User::info();
|
||||
extract($params);
|
||||
return self::get(['id' => $id, 'user_id' => $user->id])->delete();
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户