User.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. 'jinyantime_text',
  26. 'jinyantype_text'
  27. ];
  28. protected static function init()
  29. {
  30. self::beforeUpdate(function ($row) {
  31. $changed = $row->getChangedData();
  32. //如果有修改密码
  33. if (isset($changed['password'])) {
  34. if ($changed['password']) {
  35. $salt = \fast\Random::alnum();
  36. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  37. $row->salt = $salt;
  38. } else {
  39. unset($row->password);
  40. }
  41. }
  42. //手机去重
  43. if (isset($changed['mobile'])) {
  44. if($changed['mobile']){
  45. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  46. if ($exists) {
  47. abort(500,'手机号已经被使用');
  48. }
  49. }
  50. }
  51. //手机去重
  52. if (isset($changed['simplemobile'])) {
  53. if($changed['simplemobile']){
  54. $exists = db('user')->where('simplemobile', $changed['simplemobile'])->where('id', '<>', $row->id)->find();
  55. if ($exists) {
  56. abort(500,'手机号已经被使用');
  57. }
  58. }
  59. }
  60. });
  61. self::beforeInsert(function ($row){
  62. if (isset($row['password'])) {
  63. if ($row['password']) {
  64. $salt = \fast\Random::alnum();
  65. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  66. $row->salt = $salt;
  67. } else {
  68. unset($row->password);
  69. }
  70. }
  71. //判断用户名,手机号重复
  72. /*if($row['username']){
  73. $exists = db('user')->where('username', $row['username'])->find();
  74. if ($exists) {
  75. abort(500,'用户名已经被使用');
  76. }
  77. }else{
  78. abort(500,'用户名不能为空');
  79. }*/
  80. if($row['mobile']){
  81. $exists = db('user')->where('mobile', $row['mobile'])->find();
  82. if ($exists) {
  83. abort(500,'手机号已经被使用');
  84. }
  85. }else{
  86. abort(500,'手机号不能为空');
  87. }
  88. //判断用户名,手机号重复
  89. });
  90. self::afterInsert(function ($row){
  91. $username = 'u' . (10000 + $row['id']);
  92. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  93. $data = [
  94. 'user_id' => $row['id'],
  95. ];
  96. //注册钱包
  97. db('user_wallet')->insertGetId($data);
  98. //注册用户活跃
  99. db('user_active')->insertGetId($data);
  100. //注册用户权限
  101. db('user_power')->insertGetId($data);
  102. });
  103. }
  104. public function getGenderList()
  105. {
  106. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  107. }
  108. public function getIdcardStatusList()
  109. {
  110. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2'), '3' => __('Idcard_status 3')];
  111. }
  112. public function getStatusList()
  113. {
  114. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  115. }
  116. public function getHideIsFinishinfoList()
  117. {
  118. return ['1' => __('Hide_is_finishinfo 1'), '0' => __('Hide_is_finishinfo 0')];
  119. }
  120. public function getIsActiveList()
  121. {
  122. return ['1' => __('Is_active 1'), '0' => __('Is_active 0')];
  123. }
  124. public function getIsTuijianList()
  125. {
  126. return ['1' => __('Is_tuijian 1'), '0' => __('Is_tuijian 0')];
  127. }
  128. public function getJinyantypeList()
  129. {
  130. return ['1' => __('Jinyantype 1'), '2' => __('Jinyantype 2'), '3' => __('Jinyantype 3')];
  131. }
  132. public function getGenderTextAttr($value, $data)
  133. {
  134. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  135. $list = $this->getGenderList();
  136. return isset($list[$value]) ? $list[$value] : '';
  137. }
  138. public function getIdcardStatusTextAttr($value, $data)
  139. {
  140. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  141. $list = $this->getIdcardStatusList();
  142. return isset($list[$value]) ? $list[$value] : '';
  143. }
  144. public function getPrevtimeTextAttr($value, $data)
  145. {
  146. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  147. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  148. }
  149. public function getLogintimeTextAttr($value, $data)
  150. {
  151. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  152. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  153. }
  154. public function getJointimeTextAttr($value, $data)
  155. {
  156. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  157. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  158. }
  159. public function getStatusTextAttr($value, $data)
  160. {
  161. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  162. $list = $this->getStatusList();
  163. return isset($list[$value]) ? $list[$value] : '';
  164. }
  165. public function getHideIsFinishinfoTextAttr($value, $data)
  166. {
  167. $value = $value ? $value : (isset($data['hide_is_finishinfo']) ? $data['hide_is_finishinfo'] : '');
  168. $list = $this->getHideIsFinishinfoList();
  169. return isset($list[$value]) ? $list[$value] : '';
  170. }
  171. public function getIsActiveTextAttr($value, $data)
  172. {
  173. $value = $value ? $value : (isset($data['is_active']) ? $data['is_active'] : '');
  174. $list = $this->getIsActiveList();
  175. return isset($list[$value]) ? $list[$value] : '';
  176. }
  177. public function getIsTuijianTextAttr($value, $data)
  178. {
  179. $value = $value ? $value : (isset($data['is_tuijian']) ? $data['is_tuijian'] : '');
  180. $list = $this->getIsTuijianList();
  181. return isset($list[$value]) ? $list[$value] : '';
  182. }
  183. public function getJinyantimeTextAttr($value, $data)
  184. {
  185. $value = $value ? $value : (isset($data['jinyantime']) ? $data['jinyantime'] : '');
  186. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  187. }
  188. public function getJinyantypeTextAttr($value, $data)
  189. {
  190. $value = $value ? $value : (isset($data['jinyantype']) ? $data['jinyantype'] : '');
  191. $list = $this->getJinyantypeList();
  192. return isset($list[$value]) ? $list[$value] : '';
  193. }
  194. protected function setPrevtimeAttr($value)
  195. {
  196. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  197. }
  198. protected function setLogintimeAttr($value)
  199. {
  200. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  201. }
  202. protected function setJointimeAttr($value)
  203. {
  204. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  205. }
  206. protected function setJinyantimeAttr($value)
  207. {
  208. return $value === '' ? 0 : ($value && !is_numeric($value) ? strtotime($value) : $value);
  209. }
  210. public function userwallet()
  211. {
  212. return $this->belongsTo('Userwallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  213. }
  214. }