ActivityController.php 5.7 KB

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