User.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\model\ScoreLog;
  4. use fast\Random;
  5. use think\Model;
  6. class User extends Model
  7. {
  8. // 表名
  9. protected $name = 'user';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. 'logintime_text',
  18. 'is_cool_text',
  19. 'is_manager_text',
  20. ];
  21. public function getOriginData()
  22. {
  23. return $this->origin;
  24. }
  25. protected static function init()
  26. {
  27. self::beforeUpdate(function ($row) {
  28. $changed = $row->getChangedData();
  29. //如果有修改密码
  30. if (isset($changed['password'])) {
  31. if ($changed['password']) {
  32. $salt = \fast\Random::alnum();
  33. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  34. $row->salt = $salt;
  35. } else {
  36. unset($row->password);
  37. }
  38. }
  39. });
  40. self::beforeUpdate(function ($row) {
  41. $changedata = $row->getChangedData();
  42. if (isset($changedata['score'])) {
  43. $origin = $row->getOriginData();
  44. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  45. }
  46. });
  47. }
  48. public function getGenderList()
  49. {
  50. return ['1' => __('Male'), '0' => __('Female')];
  51. }
  52. public function getStatusList()
  53. {
  54. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  55. }
  56. public function getPrevtimeTextAttr($value, $data)
  57. {
  58. $value = $value ? $value : $data['prevtime'];
  59. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  60. }
  61. public function getLogintimeTextAttr($value, $data)
  62. {
  63. $value = $value ? $value : $data['logintime'];
  64. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  65. }
  66. public function getJointimeTextAttr($value, $data)
  67. {
  68. $value = $value ? $value : $data['jointime'];
  69. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  70. }
  71. protected function setPrevtimeAttr($value)
  72. {
  73. return $value && !is_numeric($value) ? strtotime($value) : $value;
  74. }
  75. protected function setLogintimeAttr($value)
  76. {
  77. return $value && !is_numeric($value) ? strtotime($value) : $value;
  78. }
  79. protected function setJointimeAttr($value)
  80. {
  81. return $value && !is_numeric($value) ? strtotime($value) : $value;
  82. }
  83. protected function setBirthdayAttr($value)
  84. {
  85. return $value ? $value : null;
  86. }
  87. public function getIsCoolList()
  88. {
  89. return ['0' =>__('Is_cool 0'), '1' =>__('Is_cool 1')];
  90. }
  91. public function getIsCoolTextAttr($value, $data)
  92. {
  93. $value = $value ? $value : (isset($data['is_cool']) ? $data['is_cool'] : '');
  94. $list = $this->getIsCoolList();
  95. return isset($list[$value]) ? $list[$value] : '';
  96. }
  97. public function getIsManagerList()
  98. {
  99. return ['0' =>__('Is_manager 0'), '1' =>__('Is_manager 1')];
  100. }
  101. public function getIsManagerTextAttr($value, $data)
  102. {
  103. $value = $value ? $value : (isset($data['is_manager']) ? $data['is_manager'] : '');
  104. $list = $this->getIsManagerList();
  105. return isset($list[$value]) ? $list[$value] : '';
  106. }
  107. public function noble()
  108. {
  109. return $this->belongsTo('app\admin\model\noble\Level', 'noble', 'id', [], 'LEFT')->setEagerlyType(0);
  110. }
  111. public function preuser()
  112. {
  113. return $this->belongsTo('app\admin\model\User', 'pre_userid', 'id', [], 'LEFT')->setEagerlyType(0);
  114. }
  115. public function auth()
  116. {
  117. return $this->belongsTo('app\admin\model\user\Auth', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  118. }
  119. public function age()
  120. {
  121. return $this->belongsTo('app\admin\model\Age', 'age_id', 'id', [], 'LEFT')->setEagerlyType(0);
  122. }
  123. public function constellation()
  124. {
  125. return $this->belongsTo('app\admin\model\Constellation', 'constellation_id', 'id', [], 'LEFT')->setEagerlyType(0);
  126. }
  127. public function getUinqueId($length = 8, $ids = [])
  128. {
  129. $newid = Random::build("nozero", $length);
  130. if (in_array($newid, $ids)) {
  131. $newid = $this->getUinqueId($length, $ids);
  132. }
  133. return $newid;
  134. }
  135. /**
  136. * 生成不重复的随机数字字母组合
  137. */
  138. function getUinqueNo($length = 8, $nos = [])
  139. {
  140. $newid = Random::build("alnum", $length);
  141. if (in_array($newid, $nos)) {
  142. $newid = $this->getUinqueNo($length, $nos);
  143. }
  144. return $newid;
  145. }
  146. }