User.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class User extends Model
  5. {
  6. // 表名
  7. protected $name = 'user';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'gender_text',
  17. 'idcard_status_text',
  18. 'prevtime_text',
  19. 'logintime_text',
  20. 'jointime_text',
  21. 'status_text'
  22. ];
  23. protected static function init()
  24. {
  25. self::beforeUpdate(function ($row) {
  26. $changed = $row->getChangedData();
  27. //如果有修改密码
  28. if (isset($changed['password'])) {
  29. if ($changed['password']) {
  30. $salt = \fast\Random::alnum();
  31. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  32. $row->salt = $salt;
  33. } else {
  34. unset($row->password);
  35. }
  36. }
  37. //手机去重
  38. if (isset($changed['mobile'])) {
  39. if($changed['mobile']){
  40. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  41. if ($exists) {
  42. abort(500,'手机号已经被使用');
  43. }
  44. }
  45. }
  46. });
  47. self::beforeInsert(function ($row){
  48. if (isset($row['password'])) {
  49. if ($row['password']) {
  50. $salt = \fast\Random::alnum();
  51. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  52. $row->salt = $salt;
  53. } else {
  54. unset($row->password);
  55. }
  56. }
  57. //判断用户名,手机号重复
  58. /*if($row['username']){
  59. $exists = db('user')->where('username', $row['username'])->find();
  60. if ($exists) {
  61. abort(500,'用户名已经被使用');
  62. }
  63. }else{
  64. abort(500,'用户名不能为空');
  65. }*/
  66. if($row['mobile']){
  67. $exists = db('user')->where('mobile', $row['mobile'])->find();
  68. if ($exists) {
  69. abort(500,'手机号已经被使用');
  70. }
  71. }else{
  72. abort(500,'手机号不能为空');
  73. }
  74. //判断用户名,手机号重复
  75. });
  76. self::afterInsert(function ($row){
  77. /*$data = [
  78. 'user_id' => $row['id'],
  79. ];
  80. db('wallet')->insertGetId($data);*/
  81. $username = 'u' . (10000 + $row['id']);
  82. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  83. });
  84. }
  85. public function getGenderList()
  86. {
  87. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  88. }
  89. public function getIdcardStatusList()
  90. {
  91. return ['-1' => __('Idcard_status -1'),'0' => __('Idcard_status 0'),'1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  92. }
  93. public function getStatusList()
  94. {
  95. return ['1' => __('Status 1'), '0' => __('Status 0')];
  96. }
  97. public function getGenderTextAttr($value, $data)
  98. {
  99. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  100. $list = $this->getGenderList();
  101. return isset($list[$value]) ? $list[$value] : '';
  102. }
  103. public function getIdcardStatusTextAttr($value, $data)
  104. {
  105. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  106. $list = $this->getIdcardStatusList();
  107. return isset($list[$value]) ? $list[$value] : '';
  108. }
  109. public function getPrevtimeTextAttr($value, $data)
  110. {
  111. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  112. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  113. }
  114. public function getLogintimeTextAttr($value, $data)
  115. {
  116. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  117. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  118. }
  119. public function getJointimeTextAttr($value, $data)
  120. {
  121. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  122. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  123. }
  124. public function getStatusTextAttr($value, $data)
  125. {
  126. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  127. $list = $this->getStatusList();
  128. return isset($list[$value]) ? $list[$value] : '';
  129. }
  130. protected function setPrevtimeAttr($value)
  131. {
  132. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  133. }
  134. protected function setLogintimeAttr($value)
  135. {
  136. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  137. }
  138. protected function setJointimeAttr($value)
  139. {
  140. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  141. }
  142. }