DefaultSettingsLogic.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. namespace plugin\saiadmin\app\logic\miniProgram;
  6. use plugin\saiadmin\app\model\miniProgram\MiniProgramDefaultSetting;
  7. use plugin\saiadmin\basic\think\BaseLogic;
  8. use plugin\saiadmin\exception\ApiException;
  9. /**
  10. * 小程序默认计费设置逻辑层
  11. */
  12. class DefaultSettingsLogic extends BaseLogic
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new MiniProgramDefaultSetting();
  17. }
  18. public function getSettings(): array
  19. {
  20. $items = $this->model
  21. ->field([
  22. 'id',
  23. 'device_type',
  24. 'device_name',
  25. 'free_minutes',
  26. 'overdue_deposit',
  27. 'min_deposit',
  28. 'max_deposit',
  29. 'sort',
  30. ])
  31. ->order('sort', 'asc')
  32. ->order('id', 'asc')
  33. ->select()
  34. ->toArray();
  35. foreach ($items as &$item) {
  36. $item['id'] = (int) $item['id'];
  37. $item['free_minutes'] = (int) $item['free_minutes'];
  38. $item['overdue_deposit'] = (float) $item['overdue_deposit'];
  39. $item['min_deposit'] = (float) $item['min_deposit'];
  40. $item['max_deposit'] = (float) $item['max_deposit'];
  41. $item['sort'] = (int) $item['sort'];
  42. }
  43. unset($item);
  44. return $items;
  45. }
  46. public function updateSetting(array $data): bool
  47. {
  48. $id = (int) ($data['id'] ?? 0);
  49. if ($id <= 0) {
  50. throw new ApiException('默认设置参数错误');
  51. }
  52. $model = $this->model->findOrEmpty($id);
  53. if ($model->isEmpty()) {
  54. throw new ApiException('默认设置不存在');
  55. }
  56. $freeMinutes = (int) $this->number($data['free_minutes'] ?? null, 0, 10080, '免费时长');
  57. $overdueDeposit = $this->number($data['overdue_deposit'] ?? null, 0, 99999999, '超时订单押金');
  58. $minDeposit = $this->number($data['min_deposit'] ?? null, 0, 99999999, '最低押金');
  59. $maxDeposit = $this->number($data['max_deposit'] ?? null, 0, 99999999, '最高押金');
  60. if ($maxDeposit < $minDeposit) {
  61. throw new ApiException('最高押金不能小于最低押金');
  62. }
  63. return $model->save([
  64. 'free_minutes' => $freeMinutes,
  65. 'overdue_deposit' => $overdueDeposit,
  66. 'min_deposit' => $minDeposit,
  67. 'max_deposit' => $maxDeposit,
  68. ]) !== false;
  69. }
  70. private function number(mixed $value, float $min, float $max, string $label): float
  71. {
  72. if (!is_numeric($value)) {
  73. throw new ApiException($label . '必须是数字');
  74. }
  75. $number = (float) $value;
  76. if ($number < $min || $number > $max) {
  77. throw new ApiException($label . '超出允许范围');
  78. }
  79. return $number;
  80. }
  81. }