订单发货优化

This commit is contained in:
Dev
2026-10-14 17:21:51 +08:00
parent cc0c6729a7
commit a070927b15
5 changed files with 101 additions and 14 deletions
@@ -48,7 +48,7 @@ class Orders extends Backend
if ($this->request->isAjax()) {
try {
OrderService::orderShip($ids);
$this->success("发货成功", null, ['id' => $ids]);
$this->success("发货成功,请到订单发货管理中编辑并确认发货以通知用户", null, ['id' => $ids]);
} catch (\Exception $e) {
if ($e instanceof HttpResponseException) {
throw $e;
@@ -3,6 +3,11 @@
namespace app\admin\controller\order;
use app\common\controller\Backend;
use app\common\service\OrderService;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
* 订单发货管理
@@ -25,13 +30,62 @@ class Ships extends Backend
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
* 编辑(选择「确认发货」后保存时发送发货通知)
* @param $ids
* @return string
* @throws \think\Exception
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
//需求交付发货单由需求流程自行通知,这里只对软件订单发货单提供确认发货
$canConfirmShip = empty($row['demand_id']);
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
$this->view->assign('canConfirmShip', $canConfirmShip);
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
$confirmShip = $canConfirmShip && $this->request->post('confirm_ship/d', 0) === 1;
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException()->validate($validate);
}
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
if ($confirmShip) {
try {
$sent = OrderService::sendShipNotice($row['order_no']);
} catch (Exception $e) {
$this->error('保存成功,但' . $e->getMessage());
}
$this->success($sent ? '保存成功,已发送发货通知' : '保存成功,发货通知发送失败,请查看日志');
}
$this->success();
}
}
@@ -9,5 +9,7 @@ return [
'Files' => '多文件',
'Remark' => '备注',
'Createtime' => '添加时间',
'Updatetime' => '更新时间'
'Updatetime' => '更新时间',
'Confirm_ship' => '确认发货',
'Confirm_ship_tips' => '选择「是」保存后会给用户发送订单发货通知(用户下单时只授权一次,请在发货内容填写完整后再确认)'
];
@@ -52,6 +52,18 @@
<input id="c-remark" class="form-control" name="row[remark]" type="text" value="{$row.remark|htmlentities}">
</div>
</div>
{if $canConfirmShip}
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Confirm_ship')}:</label>
<div class="col-xs-12 col-sm-8">
<div class="radio">
<label for="confirm_ship-1"><input id="confirm_ship-1" name="confirm_ship" type="radio" value="1" /> {:__('Yes')}</label>
<label for="confirm_ship-0"><input id="confirm_ship-0" name="confirm_ship" type="radio" value="0" checked /> {:__('No')}</label>
</div>
<span class="help-block">{:__('Confirm_ship_tips')}</span>
</div>
</div>
{/if}
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
@@ -651,14 +651,14 @@ class OrderService
}
/**
* 订单发货
* 订单发货(只生成发货单并更新状态,发货通知在后台「订单发货管理」编辑并确认发货时发送)
* @param $id
* @return void
* @throws \Exception
*/
public static function orderShip($id)
{
$order = Db::transaction(function () use ($id) {
Db::transaction(function () use ($id) {
//加锁读取,防止并发重复发货、重复分配激活码
$order = OrderService::where(['id' => $id])->lock(true)->find();
if (!$order) {
@@ -677,11 +677,27 @@ class OrderService
if (!$saveRes) {
exception('更新发货状态失败');
}
return $order;
});
}
//发送订单发货通知(事务提交后发送,失败只记日志,不影响发货结果)
/**
* 发送软件订单发货通知(订阅消息 + 站内消息),失败只记日志
* @param string $orderNo 订单号
* @return bool 是否发送成功
* @throws \Exception 订单不存在或不是已发货的软件订单
*/
public static function sendShipNotice($orderNo): bool
{
$order = self::getOne(['order_no' => $orderNo], ['orderGoods']);
if (!$order) {
exception('订单不存在');
}
if ($order->order_type != self::ORDER_TYPE_SOFTWARE) {
exception('仅软件订单支持发送发货通知');
}
if ($order->order_status != self::ORDER_STATUS_SHIPPED) {
exception('订单未发货,不能发送发货通知');
}
try {
$msgData = [
'page' => SubscribeMessageService::PAGE_ORDER_DETAIL,
@@ -696,10 +712,13 @@ class OrderService
(new EasyWeChatService())->sendMsg($order->user_id, SubscribeMessageService::MSG_TEMP_ORDER_SHIPPED, $msgData);
} catch (\Exception $e) {
Log::error(__CLASS__ . ' 订单发货通知发送失败:' . json_encode([
'order_id' => $id,
'order_no' => $orderNo,
'error' => $e->getMessage(),
], JSON_UNESCAPED_UNICODE));
return false;
}
return true;
}
}