User.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class User extends Model
  6. {
  7. // 表名
  8. protected $table = 'user';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'integer';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'prevtime_text',
  18. 'logintime_text',
  19. 'jointime_text',
  20. 'status_text',
  21. 'last_paytime_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['username'])) {
  39. if($changed['username']){
  40. $exists = db('user')->where('username', $changed['username'])->where('id', '<>', $row->id)->find();
  41. if ($exists) {
  42. abort(500,'用户名已经存在');
  43. }
  44. }
  45. }
  46. //手机号重复
  47. if (isset($changed['mobile'])) {
  48. if($changed['mobile']){
  49. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  50. if ($exists) {
  51. abort(500,'手机号已经被使用');
  52. }
  53. }
  54. }
  55. //邀请码重复
  56. if (isset($changed['introcode'])) {
  57. if($changed['introcode']){
  58. $exists = db('user')->where('introcode', $changed['introcode'])->where('id', '<>', $row->id)->find();
  59. if ($exists) {
  60. abort(500,'邀请码已经存在');
  61. }
  62. }
  63. }
  64. });
  65. }
  66. public function getStatusList()
  67. {
  68. return ['0' => __('Status 0'), '1' => __('Status 1')];
  69. }
  70. public function getPrevtimeTextAttr($value, $data)
  71. {
  72. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  73. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  74. }
  75. public function getLogintimeTextAttr($value, $data)
  76. {
  77. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  78. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  79. }
  80. public function getJointimeTextAttr($value, $data)
  81. {
  82. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  83. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  84. }
  85. public function getStatusTextAttr($value, $data)
  86. {
  87. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  88. $list = $this->getStatusList();
  89. return isset($list[$value]) ? $list[$value] : '';
  90. }
  91. public function getLastPaytimeTextAttr($value, $data)
  92. {
  93. $value = $value ? $value : (isset($data['last_paytime']) ? $data['last_paytime'] : '');
  94. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  95. }
  96. protected function setPrevtimeAttr($value)
  97. {
  98. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  99. }
  100. protected function setLogintimeAttr($value)
  101. {
  102. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  103. }
  104. protected function setJointimeAttr($value)
  105. {
  106. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  107. }
  108. protected function setLastPaytimeAttr($value)
  109. {
  110. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  111. }
  112. public function userwallet()
  113. {
  114. return $this->belongsTo('Userwallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  115. }
  116. }