文件

92 行
3.1 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\admin\controller\shopro_aliyun_express;
use addons\shopro_aliyun_express\library\ExpressConfig;
use addons\shopro_aliyun_express\library\ExpressQueryService;
use app\common\controller\Backend;
use think\Db;
class Config extends Backend
{
public function index()
{
if ($this->request->isAjax()) {
$data = ExpressConfig::getInstance()->all();
$defaults = [
'express_enable' => '1',
'express_provider' => 'aliyun',
'aliyun_appcode' => '',
'aliyun_api_url' => '',
'aliyun_api_mode' => 'auto',
'aliyun_http_method' => 'auto',
'expressbird_appcode' => '',
'cache_ttl' => '300',
];
return $this->success('操作成功', null, array_merge($defaults, $data));
}
return $this->view->fetch();
}
public function save()
{
$params = $this->request->post('row/a');
if (!$params) {
$this->error('参数错误');
}
$allowed = [
'express_enable',
'express_provider',
'aliyun_appcode',
'aliyun_api_url',
'aliyun_api_mode',
'aliyun_http_method',
'expressbird_appcode',
'cache_ttl',
];
foreach ($allowed as $name) {
if (!array_key_exists($name, $params)) {
continue;
}
$exists = Db::name('shopro_aliyun_express_config')->where('name', $name)->find();
if ($exists) {
Db::name('shopro_aliyun_express_config')->where('name', $name)->update(['value' => $params[$name]]);
} else {
Db::name('shopro_aliyun_express_config')->insert(['name' => $name, 'value' => $params[$name]]);
}
}
ExpressConfig::reload();
$this->success('保存成功');
}
public function test()
{
$waybillNo = trim($this->request->post('waybill_number', ''));
$phone = trim($this->request->post('phone', ''));
$expressCode = trim($this->request->post('express_code', ''));
$provider = trim($this->request->post('express_provider', ''));
if ($waybillNo === '') {
$this->error('请填写快递单号');
}
try {
$service = new ExpressQueryService();
$normalized = $service->queryTrack($waybillNo, $phone, $expressCode, $provider ?: null, false);
if (empty($normalized['success'])) {
$raw = !empty($normalized['raw']) ? json_encode($normalized['raw'], JSON_UNESCAPED_UNICODE) : '';
$this->error('测试查询失败:未获取到有效轨迹' . ($raw ? ':' . mb_substr($raw, 0, 300) : ''));
}
$this->success('测试查询成功', null, $normalized);
} catch (\think\exception\HttpResponseException $e) {
throw $e;
} catch (\Throwable $e) {
$msg = trim($e->getMessage());
$this->error('测试查询失败:' . ($msg !== '' ? $msg : get_class($e)));
}
}
}