model = new MiniProgramShareSetting(); } public function getItems(): array { return $this->model ->field(['id', 'share_name', 'share_title', 'share_image']) ->order('id', 'asc') ->select() ->toArray(); } public function updateSetting(array $data): bool { $id = (int) ($data['id'] ?? 0); if ($id <= 0) { throw new ApiException('分享设置参数错误'); } $model = $this->model->findOrEmpty($id); if ($model->isEmpty()) { throw new ApiException('分享设置不存在'); } $shareName = trim((string) ($data['share_name'] ?? '')); $shareTitle = trim((string) ($data['share_title'] ?? '')); $shareImage = trim((string) ($data['share_image'] ?? '')); if ($shareName === '' || mb_strlen($shareName) > 100) { throw new ApiException('请输入正确的分享名称'); } if ($shareTitle === '' || mb_strlen($shareTitle) > 255) { throw new ApiException('请输入正确的分享标题'); } if (mb_strlen($shareImage) > 500) { throw new ApiException('分享图片地址不能超过 500 个字符'); } return $model->save([ 'share_name' => $shareName, 'share_title' => $shareTitle, 'share_image' => $shareImage, ]) !== false; } }