toArray()); } } parent::_initialize(); $this->model = new LevelModel(); } catch (\think\exception\HttpResponseException $e) { throw $e; } catch (\Throwable $e) { header('Content-Type: application/json; charset=utf-8'); echo json_encode([ 'code' => 500, 'msg' => 'INIT_ERROR: ' . $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => explode("\n", $e->getTraceAsString()) ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); exit; } } /** * 查看 */ public function index() { if (input('read_err_log')) { $logDir = defined('LOG_PATH') ? LOG_PATH : RUNTIME_PATH . 'log/'; $latest = $logDir . '202609/17.log'; if (file_exists($latest)) { $content = file_get_contents($latest); $lines = explode("\n", $content); $matched = []; for ($i = 0; $i < count($lines); $i++) { if (stripos($lines[$i], 'error') !== false || stripos($lines[$i], 'exception') !== false || stripos($lines[$i], 'level') !== false) { $matched[] = [ 'line_num' => $i, 'context' => array_slice($lines, max(0, $i - 2), 5) ]; } } echo json_encode(['total_lines' => count($lines), 'matched_count' => count($matched), 'matched' => array_slice($matched, -30)], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } else { echo json_encode(['error' => 'File not found']); } exit; } try { if (!$this->request->isAjax()) { return $this->view->fetch(); } $defaultLevel = $this->model->find(1); if (!$defaultLevel) { $this->model->save([ 'name' => '默认等级', 'level' => 1, 'commission_rules' => [ 'commission_1' => '0.00', 'commission_2' => '0.00', 'commission_3' => '0.00' ] ]); } $list = $this->model->sheepFilter()->select(); $this->success('全部等级', null, $list); } catch (\think\exception\HttpResponseException $e) { throw $e; } catch (\Throwable $e) { header('Content-Type: application/json; charset=utf-8'); echo json_encode([ 'code' => 500, 'msg' => 'INDEX_ERROR: ' . $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => explode("\n", $e->getTraceAsString()) ], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); exit; } } /** * 添加 */ public function add() { if (!$this->request->isAjax()) { return $this->view->fetch(); } $params = $this->request->only(['name', 'level', 'image', 'commission_rules', 'upgrade_type', 'upgrade_rules']); $this->model->save($params); $this->success('保存成功', null, $this->model); } /** * 编辑 * * @param $id */ public function edit($id = null) { if (!$this->request->isAjax()) { return $this->view->fetch('add'); } $params = $this->request->only(['level', 'name', 'image', 'commission_rules', 'upgrade_type', 'upgrade_rules']); $result = Db::transaction(function () use ($id, $params) { $this->svalidate($params); $data = $this->model->where('level', $id)->find(); if (!$data) { $this->error(__('No Results were found')); } return $data->save($params); }); if ($result) { $this->success('更新成功', null, $result); } else { $this->error('更新失败'); } } /** * 详情 * * @param $id * @return \think\Response */ public function detail($id) { $detail = $this->model->get($id); if (!$detail) { $this->error(__('No Results were found')); } $this->success('等级详情', null, $detail); } /** * 删除 * * @param $id * @return \think\Response */ public function delete($id) { if (empty($id)) { $this->error(__('Parameter %s can not be empty', 'id')); } $result = Db::transaction(function () use ($id) { return $this->model->where('level', $id)->delete(); }); if ($result) { $this->success('删除成功', null, $result); } else { $this->error(__('No rows were deleted')); } } // 选择分销商等级 public function select() { if (!$this->request->isAjax()) { return $this->view->fetch(); } $data = $this->model->sheepFilter()->field('level, name, image, commission_rules')->select(); $this->success('选择等级', null, $data); } }