WebController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if (time() >= date_create("2016-11-10")->format('U') && time() <= date_create("2016-11-13")->format('U')) {
  58. $coupons = array(
  59. 0 => array('5822df8aa84ea04e238b456f', '5822df7ba84ea03e238b456d'),
  60. 1 => array('57fe10c69f5160b5048b45d0', '57fe0ed09f5160a2048b456b'),
  61. 2 => array('57fe0e029f5160a9048b456a', '57fe0d9b9f5160a3048b4572')
  62. );
  63. foreach ($coupons as $key => $coupon) {
  64. switch ($key) {
  65. //第一批
  66. case 0:
  67. $start_time = date_create("2016-11-14")->format('U');//发放优惠券可用开始时间 2016.11.14
  68. $end_time = date_create("2016-11-30")->format('U'); //发放优惠券过期时间 17天
  69. break;
  70. //第二批
  71. case 1:
  72. $start_time = date_create("2016-12-01")->format('U');//发放优惠券可用开始时间 2016.11.14
  73. $end_time = date_create("2016-12-15")->format('U'); //发放优惠券过期时间 15天
  74. break;
  75. //第三批
  76. case 2:
  77. $start_time = date_create("2016-12-16")->format('U');//发放优惠券可用开始时间 2016.11.14
  78. $end_time = $start_time + 86400 * 16; //发放优惠券过期时间 15天
  79. break;
  80. }
  81. foreach ($coupon as $c) {
  82. $coupon_id = new MongoId($c);
  83. Service::factory('CouponService')->giveCoupon($userId, $coupon_id, $start_time, $end_time);//发放代金券
  84. }
  85. }
  86. }
  87. //end
  88. }else{
  89. var_dump($userAr);exit;
  90. }
  91. }
  92. }else{
  93. echo $accessInfo['errcode'];
  94. die();
  95. }
  96. }
  97. }
  98. if($home_page){
  99. $this->renderpartial($home_page.'Index', array(
  100. 'version' => '2015082505',
  101. 'debug' => 'false',
  102. 'signPackage' => $signPackage,
  103. 'userId' => $userId,
  104. 'appToken' => $appToken,
  105. ));
  106. }else{
  107. /* var_dump(array(
  108. 'version' => '2015111601',
  109. 'debug' => 'false',
  110. 'signPackage' => $signPackage,
  111. 'userId' => $userId,
  112. 'appToken' => $appToken,
  113. ));exit;*/
  114. $this->renderpartial('index', array(
  115. 'version' => '2015111601',
  116. 'debug' => 'false',
  117. 'signPackage' => $signPackage,
  118. 'userId' => $userId,
  119. 'appToken' => $appToken,
  120. ));
  121. }
  122. }
  123. //检查微信登录页
  124. public function actionWxIndex() {
  125. $wxConfig = Yii::app()->params['wxConfig'];
  126. $home_page = Yii::app()->getRequest()->getParam("home_page",'');
  127. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/o2o/web/index';
  128. if($home_page){
  129. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index'.'&home_page='.$home_page;
  130. $redirectURI = $redirectURI.'/'.$home_page;
  131. }else{
  132. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index';
  133. }
  134. $scope = 'snsapi_userinfo';
  135. $state = 'yiguanjia';
  136. $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';
  137. $this->renderpartial('wxIndex', array(
  138. 'codeURI' => $codeURI,
  139. 'appURI' => $appURI
  140. ));
  141. }
  142. }