VCoachInfo.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 技师信息视图模型
  6. *
  7. * 包含字段:
  8. * - coach_id: 技师编号
  9. * - user_id: 用户ID
  10. * - nickname: 昵称
  11. * - avatar: 头像
  12. * - gender: 性别
  13. * - mobile: 手机号
  14. * - birthday: 出生日期
  15. * - work_years: 从业年份
  16. * - intention_city: 意向城市
  17. * - introduction: 个人简介
  18. * - life_photos: 生活照片
  19. * - coach_state: 技师状态
  20. * - info_state: 基本信息认证状态
  21. * - info_audit_remark: 基本信息审核回执
  22. * - info_auditor: 基本信息审核人
  23. * - info_audit_time: 基本信息审核时间
  24. */
  25. class VCoachInfo extends Model
  26. {
  27. /**
  28. * 关联到模型的数据表
  29. *
  30. * @var string
  31. */
  32. protected $table = 'v_coach_info';
  33. /**
  34. * 表明模型是否应该被打上时间戳
  35. *
  36. * @var bool
  37. */
  38. public $timestamps = false;
  39. /**
  40. * 可以被批量赋值的属性
  41. *
  42. * @var array
  43. */
  44. protected $guarded = [];
  45. /**
  46. * 应该被追加到模型数组的访问器
  47. *
  48. * @var array
  49. */
  50. protected $appends = [
  51. 'avatar_url', // 头像URL
  52. 'life_photos_url', // 生活照片URL数组
  53. ];
  54. /**
  55. * 获取头像完整URL
  56. */
  57. public function getAvatarUrlAttribute(): ?string
  58. {
  59. if (!$this->avatar) {
  60. return null;
  61. }
  62. // 如果是微信完整链接(http或https开头),直接返回
  63. if (preg_match('/^https?:\/\//', $this->avatar)) {
  64. return $this->avatar;
  65. }
  66. // 否则拼接下载路径
  67. return "/api/download?filename={$this->avatar}&bucket=user-avatar";
  68. }
  69. /**
  70. * 获取生活照片完整URL数组
  71. */
  72. public function getLifePhotosUrlAttribute(): ?array
  73. {
  74. if (!$this->life_photos) {
  75. return null;
  76. }
  77. $photos = is_string($this->life_photos)
  78. ? json_decode($this->life_photos, true)
  79. : $this->life_photos;
  80. return array_map(function ($photo) {
  81. return "/api/download?filename={$photo}&bucket=user-avatar";
  82. }, $photos);
  83. }
  84. /**
  85. * 关联技师用户
  86. */
  87. public function coach()
  88. {
  89. return $this->belongsTo(CoachUser::class, 'coach_id', 'id');
  90. }
  91. /**
  92. * 关联用户
  93. */
  94. public function user()
  95. {
  96. return $this->belongsTo(MemberUser::class, 'user_id', 'id');
  97. }
  98. }