ActivityController.php 6.4 KB

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