ActivityController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: PHP
  5. * Date: 2016/10/9
  6. * Time: 14:44
  7. */
  8. class ActivityController extends O2oBaseController {
  9. /**
  10. * 双十一给所有用户发券
  11. * 57fe0aaa9f5160b1048b456c
  12. */
  13. public function actionDoubleEleven(){
  14. $coupon_id = "5816a413a84ea0023f8b46ba";
  15. $coupon_id =new MongoId($coupon_id);
  16. $start_time = date_create("2016-11-01")->format('U'); //发放优惠券可用开始时间 2016.11.11
  17. $end_time = $start_time + 86400*30; //发放优惠券过期时间 一个月
  18. $cursor = RUser::model()->findAll();
  19. $rows = CommonFn::getRowsFromCursor($cursor);
  20. $flag = 0;
  21. foreach ($rows as $value) {
  22. if (CommonFn::isMongoId($value['_id'])) {
  23. $userId = $value['_id'];
  24. Service::factory('CouponService')->giveCoupon($userId, $coupon_id, $start_time, $end_time);//发放代金券
  25. $flag +=1;
  26. }
  27. }
  28. echo "总共发送".$flag."个用户";
  29. }
  30. //扫码跳转11元订单页面,回掉微信接口,注册用户信息,然后生成订单
  31. public function actionScanCoupon(){
  32. echo "hello world";
  33. }
  34. //扫码获取代金券
  35. public function actionGetCoupon() {
  36. $signPackage = CommonWeixin::get_sign();
  37. $coupon_id = Yii::app()->request->getParam('coupon_id','');//获取代金券id
  38. $code = Yii::app()->getRequest()->getParam("code");
  39. $state = Yii::app()->getRequest()->getParam("state");
  40. $userId = Yii::app()->getRequest()->getParam("userId",'');
  41. $appToken = '';
  42. if($code && $state){
  43. $accessInfo = CommonWeixin::getAccessInfo($code);
  44. if (!isset($accessInfo['errcode']) && $state == 'yiguanjia') {
  45. $appToken = md5(substr($accessInfo['openid'],2));
  46. //微信校验通过,登录(注册),分发token
  47. $userInfo = CommonWeixin::getUserInfo($accessInfo['access_token'], $accessInfo['openid']);
  48. if (!isset($accessInfo['errcode'])) {
  49. //检查是否有注册,没有就注册
  50. $criteria = new EMongoCriteria();
  51. $criteria->unionid('==', $accessInfo['unionid']);
  52. $user = RUser::model()->find($criteria);
  53. if ($user) {
  54. $userId = $user->_id;
  55. if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid)){
  56. $user->wx_pub_openid = $accessInfo['openid'];
  57. $user->wx_have_follow = 1;
  58. $user->update(array('wx_pub_openid','wx_have_follow'),true);
  59. }
  60. }else{
  61. $userAr = new RUser();
  62. $userAr->user_name = $userInfo['nickname'];
  63. $userAr->avatar = $userInfo['headimgurl'];
  64. $userAr->wx_pub_openid = $userInfo['openid'];
  65. $userAr->unionid = $userInfo['unionid'];
  66. $userAr->sex = $userInfo['sex'];
  67. $userAr->register_time = time();
  68. $userAr->channel = 'wxpub';
  69. $userAr->wx_have_follow = 1;
  70. $u_criteria = new EMongoCriteria();
  71. $u_criteria->user_name('==',$userInfo['nickname']);
  72. $olduser = RUser::model()->find($u_criteria);
  73. if($olduser){
  74. $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
  75. $userAr->user_name = $user_new_neme;
  76. }
  77. $result = $userAr->save();
  78. if($result){
  79. //异步同步微信头像到七牛
  80. if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
  81. $list = new ARedisList('after_user_reg');
  82. $user_id = (string)$userAr->_id;
  83. $list->push($user_id);
  84. }
  85. $userId = (string)$userAr->_id;
  86. }else{
  87. var_dump($userAr);exit;
  88. }
  89. }
  90. }else{
  91. echo $accessInfo['errcode'];
  92. die();
  93. }
  94. }
  95. }
  96. $coupon_id =new MongoId($coupon_id);
  97. $start_time = time(); //发放优惠券可用开始时间
  98. $end_time = $start_time + 86400*30; //发放优惠券过期时间
  99. $flag = 0;
  100. if ($userId != '') {
  101. $userId = new MongoId($userId);
  102. $criteria = new EMongoCriteria();
  103. //$criteria->coupon('==',$coupon_id);
  104. $criteria->user('==',$userId);
  105. $user_coupon = UserCoupon::model()->find($criteria);
  106. if (!$user_coupon) {
  107. Service::factory('CouponService')->giveCoupon($userId, $coupon_id, $start_time, $end_time);//发放代金券
  108. }
  109. $flag = 1;
  110. }
  111. if($flag){
  112. $this->renderpartial('index');
  113. }else{
  114. $this->renderpartial('getCoupon', array(
  115. 'version' => '2015111601',
  116. 'signPackage' => $signPackage,
  117. 'userId' => $userId,
  118. 'appToken' => $appToken,
  119. 'coupon_id' => $coupon_id,
  120. ));
  121. }
  122. }
  123. //检查微信登录页
  124. public function actionWxIndex() {
  125. $wxConfig = Yii::app()->params['wxConfig'];
  126. $coupon_id = Yii::app()->request->getParam('coupon_id', '');
  127. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/index.php?r=/common/activity/getCoupon&coupon_id='.$coupon_id;
  128. $appURI = Yii::app()->request->baseUrl . '/index.php?r=common/activity/getCoupon&coupon_id='.$coupon_id;
  129. $scope = 'snsapi_userinfo';
  130. $state = 'yiguanjia';
  131. $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';
  132. $this->renderpartial('wxIndex', array(
  133. 'codeURI' => $codeURI,
  134. 'appURI' => $appURI
  135. ));
  136. }
  137. }