WebController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. class WebController extends O2oBaseController {
  3. public function actionIndex() {
  4. $signPackage = CommonWeixin::get_sign();
  5. $home_page = Yii::app()->getRequest()->getParam("home_page",'');
  6. $code = Yii::app()->getRequest()->getParam("code");
  7. $state = Yii::app()->getRequest()->getParam("state");
  8. $userId = '';
  9. $appToken = '';
  10. if($code && $state){
  11. $accessInfo = CommonWeixin::getAccessInfo($code);
  12. $userId = '';
  13. if (!isset($accessInfo['errcode']) && $state == 'yiguanjia') {
  14. $appToken = md5(substr($accessInfo['openid'],2));
  15. //微信校验通过,登录(注册),分发token
  16. $userInfo = CommonWeixin::getUserInfo($accessInfo['access_token'], $accessInfo['openid']);
  17. if (!isset($accessInfo['errcode'])) {
  18. //检查是否有注册,没有就注册
  19. $criteria = new EMongoCriteria();
  20. $criteria->unionid('==', $accessInfo['unionid']);
  21. $user = RUser::model()->find($criteria);
  22. if ($user) {
  23. $userId = $user->_id;
  24. if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid)){
  25. $user->wx_pub_openid = $accessInfo['openid'];
  26. $user->wx_have_follow = 1;
  27. $user->update(array('wx_pub_openid','wx_have_follow'),true);
  28. }
  29. }else{
  30. $userAr = new RUser();
  31. $userAr->user_name = $userInfo['nickname'];
  32. $userAr->avatar = $userInfo['headimgurl'];
  33. $userAr->wx_pub_openid = $userInfo['openid'];
  34. $userAr->unionid = $userInfo['unionid'];
  35. $userAr->sex = $userInfo['sex'];
  36. $userAr->register_time = time();
  37. $userAr->channel = 'wxpub';
  38. $userAr->wx_have_follow = 1;
  39. $u_criteria = new EMongoCriteria();
  40. $u_criteria->user_name('==',$userInfo['nickname']);
  41. $olduser = RUser::model()->find($u_criteria);
  42. if($olduser){
  43. $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
  44. $userAr->user_name = $user_new_neme;
  45. }
  46. //异步同步微信头像到七牛
  47. if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
  48. $list = new ARedisList(Yii::app()->params['avatar_list']);
  49. $user_id = (string)$userAr->_id;
  50. $list->push($user_id);
  51. }
  52. $userId = (string)$userAr->_id;
  53. }
  54. }else{
  55. echo $accessInfo['errcode'];
  56. die();
  57. }
  58. }
  59. }
  60. if($home_page){
  61. $this->renderpartial($home_page.'Index', array(
  62. 'version' => '2015082505',
  63. 'debug' => 'false',
  64. 'signPackage' => $signPackage,
  65. 'userId' => $userId,
  66. 'appToken' => $appToken,
  67. ));
  68. }else{
  69. var_dump(array(
  70. 'version' => '2015111601',
  71. 'debug' => 'false',
  72. 'signPackage' => $signPackage,
  73. 'userId' => $userId,
  74. 'appToken' => $appToken,
  75. ));exit;
  76. $this->renderpartial('index', array(
  77. 'version' => '2015111601',
  78. 'debug' => 'false',
  79. 'signPackage' => $signPackage,
  80. 'userId' => $userId,
  81. 'appToken' => $appToken,
  82. ));
  83. }
  84. }
  85. //检查微信登录页
  86. public function actionWxIndex() {
  87. $wxConfig = Yii::app()->params['wxConfig'];
  88. $home_page = Yii::app()->getRequest()->getParam("home_page",'');
  89. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/o2o/web/index';
  90. if($home_page){
  91. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index'.'&home_page='.$home_page;
  92. $redirectURI = $redirectURI.'/'.$home_page;
  93. }else{
  94. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index';
  95. }
  96. $scope = 'snsapi_userinfo';
  97. $state = 'yiguanjia';
  98. $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';
  99. $this->renderpartial('wxIndex', array(
  100. 'codeURI' => $codeURI,
  101. 'appURI' => $appURI
  102. ));
  103. }
  104. public function actionBaiduLogin(){
  105. require_once(APP_PATH.'/protected/vendors/baiduSDK/BaiduApiClient.php');
  106. require_once(APP_PATH.'/protected/vendors/baiduSDK/BaiduOAuth2.php');
  107. require_once(APP_PATH.'/protected/vendors/baiduSDK/BaiduUtils.php');
  108. //回调页地址
  109. if(!isset($_GET['code'])){
  110. $this->renderpartial('index',array('from_channel' => 'baidu'));
  111. }else{
  112. $code = $_GET['code'];
  113. $oauth = new BaiduOAuth2(Yii::app()->params['baiduAK'], Yii::app()->params['baiduSK']);
  114. $oauth->setRedirectUri('http://apitest.wozhua.mobi/index.php?r=o2o/web/baiduLogin');
  115. $tokenArr = $oauth->getAccessTokenByAuthorizationCode($code);
  116. if (is_array($tokenArr)) {
  117. // 换取token成功
  118. $accessToken = $tokenArr['access_token'];
  119. $expires_in = $tokenArr['expires_in'];
  120. // 获取用户信息
  121. $client = new BaiduApiClient(Yii::app()->params['baiduAK'], $accessToken);
  122. $infoArr = $client->api('/rest/2.0/passport/users/getInfo', array('fields' => 'userid,username,portrait'));
  123. if (is_array($infoArr)) {
  124. // 获取用户信息成功
  125. // 在这里将百度账号与应用自身的账号系统做联合登录处理
  126. // setcookie('bd_access_token', $accessToken, strtotime('2030-1-1 12:00:00'), '/');
  127. // setcookie('bd_username', $infoArr['username'], strtotime('2030-1-1 12:00:00'), '/');
  128. // setcookie('bd_uid', $infoArr['userid'], strtotime('2030-1-1 12:00:00'), '/');
  129. // setcookie('bd_portrait', $infoArr['portrait'], strtotime('2030-1-1 12:00:00'), '/');
  130. //检查是否有注册,没有就注册
  131. $criteria = new EMongoCriteria();
  132. $criteria->openid('==', $infoArr['userid']);
  133. $user = RUser::model()->find($criteria);
  134. if ($user) {
  135. $userId = (string)$user->_id;
  136. }else{
  137. $userAr = new RUser();
  138. $userAr->user_name = $infoArr['username'];
  139. $userAr->register_time = time();
  140. $userAr->app_client_id = 3;
  141. $userAr->channel = 'baidu';
  142. $userAr->openid = $infoArr['userid'];
  143. $userAr->last_visit_time = time();
  144. $u_criteria = new EMongoCriteria();
  145. $u_criteria->user_name('==',$infoArr['username']);
  146. $olduser = RUser::model()->find($u_criteria);
  147. if($olduser){
  148. $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
  149. $userAr->user_name = $user_new_neme;
  150. }
  151. //用户注册后默认关注几个圈子
  152. $z_group = new ZGroup();
  153. $userAr->groups = $z_group->get_default_fllow_group();
  154. if($userAr->save()){
  155. if(!empty($userAr->groups)){
  156. foreach ($userAr->groups as $group_id) {
  157. $z_group = new ZGroup();
  158. $group_obj = $z_group->get($group_id);
  159. if($group_obj){
  160. $group_obj->users_count = $group_obj->users_count+1;
  161. $group_obj->update(array('users_count'),true);
  162. }
  163. }
  164. }
  165. $userId = (string)$userAr->_id;
  166. }
  167. }
  168. }
  169. $this->renderpartial('index', array(
  170. 'version' => '2015111601',
  171. 'debug' => 'false',
  172. 'userId' => $userId,
  173. 'from_channel' => 'baidu'
  174. ));
  175. }
  176. }
  177. }
  178. }