User.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 = false;
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'yaoqingtime_text',
  17. 'gender_text',
  18. 'idcard_status_text',
  19. 'real_status_text',
  20. 'logintime_text',
  21. 'jointime_text',
  22. 'status_text',
  23. 'hide_is_finishinfo_text',
  24. 'is_active_text',
  25. 'open_match_video_text',
  26. 'open_match_audio_text',
  27. 'jinyantime_text',
  28. 'jinyantype_text',
  29. 'is_shouchong_text'
  30. ];
  31. protected static function init()
  32. {
  33. self::beforeUpdate(function ($row) {
  34. $changed = $row->getChangedData();
  35. //如果有修改密码
  36. if (isset($changed['real_status'])) {
  37. if ($changed['real_status']) {
  38. $task_rs = \app\common\model\TaskLog::tofinish($row->id,6);
  39. } else {
  40. }
  41. }
  42. });
  43. /*self::beforeUpdate(function ($row) {
  44. $changed = $row->getChangedData();
  45. //如果有修改密码
  46. if (isset($changed['password'])) {
  47. if ($changed['password']) {
  48. $salt = \fast\Random::alnum();
  49. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
  50. $row->salt = $salt;
  51. } else {
  52. unset($row->password);
  53. }
  54. }
  55. //手机去重
  56. if (isset($changed['mobile'])) {
  57. if($changed['mobile']){
  58. $exists = db('user')->where('mobile', $changed['mobile'])->where('id', '<>', $row->id)->find();
  59. if ($exists) {
  60. abort(500,'区号手机号已经被使用');
  61. }
  62. }else {
  63. unset($row->mobile);
  64. }
  65. }
  66. //手机去重
  67. if (isset($changed['simplemobile'])) {
  68. if($changed['simplemobile']){
  69. }else {
  70. unset($row->simplemobile);
  71. }
  72. }
  73. });
  74. self::beforeInsert(function ($row){
  75. if (isset($row['password'])) {
  76. if ($row['password']) {
  77. $salt = \fast\Random::alnum();
  78. $row->password = \app\common\library\Auth::instance()->getEncryptPassword($row['password'], $salt);
  79. $row->salt = $salt;
  80. } else {
  81. unset($row->password);
  82. }
  83. }
  84. if($row['mobile']){
  85. $exists = db('user')->where('mobile', $row['mobile'])->find();
  86. if ($exists) {
  87. abort(500,'手机号已经被使用');
  88. }
  89. }else{
  90. abort(500,'手机号不能为空');
  91. }
  92. //判断用户名,手机号重复
  93. });
  94. self::afterInsert(function ($row){
  95. $username = 'u' . (10000 + $row['id']);
  96. db('user')->where('id',$row['id'])->update(['username'=>$username]);
  97. $data = [
  98. 'user_id' => $row['id'],
  99. ];
  100. //注册钱包
  101. db('user_wallet')->insertGetId($data);
  102. //注册用户活跃
  103. db('user_active')->insertGetId($data);
  104. //注册用户权限
  105. db('user_power')->insertGetId($data);
  106. });*/
  107. }
  108. public function getGenderList()
  109. {
  110. return ['-1' => __('Gender -1'),'1' => __('Gender 1'), '0' => __('Gender 0')];
  111. }
  112. public function getIdcardStatusList()
  113. {
  114. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  115. }
  116. public function getRealStatusList()
  117. {
  118. return ['-1' => __('Real_status -1'), '0' => __('Real_status 0'), '1' => __('Real_status 1'), '2' => __('Real_status 2')];
  119. }
  120. public function getStatusList()
  121. {
  122. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  123. }
  124. public function getHideIsFinishinfoList()
  125. {
  126. return ['1' => __('Hide_is_finishinfo 1'), '0' => __('Hide_is_finishinfo 0')];
  127. }
  128. public function getIsActiveList()
  129. {
  130. return ['1' => __('Is_active 1'), '0' => __('Is_active 0')];
  131. }
  132. public function getOpenMatchVideoList()
  133. {
  134. return ['1' => __('Open_match_video 1'), '0' => __('Open_match_video 0')];
  135. }
  136. public function getOpenMatchAudioList()
  137. {
  138. return ['1' => __('Open_match_audio 1'), '0' => __('Open_match_audio 0')];
  139. }
  140. public function getJinyantypeList()
  141. {
  142. return ['1' => __('Jinyantype 1'), '2' => __('Jinyantype 2'), '3' => __('Jinyantype 3')];
  143. }
  144. public function getIsShouchongList()
  145. {
  146. return ['0' => __('Is_shouchong 0'), '1' => __('Is_shouchong 1')];
  147. }
  148. public function getYaoqingtimeTextAttr($value, $data)
  149. {
  150. $value = $value ? $value : (isset($data['yaoqingtime']) ? $data['yaoqingtime'] : '');
  151. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  152. }
  153. public function getGenderTextAttr($value, $data)
  154. {
  155. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  156. $list = $this->getGenderList();
  157. return isset($list[$value]) ? $list[$value] : '';
  158. }
  159. public function getIdcardStatusTextAttr($value, $data)
  160. {
  161. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  162. $list = $this->getIdcardStatusList();
  163. return isset($list[$value]) ? $list[$value] : '';
  164. }
  165. public function getRealStatusTextAttr($value, $data)
  166. {
  167. $value = $value ? $value : (isset($data['real_status']) ? $data['real_status'] : '');
  168. $list = $this->getRealStatusList();
  169. return isset($list[$value]) ? $list[$value] : '';
  170. }
  171. public function getLogintimeTextAttr($value, $data)
  172. {
  173. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  174. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  175. }
  176. public function getJointimeTextAttr($value, $data)
  177. {
  178. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  179. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  180. }
  181. public function getStatusTextAttr($value, $data)
  182. {
  183. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  184. $list = $this->getStatusList();
  185. return isset($list[$value]) ? $list[$value] : '';
  186. }
  187. public function getHideIsFinishinfoTextAttr($value, $data)
  188. {
  189. $value = $value ? $value : (isset($data['hide_is_finishinfo']) ? $data['hide_is_finishinfo'] : '');
  190. $list = $this->getHideIsFinishinfoList();
  191. return isset($list[$value]) ? $list[$value] : '';
  192. }
  193. public function getIsActiveTextAttr($value, $data)
  194. {
  195. $value = $value ? $value : (isset($data['is_active']) ? $data['is_active'] : '');
  196. $list = $this->getIsActiveList();
  197. return isset($list[$value]) ? $list[$value] : '';
  198. }
  199. public function getOpenMatchVideoTextAttr($value, $data)
  200. {
  201. $value = $value ? $value : (isset($data['open_match_video']) ? $data['open_match_video'] : '');
  202. $list = $this->getOpenMatchVideoList();
  203. return isset($list[$value]) ? $list[$value] : '';
  204. }
  205. public function getOpenMatchAudioTextAttr($value, $data)
  206. {
  207. $value = $value ? $value : (isset($data['open_match_audio']) ? $data['open_match_audio'] : '');
  208. $list = $this->getOpenMatchAudioList();
  209. return isset($list[$value]) ? $list[$value] : '';
  210. }
  211. public function getJinyantimeTextAttr($value, $data)
  212. {
  213. $value = $value ? $value : (isset($data['jinyantime']) ? $data['jinyantime'] : '');
  214. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  215. }
  216. public function getJinyantypeTextAttr($value, $data)
  217. {
  218. $value = $value ? $value : (isset($data['jinyantype']) ? $data['jinyantype'] : '');
  219. $list = $this->getJinyantypeList();
  220. return isset($list[$value]) ? $list[$value] : '';
  221. }
  222. public function getIsShouchongTextAttr($value, $data)
  223. {
  224. $value = $value ? $value : (isset($data['is_shouchong']) ? $data['is_shouchong'] : '');
  225. $list = $this->getIsShouchongList();
  226. return isset($list[$value]) ? $list[$value] : '';
  227. }
  228. protected function setYaoqingtimeAttr($value)
  229. {
  230. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  231. }
  232. protected function setLogintimeAttr($value)
  233. {
  234. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  235. }
  236. protected function setJointimeAttr($value)
  237. {
  238. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  239. }
  240. protected function setJinyantimeAttr($value)
  241. {
  242. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  243. }
  244. public function usergroup()
  245. {
  246. return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
  247. }
  248. public function userwallet()
  249. {
  250. return $this->belongsTo('Userwallet', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  251. }
  252. }