ActivityController.php 5.7 KB

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