WebController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if($result){
  48. //异步同步微信头像到七牛
  49. if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
  50. $list = new ARedisList('after_user_reg');
  51. $user_id = (string)$userAr->_id;
  52. $list->push($user_id);
  53. }
  54. $userId = (string)$userAr->_id;
  55. //start双十一三天登陆发送优惠券 用后删除
  56. //三批
  57. /* $coupons = array(
  58. array(),
  59. array(),
  60. array()
  61. );
  62. $coupon_id = "5816a413a84ea0023f8b46ba";
  63. $coupon_id = new MongoId($coupon_id);
  64. $start_time = date_create("2016-11-11")->format('U'); //发放优惠券可用开始时间 2016.11.11
  65. $end_time = $start_time + 86400 * 30; //发放优惠券过期时间 一个月
  66. $cursor = RUser::model()->findAll();
  67. $rows = CommonFn::getRowsFromCursor($cursor);
  68. $flag = 0;
  69. foreach ($rows as $value) {
  70. if (CommonFn::isMongoId($value['_id'])) {
  71. $userId = $value['_id'];
  72. Service::factory('CouponService')->giveCoupon($userId, $coupon_id, $start_time, $end_time);//发放代金券
  73. $flag += 1;
  74. }
  75. }
  76. */
  77. //end
  78. }else{
  79. var_dump($userAr);exit;
  80. }
  81. }
  82. }else{
  83. echo $accessInfo['errcode'];
  84. die();
  85. }
  86. }
  87. }
  88. if($home_page){
  89. $this->renderpartial($home_page.'Index', array(
  90. 'version' => '2015082505',
  91. 'debug' => 'false',
  92. 'signPackage' => $signPackage,
  93. 'userId' => $userId,
  94. 'appToken' => $appToken,
  95. ));
  96. }else{
  97. /* var_dump(array(
  98. 'version' => '2015111601',
  99. 'debug' => 'false',
  100. 'signPackage' => $signPackage,
  101. 'userId' => $userId,
  102. 'appToken' => $appToken,
  103. ));exit;*/
  104. $this->renderpartial('index', array(
  105. 'version' => '2015111601',
  106. 'debug' => 'false',
  107. 'signPackage' => $signPackage,
  108. 'userId' => $userId,
  109. 'appToken' => $appToken,
  110. ));
  111. }
  112. }
  113. //检查微信登录页
  114. public function actionWxIndex() {
  115. $wxConfig = Yii::app()->params['wxConfig'];
  116. $home_page = Yii::app()->getRequest()->getParam("home_page",'');
  117. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/o2o/web/index';
  118. if($home_page){
  119. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index'.'&home_page='.$home_page;
  120. $redirectURI = $redirectURI.'/'.$home_page;
  121. }else{
  122. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index';
  123. }
  124. $scope = 'snsapi_userinfo';
  125. $state = 'yiguanjia';
  126. $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';
  127. $this->renderpartial('wxIndex', array(
  128. 'codeURI' => $codeURI,
  129. 'appURI' => $appURI
  130. ));
  131. }
  132. }