User.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. 'real_status_text',
  17. 'gender_text',
  18. 'idcard_status_text',
  19. 'prevtime_text',
  20. 'logintime_text',
  21. 'jointime_text',
  22. 'status_text',
  23. 'open_match_video_text',
  24. 'open_match_audio_text',
  25. 'open_match_typing_text',
  26. 'free_video_text',
  27. 'free_audio_text',
  28. 'free_typing_text'
  29. ];
  30. protected static function init()
  31. {
  32. self::beforeUpdate(function ($row) {
  33. $changed = $row->getChangedData();
  34. //如果有修改密码
  35. if (isset($changed['password'])) {
  36. if ($changed['password']) {
  37. $salt = \fast\Random::alnum();
  38. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  39. $row->salt = $salt;
  40. } else {
  41. unset($row->password);
  42. }
  43. }
  44. //手机去重
  45. if (isset($changed['mobile'])) {
  46. if($changed['mobile']){
  47. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  48. if ($exists) {
  49. abort(500,'手机号已经被使用');
  50. }
  51. }
  52. }
  53. });
  54. self::beforeInsert(function ($row){
  55. if (isset($row['password'])) {
  56. if ($row['password']) {
  57. $salt = \fast\Random::alnum();
  58. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  59. $row->salt = $salt;
  60. } else {
  61. unset($row->password);
  62. }
  63. }
  64. //判断用户名,手机号重复
  65. /*if($row['username']){
  66. $exists = db('user')->where('username', $row['username'])->find();
  67. if ($exists) {
  68. abort(500,'用户名已经被使用');
  69. }
  70. }else{
  71. abort(500,'用户名不能为空');
  72. }*/
  73. if($row['mobile']){
  74. $exists = db('user')->where('mobile', $row['mobile'])->find();
  75. if ($exists) {
  76. abort(500,'手机号已经被使用');
  77. }
  78. }else{
  79. abort(500,'手机号不能为空');
  80. }
  81. //判断用户名,手机号重复
  82. });
  83. self::afterInsert(function ($row){
  84. $data = [
  85. 'user_id' => $row['id'],
  86. ];
  87. db('user_wallet')->insertGetId($data);
  88. $username = 'u' . (10000 + $row['id']);
  89. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  90. });
  91. }
  92. public function getRealStatusList()
  93. {
  94. return ['-1' => __('Real_status -1'), '0' => __('Real_status 0'), '1' => __('Real_status 1'), '2' => __('Real_status 2')];
  95. }
  96. public function getGenderList()
  97. {
  98. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  99. }
  100. public function getIdcardStatusList()
  101. {
  102. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  103. }
  104. public function getStatusList()
  105. {
  106. return ['1' => __('Status 1'), '0' => __('Status 0')];
  107. }
  108. public function getOpenMatchVideoList()
  109. {
  110. return ['1' => __('Open_match_video 1'), '0' => __('Open_match_video 0')];
  111. }
  112. public function getOpenMatchAudioList()
  113. {
  114. return ['1' => __('Open_match_audio 1'), '0' => __('Open_match_audio 0')];
  115. }
  116. public function getOpenMatchTypingList()
  117. {
  118. return ['1' => __('Open_match_typing 1'), '0' => __('Open_match_typing 0')];
  119. }
  120. public function getFreeVideoList()
  121. {
  122. return ['1' => __('Free_video 1'), '0' => __('Free_video 0')];
  123. }
  124. public function getFreeAudioList()
  125. {
  126. return ['1' => __('Free_audio 1'), '0' => __('Free_audio 0')];
  127. }
  128. public function getFreeTypingList()
  129. {
  130. return ['1' => __('Free_typing 1'), '0' => __('Free_typing 0')];
  131. }
  132. public function getRealStatusTextAttr($value, $data)
  133. {
  134. $value = $value ? $value : (isset($data['real_status']) ? $data['real_status'] : '');
  135. $list = $this->getRealStatusList();
  136. return isset($list[$value]) ? $list[$value] : '';
  137. }
  138. public function getGenderTextAttr($value, $data)
  139. {
  140. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  141. $list = $this->getGenderList();
  142. return isset($list[$value]) ? $list[$value] : '';
  143. }
  144. public function getIdcardStatusTextAttr($value, $data)
  145. {
  146. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  147. $list = $this->getIdcardStatusList();
  148. return isset($list[$value]) ? $list[$value] : '';
  149. }
  150. public function getPrevtimeTextAttr($value, $data)
  151. {
  152. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  153. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  154. }
  155. public function getLogintimeTextAttr($value, $data)
  156. {
  157. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  158. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  159. }
  160. public function getJointimeTextAttr($value, $data)
  161. {
  162. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  163. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  164. }
  165. public function getStatusTextAttr($value, $data)
  166. {
  167. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  168. $list = $this->getStatusList();
  169. return isset($list[$value]) ? $list[$value] : '';
  170. }
  171. public function getOpenMatchVideoTextAttr($value, $data)
  172. {
  173. $value = $value ? $value : (isset($data['open_match_video']) ? $data['open_match_video'] : '');
  174. $list = $this->getOpenMatchVideoList();
  175. return isset($list[$value]) ? $list[$value] : '';
  176. }
  177. public function getOpenMatchAudioTextAttr($value, $data)
  178. {
  179. $value = $value ? $value : (isset($data['open_match_audio']) ? $data['open_match_audio'] : '');
  180. $list = $this->getOpenMatchAudioList();
  181. return isset($list[$value]) ? $list[$value] : '';
  182. }
  183. public function getOpenMatchTypingTextAttr($value, $data)
  184. {
  185. $value = $value ? $value : (isset($data['open_match_typing']) ? $data['open_match_typing'] : '');
  186. $list = $this->getOpenMatchTypingList();
  187. return isset($list[$value]) ? $list[$value] : '';
  188. }
  189. public function getFreeVideoTextAttr($value, $data)
  190. {
  191. $value = $value ? $value : (isset($data['free_video']) ? $data['free_video'] : '');
  192. $list = $this->getFreeVideoList();
  193. return isset($list[$value]) ? $list[$value] : '';
  194. }
  195. public function getFreeAudioTextAttr($value, $data)
  196. {
  197. $value = $value ? $value : (isset($data['free_audio']) ? $data['free_audio'] : '');
  198. $list = $this->getFreeAudioList();
  199. return isset($list[$value]) ? $list[$value] : '';
  200. }
  201. public function getFreeTypingTextAttr($value, $data)
  202. {
  203. $value = $value ? $value : (isset($data['free_typing']) ? $data['free_typing'] : '');
  204. $list = $this->getFreeTypingList();
  205. return isset($list[$value]) ? $list[$value] : '';
  206. }
  207. protected function setPrevtimeAttr($value)
  208. {
  209. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  210. }
  211. protected function setLogintimeAttr($value)
  212. {
  213. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  214. }
  215. protected function setJointimeAttr($value)
  216. {
  217. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  218. }
  219. public function gonghui()
  220. {
  221. return $this->belongsTo('Gonghui', 'gh_id', 'id', [], 'LEFT')->setEagerlyType(0);
  222. }
  223. }