Пример #1
0
 protected function processText()
 {
     date_default_timezone_set("Asia/Beijing");
     $help_url = "http://wechat.vtshow.top/home/help/index";
     $data = $this->_wechat->getRevData();
     \Common\Lib\Utils::log('wechat', 'request.log', $data);
     $fromusername = $data['FromUserName'];
     $filter = array('openid' => $fromusername);
     $userMdl = D('User');
     $user = $userMdl->getRow($filter);
     if ($user) {
         $userid = $user['id'];
         $level = $user['level'] ? $user['level'] : '1';
     } else {
         $params = $filter;
         $params['level'] = 1;
         $params['created_at'] = time();
         $params['updated_at'] = time();
         $res = $userMdl->saveData($params);
         if ($res['status'] == 'success') {
             $userid = $res['data'];
             $level = 1;
         }
     }
     $rs['msg_info_type'] = 1;
     if (!$userid) {
         $rs['content'] = '请输入正确的商品链接地址, <a href="' . $help_url . '">查看帮助</a>';
         $this->reply($rs);
         exit;
     }
     $itemsMdl = D('Items');
     $ifilter['uid'] = $userid;
     $ifilter['due_at'] = array('gt', time());
     $count = $itemsMdl->getCount($ifilter);
     //\Common\Lib\Utils::log('wechat', 'request.log', $ifilter);
     $level_str = '';
     switch ($level) {
         case '1':
             if ($count >= 1) {
                 $rs['content'] = '您的会员等级,只能创建1次链接,<a href="http://wechat.vtshow.top/home/pay/index">点击成为会员</a>,可创建更多';
             } else {
                 $yesterday_end = strtotime(date('Y-m-d', time()));
                 $ifilter['due_at'] = array('lt', time());
                 $ifilter['updated_at'] = array('between', array($yesterday_end, $yesterday_end + 86400));
                 //\Common\Lib\Utils::log('wechat', 'request.log', $ifilter);
                 //$count = $itemsMdl->getCount($ifilter);
                 //\Common\Lib\Utils::log('wechat', 'request.log', $itemsMdl->getLastSql());
                 if ($itemsMdl->getCount($ifilter) > 0) {
                     $rs['content'] = '您的会员等级,今天已经创建了1次链接,请等后天再创建, <a href="http://wechat.vtshow.top/home/pay/index">点击成为会员</a>,可创建更多';
                 } else {
                     $yesterday_start = $yesterday_end - 86400;
                     $ifilter['updated_at'] = array('between', array($yesterday_start, $yesterday_end));
                     \Common\Lib\Utils::log('wechat', 'request.log', $ifilter);
                     if ($itemsMdl->getCount($ifilter) > 0) {
                         $rs['content'] = '您的会员等级,昨天已经创建了1次链接,请等明天再创建, <a href="http://wechat.vtshow.top/home/pay/index">点击成为会员</a>,可创建更多';
                     }
                 }
             }
             $level_str = '普通会员';
             $due_at = time() + 1800;
             break;
         case '2':
             if ($count >= 10) {
                 $rs['content'] = '您是银牌会员,已经创建10次链接,, <a href="http://wechat.vtshow.top/home/pay/index">点击成为会员</a>,可创建更多';
             }
             //$due_at = time() + 7 * 86400;
             $due_at = $user['due_at'];
             $level_str = '银牌会员';
             break;
         case '3':
             //$due_at = time() + 30 * 86400;
             $due_at = $user['due_at'];
             $level_str = '金牌会员';
             break;
         default:
             break;
     }
     if (isset($rs['content']) && $rs['content']) {
         $this->reply($rs);
         exit;
     }
     $content_str = $data['Content'];
     /*
     $h_pos = strpos($content_str, 'http');
     if(!$h_pos) {
         $h_pos = strpos($content_str, 'https');
     }
     $content = substr($content_str, $h_pos, strlen($content_str)); 
     */
     //获得字符串中的url
     $urls = array();
     preg_match_all("/http[s]?:\\/\\/?[^\\s]+/i", $content_str, $urls);
     $content = $urls[0][0];
     $o_url = $content;
     \Common\Lib\Utils::log('wechat', 'request.log', $content);
     $title = "";
     $http_pos = strpos($content_str, 'http');
     if ($http_pos) {
         $title = substr($content_str, 0, $http_pos);
     }
     if (preg_match('/mashort.cn/', $content) || preg_match('/tb.cn/', $content)) {
         $n_content = file_get_contents($content);
         $pa = '%url = ("|\')?(.*?)("|\')?;%s';
         preg_match($pa, $n_content, $r);
         \Common\Lib\Utils::log('wechat', 'request.log', $r);
         if ($r) {
             $url = urldecode($r[2]);
             \Common\Lib\Utils::log('wechat', 'request.log', $url);
             $n_query = explode('url=', $url);
             \Common\Lib\Utils::log('wechat', 'request.log', $n_query);
             $n_url = $n_query[1] ? $n_query[1] : $n_query[0];
             $content = $n_url;
         }
     }
     \Common\Lib\Utils::log('wechat', 'request.log', $content);
     if (preg_match('/^http[s]?:\\/\\//i', $content) === 0) {
         $rs['content'] = '请输入正确的商品链接地址, <a href="' . $help_url . '">查看帮助</a>';
     } else {
         $url_arr = parse_url($content);
         $type = '';
         $host = $url_arr['host'];
         if (preg_match('/tmall.com/', $host) || preg_match('/taobao.com/', $host)) {
             $type = 'taobao';
         } elseif (preg_match('/jd.com/', $host)) {
             $type = 'jd';
         }
         $nid = '';
         if ($type == 'taobao') {
             if ($url_arr['query']) {
                 parse_str($url_arr['query'], $query);
                 $nid = $query['id'];
             }
         } elseif ($type == 'jd') {
             $path = $url_arr['path'];
             if ($path) {
                 $nid = substr($path, 1, strlen($path) - 6);
             }
         }
         if ($nid) {
             //判断是否已经生成
             $itemRes = $itemsMdl->getRow(array('nid' => $nid, 'uid' => $userid));
         } else {
             $itemRes = $itemsMdl->getRow(array('o_url' => $o_url, 'uid' => $userid));
         }
         \Common\Lib\Utils::log('wechat', 'request.log', $itemRes);
         $s_url = '';
         if ($itemRes) {
             $params['id'] = $itemRes['id'];
             $params['uid'] = $userid;
             $params['url'] = $content;
             $params['o_url'] = $o_url;
             $params['due_at'] = $due_at;
             $params['title'] = $itemRes['title'] ? $itemRes['title'] : $title;
             $params['updated_at'] = time();
             $itemsMdl->saveData($params);
             $id = $itemRes['id'];
             $s_url = $itemRes['s_url'];
             \Common\Lib\Utils::log('wechat', 'request.log', $params);
         } else {
             $params['url'] = $content;
             $params['o_url'] = $o_url;
             $params['nid'] = $nid;
             $params['uid'] = $userid;
             $params['platform'] = $type;
             $params['created_at'] = time();
             $params['updated_at'] = time();
             $params['due_at'] = $due_at;
             $params['title'] = trim($title);
             $res = $itemsMdl->saveData($params);
             //\Common\Lib\Utils::log('wechat', 'request.log', $res);
             if ($res['status'] == 'success') {
                 $id = $res['data'];
             }
             //\Common\Lib\Utils::log('wechat', 'request.log', $itemsMdl->getLastSql());
             //\Common\Lib\Utils::log('wechat', 'request.log', $params);
         }
         if ($id) {
             if (!$s_url) {
                 $url = 'http://i.shopflow.cn/show/' . \Common\Lib\Idhandler::encode($id);
                 $s_url = \Common\Lib\Utils::short_url($url);
                 if (!$s_url) {
                     $s_url = $url;
                 }
                 $itemsMdl->saveData(array('id' => $id, 's_url' => $s_url));
             }
             if ($s_url) {
                 $out_content = "链接1: " . $s_url . "\n链接2: " . 'http://i.shopflow.cn/show/' . \Common\Lib\Idhandler::encode($id) . "\n请选择可以访问的链接分发\n\n您是" . $level_str . ",链接已创建!\n过期时间:" . date('Y-m-d H:i', $due_at);
             } else {
                 $out_content = 'http://i.shopflow.cn/show/' . \Common\Lib\Idhandler::encode($id) . "\n您是" . $level_str . ",链接已创建!\n过期时间:" . date('Y-m-d H:i', $due_at);
             }
             $rs['content'] = $out_content;
         } else {
             $rs['content'] = '操作错误';
         }
     }
     $this->reply($rs);
     /*
             $keyword = $keywordMdl->getList($this->_shopId, $kwd); 
             if ($keyword) {
                 $rs = $keyword[0];
             }
             else {
                 $messageMdl = D('Message');
                 $rs = $messageMdl->getRow($this->_shopId, 'message');
             }
             $this->reply($rs);
     */
 }