Configuration.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Models\Api\Promoter;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. /**
  6. * 推广员配置模型
  7. * @author 唐远望
  8. * @version 1.0
  9. * @date 2025-09-05
  10. */
  11. class Configuration extends Model
  12. {
  13. use HasFactory;
  14. protected $table = 'promoter_configuration';
  15. // 是否主动维护时间戳
  16. public $timestamps = false;
  17. protected $connection = 'company';
  18. // 定义时间戳字段名
  19. // const CREATED_AT = 'insert_time';
  20. // const UPDATED_AT = 'update_time';
  21. protected $appends = ['content'];
  22. /**
  23. * 获取推广员配置内容
  24. * @author 唐远望
  25. * @version 1.0
  26. * @date 2025-09-05
  27. * @return string
  28. */
  29. public function getContentAttribute()
  30. {
  31. $content = $this->attributes['content'];
  32. //判断是否json格式
  33. if ($this->isValidJson($content)) {
  34. $content = json_decode($content, true);
  35. }
  36. return $content;
  37. }
  38. /**
  39. * 获取推广员配置内容
  40. * @author 唐远望
  41. * @version 1.0
  42. * @date 2025-09-05
  43. * @return string
  44. */
  45. function isValidJson($string)
  46. {
  47. if (!is_string($string) || empty($string)) {
  48. return false;
  49. }
  50. // 尝试解码
  51. $result = json_decode($string);
  52. // 检查解码错误
  53. if (json_last_error() !== JSON_ERROR_NONE) {
  54. return false;
  55. }
  56. // 确保解码后不是null(除非原始JSON就是"null")
  57. if ($result === null && $string !== 'null') {
  58. return false;
  59. }
  60. return true;
  61. }
  62. }