mojo 接口示例

<pre name="code" class="python">use Mojolicious::Lite;
use JSON qw/encode_json decode_json/;  
use Encode;
no strict;
no warnings;
use JSON; 
use POSIX;
# /foo?user=sri
 get '/api/env' => sub {
   my $c = shift;
     my $env = $c->param('env');
     my  $ip = $c->param('ip');
      
     if ($env){
              use DBI;
my $dbName = 'oadb';  
my $dbUser = 'query';  
my $dbUserPass = 'kjk7787czcb';
my @arr2="";  
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
my $hostSql = qq{select  IP,INFO,ENV from  machine_info  where ENV='env-$env' }; 
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);  
my $selStmt = $dbh->prepare($hostSql);  
$selStmt->bind_columns( \$a1, \$a2, \$a3);  
$selStmt->execute();  
while( $selStmt->fetch() ){ push (@arr2, "$a1  $a2  $a3" );

       };
         $c->render(json => \@arr2);
}elsif($ip){

use DBI;  
my $dbName = 'oadb';  
my $dbUser = 'query';  
my $dbUserPass = 'kjk7787czcb';
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ;
my $hostSql = qq{select  IP,INFO,ENV from  machine_info  where IP='$ip' };   
my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9); 
my @arr2="";
my $selStmt = $dbh->prepare($hostSql);  
$selStmt->bind_columns(undef, \$a1, \$a2, \$a3);  
$selStmt->execute();  
while( $selStmt->fetch() ){ 
push (@arr2, "$a1  $a2  $a3" )
};
 #sleep (30);
 $c->render(json => \@arr2);
 }};

 get '/api/bus' => sub {
my $c = shift;
    my  $bus= $c->param('bus');
     my  $flag=$c->param('flag');
 if( ($bus)  || ($flag) ) {
    use URI::Escape;
use Encode;
use JSON qw/encode_json decode_json/;
my $area="杭州";
$area=uri_escape_utf8("$area"); 
use LWP::UserAgent;
my  $ua = LWP::UserAgent->new;
my  @header = (
    'accept'=> "application/json",
    'content-type'=> "application/json",
    'apikey'=> "3074a7f9926a4bbb484aa8bb366e5b87",
    'User-Agent' => "Mozilla/8.0"
    );
my $url = "http://apis.baidu.com/xiaota/bus_lines/buses_lines?city=$area&bus=$bus&direction=$flag";
my $request = HTTP::Request->new(GET=>"$url");
$request->header(@header);
my $response = $ua->request($request); 
 $content= $response->decoded_content;
 $content =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg; 
 $content  = encode_utf8($content);
my $json_out = $content ; 

$json_out = decode_json($content);

@bus="";
#
foreach (@{($json_out->{data}->{stations})}){$bus="$_->{stateName}";push (@bus ,$bus)};
#$client->reply_msg($msg,"@bus")
print "\@bus is @bus\n";
$c->render(json => \@bus);

                   };
				   };

 get '/api/ipsearch' => sub {
 my $c = shift;
  my  $ip=$c->param('ip');
  if ($ip){
use LWP::UserAgent;
my  $ua = LWP::UserAgent->new;

 my $host = "http://ip.taobao.com/service/getIpInfo.php?ip=$ip";
my $request = HTTP::Request->new(GET=>"$host");
my $response = $ua->request($request);
my  $content= $response->decoded_content;
 $content =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;  
  $content=encode(utf8,$content);
  $json_out       = decode_json($content);
 
 $c->render(json =>  $json_out->{data} );
				   };
				    };

get '/api/update' => sub {
my $dbName = 'oadb';  
my $dbUser = 'query';  
my $dbUserPass = 'kjk7787czcb';
my  $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass,{
                          RaiseError => 1,
                          AutoCommit => 0
                        }) or die "can't connect to database " ;
   my $c = shift;
my  $id=$c->param('id');
eval{
$dbh->do("update query.test set id=100 where id =$id") or $!;
$dbh->commit();
 $c->render(text =>  "update successed" );

};

if( $@ ) {
   #warn "Database error: $DBI::errstr\n";
    	 $dbh->rollback(); #just die if rollback is failing 
          $c->render(text =>  "update failed" );
    		 	 };
};
				   
get '/api/getcode' => sub{
  my $c = shift;
  my $env = $c->param('env');
  my  $phone = $c->param('phone');
  print "\$env is $env\n";
  print "\$phone is $phone\n";
  

  
  if ($env eq "zj"){
@arr1=();
@arr2=();
$str="";
@arr_out=();
@arr=();

  my $starttime=strftime("%Y%m%d%H%M%S",localtime());
print "starttime is $starttime\n";
open (LOG1 ,"<",'/data01/applog_backup/zj-api01-catalina.out') or die  $!;
                     while (<LOG1>) {
					#2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed!
								 
					if ( ($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/) ){$str=$_;
					
					my $d=encode_utf8('验证');
					next if ($str !~ /$d/);
					print "\$str is $str\n";
					push (@arr1 ,$str);
					}};
					
					close LOG1;
					
				
					
					open (LOG2 ,"<",'/data01/applog_backup/zj-api02-catalina.out') or die  $!;
					while (<LOG2>) {
                  
					 
					if (($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/)  ){$str= $_; 
					
					my $d=encode_utf8('验证');
					next if ($str !~ /$d/);
					print "\$str is $str\n";
					push (@arr2  ,$str);
					}};
					
					close LOG2;
					
		my @arr=($arr1[-1], $arr2[-1]);			
			print "\@arr ---> @arr\n";		

if  (($arr1[-1]) or  ($arr2[-1])){

foreach (@arr){  
##2015-11-04 15:51:54,134 DEBUG com.zjzc.thirdparty.sms.SMSSendThread 

##2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed!
   if ($_ =~  /([0-9]{4}-[0-9]{1,2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}:[0-9]{2}.*?)\s+(.*?Send message:)\s*(.*?)(to\s+phoneNo:\s*[0-9]{11}).*/){
	
	$a1=$1;
	$a2=$2;
	$a3=$3;
	$a4=$4;
    
	##微信是UTF-8字符集
	
	
	my 	$endtime=strftime("%Y%m%d%H%M%S",localtime());
	
	my $d=encode_utf8('验证');
	if ($a3 =~/$d/){ 
	#$msg->reply("\@$friend $a1-$a3-$a4");
	print "come in\n";
	
	$a3=decode_utf8("$a3");
	##encode("utf8",decode("gbk","格式为:中均/温金 客户手机号"))
	 #@content=($a1,$a3,$a4);
	 my @content=("$a1","$a3","$a4");
	 my $href= \@content;
	 push (@arr_out ,$href);
	
	
	print "endtime is $endtime\n";
                  }
				                              };
           };
    $c->render(json =>  \@arr_out );
	
                                      }
else{ $a5="$phone--ZJ没有找到需要的验证码信息";

$c->render(text =>  "$a5" );

   }
}elsif($env eq "wj"){
@arr1=();
@arr2=();
$str="";
@arr_out=();
@arr=();

my $starttime=strftime("%Y%m%d%H%M%S",localtime());
print "starttime is $starttime\n";
open (LOG1 ,"<",'/data01/applog_backup/wj-api01-catalina.out') or die $!;
                                 while (<LOG1>) {
								#2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed!
								 
					if ( ($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/) ){$str=$_;
					my $d=encode_utf8('验证');
						 next if ($str !~ /$d/);
						 print "\$str is $str\n";
					push (@arr1 ,$str);
					}};
					
					close LOG1;
					
				
					
					open (LOG2 ,"<",'/data01/applog_backup/wj-api02-catalina.out') or die  $!;
					while (<LOG2>) {
                  
					 
					if ( ($phone) and ($_ =~/$phone/) and ($_ =~/SMSUtils/)  ){$str=$_; 
					my $d=encode_utf8('验证');
							 next if ($str !~ /$d/);
							  print "\$str is $str\n";
					push (@arr2  ,$str);
					}};
					
					close LOG2;
					
		my @arr=($arr1[-1], $arr2[-1]);			
			print "\@arr ---> @arr\n";		

if  (($arr1[-1]) or  ($arr2[-1])){

foreach (@arr){  
##2015-11-04 15:51:54,134 DEBUG com.zjzc.thirdparty.sms.SMSSendThread 

##2016-03-09 09:35:12,380 INFO com.zjzc.thirdparty.sms.SMSUtils - Send message:您的验证码为132435,本验证码5分钟内有效。 to phoneNo:18072722237 succeed!
   if ($_ =~  /([0-9]{4}-[0-9]{1,2}-[0-9]{2}\s+[0-9]{2}:[0-9]{2}:[0-9]{2}.*?)\s+(.*?Send message:)\s*(.*?)(to\s+phoneNo:\s*[0-9]{11}).*/){
	
	$a1=$1;
	$a2=$2;
	$a3=$3;
	$a4=$4;
    
	##微信是UTF-8字符集
	
	
	my 	$endtime=strftime("%Y%m%d%H%M%S",localtime());
	
	my $d=encode_utf8('验证');
	if ($a3 =~/$d/){ 
	#$msg->reply("\@$friend $a1-$a3-$a4");
	print "come in\n";
	
	$a3=decode_utf8("$a3");
	##encode("utf8",decode("gbk","格式为:中均/温金 客户手机号"))
	 #@content=($a1,$a3,$a4);
	 my @content=($a1,$a3,$a4);
	 my $href= \@content;
	 push (@arr_out ,$href);
	# print "\@arr_out is @arr_out\n";
	#$c->render(text =>  ("$a1,$a3,$a4") );
	
	print "endtime is $endtime\n";
                  }
				                              };
           };
    $c->render(json =>  \@arr_out );
	
                                      }
else{ $a5="$phone--WJ没有找到需要的验证码信息";

$c->render(text =>  "$a5" );

   }
  

               }
  

};

  app->start;






   

posted @ 2016-03-25 16:58  czcb  阅读(165)  评论(0编辑  收藏  举报