UserServer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }else{
  38. $p_id = 0;//找不到推广上级就为0既是上级为平台
  39. }
  40. $invite_code = $this->createCode();
  41. $member_info['invite_code'] = $invite_code;
  42. $member_info['openid'] = $wx_info['openid'];
  43. $member_info['p_id'] = $p_id;
  44. $member_info = WxUser::create($member_info);
  45. if (empty($member_info)) {
  46. return false;
  47. }
  48. $info = ['m_id' => $member_info->{'id'},'status' => 0, 't_code' => $invite_code];
  49. }else{
  50. $info = ['m_id' => $user->{'id'},'status' => $user->{'status'}, 't_code' => $user->{'invite_code'}];
  51. }
  52. return $info;
  53. }
  54. /**
  55. * 随机8位字符串
  56. * @return string
  57. */
  58. function randString()
  59. {
  60. $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  61. $rand = $code[rand(0,25)]
  62. .strtoupper(dechex(date('m')))
  63. .date('d').substr(time(),-5)
  64. .substr(microtime(),2,5)
  65. .sprintf('%02d',rand(0,99));
  66. for(
  67. $a = md5( $rand, true ),
  68. $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
  69. $d = '',
  70. $f = 0;
  71. $f < 8;
  72. $g = ord( $a[ $f ] ),
  73. $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  74. $f++
  75. );
  76. return $d;
  77. }
  78. /**
  79. * 生成推荐码
  80. * @return int
  81. */
  82. function createCode(){
  83. $is_ok = true;
  84. while ($is_ok) {
  85. $invite_code = rand(10000000, 99999999);
  86. $find = WxUser::where('invite_code',$invite_code)->select(['id'])->first();
  87. if (!$find) {
  88. $is_ok = false;
  89. }
  90. }
  91. return $invite_code;
  92. }
  93. }