文件
yjyf/server/application/admin/controller/UserCoupon.php
MiAn fafddceaf2 feat: 新增发票抬头、晒单、钱包日志等模型及后台管理功能
- 新增 invoice_title、showcase、wallet_log 模型及对应语言包
- 新增后台控制器、视图及搜索字段配置
- 修复服务类目删除保护逻辑
- 优化后台登录页面文案及样式
- 新增前端角色切换逻辑及页面拦截
- 新增 SVG 图标及样式变量
- 修复接口权限及下拉框默认值问题
2026-11-11 17:02:35 +08:00

106 行
3.3 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
use Exception;
/**
* 用户领券记录管理
*
* @icon fa fa-ticket
*/
class UserCoupon extends Backend
{
/**
* @var \app\admin\model\UserCoupon
*/
protected $model = null;
protected $relationSearch = true;
protected $searchFields = 'id,user.nickname,user.mobile,coupon.name,order.order_no';
protected $multiFields = 'status';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\UserCoupon;
$this->view->assign('statusList', $this->model->getStatusList());
}
/**
* 领券记录列表(支持按状态 unused/used/expired 筛选及关联会员/优惠券/核销订单检索)
*/
public function index()
{
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
return $this->view->fetch();
}
if ($this->request->request('keyField')) {
return $this->selectpage();
}
// 自动将已超过优惠券截止时间的未使用领券记录同步为 expired
$now = time();
$expiredCouponIds = Db::name('coupon')
->where('end_time', '>', 0)
->where('end_time', '<', $now)
->column('id');
if (!empty($expiredCouponIds)) {
Db::name('user_coupon')
->where('status', 'unused')
->where('coupon_id', 'in', $expiredCouponIds)
->update(['status' => 'expired']);
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = $this->model
->with(['user', 'coupon', 'order'])
->where($where)
->order($sort, $order)
->paginate($limit);
return json(['total' => $list->total(), 'rows' => $list->items()]);
}
/**
* 禁止后台空造领券流水
*/
public function add()
{
$this->error('用户领券记录由客户端领券中心真实发放生成,不支持后台手工伪造');
}
/**
* 删除未使用或已过期的无效领券记录(已核销关联订单的记录受账务保护禁止删除)
*/
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__('Invalid parameters'));
}
$ids = $ids ?: $this->request->post('ids');
$idArr = array_values(array_filter(array_map('intval', explode(',', (string)$ids))));
if (!$idArr) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$usedCnt = Db::name('user_coupon')->where('id', 'in', $idArr)->where('status', 'used')->count();
if ($usedCnt > 0) {
$this->error('所选记录中包含已核销抵扣订单的优惠券,为保障账务闭环禁止删除');
}
Db::startTrans();
try {
$deleted = Db::name('user_coupon')->where('id', 'in', $idArr)->delete();
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($deleted) {
$this->success();
}
$this->error(__('No rows were deleted'));
}
}