Config.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Config extends BaseModel
  5. {
  6. protected $table = 'config';
  7. protected $fillable = ['field', 'val', 'remark', 'group_id', 'type', 'data'];
  8. const GROUP_BASIC = 0;//基础配置
  9. const GROUP_USER = 1;//用户自定义配置
  10. const TYPE_STRING = 'string';//单行文本
  11. const TYPE_TEXT = 'text';//多行文本
  12. const TYPE_AMOUNT = 'amount';//金额
  13. const TYPE_NUMBER = 'number';//整型
  14. const TYPE_RADIO = 'radio';//单选
  15. const TYPE_CHECKBOX = 'checkbox';//多选
  16. const TYPE_PERCENTAGE = 'percentage';//百分比
  17. public static array $rules = [
  18. Config::TYPE_STRING => ['required', 'string', 'min:1', 'max:140'],
  19. Config::TYPE_TEXT => ['required', 'string'],
  20. Config::TYPE_AMOUNT => ['required', 'numeric', 'min:0.01', 'regex:/^\d+(\.\d{1,2})?$/'],
  21. Config::TYPE_NUMBER => ['required', 'integer', 'min:0'],
  22. // Config::TYPE_RADIO => ['required', 'integer', 'min:0'],
  23. // Config::TYPE_CHECKBOX => ['required', 'array', 'min:1'],
  24. Config::TYPE_PERCENTAGE => ['required', 'numeric', 'min:0.0001', 'max:1', 'regex:/^\d+(\.\d{1,4})?$/'],
  25. ];
  26. public function getRule()
  27. {
  28. if (Config::TYPE_RADIO == $this->type) {
  29. $val = [];
  30. foreach ($this->data as $value) $val[] = $value['value'];
  31. $val = implode(',', $val);
  32. return ['val' => ['required', 'integer', "in:{$val}"]];
  33. }
  34. if (Config::TYPE_CHECKBOX == $this->type) {
  35. $val = [];
  36. foreach ($this->data as $value) $val[] = $value['value'];
  37. $val = implode(',', $val);
  38. return [
  39. 'val' => ['required', 'array', 'min:1'],
  40. 'val.*' => ['required', 'integer', "in:{$val}"]
  41. ];
  42. }
  43. return ['val' => Config::$rules[$this->type]];
  44. }
  45. public static function groups()
  46. {
  47. return [
  48. static::GROUP_BASIC,
  49. static::GROUP_USER,
  50. ];
  51. }
  52. protected function getDataAttribute($value)
  53. {
  54. return json_decode($value, true);
  55. }
  56. }