User.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. 'hide_is_finishinfo_text',
  23. 'is_active_text',
  24. 'is_tuijian_text'
  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. if (isset($changed['mobile'])) {
  42. if($changed['mobile']){
  43. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  44. if ($exists) {
  45. abort(500,'手机号已经被使用');
  46. }
  47. }
  48. }
  49. });
  50. self::beforeInsert(function ($row){
  51. if (isset($row['password'])) {
  52. if ($row['password']) {
  53. $salt = \fast\Random::alnum();
  54. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  55. $row->salt = $salt;
  56. } else {
  57. unset($row->password);
  58. }
  59. }
  60. //判断用户名,手机号重复
  61. /*if($row['username']){
  62. $exists = db('user')->where('username', $row['username'])->find();
  63. if ($exists) {
  64. abort(500,'用户名已经被使用');
  65. }
  66. }else{
  67. abort(500,'用户名不能为空');
  68. }*/
  69. if($row['mobile']){
  70. $exists = db('user')->where('mobile', $row['mobile'])->find();
  71. if ($exists) {
  72. abort(500,'手机号已经被使用');
  73. }
  74. }else{
  75. abort(500,'手机号不能为空');
  76. }
  77. //判断用户名,手机号重复
  78. });
  79. self::afterInsert(function ($row){
  80. $data = [
  81. 'user_id' => $row['id'],
  82. ];
  83. db('user_wallet')->insertGetId($data);
  84. $username = 'u' . (10000 + $row['id']);
  85. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  86. });
  87. }
  88. public function getGenderList()
  89. {
  90. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  91. }
  92. public function getIdcardStatusList()
  93. {
  94. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  95. }
  96. public function getStatusList()
  97. {
  98. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  99. }
  100. public function getHideIsFinishinfoList()
  101. {
  102. return ['1' => __('Hide_is_finishinfo 1'), '0' => __('Hide_is_finishinfo 0')];
  103. }
  104. public function getIsActiveList()
  105. {
  106. return ['1' => __('Is_active 1'), '0' => __('Is_active 0')];
  107. }
  108. public function getIsTuijianList()
  109. {
  110. return ['1' => __('Is_tuijian 1'), '0' => __('Is_tuijian 0')];
  111. }
  112. public function getGenderTextAttr($value, $data)
  113. {
  114. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  115. $list = $this->getGenderList();
  116. return isset($list[$value]) ? $list[$value] : '';
  117. }
  118. public function getIdcardStatusTextAttr($value, $data)
  119. {
  120. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  121. $list = $this->getIdcardStatusList();
  122. return isset($list[$value]) ? $list[$value] : '';
  123. }
  124. public function getPrevtimeTextAttr($value, $data)
  125. {
  126. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  127. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  128. }
  129. public function getLogintimeTextAttr($value, $data)
  130. {
  131. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  132. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  133. }
  134. public function getJointimeTextAttr($value, $data)
  135. {
  136. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  137. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  138. }
  139. public function getStatusTextAttr($value, $data)
  140. {
  141. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  142. $list = $this->getStatusList();
  143. return isset($list[$value]) ? $list[$value] : '';
  144. }
  145. public function getHideIsFinishinfoTextAttr($value, $data)
  146. {
  147. $value = $value ? $value : (isset($data['hide_is_finishinfo']) ? $data['hide_is_finishinfo'] : '');
  148. $list = $this->getHideIsFinishinfoList();
  149. return isset($list[$value]) ? $list[$value] : '';
  150. }
  151. public function getIsActiveTextAttr($value, $data)
  152. {
  153. $value = $value ? $value : (isset($data['is_active']) ? $data['is_active'] : '');
  154. $list = $this->getIsActiveList();
  155. return isset($list[$value]) ? $list[$value] : '';
  156. }
  157. public function getIsTuijianTextAttr($value, $data)
  158. {
  159. $value = $value ? $value : (isset($data['is_tuijian']) ? $data['is_tuijian'] : '');
  160. $list = $this->getIsTuijianList();
  161. return isset($list[$value]) ? $list[$value] : '';
  162. }
  163. protected function setPrevtimeAttr($value)
  164. {
  165. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  166. }
  167. protected function setLogintimeAttr($value)
  168. {
  169. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  170. }
  171. protected function setJointimeAttr($value)
  172. {
  173. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  174. }
  175. }