ActivityController.php 5.4 KB

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