UserServer.php 2.7 KB

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