SystemUser.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. *
  7. * 用户表
  8. */
  9. namespace app\common\model;
  10. use think\Model;
  11. use app\common\model\SystemUserLevel;
  12. class SystemUser extends Model{
  13. protected $pk = 'id';
  14. /**
  15. * 判断是否邀请用户
  16. * @param integer $user_id 当前用户ID
  17. * @param string $code 邀请码
  18. * @return int 邀请用户ID
  19. */
  20. public static function isInvite($invite_code){
  21. if(empty($invite_code)){
  22. return;
  23. }
  24. $id = de_code(strtoupper($invite_code));
  25. $is_invite = self::where(['id' => $id])->field('id')->find();
  26. return empty($is_invite) ? 0 : $is_invite['id'];
  27. }
  28. /**
  29. * 通过微信注册或更新
  30. * @param array $wechat
  31. * @param int $is_miniapp 0小程序 1公众号
  32. * @return 成功后返回用户的ID
  33. */
  34. public static function wechatReg(array $wechat,$is_miniapp = true){
  35. //查询用户类型
  36. $condition['member_miniapp_id'] = $wechat['miniapp_id'];
  37. if($is_miniapp){
  38. $condition['miniapp_uid'] = $wechat['miniapp_uid'];
  39. }else{
  40. $condition['official_uid'] = $wechat['official_uid'];
  41. }
  42. $info = self::where($condition)->find();
  43. if(empty($info)){
  44. $data = [];
  45. $data['member_miniapp_id'] = $wechat['miniapp_id'];
  46. $data['nickname'] = $wechat['nickname'];
  47. $data['face'] = $wechat['avatar'];
  48. $data['official_uid'] = $wechat['official_uid'];
  49. $data['miniapp_uid'] = $wechat['miniapp_uid'] ?: '';
  50. $data['wechat_uid'] = $wechat['wechat_uid'] ?: '';
  51. $data['create_time'] = time();
  52. $data['login_time'] = time();
  53. $data['update_time'] = time();
  54. $data['login_ip'] = request()->ip();
  55. if($is_miniapp){
  56. $data['session_key'] = $wechat['session_key'];
  57. }
  58. //在小程序端进行公众号绑定操作
  59. if($is_miniapp && !empty($data['official_uid'])){
  60. $official_info = self::where(['member_miniapp_id' => $wechat['miniapp_id'],'official_uid' => $data['official_uid']])->find();
  61. if($official_info){
  62. $official_info->miniapp_uid = $data['miniapp_uid'];
  63. $official_info->session_key = $data['session_key'];
  64. $official_info->save();
  65. return $official_info->id;
  66. }
  67. }
  68. //创建
  69. $last_id = self::insertGetId($data);
  70. if($last_id){
  71. self::where('id',$last_id)->data(['invite_code' => create_code($last_id)])->update();
  72. if(!empty($wechat['invite_code'])){ //创建邀请欢喜
  73. $is_invite = self::isInvite($wechat['invite_code']);
  74. if($is_invite){
  75. SystemUserLevel::addLevel($last_id,$is_invite);
  76. }
  77. }
  78. }
  79. return $last_id;
  80. }else{
  81. $info->nickname = $wechat['nickname'];
  82. $info->face = $wechat['avatar'];
  83. $info->login_time = time();
  84. $info->login_ip = request()->ip();
  85. if(!empty($wechat['session_key'])){
  86. $info->session_key = $wechat['session_key'];
  87. }
  88. if($is_miniapp && !empty($wechat['official_uid'])){
  89. $info->official_uid = $wechat['official_uid'];
  90. }
  91. $info->save();
  92. return $info->id;
  93. }
  94. }
  95. /**
  96. * 更新安全密码
  97. * @param array $param 更新的用户信息
  98. */
  99. public static function updateSafePasspord(int $uid,string $safepassword){
  100. $data['safe_password'] = password_hash(md5($safepassword),PASSWORD_DEFAULT);
  101. return SystemUser::where(['id' => $uid])->update($data);
  102. }
  103. /**
  104. * 修改登录密码
  105. * @access public
  106. */
  107. public function upDatePasspowrd(int $uid,string $password){
  108. $data['password'] = password_hash(md5($password),PASSWORD_DEFAULT);
  109. return SystemUser::where(['id' => $uid])->update($data);
  110. }
  111. /**
  112. * 锁定用户
  113. * @param integer $id
  114. */
  115. public static function lock(int $appid,int $id){
  116. $result = self::where(['member_miniapp_id' => $appid,'id' => $id])->find();
  117. if($result->is_delete >= 1){
  118. return FALSE;
  119. }
  120. $result->is_lock = $result->is_lock ? 0 : 1;
  121. return $result->save();
  122. }
  123. /**
  124. * 登录用户ID
  125. */
  126. public static function edit(array $data,int $id){
  127. return SystemUser::where(['id' => $id])->update($data);
  128. }
  129. /**
  130. * 作废
  131. * @param integer $id
  132. */
  133. public static function isDelete(int $appid,int $id){
  134. $result = self::where(['member_miniapp_id' => $appid,'id' => $id])->find();
  135. if($result->is_delete >= 1){
  136. return FALSE;
  137. }
  138. $result->is_lock = 1;
  139. $result->is_delete = 1;
  140. $result->phone_uid = '';
  141. $result->wechat_uid = '';
  142. $result->official_uid = '';
  143. $result->miniapp_uid = '';
  144. return $result->save();
  145. }
  146. }