protected function _getWechatInfo($id)
 {
     $wechatInfo = WechatModel::model()->findByPk($id);
     if (!$wechatInfo || $wechatInfo->uid != Yii::app()->session['userInfo']['uid']) {
         ShowMessage::error('公众帐号不存在!');
     }
     return $wechatInfo;
 }
Exemple #2
0
 public function actionIndex($id)
 {
     $response = '';
     $originalId = $id;
     $wechatInfo = WechatModel::model()->find('originalId=:originalId', array(':originalId' => $originalId));
     if ($wechatInfo) {
         $wechatApi = new WechatApi($wechatInfo->token);
         switch (Yii::app()->request->requestType) {
             case 'GET':
                 if (Yii::app()->request->getParam('echostr')) {
                     $response = $wechatApi->valid();
                 }
                 break;
             case 'POST':
                 $post_string = $GLOBALS["HTTP_RAW_POST_DATA"];
                 $builder = new WeChatRequestBuilder();
                 $request = $builder->builder($post_string);
                 Yii::log(json_encode($request), 'warning', "api_request");
                 switch ($request) {
                     case $request instanceof WeChatTextRequest:
                         $response = $this->textResponse($wechatInfo->id, $request);
                         if (!$response && $wechatInfo->originalId == 'gh_354e9ce866ed') {
                             $content = "亲,感谢您关注我们的微信,微信福利不要忘记领取哟!亲,请根据您的手机型号,领取对应的礼包哦!安卓越狱玩家请按1,正版IOS玩家请按2!PPS安卓玩家请按3,即可获得激活码哟!【激活码兑换】登入游戏,点击公告,选择激活码,输入后即可兑换!礼包内容会在背包内哟!";
                             $xml = new WeChatTextResponse($content);
                             $response = $xml->_to_xml($request);
                         }
                         break;
                     case $request instanceof WeChatEventRequest:
                         switch ($request->event_type) {
                             case WeChatEventRequest::$type_subscribe:
                                 $response = $this->subscribeResponse($wechatInfo, $request);
                                 break;
                             case WeChatEventRequest::$type_menu:
                                 //menu response
                                 $key = $request->event_key;
                                 $response = $this->menuResponse($key, $request);
                                 break;
                             case WeChatEventRequest::$type_location:
                                 //TODO 上报地址位置
                                 break;
                             case WeChatEventRequest::$type_scan:
                                 //TODO 扫描二维码
                                 break;
                         }
                         break;
                     case $request instanceof WeChatLocationRequest:
                         //TODO 地理位置消息
                         break;
                 }
                 break;
         }
         echo $response;
     }
 }
Exemple #3
0
 public function actionIndex()
 {
     $logTable = 'active_log';
     $code = Yii::app()->request->getParam('code');
     $prize = 1;
     list($openId, $activeId, $type) = explode('|', Globals::authcode($code, 'DECODE'));
     //type 越狱或正版
     $active = ActiveModel::model()->findByPk($activeId);
     $startDate = $active->startTime;
     $endDate = $active->endTime;
     //活动是否开始
     if ($startDate > date('Y-m-d H:i:s')) {
         $prize = 1;
     } elseif ($endDate < date('Y-m-d H:i:s')) {
         $prize = 1;
     } elseif ($active->status == 0) {
         $prize = 1;
     }
     $prize = $active->status == 0 ? 0 : $prize;
     $connection = Yii::app()->db;
     $sql = "SELECT FROM_UNIXTIME(datetime,'%m%d') as date FROM {$logTable} where activeId={$activeId} and openId='{$openId}'";
     $command = $connection->createCommand($sql);
     $dataReader = $command->query();
     while (($row = $dataReader->read()) !== false) {
         $logList[$row['date']] = $row['date'];
     }
     $MouthStart = strtotime(date('Y-m', strtotime($startDate)) . '-01');
     $days = (strtotime(date('Y-m-d', strtotime("{$startDate} +1 month -1 day"))) - $MouthStart) / 86400;
     for ($i = 1; $i <= $days + 1; $i++) {
         $time = strtotime(date('Y-m', $MouthStart) . '-' . $i);
         $key = date('md', $time);
         $isSin = isset($logList[$key]) ? 1 : 0;
         $date[$key] = array('m' => date('m', $time), 'd' => date('d', $time), 'isSin' => $isSin);
     }
     $weekNum = date('w', $MouthStart);
     if ($weekNum > 0) {
         for ($i = 1; $i <= $weekNum; $i++) {
             array_unshift($date, array('m' => 0, 'd' => 0, 'isSin' => 0));
         }
     }
     $wechatInfo = WechatModel::model()->findByPk($active->wechatId);
     $jsToken = Globals::getJsToken($active->wechatId);
     $jsTokenValue = $jsToken['tokenValue'];
     $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
     $timestamp = time();
     $nonceStr = $code;
     $string = 'jsapi_ticket=' . $jsTokenValue . '&noncestr=' . $nonceStr . '&timestamp=' . $timestamp . '&url=' . $url;
     $signature = sha1($string);
     $this->renderPartial('active', array('active' => $active, 'prize' => $prize, 'encryption' => $code, 'jsToken' => $jsToken, 'appId' => $wechatInfo->appid, 'signature' => $signature, 'timestamp' => $timestamp, 'nonceStr' => $nonceStr, 'date' => $date));
 }
Exemple #4
0
 public function actionApi($id)
 {
     $wechatInfo = WechatModel::model()->findByPk($id);
     echo json_encode(array('token' => $wechatInfo->token, 'apiUrl' => $wechatInfo->apiUrl));
 }
Exemple #5
0
 public static function getToken($wechatId)
 {
     $msg = '参数有误';
     $tokenValue = '';
     $tokenModel = SettingModel::model()->find("wechatId = :wechatId and `key`=:key", array(':wechatId' => $wechatId, ':key' => Globals::SETTING_KEY_ACCESS_TOKEN));
     if ($tokenModel) {
         if (time() - $tokenModel->created_at < WechatToken::EXPIRES_IN) {
             $tokenValue = $tokenModel->value;
         }
     }
     if (!$tokenValue) {
         $wechat = WechatModel::model()->findByPk($wechatId);
         $appid = $wechat->appid;
         $secret = $wechat->secret;
         $token = WechatToken::getToken($appid, $secret);
         if ($token['status'] == WechatToken::OK) {
             $tokenValue = $token['result'];
             //update token
             if (!$tokenModel) {
                 $tokenModel = new SettingModel();
                 $tokenModel->wechatId = $wechatId;
                 $tokenModel->key = Globals::SETTING_KEY_ACCESS_TOKEN;
             }
             $tokenModel->value = $tokenValue;
             $tokenModel->created_at = time();
             $tokenModel->save();
         } else {
             $msg = $token['result'];
         }
     }
     return array('tokenValue' => $tokenValue, 'msg' => $msg);
 }