WebController.php 9.3 KB

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