User.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. * 会员模型
  7. */
  8. class User extends Model
  9. {
  10. use SoftDelete;
  11. // 开启自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. public static function init()
  18. {
  19. parent::init();
  20. }
  21. /**
  22. * 关联上级用户
  23. */
  24. public function parent_user()
  25. {
  26. return $this->belongsTo('app\common\model\User', 'parent_user_id', 'id')
  27. ->field('id,nickname,avatar,mobile');
  28. }
  29. /**
  30. * 别名关联上级用户(支持驼峰命名)
  31. */
  32. public function parentUser()
  33. {
  34. return $this->parent_user();
  35. }
  36. // /**
  37. // * 获取头像
  38. // * @param string $value
  39. // * @param array $data
  40. // * @return string
  41. // */
  42. // public function getAvatarAttr($value, $data)
  43. // {
  44. // return $value ? $value : '/assets/img/avatar.png';
  45. // }
  46. /**
  47. * 获取验证字段数组值
  48. * @param string $value
  49. * @param array $data
  50. * @return object
  51. */
  52. public function getVerificationAttr($value, $data)
  53. {
  54. $value = array_filter((array)json_decode($value, true));
  55. $value = array_merge(['email' => 0, 'mobile' => 0], $value);
  56. return (object)$value;
  57. }
  58. /**
  59. * 设置验证字段
  60. * @param mixed $value
  61. * @return string
  62. */
  63. public function setVerificationAttr($value)
  64. {
  65. $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
  66. return $value;
  67. }
  68. public function group()
  69. {
  70. return $this->belongsTo('app\\common\\model\\UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  71. }
  72. /**
  73. * 关联分销商信息
  74. */
  75. public function agent()
  76. {
  77. return $this->hasOne('app\\common\\model\\commission\\Agent', 'user_id', 'id');
  78. }
  79. }