User.php 5.7 KB

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