jk8Services = app(Jk8Services::class); $this->model = new UserModel(); $this->score = new UserScore(); } /** * 验证逻辑 */ protected function validateModelData(array $data, string $scene = ''): void { if (!$this->modelValidate) return; $validateClass = str_replace("\\model\\", "\\validate\\", get_class($this->model)); if (class_exists($validateClass)) { $validate = new $validateClass(); if ($scene) $validate->scene($scene); $validate->check($data); } } /** * 查看 * @throws Throwable */ public function index(): void { if ($this->request->param('select')) { $this->select(); } list($where, $alias, $limit, $order) = $this->queryBuilder(); $res = $this->model ->withoutField('password,salt') ->withJoin($this->withJoinTable, $this->withJoinType) ->alias($alias) ->where($where) ->order($order) ->paginate($limit); $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } /** * 添加 * @throws Throwable */ public function add(): void { if (!$this->request->isPost()) { $this->error(__('Parameter error')); } $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } $result = false; $passwd = $data['password']; // 密码将被排除不直接入库 $data = $this->excludeFields($data); try { // 模型验证 $this->validateModelData($data, 'add'); $jkId = $this->jk8Services->register($data['username'], $data['nickname'] ?? '', $data['referrer_code'] ?? ''); } catch (Throwable $e) { $this->error($e->getMessage()); } $this->model->startTrans(); try { $data['jk_user_id'] = $jkId; $result = $this->model->save($data); $this->model->commit(); if (!empty($passwd)) { $this->model->resetPassword($this->model->id, $passwd); } } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(__('Added successfully')); } else { $this->error(__('No rows were added')); } } /** * 编辑 * @throws Throwable */ public function edit(): void { $pk = $this->model->getPk(); $id = $this->request->param($pk); $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } if ($this->request->isPost()) { $password = $this->request->post('password', ''); if ($password) { $this->model->resetPassword($id, $password); } parent::edit(); } unset($row->salt); $row->password = ''; $this->success('', [ 'row' => $row ]); } /** * 重写select * @throws Throwable */ public function select(): void { list($where, $alias, $limit, $order) = $this->queryBuilder(); $res = $this->model ->withoutField('password,salt') ->withJoin($this->withJoinTable, $this->withJoinType) ->alias($alias) ->where($where) ->order($order) ->paginate($limit); foreach ($res as $re) { $re->nickname_text = $re->username . '(ID:' . $re->id . ')'; } $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } /** * 钱包 * @throws Throwable */ public function wallet(): void { $miniGames = config('mini_game') ?: []; $pk = $this->model->getPk(); $id = $this->request->param($pk); if ($this->request->isPost()) { $this->model = new UserScore(); parent::edit(); return; } $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } $existGameTypes = $row->userScore() ->whereIn('game_type', array_keys($miniGames)) ->column('game_type'); $scoreInsert = []; foreach ($miniGames as $key => $name) { if (!in_array($key, $existGameTypes)) { $scoreInsert[] = [ 'user_id' => $id, 'game_type' => $key, 'score' => 0, ]; } } if (!empty($scoreInsert)) { $row->userScore()->saveAll($scoreInsert); } $data = [ 'mini_game' => $miniGames, 'score' => $row->userScore() ->whereIn('game_type', array_keys($miniGames)) ->withoutField(['user_id']) ->order('game_type', 'asc') ->select() ]; $this->success('', $data); } }