[fastadmin] 第三十七篇 FastAdmin php代码 try catch 一直走error 区间
错误场景
代码
/**
* 更新订单收货信息
* @ApiTitle (更新订单收货信息)
* @ApiMethod (POST)
* @ApiParams (name="id", type="integer", required=true, description="订单ID")
* @ApiParams (name="receiver", type="string", required=true, description="收货人姓名")
* @ApiParams (name="address", type="string", required=true, description="收货地址")
* @ApiParams (name="mobile", type="string", required=true, description="收货手机号")
*/
public function updateReceiveInfo()
{
// 获取参数
$id = $this->request->post('id');
$receiver = $this->request->post('receiver');
$address = $this->request->post('address');
$mobile = $this->request->post('mobile');
// 参数验证
if (!$id || !$receiver || !$address || !$mobile) {
$this->error('参数不完整');
}
// 验证手机号格式
if (!preg_match("/^1[3-9]\d{9}$/", $mobile)) {
$this->error('手机号格式不正确');
}
// 查找订单
$row = $this->model->where('id', $id)->find();
if (!$row) {
$this->error('订单未找到');
}
// 检查订单状态
if ($row['orderstate'] != 0 || $row['shippingstate'] != 0) {
$this->error('订单状态不允许修改收货信息');
}
// 更新数据
try {
$updateData = [
'receiver' => strip_tags(trim($receiver)),
'address' => strip_tags(trim($address)),
'mobile' => trim($mobile),
'updatetime' => time()
];
// 先检查数据是否有变化
$isChanged = false;
foreach ($updateData as $key => $value) {
if ($row->$key != $value) {
$isChanged = true;
break;
}
}
if ($isChanged) {
$res = $row->save($updateData);
} else {
$res = true; // 数据没变化也算成功
}
if ($res !== false) {
$this->success('更新成功');
} else {
$this->error('更新失败');
}
} catch (\Exception $e) {
// 打印完整异常信息
var_dump('异常信息:', [
'message' => $e->getMessage(),
'code' => $e->getCode(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
]);
$this->error('更新失败:' . $e->getMessage());
}
}
错误打印
string(15) “异常信息:”
array(5) {
[“message”]=>
string(0) “”
[“code”]=>
int(0)
[“file”]=>
string(70) “D:\work\phpstudy\5i40\code\thinkphp\library\traits\controller\Jump.php”
[“line”]=>
int(64)
[“trace”]=>
string(768) “#0 D:\work\phpstudy\5i40\code\application\admin\controller\shop\Order.php(328): think\Controller->success(‘\xE6\x9B\xB4\xE6\x96\xB0\xE6\x88\x90\xE5\x8A\x9F’)
#1 [internal function]: app\admin\controller\shop\Order->updateReceiveInfo()
#2 D:\work\phpstudy\5i40\code\thinkphp\library\think\App.php(343): ReflectionMethod->invokeArgs(Object(app\admin\controller\shop\Order), Array)
#3 D:\work\phpstudy\5i40\code\thinkphp\library\think\App.php(611): think\App::invokeMethod(Array, Array)
#4 D:\work\phpstudy\5i40\code\thinkphp\library\think\App.php(456): think\App::module(Array, Array, true)
#5 D:\work\phpstudy\5i40\code\thinkphp\library\think\App.php(139): think\App::exec(Array, Array)
#6 D:\work\phpstudy\5i40\code\public\boboadmin.php(37): think\App::run()
#7 {main}”
}
问题分析
1. 这个问题可能是因为 save() 方法的返回值判断问题。在 ThinkPHP 中,save() 方法即使更新成功,返回值也可能是 0(当数据没有实际变化时)
2. ThinkPHP 的一个特性,success() 方法会抛出一个异常来实现跳转。
问题解决
经过 1 和 2 分析后,代码优化如下
/**
* 更新订单收货信息
* @ApiTitle (更新订单收货信息)
* @ApiMethod (POST)
* @ApiParams (name="id", type="integer", required=true, description="订单ID")
* @ApiParams (name="receiver", type="string", required=true, description="收货人姓名")
* @ApiParams (name="address", type="string", required=true, description="收货地址")
* @ApiParams (name="mobile", type="string", required=true, description="收货手机号")
*/
public function updateReceiveInfo()
{
// 获取参数
$id = $this->request->post('id');
$receiver = $this->request->post('receiver');
$address = $this->request->post('address');
$mobile = $this->request->post('mobile');
// 参数验证
if (!$id || !$receiver || !$address || !$mobile) {
$this->error('参数不完整');
}
// 验证手机号格式
if (!preg_match("/^1[3-9]\d{9}$/", $mobile)) {
$this->error('手机号格式不正确');
}
// 查找订单
$row = $this->model->where('id', $id)->find();
if (!$row) {
$this->error('订单未找到');
}
// 检查订单状态
if ($row['orderstate'] != 0 || $row['shippingstate'] != 0) {
$this->error('订单状态不允许修改收货信息');
}
// 更新数据
try {
$updateData = [
'receiver' => strip_tags(trim($receiver)),
'address' => strip_tags(trim($address)),
'mobile' => trim($mobile),
'updatetime' => time()
];
// 先检查数据是否有变化
$isChanged = false;
foreach ($updateData as $key => $value) {
if ($row->$key != $value) {
$isChanged = true;
break;
}
}
if ($isChanged) {
$res = $row->save($updateData);
} else {
$res = true; // 数据没变化也算成功
}
if ($res !== false) {
$this->success('更新成功');
} else {
$this->error('更新失败');
}
} catch (\think\exception\HttpResponseException $e) {
// 这是 success 或 error 抛出的异常,直接抛出
throw $e;
} catch (\Exception $e) {
// 其他异常,返回错误信息
$this->error('系统错误:' . $e->getMessage());
}
// } catch (\Exception $e) {
// // 打印完整异常信息
// var_dump('异常信息:', [
// 'message' => $e->getMessage(),
// 'code' => $e->getCode(),
// 'file' => $e->getFile(),
// 'line' => $e->getLine(),
// 'trace' => $e->getTraceAsString()
// ]);
// $this->error('更新失败:' . $e->getMessage());
// }
}
成功结果
本作品采用《CC 协议》,转载必须注明作者和本文链接