UserServer.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Servers\Member;
  3. use App\Models\UserModels\WxUser;
  4. use App\Servers\Common\PassServer;
  5. class UserServer
  6. {
  7. /**
  8. * 单列对象
  9. * @var
  10. */
  11. private static $server = '';
  12. private function __construct(){
  13. }
  14. /**
  15. * 创建对象
  16. * @return UserServer
  17. */
  18. static function creatServer()
  19. {
  20. if(empty(self::$server)){
  21. self::$server = new UserServer();
  22. }
  23. return self::$server;
  24. }
  25. /**
  26. * 微信登陆
  27. * @param $wx_info
  28. * @param $invite_code
  29. * @return array|false
  30. */
  31. function setWxOpen($wx_info,$invite_code){
  32. //检查这个微信openid是否存在
  33. $user = WxUser::where('openid',$wx_info['openid'])->select(['id','openid','invite_code','status'])->first();
  34. if(!$user){//不存在就创建信息
  35. //查找推广者
  36. if ($invite_code){
  37. $p_id = WxUser::where('invite_code', $invite_code)->value('id');
  38. if(!$p_id){
  39. $p_id = 1;
  40. }
  41. }else{
  42. $p_id = 1;//找不到推广上级就为0既是上级为平台
  43. }
  44. $invite_code = $this->createCode();
  45. $member_info['invite_code'] = $invite_code;
  46. $member_info['openid'] = $wx_info['openid'];
  47. $member_info['p_id'] = $p_id;
  48. $member_info = WxUser::create($member_info);
  49. if (empty($member_info)) {
  50. return false;
  51. }
  52. $token_str = PassServer::creatServer()->getMemberToken($member_info->{'id'});
  53. WxUser::where('id', $member_info->{'id'})->update(['token_str' => $token_str]);
  54. $info = ['m_id' => $member_info->{'id'},'status' => 0, 't_code' => $invite_code, 'token_str' => $token_str];
  55. // $info = ['m_id' => $member_info->{'id'},'status' => 0, 't_code' => $invite_code];
  56. }else{
  57. $token_str = PassServer::creatServer()->getMemberToken($user->{'id'});
  58. WxUser::where('id', $user->{'id'})->update(['token_str' => $token_str]);
  59. $info = ['m_id' => $user->{'id'},'status' => $user->{'status'}, 't_code' => $user->{'invite_code'}, 'token_str' => $token_str];
  60. // $info = ['m_id' => $user->{'id'},'status' => $user->{'status'}, 't_code' => $user->{'invite_code'}];
  61. }
  62. return $info;
  63. }
  64. /**
  65. * 随机8位字符串
  66. * @return string
  67. */
  68. function randString()
  69. {
  70. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  71. $rand = $code[rand(0,25)]
  72. .strtoupper(dechex(date('m')))
  73. .date('d').substr(time(),-5)
  74. .substr(microtime(),2,5)
  75. .sprintf('%02d',rand(0,99));
  76. for(
  77. $a = md5( $rand, true ),
  78. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
  79. $d = '',
  80. $f = 0;
  81. $f < 8;
  82. $g = ord( $a[ $f ] ),
  83. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  84. $f++
  85. );
  86. return $d;
  87. }
  88. /**
  89. * 生成推荐码
  90. * @return int
  91. */
  92. function createCode(){
  93. $is_ok = true;
  94. while ($is_ok) {
  95. $invite_code = rand(10000000, 99999999);
  96. $find = WxUser::where('invite_code',$invite_code)->select(['id'])->first();
  97. if (!$find) {
  98. $is_ok = false;
  99. }
  100. }
  101. return $invite_code;
  102. }
  103. }