public function post($f3) { // 权限检查 $this->requirePrivilege('manage_plugin_plugin_configure'); global $smarty; // 参数验证 $validator = new Validator($f3->get('POST')); // shop $shop_dev360auth_app_id = $validator->required()->digits()->validate('shop_dev360auth_app_id'); $shop_dev360auth_app_key = $validator->required()->validate('shop_dev360auth_app_key'); $shop_dev360auth_app_secrect = $validator->required()->validate('shop_dev360auth_app_secrect'); // aimeidaren $aimeidaren_dev360auth_app_id = $validator->required()->digits()->validate('aimeidaren_dev360auth_app_id'); $aimeidaren_dev360auth_app_key = $validator->required()->validate('aimeidaren_dev360auth_app_key'); $aimeidaren_dev360auth_app_secrect = $validator->required()->validate('aimeidaren_dev360auth_app_secrect'); if (!$this->validate($validator)) { goto out_display; } // 保存设置 shop Dev360AuthPlugin::saveOptionValue('shop_dev360auth_app_id', $shop_dev360auth_app_id); Dev360AuthPlugin::saveOptionValue('shop_dev360auth_app_key', $shop_dev360auth_app_key); Dev360AuthPlugin::saveOptionValue('shop_dev360auth_app_secrect', $shop_dev360auth_app_secrect); // 保存设置 aimeidaren Dev360AuthPlugin::saveOptionValue('aimeidaren_dev360auth_app_id', $aimeidaren_dev360auth_app_id); Dev360AuthPlugin::saveOptionValue('aimeidaren_dev360auth_app_key', $aimeidaren_dev360auth_app_key); Dev360AuthPlugin::saveOptionValue('aimeidaren_dev360auth_app_secrect', $aimeidaren_dev360auth_app_secrect); $this->addFlashMessage('保存设置成功'); out_display: $smarty->display('dev360auth_configure.tpl', 'post'); }
/** * 360 一站通登陆 */ public function get($f3) { // 标准动态链接,不能伪静态地址 $callback = RouteHelper::makeUrl('/Thirdpart/Dev360Auth/Callback', null, false, true, false); $params = array('client_id' => Dev360AuthPlugin::getOptionValue(self::$optionKeyPrefix . 'dev360auth_app_key'), 'redirect_uri' => $callback, 'response_type' => 'code'); $url = 'https://openapi.360.cn/oauth2/authorize?' . http_build_query($params); header("Location: {$url}"); }
/** * 360 登陆 */ public function get($f3) { global $logger; if (empty($_GET['code'])) { $this->addFlashMessage('360联合登陆失败,Code 不存在'); goto out; } require_once 'sdk/QClient.php'; // 获取access token $callback = RouteHelper::makeUrl('/Thirdpart/Dev360Auth/Callback', null, false, true); $oauth = new \QOAuth2(Dev360AuthPlugin::getOptionValue(self::$optionKeyPrefix . 'dev360auth_app_key'), Dev360AuthPlugin::getOptionValue(self::$optionKeyPrefix . 'dev360auth_app_secrect'), ''); $token = $oauth->getAccessTokenByCode($_GET['code'], $callback); if (empty($token['access_token'])) { $this->addFlashMessage('360联合登陆失败,获取 access_token 失败'); goto out; } // 调用API,获取用户信息 $client = new \QClient(Dev360AuthPlugin::getOptionValue(self::$optionKeyPrefix . 'dev360auth_app_key'), Dev360AuthPlugin::getOptionValue(self::$optionKeyPrefix . 'dev360auth_app_secrect'), $token['access_token']); $user = $client->userMe(); if (empty($user)) { $this->addFlashMessage('360联合登陆失败,用户信息为空'); goto out; } $param = array('user_id' => $user['id'], 'username' => !empty($user['name']) ? (string) $user['name'] : '网友', 'token' => $token['access_token']); // put all values into $_POST[] $qid = $param['user_id']; $qname = urldecode($param['username']); $qmail = ''; if (empty($qid)) { // 没有 qid 没法登陆 $this->addFlashMessage('360联合登陆失败,没有 qid'); goto out; } $sns_login = "******"; // 用户登陆操作 $userBasicService = new UserBasicService(); $authUser = $userBasicService->doAuthSnsUser($sns_login, null, null, false); if ($authUser) { goto out_login_user; } // 用户不存在,自动注册一个用户 if (empty($qmail)) { $qmail = '' . $qid . '@360.cn'; } if (empty($qname)) { $qname = $qmail; } $retry = 10; // 重试 10 次 $regUserName = $qname; while ($userBasicService->isUserExist($regUserName, null) && $retry-- > 0) { $regUserName = $qname . '_' . rand(10000, 99999); } if ($retry <= 0) { $this->addFlashMessage('360联合登陆失败,用户名已经存在,无法自动注册'); goto out; } $authUser = $userBasicService->doAuthSnsUser($sns_login, $qname, $qmail, true); $logger->addLogInfo(\Core\Log\Base::INFO, 'DEV360AUTH', '注册360用户' . print_r(array('sns_login' => $sns_login, 'qname' => $qname, 'qmail' => $qmail), true)); out_login_user: AuthHelper::saveAuthUser($authUser->toArray(), 'dev360auth'); // 设置用户名在网页显示 ClientData::saveClientData(\Controller\User\Login::$clientDataIsUserLoginKey, true); ClientData::saveClientData(\Controller\User\Login::$clientDataUserNameDisplayKey, '360用户:' . $authUser['user_name']); out: // 跳转到用户之前看的页面,如果之前没有看过的页面那就回到首页 RouteHelper::jumpBack($this, '/', true); }
public function doUpdate($currentVersion) { // 简单的把版本设置为 1.0.0 就算完成升级了 Dev360AuthPlugin::saveOptionValue('version', $this->targetVersion); return true; }