ShareSettingsLogic.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. namespace plugin\saiadmin\app\logic\miniProgram;
  6. use plugin\saiadmin\app\model\miniProgram\MiniProgramShareSetting;
  7. use plugin\saiadmin\basic\think\BaseLogic;
  8. use plugin\saiadmin\exception\ApiException;
  9. /**
  10. * 小程序分享设置逻辑层
  11. */
  12. class ShareSettingsLogic extends BaseLogic
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new MiniProgramShareSetting();
  17. }
  18. public function getItems(): array
  19. {
  20. return $this->model
  21. ->field(['id', 'share_name', 'share_title', 'share_image'])
  22. ->order('id', 'asc')
  23. ->select()
  24. ->toArray();
  25. }
  26. public function updateSetting(array $data): bool
  27. {
  28. $id = (int) ($data['id'] ?? 0);
  29. if ($id <= 0) {
  30. throw new ApiException('分享设置参数错误');
  31. }
  32. $model = $this->model->findOrEmpty($id);
  33. if ($model->isEmpty()) {
  34. throw new ApiException('分享设置不存在');
  35. }
  36. $shareName = trim((string) ($data['share_name'] ?? ''));
  37. $shareTitle = trim((string) ($data['share_title'] ?? ''));
  38. $shareImage = trim((string) ($data['share_image'] ?? ''));
  39. if ($shareName === '' || mb_strlen($shareName) > 100) {
  40. throw new ApiException('请输入正确的分享名称');
  41. }
  42. if ($shareTitle === '' || mb_strlen($shareTitle) > 255) {
  43. throw new ApiException('请输入正确的分享标题');
  44. }
  45. if (mb_strlen($shareImage) > 500) {
  46. throw new ApiException('分享图片地址不能超过 500 个字符');
  47. }
  48. return $model->save([
  49. 'share_name' => $shareName,
  50. 'share_title' => $shareTitle,
  51. 'share_image' => $shareImage,
  52. ]) !== false;
  53. }
  54. }