ActivityController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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-11")->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. //扫码获取代金券
  31. public function actionGetCoupon() {
  32. $signPackage = CommonWeixin::get_sign();
  33. $coupon_id = Yii::app()->request->getParam('coupon_id','');//获取代金券id
  34. $code = Yii::app()->getRequest()->getParam("code");
  35. $state = Yii::app()->getRequest()->getParam("state");
  36. $userId = Yii::app()->getRequest()->getParam("userId",'');
  37. $appToken = '';
  38. if($code && $state){
  39. $accessInfo = CommonWeixin::getAccessInfo($code);
  40. if (!isset($accessInfo['errcode']) && $state == 'yiguanjia') {
  41. $appToken = md5(substr($accessInfo['openid'],2));
  42. //微信校验通过,登录(注册),分发token
  43. $userInfo = CommonWeixin::getUserInfo($accessInfo['access_token'], $accessInfo['openid']);
  44. if (!isset($accessInfo['errcode'])) {
  45. //检查是否有注册,没有就注册
  46. $criteria = new EMongoCriteria();
  47. $criteria->unionid('==', $accessInfo['unionid']);
  48. $user = RUser::model()->find($criteria);
  49. if ($user) {
  50. $userId = $user->_id;
  51. if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid)){
  52. $user->wx_pub_openid = $accessInfo['openid'];
  53. $user->wx_have_follow = 1;
  54. $user->update(array('wx_pub_openid','wx_have_follow'),true);
  55. }
  56. }else{
  57. $userAr = new RUser();
  58. $userAr->user_name = $userInfo['nickname'];
  59. $userAr->avatar = $userInfo['headimgurl'];
  60. $userAr->wx_pub_openid = $userInfo['openid'];
  61. $userAr->unionid = $userInfo['unionid'];
  62. $userAr->sex = $userInfo['sex'];
  63. $userAr->register_time = time();
  64. $userAr->channel = 'wxpub';
  65. $userAr->wx_have_follow = 1;
  66. $u_criteria = new EMongoCriteria();
  67. $u_criteria->user_name('==',$userInfo['nickname']);
  68. $olduser = RUser::model()->find($u_criteria);
  69. if($olduser){
  70. $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
  71. $userAr->user_name = $user_new_neme;
  72. }
  73. $result = $userAr->save();
  74. if($result){
  75. //异步同步微信头像到七牛
  76. if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
  77. $list = new ARedisList('after_user_reg');
  78. $user_id = (string)$userAr->_id;
  79. $list->push($user_id);
  80. }
  81. $userId = (string)$userAr->_id;
  82. }else{
  83. var_dump($userAr);exit;
  84. }
  85. }
  86. }else{
  87. echo $accessInfo['errcode'];
  88. die();
  89. }
  90. }
  91. }
  92. $coupon_id =new MongoId($coupon_id);
  93. $start_time = time(); //发放优惠券可用开始时间
  94. $end_time = $start_time + 86400*30; //发放优惠券过期时间
  95. $flag = 0;
  96. if ($userId != '') {
  97. $userId = new MongoId($userId);
  98. $criteria = new EMongoCriteria();
  99. //$criteria->coupon('==',$coupon_id);
  100. $criteria->user('==',$userId);
  101. $user_coupon = UserCoupon::model()->find($criteria);
  102. if (!$user_coupon) {
  103. Service::factory('CouponService')->giveCoupon($userId, $coupon_id, $start_time, $end_time);//发放代金券
  104. }
  105. $flag = 1;
  106. }
  107. if($flag){
  108. $this->renderpartial('index');
  109. }else{
  110. $this->renderpartial('getCoupon', array(
  111. 'version' => '2015111601',
  112. 'signPackage' => $signPackage,
  113. 'userId' => $userId,
  114. 'appToken' => $appToken,
  115. 'coupon_id' => $coupon_id,
  116. ));
  117. }
  118. }
  119. //检查微信登录页
  120. public function actionWxIndex() {
  121. $wxConfig = Yii::app()->params['wxConfig'];
  122. $coupon_id = Yii::app()->request->getParam('coupon_id', '');
  123. $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/index.php?r=/common/activity/getCoupon&coupon_id='.$coupon_id;
  124. $appURI = Yii::app()->request->baseUrl . '/index.php?r=common/activity/getCoupon&coupon_id='.$coupon_id;
  125. $scope = 'snsapi_userinfo';
  126. $state = 'yiguanjia';
  127. $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';
  128. $this->renderpartial('wxIndex', array(
  129. 'codeURI' => $codeURI,
  130. 'appURI' => $appURI
  131. ));
  132. }
  133. }