User.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. 'is_stealth_text',
  21. ];
  22. public function getOriginData()
  23. {
  24. return $this->origin;
  25. }
  26. protected static function init()
  27. {
  28. self::beforeUpdate(function ($row) {
  29. $changed = $row->getChangedData();
  30. //如果有修改密码
  31. if (isset($changed['password'])) {
  32. if ($changed['password']) {
  33. $salt = \fast\Random::alnum();
  34. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  35. $row->salt = $salt;
  36. } else {
  37. unset($row->password);
  38. }
  39. }
  40. });
  41. self::beforeUpdate(function ($row) {
  42. $changedata = $row->getChangedData();
  43. if (isset($changedata['score'])) {
  44. $origin = $row->getOriginData();
  45. ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
  46. }
  47. });
  48. }
  49. public function getGenderList()
  50. {
  51. return ['1' => __('Male'), '0' => __('Female')];
  52. }
  53. public function getStatusList()
  54. {
  55. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  56. }
  57. public function getPrevtimeTextAttr($value, $data)
  58. {
  59. $value = $value ? $value : $data['prevtime'];
  60. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  61. }
  62. public function getLogintimeTextAttr($value, $data)
  63. {
  64. $value = $value ? $value : $data['logintime'];
  65. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  66. }
  67. public function getJointimeTextAttr($value, $data)
  68. {
  69. $value = $value ? $value : $data['jointime'];
  70. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  71. }
  72. protected function setPrevtimeAttr($value)
  73. {
  74. return $value && !is_numeric($value) ? strtotime($value) : $value;
  75. }
  76. protected function setLogintimeAttr($value)
  77. {
  78. return $value && !is_numeric($value) ? strtotime($value) : $value;
  79. }
  80. protected function setJointimeAttr($value)
  81. {
  82. return $value && !is_numeric($value) ? strtotime($value) : $value;
  83. }
  84. protected function setBirthdayAttr($value)
  85. {
  86. return $value ? $value : null;
  87. }
  88. public function getIsCoolList()
  89. {
  90. return ['0' =>__('Is_cool 0'), '1' =>__('Is_cool 1')];
  91. }
  92. public function getIsCoolTextAttr($value, $data)
  93. {
  94. $value = $value ? $value : (isset($data['is_cool']) ? $data['is_cool'] : '');
  95. $list = $this->getIsCoolList();
  96. return isset($list[$value]) ? $list[$value] : '';
  97. }
  98. public function getIsManagerList()
  99. {
  100. return ['0' =>__('Is_manager 0'), '1' =>__('Is_manager 1')];
  101. }
  102. public function getIsManagerTextAttr($value, $data)
  103. {
  104. $value = $value ? $value : (isset($data['is_manager']) ? $data['is_manager'] : '');
  105. $list = $this->getIsManagerList();
  106. return isset($list[$value]) ? $list[$value] : '';
  107. }
  108. public function getIsStealthList()
  109. {
  110. return ['0' =>__('Is_stealth 0'), '1' =>__('Is_stealth 1')];
  111. }
  112. public function getIsStealthTextAttr($value, $data)
  113. {
  114. $value = $value ? $value : (isset($data['is_stealth']) ? $data['is_stealth'] : '');
  115. $list = $this->getIsStealthList();
  116. return isset($list[$value]) ? $list[$value] : '';
  117. }
  118. public function noble()
  119. {
  120. return $this->belongsTo('app\admin\model\noble\Level', 'noble', 'id', [], 'LEFT')->setEagerlyType(0);
  121. }
  122. public function preuser()
  123. {
  124. return $this->belongsTo('app\admin\model\User', 'pre_userid', 'id', [], 'LEFT')->setEagerlyType(0);
  125. }
  126. public function auth()
  127. {
  128. return $this->belongsTo('app\admin\model\user\Auth', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  129. }
  130. public function age()
  131. {
  132. return $this->belongsTo('app\admin\model\Age', 'age_id', 'id', [], 'LEFT')->setEagerlyType(0);
  133. }
  134. public function constellation()
  135. {
  136. return $this->belongsTo('app\admin\model\Constellation', 'constellation_id', 'id', [], 'LEFT')->setEagerlyType(0);
  137. }
  138. public function getUinqueId($length = 8, $ids = [])
  139. {
  140. $newid = Random::build("nozero", $length);
  141. if (in_array($newid, $ids)) {
  142. $newid = $this->getUinqueId($length, $ids);
  143. }
  144. return $newid;
  145. }
  146. /**
  147. * 生成不重复的随机数字字母组合
  148. */
  149. function getUinqueNo($length = 8, $nos = [])
  150. {
  151. $newid = Random::build("alnum", $length);
  152. if (in_array($newid, $nos)) {
  153. $newid = $this->getUinqueNo($length, $nos);
  154. }
  155. return $newid;
  156. }
  157. }