feat: 新增发票抬头、晒单、钱包日志等模型及后台管理功能
- 新增 invoice_title、showcase、wallet_log 模型及对应语言包 - 新增后台控制器、视图及搜索字段配置 - 修复服务类目删除保护逻辑 - 优化后台登录页面文案及样式 - 新增前端角色切换逻辑及页面拦截 - 新增 SVG 图标及样式变量 - 修复接口权限及下拉框默认值问题
这个提交包含在:
@@ -3,20 +3,21 @@
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* 优惠券模板
|
||||
* 优惠券模板管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
* @icon fa fa-ticket
|
||||
*/
|
||||
class Coupon extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Coupon模型对象
|
||||
* @var \app\admin\model\Coupon
|
||||
*/
|
||||
protected $model = null;
|
||||
protected $searchFields = 'id,name';
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
@@ -26,13 +27,181 @@ class Coupon extends Backend
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券模板列表(聚合真实领取张数与核销使用张数)
|
||||
*/
|
||||
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();
|
||||
}
|
||||
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
|
||||
$list = $this->model
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
$rows = $list->items();
|
||||
$ids = [];
|
||||
foreach ($rows as $r) {
|
||||
$ids[] = (int)$r['id'];
|
||||
}
|
||||
|
||||
$claimMap = [];
|
||||
$usedMap = [];
|
||||
if ($ids) {
|
||||
$stats = Db::name('user_coupon')
|
||||
->where('coupon_id', 'in', $ids)
|
||||
->field('coupon_id, COUNT(*) AS claim_cnt, SUM(CASE WHEN status = \'used\' THEN 1 ELSE 0 END) AS used_cnt')
|
||||
->group('coupon_id')
|
||||
->select();
|
||||
foreach ($stats as $st) {
|
||||
$cid = (int)$st['coupon_id'];
|
||||
$claimMap[$cid] = (int)$st['claim_cnt'];
|
||||
$usedMap[$cid] = (int)$st['used_cnt'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($rows as &$item) {
|
||||
$cid = (int)$item['id'];
|
||||
$item['real_claimed'] = max((int)$item['received'], $claimMap[$cid] ?? 0);
|
||||
$item['used_count'] = $usedMap[$cid] ?? 0;
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return json(['total' => $list->total(), 'rows' => $rows]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
* 优惠券领用与核销全景详情(关联 fa_user_coupon + fa_user + fa_order)
|
||||
*/
|
||||
public function detail($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$cid = (int)$row['id'];
|
||||
$totalClaimed = (int)Db::name('user_coupon')->where('coupon_id', $cid)->count();
|
||||
$totalUsed = (int)Db::name('user_coupon')->where('coupon_id', $cid)->where('status', 'used')->count();
|
||||
|
||||
$records = Db::name('user_coupon')->alias('uc')
|
||||
->join('__USER__ u', 'u.id = uc.user_id', 'LEFT')
|
||||
->join('__ORDER__ o', 'o.id = uc.order_id', 'LEFT')
|
||||
->where('uc.coupon_id', $cid)
|
||||
->field('uc.*, u.nickname, u.username, u.mobile, o.order_no, o.service_name, o.pay_amount, o.discount_amount')
|
||||
->order('uc.id desc')
|
||||
->limit(30)
|
||||
->select();
|
||||
|
||||
$drivenGmv = (float)Db::name('user_coupon')->alias('uc')
|
||||
->join('__ORDER__ o', 'o.id = uc.order_id', 'INNER')
|
||||
->where('uc.coupon_id', $cid)
|
||||
->where('uc.status', 'used')
|
||||
->sum('o.pay_amount');
|
||||
|
||||
$this->view->assign([
|
||||
'row' => $row,
|
||||
'totalClaimed' => max((int)$row['received'], $totalClaimed),
|
||||
'totalUsed' => $totalUsed,
|
||||
'drivenGmv' => round($drivenGmv, 2),
|
||||
'records' => $records,
|
||||
]);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增卡券模板(强制初始化已领取数为 0)
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if (false === $this->request->isPost()) {
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a', []);
|
||||
if (!$params) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
$params['received'] = 0;
|
||||
if (($params['type'] ?? 'cash') === 'discount') {
|
||||
$disc = (float)($params['discount'] ?? 0);
|
||||
if ($disc <= 0 || $disc >= 1) {
|
||||
$this->error('折扣券的折扣系数须大于 0 且小于 1(如 0.85 表示 8.5 折)');
|
||||
}
|
||||
$params['amount'] = 0.00;
|
||||
} else {
|
||||
if ((float)($params['amount'] ?? 0) <= 0) {
|
||||
$this->error('满减代金券的减免金额须大于 0');
|
||||
}
|
||||
$params['discount'] = 1.00;
|
||||
}
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
}
|
||||
$this->error($this->model->getError() ?: __('No rows were inserted'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑卡券模板(锁定已领取数量防止人工篡改库存)
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
if (false === $this->request->isPost()) {
|
||||
$this->view->assign('row', $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
$params = $this->request->post('row/a', []);
|
||||
if (!$params) {
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$params = $this->preExcludeFields($params);
|
||||
unset($params['received']);
|
||||
if (($params['type'] ?? $row['type']) === 'discount') {
|
||||
$disc = (float)($params['discount'] ?? $row['discount']);
|
||||
if ($disc <= 0 || $disc >= 1) {
|
||||
$this->error('折扣券的折扣系数须大于 0 且小于 1(如 0.85 表示 8.5 折)');
|
||||
}
|
||||
$params['amount'] = 0.00;
|
||||
} else {
|
||||
if ((float)($params['amount'] ?? $row['amount']) <= 0) {
|
||||
$this->error('满减代金券的减免金额须大于 0');
|
||||
}
|
||||
$params['discount'] = 1.00;
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
}
|
||||
$this->error($row->getError() ?: __('No rows were updated'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除保护:已有用户领取的卡券禁止物理删除
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
$claimedCnt = (int)Db::name('user_coupon')->where('coupon_id', 'in', $idArr)->count();
|
||||
if ($claimedCnt > 0) {
|
||||
$this->error('所选优惠券已有用户领取记录(共 ' . $claimedCnt . ' 张),禁止物理删除,请改为「隐藏」停止发放');
|
||||
}
|
||||
return parent::del($ids);
|
||||
}
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户