Commit 07338926 authored by caren's avatar caren

快递100 智能解析地址接口增加area表的省市区ID返回

parent 0cb1bd7c
......@@ -21,7 +21,7 @@ use think\Validate;
class User extends Common
{
protected $noNeedLogin = ['smsRegister', 'accountLogin', 'smsLogin', 'resetPassword'];
protected $noNeedLogin = ['smsRegister', 'accountLogin', 'smsLogin', 'resetPassword', 'addressResolution'];
protected $noNeedRight = ['*'];
......@@ -437,7 +437,7 @@ class User extends Common
$this->error('快递100接口配置缺失');
}
$param = json_encode(['content' => $content]);
$param = json_encode(['content' => $content, 'addressLevel' => 3]);
$t = (string)round(microtime(true) * 1000);
$sign = strtoupper(md5($param . $t . $key . $secret));
......@@ -458,15 +458,81 @@ class User extends Common
$result = json_decode($req['msg'], true);
if (!isset($result['success'])) {
if (!isset($result['code'])) {
$this->error('快递100接口返回数据异常');
}
if (!$result['success']) {
if ($result['code'] !== 200) {
$this->error($result['message'] ?? '地址解析失败');
}
$this->success($result['message'] ?? '解析成功', $result['data'] ?? null);
// 获取第一条结果
$resItem = $result['data']['result'][0] ?? [];
if (empty($resItem)) {
$this->success('解析成功', ['taskId' => $result['data']['taskId'] ?? '', 'result' => []]);
}
$xzq = $resItem['xzq'] ?? [];
// 快递100原始简称
$provinceShort = $xzq['province'] ?? '';
$cityShort = $xzq['city'] ?? '';
$districtShort = $xzq['district'] ?? '';
// 查询数据库拿id 和 name全称
$provinceInfo = $this->getAreaInfoByName($provinceShort, 1, 0);
$cityInfo = $this->getAreaInfoByName($cityShort, 2, $provinceInfo['id']);
$districtInfo = $this->getAreaInfoByName($districtShort, 3, $cityInfo['id']);
// 替换xzq内省市区名称为数据库name,新增id字段,其他全部保留原始值
$resItem['xzq']['province'] = $provinceInfo['name'];
$resItem['xzq']['province_id'] = $provinceInfo['id'];
$resItem['xzq']['city'] = $cityInfo['name'];
$resItem['xzq']['city_id'] = $cityInfo['id'];
$resItem['xzq']['district'] = $districtInfo['name'];
$resItem['xzq']['district_id'] = $districtInfo['id'];
// 回写result数组
$result['data']['result'][0] = $resItem;
$this->success('success', $result['data']);
}
/**
* 根据简称+层级+父pid查询地区
* @param string $shortName 快递100返回简称
* @param int $level 1省 2市 3区
* @param int $pid 父级ID
* @return array ['id'=>int, 'name'=>string]
*/
protected function getAreaInfoByName(string $shortName, int $level, int $pid): array
{
if (empty($shortName)) {
return ['id' => 0, 'name' => ''];
}
$row = Db::name('area')
->where('level', $level)
->where('pid', $pid)
->where(function ($query) use ($shortName) {
$query->where('shortname', $shortName)
->whereOr('name', $shortName);
})
->field('id,name')
->find();
if ($row) {
return [
'id' => intval($row['id']),
'name' => $row['name']
];
}
// 未匹配到兜底返回原始简称
return [
'id' => 0,
'name' => $shortName
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment