WebController.php 7.8 KB

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