WebController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. class WebController extends JBaseController {
  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. $wxConfig = Yii::app()->params['wxJgConfig'];
  12. $accessInfo = CommonWeixin::getAccessInfo($code,$wxConfig['appId'],$wxConfig['appSecret']);
  13. $userId = '';
  14. if (!isset($accessInfo['errcode']) && $state == 'yiguanjia') {
  15. $appToken = md5(substr($accessInfo['openid'],2));
  16. //微信校验通过,登录(注册),分发token
  17. $userInfo = CommonWeixin::getUserInfo($accessInfo['access_token'], $accessInfo['openid']);
  18. if (!isset($accessInfo['errcode'])) {
  19. //检查是否有注册,没有就注册
  20. $criteria = new EMongoCriteria();
  21. $criteria->wx_pub_openid('==', $accessInfo['openid']);
  22. $user = RUser::model()->find($criteria);
  23. if ($user) {
  24. $userId = $user->_id;
  25. if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid) || $user->source != "真·巾帼家政"){
  26. $user->wx_pub_openid = $accessInfo['openid'];
  27. $user->wx_have_follow = 1;
  28. $user->source = '真·巾帼家政';
  29. $user->update(array('source','wx_pub_openid','wx_have_follow'),true);
  30. }
  31. }else{
  32. $userAr = new RUser();
  33. $userAr->user_name = $userInfo['nickname'];
  34. $userAr->avatar = $userInfo['headimgurl'];
  35. $userAr->wx_pub_openid = $userInfo['openid'];
  36. $userAr->unionid = $userInfo['openid'];
  37. $userAr->sex = $userInfo['sex'];
  38. $userAr->register_time = time();
  39. $userAr->channel = 'wxpub';
  40. $userAr->wx_have_follow = 1;
  41. $userAr->source = '真·巾帼家政';
  42. $u_criteria = new EMongoCriteria();
  43. $u_criteria->user_name('==',$userInfo['nickname']);
  44. $olduser = RUser::model()->find($u_criteria);
  45. if($olduser){
  46. $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
  47. $userAr->user_name = $user_new_neme;
  48. }
  49. $result = $userAr->save();
  50. if($result){
  51. //异步同步微信头像到七牛
  52. if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
  53. $list = new ARedisList('after_user_reg');
  54. $user_id = (string)$userAr->_id;
  55. $list->push($user_id);
  56. }
  57. $userId = (string)$userAr->_id;
  58. }else{
  59. var_dump($userAr);exit;
  60. }
  61. }
  62. }else{
  63. echo $accessInfo['errcode'];
  64. die();
  65. }
  66. }
  67. }
  68. if($home_page){
  69. $this->renderpartial($home_page.'Index', array(
  70. 'version' => '2015082505',
  71. 'debug' => 'false',
  72. 'signPackage' => $signPackage,
  73. 'userId1' => $userId,
  74. 'appToken1' => $appToken,
  75. ));
  76. }else{
  77. /* var_dump(array(
  78. 'version' => '2015111601',
  79. 'debug' => 'false',
  80. 'signPackage' => $signPackage,
  81. 'userId' => $userId,
  82. 'appToken' => $appToken,
  83. ));exit;*/
  84. $this->renderpartial('index', array(
  85. 'version' => '2015111601',
  86. 'debug' => 'false',
  87. 'signPackage' => $signPackage,
  88. 'userId1' => $userId,
  89. 'appToken1' => $appToken,
  90. ));
  91. }
  92. }
  93. //检查微信登录页
  94. public function actionWxIndex() {
  95. $wxConfig = Yii::app()->params['wxJgConfig'];
  96. $home_page = Yii::app()->getRequest()->getParam("home_page",'');
  97. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/index.php?r=j/web/index';
  98. if($home_page){
  99. $appURI = Yii::app()->request->baseUrl . 'index.php?r=j/web/index'.'&home_page='.$home_page;
  100. $redirectURI = $redirectURI.'/'.$home_page;
  101. }else{
  102. $appURI = Yii::app()->request->baseUrl . 'index.php?r=j/web/index';
  103. }
  104. $scope = 'snsapi_userinfo';
  105. $state = 'yiguanjia';
  106. $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';
  107. $this->renderpartial('wxIndex', array(
  108. 'codeURI' => $codeURI,
  109. 'appURI' => $appURI
  110. ));
  111. }
  112. // 服务介绍
  113. public function actionServiceIntroduction()
  114. {
  115. $this->renderpartial('serviceIntroduction');
  116. }
  117. // 礼包介绍 Gift package introduction
  118. public function actionGiftPackage()
  119. {
  120. $this->renderpartial('giftPackage');
  121. }
  122. }