User.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\admin\model\shopro\user;
  3. use app\admin\model\shopro\Common;
  4. use app\admin\model\shopro\Coupon as CouponModel;
  5. use app\admin\model\shopro\ThirdOauth;
  6. use addons\shopro\library\notify\traits\Notifiable;
  7. use fast\Random;
  8. class User extends Common
  9. {
  10. use Notifiable;
  11. protected $name = 'user';
  12. protected $type = [
  13. 'logintime' => 'timestamp',
  14. 'jointime' => 'timestamp',
  15. 'prevtime' => 'timestamp',
  16. ];
  17. protected $hidden = ['password', 'salt'];
  18. protected $append = [
  19. 'status_text',
  20. 'gender_text'
  21. ];
  22. public function getGenderList()
  23. {
  24. return ['1' => __('Male'), '0' => __('Female')];
  25. }
  26. /**
  27. * 获取性别文字
  28. * @param string $value
  29. * @param array $data
  30. * @return object
  31. */
  32. public function getGenderTextAttr($value, $data)
  33. {
  34. $value = $value ?: ($data['gender'] ?? 0);
  35. $list = $this->getGenderList();
  36. return isset($list[$value]) ? $list[$value] : '';
  37. }
  38. public function setMobileAttr($value, $data)
  39. {
  40. if ($value !== "") {
  41. return $value;
  42. }
  43. return null;
  44. }
  45. public function setUsernameAttr($value, $data)
  46. {
  47. if ($value !== "") {
  48. return $value;
  49. }
  50. return null;
  51. }
  52. public function getAvatarAttr($value, $data)
  53. {
  54. if (empty($value)) {
  55. $config = sheep_config('shop.user');
  56. $value = $config['avatar'];
  57. }
  58. return $value;
  59. }
  60. public function setEmailAttr($value, $data)
  61. {
  62. if ($value !== "") {
  63. return $value;
  64. }
  65. return null;
  66. }
  67. public function setPasswordAttr($value, $data)
  68. {
  69. $salt = Random::alnum();
  70. $this->salt = $salt;
  71. return \app\common\library\Auth::instance()->getEncryptPassword($value, $salt);
  72. }
  73. public function getNicknameHideAttr($value, $data)
  74. {
  75. $value = $value ?: ($data['nickname'] ?? '');
  76. return $value ? string_hide($value, 2) : $value;
  77. }
  78. public function getMobileAttr($value, $data)
  79. {
  80. $value = $value ?: ($data['mobile'] ?? '');
  81. return $value ? (operate_filter(false) ? $value : account_hide($value, 3, 3)) : $value;
  82. }
  83. /**
  84. * 获取验证字段数组值
  85. * @param string $value
  86. * @param array $data
  87. * @return object
  88. */
  89. public function getVerificationAttr($value, $data)
  90. {
  91. $value = array_filter((array)json_decode($value, true));
  92. $value = array_merge(['username' => 0, 'password' => 0, 'mobile' => 0], $value);
  93. return (object)$value;
  94. }
  95. public function parentUser()
  96. {
  97. return $this->hasOne(User::class, 'id', 'parent_user_id')->field(['id', 'avatar', 'nickname', 'gender']);
  98. }
  99. public function thirdOauth()
  100. {
  101. return $this->hasMany(ThirdOauth::class, 'user_id', 'id');
  102. }
  103. // -- commission code start --
  104. public function agent()
  105. {
  106. return $this->hasOne(\app\admin\model\shopro\commission\Agent::class, 'user_id', 'id');
  107. }
  108. // -- commission code end --
  109. }