HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot//www.elwrky.com/addons/synclogin/library/Service.php
<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 第三方服务类
// +----------------------------------------------------------------------
namespace addons\synclogin\library;

use addons\synclogin\model\SyncLogin as SyncLoginModel;
use app\member\model\Member as MemberModel;

class Service
{
    /**
     * 第三方登录
     * @param string $type 平台
     * @param array  $params   参数
     * @param array  $extend   会员扩展信息
     * @param int    $keeptime 有效时长
     * @return boolean
     *
     */
    public static function connect($type, $params = [], $extend = [], $keeptime = 0)
    {
        $token    = $params['token'];
        $nickname = $params['nickname'] ?? '';
        $avatar   = $params['avatar'] ?? '';
        $data     = [
            'openid'       => $token['openid'],
            'access_token' => $token['access_token'],
            'openname'     => $nickname,
            'type'         => $type,
            'login_time'   => time(),
        ];
        $auth = \app\member\service\User::instance();
        //查询是否有第三方登录记录
        $third = SyncLoginModel::get(['type' => $type, 'openid' => $token['openid']], 'member');
        if ($third) {
            if (!$third->member) {
                //删除不存在会员记录
                $third->delete();
            } else {
                $third->allowField(true)->save($data);
                // 写入登录Cookies和Token
                return $auth->direct($third->uid);
            }
        }

        //存在unionid就需要判断是否需要生成新记录 QQ和微信、淘宝可以获取unionid
        if (isset($params['unionid']) && !empty($params['unionid'])) {
            $third = SyncLoginModel::get(['platform' => $platform, 'unionid' => $params['unionid']], 'member');
            if ($third) {
                if (!$third->member) {
                    $third->delete();
                } else {
                    // 保存第三方信息
                    $data['uid'] = $third->uid;
                    $third       = SyncLoginModel::create($data, true);
                    // 写入登录Cookies和Token
                    return $auth->direct($third->uid);
                }
            }
        }
        if ($auth->id) {
            if (!$third) {
                $data['uid'] = $auth->id;
                SyncLoginModel::create($data, true);
            }
            $user = $auth->getUser();
        } else {
            //先随机一个用户名,随后再变更为u+数字id
            $username = genRandomString(10);
            $password = genRandomString(6);
            $domain   = request()->host();
            $uid      = $auth->userRegister($username, $password, $username . '@' . $domain, '', $extend);
            if ($uid > 0) {
                $user   = $auth->getUser();
                $fields = ['username' => 'u' . $uid, 'email' => 'u' . $uid . '@' . $domain];
                if ($nickname) {
                    $fields['nickname'] = $nickname;
                }
                if ($avatar) {
                    $fields['avatar'] = htmlspecialchars(strip_tags($avatar));
                }
                // 更新会员资料
                $user = MemberModel::get($user->id);
                $user->save($fields);
                // 记录数据到sync_login表中
                $data['uid'] = $user->id;
                SyncLoginModel::create($data, true);
            } else {
                $auth->logout();
                return false;
            }
        }
        // 写入登录Cookies和Token
        return $auth->direct($user->id);
    }

    /**
     * 是否绑定第三方
     */
    public static function isBindThird($type, $openid, $unionid = '')
    {
        $conddtions = [
            'type'   => $type,
            'openid' => $openid,
        ];
        $third = SyncLoginModel::get($conddtions, 'member');
        //第三方存在
        if ($third) {
            //用户失效
            if (!$third->member) {
                $third->delete();
                return false;
            }
            return true;
        }
        if ($unionid) {
            $third = SyncLoginModel::get(['type' => $type, 'unionid' => $unionid], 'member');
            if ($third) {
                //
                if (!$third->member) {
                    $third->delete();
                    return false;
                }
                return true;
            }
        }
        return false;
    }
}