|
@@ -5,14 +5,132 @@
|
|
|
* Date: 2016/10/9
|
|
|
* Time: 14:44
|
|
|
*/
|
|
|
-class ActivityController extends CController {
|
|
|
+
|
|
|
+class ActivityController extends O2oBaseController {
|
|
|
//扫码获取代金券
|
|
|
public function actionGetCoupon() {
|
|
|
- echo "123";
|
|
|
+ $signPackage = CommonWeixin::get_sign();
|
|
|
$coupon_id = Yii::app()->request->getParam('coupon_id','');//获取代金券id
|
|
|
- var_dump( $coupon_id);
|
|
|
+ $code = Yii::app()->getRequest()->getParam("code");
|
|
|
+ $state = Yii::app()->getRequest()->getParam("state");
|
|
|
+ $home_page = Yii::app()->getRequest()->getParam("home_page",'');
|
|
|
+ $userId = '';
|
|
|
+ $appToken = '';
|
|
|
+ if($code && $state){
|
|
|
+ $accessInfo = CommonWeixin::getAccessInfo($code);
|
|
|
+ $userId = '';
|
|
|
+ if (!isset($accessInfo['errcode']) && $state == 'yiguanjia') {
|
|
|
+ $appToken = md5(substr($accessInfo['openid'],2));
|
|
|
+ //微信校验通过,登录(注册),分发token
|
|
|
+ $userInfo = CommonWeixin::getUserInfo($accessInfo['access_token'], $accessInfo['openid']);
|
|
|
+ if (!isset($accessInfo['errcode'])) {
|
|
|
+ //检查是否有注册,没有就注册
|
|
|
+ $criteria = new EMongoCriteria();
|
|
|
+ $criteria->unionid('==', $accessInfo['unionid']);
|
|
|
+ $user = RUser::model()->find($criteria);
|
|
|
+ if ($user) {
|
|
|
+ $userId = $user->_id;
|
|
|
+ if(!isset($user->wx_pub_openid) || empty($user->wx_pub_openid)){
|
|
|
+ $user->wx_pub_openid = $accessInfo['openid'];
|
|
|
+ $user->wx_have_follow = 1;
|
|
|
+ $user->update(array('wx_pub_openid','wx_have_follow'),true);
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $userAr = new RUser();
|
|
|
+ $userAr->user_name = $userInfo['nickname'];
|
|
|
+ $userAr->avatar = $userInfo['headimgurl'];
|
|
|
+ $userAr->wx_pub_openid = $userInfo['openid'];
|
|
|
+ $userAr->unionid = $userInfo['unionid'];
|
|
|
+ $userAr->sex = $userInfo['sex'];
|
|
|
+ $userAr->register_time = time();
|
|
|
+ $userAr->channel = 'wxpub';
|
|
|
+ $userAr->wx_have_follow = 1;
|
|
|
+ $u_criteria = new EMongoCriteria();
|
|
|
+ $u_criteria->user_name('==',$userInfo['nickname']);
|
|
|
+ $olduser = RUser::model()->find($u_criteria);
|
|
|
+ if($olduser){
|
|
|
+ $user_new_neme = $userAr->user_name.'_'.substr(time(),-7);
|
|
|
+ $userAr->user_name = $user_new_neme;
|
|
|
+ }
|
|
|
+
|
|
|
+ $result = $userAr->save();
|
|
|
+ if($result){
|
|
|
+ //异步同步微信头像到七牛
|
|
|
+ if (!empty($userAr->unionid) && (strpos($userAr->avatar, 'qiniu') === false)) {
|
|
|
+ $list = new ARedisList('after_user_reg');
|
|
|
+ $user_id = (string)$userAr->_id;
|
|
|
+ $list->push($user_id);
|
|
|
+ }
|
|
|
+ $userId = (string)$userAr->_id;
|
|
|
+ }else{
|
|
|
+ var_dump($userAr);exit;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //用户注册后,发放代金券
|
|
|
+
|
|
|
+ $coupon_id =new MongoId($coupon_id);
|
|
|
+ $start_time = time(); //发放优惠券可用开始时间
|
|
|
+ $end_time = $start_time + 86400*30; //发放优惠券过期时间
|
|
|
+ if (CommonFn::isMongoId($coupon_id)) {
|
|
|
+ if (CommonFn::isMongoId($user->_id)) {
|
|
|
+ Service::factory('CouponService')->giveCoupon($user->_id, $coupon_id, $start_time, $end_time);//发放代金券
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ echo $accessInfo['errcode'];
|
|
|
+ die();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if($home_page){
|
|
|
+ $this->renderpartial('//o2o/web/index', array(
|
|
|
+ 'version' => '2015111601',
|
|
|
+ 'debug' => 'false',
|
|
|
+ 'signPackage' => $signPackage,
|
|
|
+ 'userId' => $userId,
|
|
|
+ 'appToken' => $appToken,
|
|
|
+ ));
|
|
|
+ }else{
|
|
|
+
|
|
|
+ $this->renderpartial('getCoupon', array(
|
|
|
+ 'version' => '2015111601',
|
|
|
+ 'debug' => 'false',
|
|
|
+ 'signPackage' => $signPackage,
|
|
|
+ 'userId' => $userId,
|
|
|
+ 'appToken' => $appToken,
|
|
|
+ 'coupon_id' => $coupon_id,
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //检查微信登录页
|
|
|
+ public function actionWxIndex() {
|
|
|
+
|
|
|
+ $wxConfig = Yii::app()->params['wxConfig'];
|
|
|
+ $home_page = Yii::app()->getRequest()->getParam("home_page",'');
|
|
|
+ $coupon_id = Yii::app()->request->getParam('coupon_id', '');
|
|
|
+ $redirectURI = 'http://' . $_SERVER['HTTP_HOST'] . Yii::app()->request->baseUrl . '/index.php?r=/common/activity/getCoupon&coupon_id='.$coupon_id;
|
|
|
+
|
|
|
+ if($home_page){
|
|
|
+ $appURI = Yii::app()->request->baseUrl . '/common/activity/getCoupon'.'&home_page='.$home_page;
|
|
|
+ $redirectURI = $redirectURI.'/'.$home_page;
|
|
|
+ }else{
|
|
|
+
|
|
|
|
|
|
+ $appURI = Yii::app()->request->baseUrl . '/common/activity/getCoupon&coupon_id='.$coupon_id;
|
|
|
+ }
|
|
|
+ $scope = 'snsapi_userinfo';
|
|
|
+ $state = 'yiguanjia';
|
|
|
+ $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';
|
|
|
|
|
|
+ $this->renderpartial('wxIndex', array(
|
|
|
+ 'codeURI' => $codeURI,
|
|
|
+ 'appURI' => $appURI
|
|
|
+ ));
|
|
|
}
|
|
|
|
|
|
}
|