WebController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. var_dump($user);
  23. if ($user) {
  24. $userId = $user->_id;
  25. if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid)){
  26. $user->wx_pub_openid = $accessInfo['openid'];
  27. $user->wx_have_follow = 1;
  28. $user->update(array('wx_pub_openid','wx_have_follow'),true);
  29. }
  30. }else{
  31. $userAr = new RUser();
  32. $userAr->user_name = $userInfo['nickname'];
  33. $userAr->avatar = $userInfo['headimgurl'];
  34. $userAr->wx_pub_openid = $userInfo['openid'];
  35. $userAr->unionid = $userInfo['unionid'];
  36. $userAr->sex = $userInfo['sex'];
  37. $userAr->register_time = time();
  38. $userAr->channel = 'wxpub';
  39. $userAr->wx_have_follow = 1;
  40. $u_criteria = new EMongoCriteria();
  41. $u_criteria->user_name('==',$userInfo['nickname']);
  42. $olduser = RUser::model()->find($u_criteria);
  43. if($olduser){
  44. $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
  45. $userAr->user_name = $user_new_neme;
  46. }
  47. $result = $userAr->save();
  48. if($result){
  49. //异步同步微信头像到七牛
  50. if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
  51. $list = new ARedisList('after_user_reg');
  52. $user_id = (string)$userAr->_id;
  53. $list->push($user_id);
  54. }
  55. $userId = (string)$userAr->_id;
  56. }else{
  57. var_dump($userAr);exit;
  58. }
  59. }
  60. }else{
  61. echo $accessInfo['errcode'];
  62. die();
  63. }
  64. }
  65. }
  66. if ($userId) {
  67. //start双十一三天登陆发送优惠券 用后删除
  68. //三批
  69. $criteria = new EMongoCriteria();
  70. $criteria->coupon('==',new MongoId('5822df8aa84ea04e238b456f'));
  71. $criteria->user('==', new MongoId('57e237ea9f5160b6048b4568'));
  72. $user_coupon = UserCoupon::model()->find($criteria);
  73. if (empty($user_coupon) && time() >= intval(date_create("2016-11-10")->format('U')) && time() <= intval(date_create("2016-11-13")->format('U'))) {
  74. $coupons = array(
  75. 0 => array('5822df8aa84ea04e238b456f', '5822df7ba84ea03e238b456d'),
  76. 1 => array('57fe10c69f5160b5048b45d0', '57fe0ed09f5160a2048b456b'),
  77. 2 => array('57fe0e029f5160a9048b456a', '57fe0d9b9f5160a3048b4572')
  78. );
  79. foreach ($coupons as $key => $coupon) {
  80. switch ($key) {
  81. //第一批
  82. case 0:
  83. $start_time = date_create("2016-11-14")->format('U');//发放优惠券可用开始时间 2016.11.14
  84. $end_time = date_create("2016-11-30")->format('U'); //发放优惠券过期时间 17天
  85. break;
  86. //第二批
  87. case 1:
  88. $start_time = date_create("2016-12-01")->format('U');//发放优惠券可用开始时间 2016.11.14
  89. $end_time = date_create("2016-12-15")->format('U'); //发放优惠券过期时间 15天
  90. break;
  91. //第三批
  92. case 2:
  93. $start_time = date_create("2016-12-16")->format('U');//发放优惠券可用开始时间 2016.11.14
  94. $end_time = $start_time + 86400 * 16; //发放优惠券过期时间 15天
  95. break;
  96. }
  97. foreach ($coupon as $c) {
  98. $coupon_id = new MongoId($c);
  99. Service::factory('CouponService')->giveCoupon($userId, $coupon_id, $start_time, $end_time);//发放代金券
  100. }
  101. }
  102. }
  103. //end
  104. }
  105. if($home_page){
  106. $this->renderpartial($home_page.'Index', array(
  107. 'version' => '2015082505',
  108. 'debug' => 'false',
  109. 'signPackage' => $signPackage,
  110. 'userId' => $userId,
  111. 'appToken' => $appToken,
  112. ));
  113. }else{
  114. /* var_dump(array(
  115. 'version' => '2015111601',
  116. 'debug' => 'false',
  117. 'signPackage' => $signPackage,
  118. 'userId' => $userId,
  119. 'appToken' => $appToken,
  120. ));exit;*/
  121. $this->renderpartial('index', array(
  122. 'version' => '2015111601',
  123. 'debug' => 'false',
  124. 'signPackage' => $signPackage,
  125. 'userId' => $userId,
  126. 'appToken' => $appToken,
  127. ));
  128. }
  129. }
  130. //检查微信登录页
  131. public function actionWxIndex() {
  132. $wxConfig = Yii::app()->params['wxConfig'];
  133. $home_page = Yii::app()->getRequest()->getParam("home_page",'');
  134. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/o2o/web/index';
  135. if($home_page){
  136. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index'.'&home_page='.$home_page;
  137. $redirectURI = $redirectURI.'/'.$home_page;
  138. }else{
  139. $appURI = Yii::app()->request->baseUrl . '/o2o/web/index';
  140. }
  141. $scope = 'snsapi_userinfo';
  142. $state = 'yiguanjia';
  143. $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';
  144. $this->renderpartial('wxIndex', array(
  145. 'codeURI' => $codeURI,
  146. 'appURI' => $appURI
  147. ));
  148. }
  149. }