TemplateMessageLogic.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. namespace plugin\saiadmin\app\logic\miniProgram;
  6. use plugin\saiadmin\app\model\miniProgram\MiniProgramTemplateMessage;
  7. use plugin\saiadmin\basic\think\BaseLogic;
  8. use plugin\saiadmin\exception\ApiException;
  9. /**
  10. * 小程序模板消息逻辑层
  11. */
  12. class TemplateMessageLogic extends BaseLogic
  13. {
  14. private const TYPES = ['一次性订阅', '长期订阅'];
  15. public function __construct()
  16. {
  17. $this->model = new MiniProgramTemplateMessage();
  18. }
  19. public function getItems(): array
  20. {
  21. return $this->model
  22. ->field(['id', 'code', 'title', 'usage_detail', 'keywords', 'template_id', 'appid', 'type'])
  23. ->order('id', 'asc')
  24. ->select()
  25. ->toArray();
  26. }
  27. public function updateMessage(array $data): bool
  28. {
  29. $id = (int) ($data['id'] ?? 0);
  30. if ($id <= 0) {
  31. throw new ApiException('模板消息参数错误');
  32. }
  33. $model = $this->model->findOrEmpty($id);
  34. if ($model->isEmpty()) {
  35. throw new ApiException('模板消息不存在');
  36. }
  37. $templateId = trim((string) ($data['template_id'] ?? ''));
  38. $appid = trim((string) ($data['appid'] ?? ''));
  39. $type = trim((string) ($data['type'] ?? '一次性订阅'));
  40. if (mb_strlen($templateId) > 128) {
  41. throw new ApiException('模板 ID 不能超过 128 个字符');
  42. }
  43. if ($appid === '' || mb_strlen($appid) > 64) {
  44. throw new ApiException('请输入正确的小程序 APPID');
  45. }
  46. if (!in_array($type, self::TYPES, true)) {
  47. throw new ApiException('订阅类型设置不正确');
  48. }
  49. return $model->save([
  50. 'template_id' => $templateId,
  51. 'appid' => $appid,
  52. 'type' => $type,
  53. ]) !== false;
  54. }
  55. }