model = new MiniProgramTemplateMessage(); } public function getItems(): array { return $this->model ->field(['id', 'code', 'title', 'usage_detail', 'keywords', 'template_id', 'appid', 'type']) ->order('id', 'asc') ->select() ->toArray(); } public function updateMessage(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('模板消息不存在'); } $templateId = trim((string) ($data['template_id'] ?? '')); $appid = trim((string) ($data['appid'] ?? '')); $type = trim((string) ($data['type'] ?? '一次性订阅')); if (mb_strlen($templateId) > 128) { throw new ApiException('模板 ID 不能超过 128 个字符'); } if ($appid === '' || mb_strlen($appid) > 64) { throw new ApiException('请输入正确的小程序 APPID'); } if (!in_array($type, self::TYPES, true)) { throw new ApiException('订阅类型设置不正确'); } return $model->save([ 'template_id' => $templateId, 'appid' => $appid, 'type' => $type, ]) !== false; } }