Authored by xiaowei

PC重构-登录

... ... @@ -25,7 +25,7 @@ class LoginData extends \LibModels\Wap\Passport\LoginData
* @param string $shoppingKey 未登录用户唯一识别码, 默认为空
* @return array 登录返回结果
*/
public static function signinByOpenID($nickname, $openId, $sourceType, $shoppingKey = null)
public static function signinByOpenID($nickname, $openId, $sourceType, $shoppingKey = null, $replaceId = '')
{
// 构建必传参数
$param = Yohobuy::param();
... ... @@ -35,13 +35,18 @@ class LoginData extends \LibModels\Wap\Passport\LoginData
$param['openId'] = $openId;
$param['source_type'] = $sourceType;
$param['nickname'] = $nickname;
//wechat传入replace_id
if (!empty($replaceId)) {
$param['replace_id'] = $replaceId;
}
//购物车
if (!empty($shoppingKey)) {
$param['shopping_key'] = $shoppingKey;
}
$param['client_secret'] = Sign::getSign($param);
return Yohobuy::get(Yohobuy::API_URL, $param);
return Yohobuy::get(Yohobuy::API_URL2, $param);
}
}
... ...
<?php
namespace Plugin\Partner\wechat;
use Plugin\Partner\Factory;
define('WECHAT_CLASS_PATH', dirname(__FILE__) . '/class/');
require WECHAT_CLASS_PATH . 'Wechat.class.php';
/**
* 微信的调用接口
*
* @name Call
* @package lib/partner/wechat
* @copyright yoho.inc
* @version 5.0 (2016-01-12 10:54:54)
* @author xiaowei <xiaowei.gong@yoho.cn>
*/
class Call extends Factory
{
//微信对象
protected $wechat;
/**
* 初始化
*/
protected function init()
{
$this->wechat = new \WechatAuth($this->apiConfig['appId'], $this->apiConfig['appKey']);
}
/**
* 获取授权URL
*
* @return string
*/
public function getAuthorizeUrl()
{
return $this->wechat->getAuthorizeURL($this->apiConfig['callback'], $this->apiConfig['scope']);
}
/**
* 获取授权的TOKEN
*
* @return array
*/
public function getAccessToken()
{
$token = array();
if (isset($_REQUEST['code']) && !empty($_REQUEST['code'])) {
try {
$token = $this->wechat->getAccessToken($_REQUEST['code']);
}
catch (Exception $e) {
// do nothing
}
}
return $token;
}
/**
* 获取当前用户的基本资料
*
* @param object $token 授权成功的TOKEN, 默认为NULL
* @return array
*/
public function getUserInfo($token)
{
$userInfo = array();
if (!empty($token)) {
$userInfo = $this->wechat->getUserInfo($token['access_token'], $token['openid']);
}
return $userInfo;
}
public function getFriends($token, $params)
{
}
public function syncShare($token, $content, $image, $link)
{
}
}
... ...
<?php
defined('SITE_MAIN') || define('SITE_MAIN', $_SERVER['HTTP_HOST']);
return array(
'appId' => 'wx3ae21dcbb82ad672',
'appKey' => 'e78afb2321e6a19085767e1a0f0d52c1',
'callback' => SITE_MAIN . '/passport/autosign/wechatback',
'scope' => 'snsapi_login'
);
... ...
<?php
/**
* @ignore
*/
class OAuthException extends Exception
{
// pass
}
class WechatAuth
{
/**
* @ignore
*/
public $appId;
/**
* @ignore
*/
public $appKey;
/**
* @ignore
*/
public $accessToken;
/**
* @ignore
*/
public $refreshToken;
/**
* Set API URLS
*/
/**
* @ignore
*/
function userInfoURL()
{
return 'https://api.weixin.qq.com/sns/userinfo';
}
/**
* @ignore
*/
function authorizeURL()
{
return 'https://open.weixin.qq.com/connect/qrconnect';
}
/**
* @ignore
*/
function accessTokenURL()
{
return 'https://api.weixin.qq.com/sns/oauth2/access_token';
}
/**
* construct WeichatOAuth object
*/
function __construct($appId, $appKey, $accessToken = NULL)
{
$this->appId = $appId;
$this->appKey = $appKey;
$this->accessToken = $accessToken;
}
/**
* authorize接口
* $callBackurl 回调地址
*/
function getAuthorizeURL($callBackurl, $scope, $responseType = 'code', $state = 'STATE#wechat_redirect')
{
$params = array();
$params['appid'] = $this->appId;
$params['redirect_uri'] = $callBackurl;
$params['response_type'] = $responseType;
$params['scope'] = $scope;
$params['state'] = $state;
return $this->authorizeURL() . "?" . http_build_query($params);
}
/*
* 获取accesstoken
* code:授权链接返回的code
*/
function getAccessToken($code, $grant_type = 'authorization_code')
{
if (empty($code)) {
return '';
}
$params = array();
$params['appid'] = $this->appId;
$params['secret'] = $this->appKey;
$params['code'] = $code;
$params['grant_type'] = $grant_type;
$url = $this->accessTokenURL() . "?" . http_build_query($params);
$result = self::getCurl($url);
return json_decode($result, true);
}
// 获取用户信息
function getUserInfo($access_token, $openid)
{
if (empty($access_token) || empty($openid)) {
return array();
}
$params = array();
$params['access_token'] = $access_token;
$params['openid'] = $openid;
$url = $this->userInfoURL() . "?" . http_build_query($params);
$result = self::getCurl($url);
return json_decode($result, true);
}
/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $get values to send
* @param array $options for cURL
* @return string
*/
public static function getCurl($url,$method="GET")
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
... ...
... ... @@ -2,7 +2,6 @@
use Action\AbstractAction;
use LibModels\Web\Passport\LoginData;
use Passport\PassportModel as PassportModel;
use Plugin\Helpers;
use Plugin\Partner\Factory;
... ... @@ -62,13 +61,14 @@ class AutosignController extends AbstractAction
$this->go(Factory::create('douban')->getAuthorizeUrl());
}
/**
/**
* 微信网站授权入口
*/
public function wechatAction(){
$url = QINWechat_Sdk_Open::getCode('http://www.yohobuy.com/passport/autosign/wechatback');
$this->helpGo($url);
public function wechatAction()
{
$this->setSession('_TOKEN', '');
$this->go(Factory::create('wechat')->getAuthorizeUrl());
}
/**
... ... @@ -88,7 +88,12 @@ class AutosignController extends AbstractAction
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
$this->go(Helpers::url('/passport/bind/index', array('openId' => $userId, 'sourceType' => 'alipay', 'nickname' => $realName)));
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$fillHerf = rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $userId, 'sourceType' => 'alipay', 'nickname' => $realName)));
$this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
}
$refer = $this->getCookie('refer');
... ... @@ -126,11 +131,6 @@ class AutosignController extends AbstractAction
$result = LoginData::signinByOpenID($partnerInfo['nickname'], $access['openid'], 'qq', $shoppingKey);
}
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
$this->go(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $access['openid'], 'sourceType' => 'qq', 'nickname' => $partnerInfo['nickname'])));
}
$refer = $this->getCookie('refer');
if (empty($refer)) {
$refer = SITE_MAIN . '/?go=1';
... ... @@ -139,6 +139,16 @@ class AutosignController extends AbstractAction
$refer = rawurldecode($refer);
}
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$fillHerf = rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $access['openid'], 'sourceType' => 'qq', 'nickname' => $partnerInfo['nickname'])));
$this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
}
if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
... ... @@ -169,7 +179,12 @@ class AutosignController extends AbstractAction
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
// $this->go(Helpers::url('/passport/bind/index', array('openId' => $access['uid'], 'sourceType' => 'sina', 'nickname' => $partnerInfo['screen_name'])));
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$fillHerf = rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $access['uid'], 'sourceType' => 'sina', 'nickname' => $partnerInfo['screen_name'])));
$this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
}
$refer = $this->getCookie('refer');
... ... @@ -199,18 +214,21 @@ class AutosignController extends AbstractAction
{
$renren = Factory::create('renren');
$access = $renren->getAccessToken();
/* 获取用户的详细信息 */
$partnerInfo = $renren->getUserInfo($access);
$result = array();
if ($partnerInfo && is_array($partnerInfo)) {
if ($access && is_array($access)) {
$shoppingKey = Helpers::getShoppingKeyByCookie();
$result = LoginData::signinByOpenID($partnerInfo['nickname'], $access['uid'], 'renren', $shoppingKey);
$result = LoginData::signinByOpenID($access['user']['name'], $access['user']['id'], 'renren', $shoppingKey);
}
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
// $this->go(Helpers::url('/passport/bind/index', array('openId' => $access['uid'], 'sourceType' => 'sina', 'nickname' => $partnerInfo['nick_name'])));
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$fillHerf = rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $access['user']['id'], 'sourceType' => 'renren', 'nickname' => $access['user']['name'])));
$this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
}
$refer = $this->getCookie('refer');
... ... @@ -238,20 +256,25 @@ class AutosignController extends AbstractAction
*/
public function doubanbackAction()
{
$sina = Factory::create('douban');
$access = $sina->getAccessToken();
$douban = Factory::create('douban');
$access = $douban->getAccessToken();
/* 获取用户的详细信息 */
$partnerInfo = $sina->getUserInfo($access);
$partnerInfo = $douban->getUserInfo($access);
$result = array();
if ($partnerInfo && is_array($partnerInfo)) {
$shoppingKey = Helpers::getShoppingKeyByCookie();
$result = LoginData::signinByOpenID($partnerInfo['nickname'], $access['uid'], 'douban', $shoppingKey);
$result = LoginData::signinByOpenID($partnerInfo['name'], $partnerInfo['uid'], 'douban', $shoppingKey);
}
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
// $this->go(Helpers::url('/passport/bind/index', array('openId' => $access['uid'], 'sourceType' => 'sina', 'nickname' => $partnerInfo['screen_name'])));
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$fillHerf =rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $access['douban_user_id'], 'sourceType' => 'douban', 'nickname' => $partnerInfo['name'])));
$this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
}
$refer = $this->getCookie('refer');
... ... @@ -273,52 +296,51 @@ class AutosignController extends AbstractAction
$this->go($refer);
}
}
/**
* 微信网站授权获取用户信息并登录
* @param string code
*/
public function wechatbackAction(){
$code = $this->helpGquery('code');
$accessToken = QINWechat_Sdk_Open::getAccessToken($code);
$userInfo = array();
if(empty($accessToken)){
$this->helpGo('/signin.html');
}
if(!isset($accessToken['access_token']) || !isset($accessToken['unionid']) || empty($accessToken['access_token']) || empty($accessToken['unionid'])){
$this->helpGo('/signin.html');
}
$userInfo = QINWechat_Sdk_Open::getUserInfo($accessToken['access_token'], $accessToken['unionid']);
if(empty($userInfo)){
$this->helpGo('/signin.html');
}
$this->wechatlogin($userInfo);
}
/**
* 微信登录
* @param array $userInfo
*/
public function wechatlogin($userInfo){
if(empty($userInfo)){
$this->helpGo('/signin.html');
}
try {
$oauthUserInfo = QINAuth_User_Utils::check('wechat', $userInfo['unionid'], $userInfo['nickname'], null, $userInfo);
$auth = QINAuth_Factory::profile('wechat');
$res=$auth->associate($oauthUserInfo['email'], array(
'password' => QINAuth_User_Utils::$defaultPassword,
'open_id' => $userInfo['unionid'],
'nick_name' => $userInfo['nickname']
));
} catch (Exception $e) {
$this->helpSession('passport_space')->__set('error_message', 'wechat' . $e->getMessage());
$this->helpGo('/signin.html');
}
//跳转到
$url = $this->authInfo($res, 'wechat' , $oauthUserInfo['email']);
$this->helpGo($url);
}
public function wechatbackAction()
{
$wechat = Factory::create('wechat');
$access = $wechat->getAccessToken();
/* 获取用户的详细信息 */
$partnerInfo = $wechat->getUserInfo($access);
$result = array();
if ($partnerInfo && is_array($partnerInfo)) {
$shoppingKey = Helpers::getShoppingKeyByCookie();
$result = LoginData::signinByOpenID($partnerInfo['nickname'],$partnerInfo['openid'], 'wechat', $shoppingKey,$partnerInfo['openid']);
}
//判定是否需要绑定手机号
if (isset($result['data']['mobile']) && $result['data']['mobile'] == '') {
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$fillHerf =rawurlencode(Helpers::url('/passport/autouserinfo/userinfo', array('openId' => $partnerInfo['openid'], 'sourceType' => 'wechat', 'nickname' => $partnerInfo['nickname'])));
$this->go(Helpers::syncUserSession($result['data']['uid'], $fillHerf));
}
$refer = $this->getCookie('refer');
if (empty($refer)) {
$refer = SITE_MAIN . '/?go=1';
}
else {
$refer = rawurldecode($refer);
}
if (isset($result['code']) && $result['code'] == 200 && !empty($result['data']['uid'])) {
$token = Helpers::makeToken($result['data']['uid']);
$this->setSession('_TOKEN', $token);
$this->setSession('_LOGIN_UID', $result['data']['uid']);
$this->setCookie('_TOKEN', $token);
$this->go(Helpers::syncUserSession($result['data']['uid'], $refer));
}
else {
$this->go($refer);
}
}
}
... ...