Commit 0cb1bd7c authored by caren's avatar caren

快递100 智能解析地址接口

parent cb83b6aa
...@@ -13,6 +13,11 @@ use app\admin\model\shopro\ThirdOauth; ...@@ -13,6 +13,11 @@ use app\admin\model\shopro\ThirdOauth;
use think\Db; use think\Db;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;
class User extends Common class User extends Common
{ {
...@@ -411,4 +416,57 @@ class User extends Common ...@@ -411,4 +416,57 @@ class User extends Common
$this->success('注销成功'); $this->success('注销成功');
} }
/**
* 智能地址解析
*
* @ApiMethod (POST)
* @ApiParams (name="content", type="string", required=true, description="需要解析的地址文本,如:张三广东省深圳市南山区粤海街道科技南十二路金蝶软件园13088888888")
*/
public function addressResolution()
{
$content = $this->request->post('content');
if (!$content) {
$this->error(__('Invalid parameters'));
}
$key = Config::get('shopro.kuaidi100_key');
$secret = Config::get('shopro.kuaidi100_secret');
if (!$key || !$secret) {
$this->error('快递100接口配置缺失');
}
$param = json_encode(['content' => $content]);
$t = (string)round(microtime(true) * 1000);
$sign = strtoupper(md5($param . $t . $key . $secret));
$postData = http_build_query([
'key' => $key,
'sign' => $sign,
't' => $t,
'param' => $param,
]);
$req = Http::sendRequest('https://api.kuaidi100.com/address/resolution', $postData, 'POST', [
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded', 'Expect:'],
]);
if (!$req['ret']) {
$this->error('请求快递100接口失败: ' . $req['msg']);
}
$result = json_decode($req['msg'], true);
if (!isset($result['success'])) {
$this->error('快递100接口返回数据异常');
}
if (!$result['success']) {
$this->error($result['message'] ?? '地址解析失败');
}
$this->success($result['message'] ?? '解析成功', $result['data'] ?? null);
}
} }
...@@ -341,6 +341,10 @@ return [ ...@@ -341,6 +341,10 @@ return [
'xhs_app_id' => Env::get('xhs.app_id', ''), 'xhs_app_id' => Env::get('xhs.app_id', ''),
'xhs_access_token' => Env::get('xhs.access_token', ''), 'xhs_access_token' => Env::get('xhs.access_token', ''),
// 快递100 地址智能识别
'kuaidi100_key' => Env::get('kuaidi100.key', ''),
'kuaidi100_secret' => Env::get('kuaidi100.secret', ''),
], ],
]; ];
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