123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Servers\Member;
- use App\Models\UserModels\WxUser;
- use App\Servers\Common\PassServer;
- class UserServer
- {
- /**
- * 单列对象
- * @var
- */
- private static $server = '';
- private function __construct(){
- }
- /**
- * 创建对象
- * @return UserServer
- */
- static function creatServer()
- {
- if(empty(self::$server)){
- self::$server = new UserServer();
- }
- return self::$server;
- }
- /**
- * 微信登陆
- * @param $wx_info
- * @param $invite_code
- * @return array|false
- */
- function setWxOpen($wx_info,$invite_code){
- //检查这个微信openid是否存在
- $user = WxUser::where('openid',$wx_info['openid'])->select(['id','openid','invite_code','status'])->first();
- if(!$user){//不存在就创建信息
- //查找推广者
- if ($invite_code){
- $p_id = WxUser::where('invite_code', $invite_code)->value('id');
- if(!$p_id){
- $p_id = 1;
- }
- }else{
- $p_id = 1;//找不到推广上级就为0既是上级为平台
- }
- $invite_code = $this->createCode();
- $member_info['invite_code'] = $invite_code;
- $member_info['openid'] = $wx_info['openid'];
- $member_info['p_id'] = $p_id;
- $member_info = WxUser::create($member_info);
- if (empty($member_info)) {
- return false;
- }
- $token_str = PassServer::creatServer()->getMemberToken($member_info->{'id'});
- WxUser::where('id', $member_info->{'id'})->update(['token_str' => $token_str]);
- $info = ['m_id' => $member_info->{'id'},'status' => 0, 't_code' => $invite_code, 'token_str' => $token_str];
- // $info = ['m_id' => $member_info->{'id'},'status' => 0, 't_code' => $invite_code];
- }else{
- $token_str = PassServer::creatServer()->getMemberToken($user->{'id'});
- WxUser::where('id', $user->{'id'})->update(['token_str' => $token_str]);
- $info = ['m_id' => $user->{'id'},'status' => $user->{'status'}, 't_code' => $user->{'invite_code'}, 'token_str' => $token_str];
- // $info = ['m_id' => $user->{'id'},'status' => $user->{'status'}, 't_code' => $user->{'invite_code'}];
- }
- return $info;
- }
- /**
- * 随机8位字符串
- * @return string
- */
- function randString()
- {
- $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $rand = $code[rand(0,25)]
- .strtoupper(dechex(date('m')))
- .date('d').substr(time(),-5)
- .substr(microtime(),2,5)
- .sprintf('%02d',rand(0,99));
- for(
- $a = md5( $rand, true ),
- $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
- $d = '',
- $f = 0;
- $f < 8;
- $g = ord( $a[ $f ] ),
- $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
- $f++
- );
- return $d;
- }
- /**
- * 生成推荐码
- * @return int
- */
- function createCode(){
- $is_ok = true;
- while ($is_ok) {
- $invite_code = rand(10000000, 99999999);
- $find = WxUser::where('invite_code',$invite_code)->select(['id'])->first();
- if (!$find) {
- $is_ok = false;
- }
- }
- return $invite_code;
- }
- }
|