request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = Db::name('fc_coupon') ->where($where) ->order($sort, $order) ->paginate($limit); return json(['total' => $list->total(), 'rows' => $list->items()]); } /** * 添加 */ public function add() { // 与 index() 一致:统一过滤 XSS 载荷(列表用 bootstrap-table 原样渲染,未过滤会造成存储型 XSS) $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $data = $this->buildData($params); $now = time(); // 已领取数量只读,新增时固定为 0 $data['received'] = 0; $data['createtime'] = $now; $data['updatetime'] = $now; if (false === Db::name('fc_coupon')->insert($data)) { $this->error('优惠券添加失败'); } $this->success(); return null; } /** * 编辑 * * @param mixed $ids */ public function edit($ids = null) { // 与 index() 一致:统一过滤 XSS 载荷 $this->request->filter(['strip_tags', 'trim']); $row = Db::name('fc_coupon')->where('id', (int)$ids)->find(); 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 (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $data = $this->buildData($params); // 已领取数量只读,编辑时不修改 $data['updatetime'] = time(); if (false === Db::name('fc_coupon')->where('id', (int)$ids)->update($data)) { $this->error(__('No rows were updated')); } $this->success(); return null; } /** * 删除 * * @param mixed $ids */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $ids = $ids ?: $this->request->post('ids'); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } $count = Db::name('fc_coupon')->where('id', 'in', $ids)->delete(); if (!$count) { $this->error(__('No rows were deleted')); } $this->success(); return null; } /** * 批量更新(仅允许「是否启用 / 排序」) * * @param mixed $ids */ public function multi($ids = null) { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $ids = $ids ?: $this->request->post('ids'); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } if (false === $this->request->has('params')) { $this->error(__('No rows were updated')); } parse_str($this->request->post('params'), $values); $values = array_intersect_key($values, array_flip(['enabled', 'sort'])); if (empty($values)) { $this->error(__('You have no permission')); } if (isset($values['enabled'])) { $values['enabled'] = !empty($values['enabled']) ? 1 : 0; } if (isset($values['sort'])) { $values['sort'] = (int)$values['sort']; } $values['updatetime'] = time(); $count = Db::name('fc_coupon')->where('id', 'in', $ids)->update($values); if (!$count) { $this->error(__('No rows were updated')); } $this->success(); return null; } /** * 校验并整理表单数据 * * @param array $params * @return array */ protected function buildData(array $params) { $name = isset($params['name']) ? trim((string)$params['name']) : ''; if ($name === '') { $this->error('优惠券名称不能为空'); } if (mb_strlen($name, 'utf-8') > 64) { $this->error('优惠券名称不能超过 64 个字'); } $type = isset($params['type']) ? trim((string)$params['type']) : 'fixed'; if (!in_array($type, self::TYPES, true)) { $this->error('优惠券类型只支持「满减券」或「折扣券」'); } $amount = isset($params['amount']) ? round((float)$params['amount'], 2) : 0; $minAmount = isset($params['min_amount']) ? round((float)$params['min_amount'], 2) : 0; $total = isset($params['total']) ? (int)$params['total'] : 0; $days = isset($params['days']) ? (int)$params['days'] : 0; $sort = isset($params['sort']) ? (int)$params['sort'] : 0; $enabled = !empty($params['enabled']) ? 1 : 0; if ($type === 'fixed') { if ($amount <= 0) { $this->error('满减券的面额必须大于 0'); } } else { if ($amount <= 0 || $amount >= 10) { $this->error('折扣券的折扣数必须大于 0 且小于 10(如 8.5 表示 8.5 折)'); } } if ($minAmount < 0) { $this->error('使用门槛不能为负(0 表示无门槛)'); } if ($total < 0) { $this->error('发放总量不能为负(0 表示不限量)'); } if ($days < 0) { $this->error('有效天数不能为负(0 表示永久有效)'); } return [ 'name' => $name, 'type' => $type, 'amount' => $amount, 'min_amount' => $minAmount, 'total' => $total, 'days' => $days, 'enabled' => $enabled, 'sort' => $sort, ]; } }