| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- // +----------------------------------------------------------------------
- // | saiadmin [ saiadmin快速开发框架 ]
- // +----------------------------------------------------------------------
- namespace plugin\saiadmin\app\logic\miniProgram;
- use plugin\saiadmin\app\model\miniProgram\MiniProgramTemplateMessage;
- use plugin\saiadmin\basic\think\BaseLogic;
- use plugin\saiadmin\exception\ApiException;
- /**
- * 小程序模板消息逻辑层
- */
- class TemplateMessageLogic extends BaseLogic
- {
- private const TYPES = ['一次性订阅', '长期订阅'];
- public function __construct()
- {
- $this->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;
- }
- }
|