123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- class WebController extends O2oBaseController {
- public function actionIndex() {
- $signPackage = CommonWeixin::get_sign();
- $home_page = Yii::app()->getRequest()->getParam("home_page",'');
- $code = Yii::app()->getRequest()->getParam("code");
- $state = Yii::app()->getRequest()->getParam("state");
- $userId = '';
- $appToken = '';
- if($code && $state){
- $accessInfo = CommonWeixin::getAccessInfo($code);
- $userId = '';
- if (!isset($accessInfo['errcode']) && $state == 'yiguanjia') {
- $appToken = md5(substr($accessInfo['openid'],2));
- //微信校验通过,登录(注册),分发token
- $userInfo = CommonWeixin::getUserInfo($accessInfo['access_token'], $accessInfo['openid']);
- if (!isset($accessInfo['errcode'])) {
- //检查是否有注册,没有就注册
- $criteria = new EMongoCriteria();
- $criteria->unionid('==', $accessInfo['unionid']);
- $user = RUser::model()->find($criteria);
- if ($user) {
- $userId = $user->_id;
- if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid)){
- $user->wx_pub_openid = $accessInfo['openid'];
- $user->wx_have_follow = 1;
- $user->update(array('wx_pub_openid','wx_have_follow'),true);
- }
- }else{
- $userAr = new RUser();
- $userAr->user_name = $userInfo['nickname'];
- $userAr->avatar = $userInfo['headimgurl'];
- $userAr->wx_pub_openid = $userInfo['openid'];
- $userAr->unionid = $userInfo['unionid'];
- $userAr->sex = $userInfo['sex'];
- $userAr->register_time = time();
- $userAr->channel = 'wxpub';
- $userAr->wx_have_follow = 1;
- $u_criteria = new EMongoCriteria();
- $u_criteria->user_name('==',$userInfo['nickname']);
- $olduser = RUser::model()->find($u_criteria);
- if($olduser){
- $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
- $userAr->user_name = $user_new_neme;
- }
-
- //异步同步微信头像到七牛
- if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
- $list = new ARedisList(Yii::app()->params['avatar_list']);
- $user_id = (string)$userAr->_id;
- $list->push($user_id);
- }
- $userId = (string)$userAr->_id;
- }
- }else{
- echo $accessInfo['errcode'];
- die();
- }
- }
- }
- if($home_page){
- $this->renderpartial($home_page.'Index', array(
- 'version' => '2015082505',
- 'debug' => 'false',
- 'signPackage' => $signPackage,
- 'userId' => $userId,
- 'appToken' => $appToken,
- ));
- }else{
- var_dump(array(
- 'version' => '2015111601',
- 'debug' => 'false',
- 'signPackage' => $signPackage,
- 'userId' => $userId,
- 'appToken' => $appToken,
- ));exit;
- $this->renderpartial('index', array(
- 'version' => '2015111601',
- 'debug' => 'false',
- 'signPackage' => $signPackage,
- 'userId' => $userId,
- 'appToken' => $appToken,
- ));
- }
- }
- //检查微信登录页
- public function actionWxIndex() {
- $wxConfig = Yii::app()->params['wxConfig'];
- $home_page = Yii::app()->getRequest()->getParam("home_page",'');
- $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/o2o/web/index';
- if($home_page){
- $appURI = Yii::app()->request->baseUrl . '/o2o/web/index'.'&home_page='.$home_page;
- $redirectURI = $redirectURI.'/'.$home_page;
- }else{
- $appURI = Yii::app()->request->baseUrl . '/o2o/web/index';
- }
- $scope = 'snsapi_userinfo';
- $state = 'yiguanjia';
- $codeURI = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $wxConfig['appId'] . '&redirect_uri=' . urlencode($redirectURI) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect';
- $this->renderpartial('wxIndex', array(
- 'codeURI' => $codeURI,
- 'appURI' => $appURI
- ));
- }
- public function actionBaiduLogin(){
- require_once(APP_PATH.'/protected/vendors/baiduSDK/BaiduApiClient.php');
- require_once(APP_PATH.'/protected/vendors/baiduSDK/BaiduOAuth2.php');
- require_once(APP_PATH.'/protected/vendors/baiduSDK/BaiduUtils.php');
- //回调页地址
- if(!isset($_GET['code'])){
- $this->renderpartial('index',array('from_channel' => 'baidu'));
- }else{
- $code = $_GET['code'];
- $oauth = new BaiduOAuth2(Yii::app()->params['baiduAK'], Yii::app()->params['baiduSK']);
- $oauth->setRedirectUri('http://apitest.wozhua.mobi/index.php?r=o2o/web/baiduLogin');
- $tokenArr = $oauth->getAccessTokenByAuthorizationCode($code);
- if (is_array($tokenArr)) {
- // 换取token成功
- $accessToken = $tokenArr['access_token'];
- $expires_in = $tokenArr['expires_in'];
- // 获取用户信息
- $client = new BaiduApiClient(Yii::app()->params['baiduAK'], $accessToken);
- $infoArr = $client->api('/rest/2.0/passport/users/getInfo', array('fields' => 'userid,username,portrait'));
- if (is_array($infoArr)) {
- // 获取用户信息成功
- // 在这里将百度账号与应用自身的账号系统做联合登录处理
- // setcookie('bd_access_token', $accessToken, strtotime('2030-1-1 12:00:00'), '/');
- // setcookie('bd_username', $infoArr['username'], strtotime('2030-1-1 12:00:00'), '/');
- // setcookie('bd_uid', $infoArr['userid'], strtotime('2030-1-1 12:00:00'), '/');
- // setcookie('bd_portrait', $infoArr['portrait'], strtotime('2030-1-1 12:00:00'), '/');
- //检查是否有注册,没有就注册
- $criteria = new EMongoCriteria();
- $criteria->openid('==', $infoArr['userid']);
- $user = RUser::model()->find($criteria);
- if ($user) {
- $userId = (string)$user->_id;
- }else{
- $userAr = new RUser();
- $userAr->user_name = $infoArr['username'];
- $userAr->register_time = time();
- $userAr->app_client_id = 3;
- $userAr->channel = 'baidu';
- $userAr->openid = $infoArr['userid'];
- $userAr->last_visit_time = time();
- $u_criteria = new EMongoCriteria();
- $u_criteria->user_name('==',$infoArr['username']);
- $olduser = RUser::model()->find($u_criteria);
- if($olduser){
- $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
- $userAr->user_name = $user_new_neme;
- }
- //用户注册后默认关注几个圈子
- $z_group = new ZGroup();
- $userAr->groups = $z_group->get_default_fllow_group();
- if($userAr->save()){
- if(!empty($userAr->groups)){
- foreach ($userAr->groups as $group_id) {
- $z_group = new ZGroup();
- $group_obj = $z_group->get($group_id);
- if($group_obj){
- $group_obj->users_count = $group_obj->users_count+1;
- $group_obj->update(array('users_count'),true);
- }
- }
- }
- $userId = (string)$userAr->_id;
- }
- }
- }
- $this->renderpartial('index', array(
- 'version' => '2015111601',
- 'debug' => 'false',
- 'userId' => $userId,
- 'from_channel' => 'baidu'
- ));
- }
- }
- }
- }
|