文件
fenleixinxi/application/admin/controller/fc/Order.php

150 行
4.8 KiB
PHP

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

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

<?php
namespace app\admin\controller\fc;
use app\common\controller\Backend;
use think\Db;
/**
* 同城-订单
*
* @icon fa fa-circle-o
*/
class Order extends Backend
{
/**
* FcOrder模型对象
* @var \app\admin\model\FcOrder
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\FcOrder;
$this->view->assign("statusList", $this->model->getStatusList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 订单退款
*/
public function refund()
{
$ids = $this->request->post('ids');
$amount = $this->request->post('amount');
$reason = $this->request->post('reason', '');
$ids = is_array($ids) ? $ids : explode(',', (string)$ids);
$ids = array_filter($ids);
if (!$ids) {
$this->error('请选择要退款的订单');
}
$orders = Db::name('fc_order')->where('id', 'in', $ids)->select();
if (!$orders) {
$this->error('订单不存在');
}
// 金额留空 = 按每笔订单实付金额全额退款
$amount = ($amount === '' || $amount === null || (float)$amount <= 0) ? null : (float)$amount;
$ok = 0;
$errors = [];
foreach ($orders as $order) {
try {
\app\common\library\OrderRefund::refund((int)$order['id'], $amount, (string)$reason);
$ok++;
} catch (\Throwable $e) {
$errors[] = $order['order_no'] . ':' . $e->getMessage();
}
}
if ($ok === 0) {
$this->error('退款失败:' . implode(';', $errors));
}
$msg = '退款成功 ' . $ok . ' 笔';
if ($errors) {
$msg .= ',失败 ' . count($errors) . ' 笔:' . implode(';', $errors);
}
$this->success($msg);
}
/**
* 订单补单(含权益发放与分销结算)
*
* @return void
*/
public function supplement()
{
$orderNo = trim((string)$this->request->post('orderNo', ''));
if ($orderNo === '') {
$this->error('订单号不能为空');
}
$order = Db::name('fc_order')->where('order_no', $orderNo)->find();
if (!$order) {
$this->error('订单不存在');
}
if ((string)$order['status'] === 'paid') {
$this->success('订单已支付');
}
if ((string)$order['status'] !== 'pending') {
$this->error('订单状态不支持补单(当前:' . $order['status'] . ')');
}
$now = time();
try {
Db::transaction(function () use ($order, $now) {
$locked = Db::name('fc_order')->where('id', (int)$order['id'])->lock(true)->find();
if (!$locked || (string)$locked['status'] !== 'pending') {
return;
}
$affected = Db::name('fc_order')
->where('id', (int)$order['id'])
->where('status', 'pending')
->update([
'status' => 'paid',
'paytime' => $now,
'updatetime' => $now,
]);
if (!$affected) {
return;
}
$locked['status'] = 'paid';
$locked['paytime'] = $now;
Db::name('fc_finance_flow')->insert([
'user_id' => (int)$locked['user_id'],
'flow_no' => 'SK' . date('YmdHis') . $locked['id'],
'type' => 'income',
'scene' => '订单补单',
'amount' => $locked['amount'],
'balance' => 0,
'related_id' => $locked['id'],
'remark' => '订单补单',
'createtime' => $now,
]);
// 统一发放订单权益(会员开通 / 余额充值入账 / 付费查看解锁 / 发帖激活)并触发分销佣金结算
\app\common\library\OrderFulfill::handle($locked);
});
} catch (\Throwable $e) {
\think\Log::record('后台订单补单失败:订单 ' . $orderNo . ',' . $e->getMessage(), 'error');
$this->error('补单失败:' . $e->getMessage());
}
$this->success('补单成功');
}
}