| 1 | package Mojo::Webqq::Plugin::UploadQRcode2; | ||
| 2 | our $CALL_ON_LOAD=1; | ||
| 3 | use strict; | ||
| 4 | use Mojo::Util (); | ||
| 5 | use Time::HiRes (); | ||
| 6 | use Digest::SHA (); | ||
| 7 | sub call{ | ||
| 8 | my $client = shift; | ||
| 9 | my $data = shift; | ||
| 10 | $client->on(input_qrcode=>sub{ | ||
| 11 | my($client,$qrcode_path,$qrcode_data) = @_; | ||
| 12 | #需要产生随机的云存储路径,防止好像干扰 | ||
| 13 | my $uniq_path = "mojo_webqq_" . substr(Time::HiRes::gettimeofday(),4) . sprintf("%.6f",rand(1)) . ".png"; | ||
| 14 | my $url = upload($client,$data,$uniq_path,$qrcode_data); | ||
| 15 | return if not defined $url; | ||
| 16 | $client->qrcode_upload_url($url); | ||
| 17 | $client->info("二维码已上传云存储[ $url ]"); | ||
| 18 | }); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub upload { | ||
| 22 | my($client,$opt,$name,$data) = @_; | ||
| 23 | my $mydomain = $opt->{mydomain} // "qr.perfi.wang"; | ||
| 24 | my $appid = $opt->{appid} // 10063136; | ||
| 25 | my $bucket = $opt->{bucket} // 'qr'; | ||
| 26 | my $secretid = $opt->{secretid} // 'AKIDGfoZzPrHrWW98rqFbCF5EHP0DenTqO4N'; | ||
| 27 | my $secretkey = $opt->{secretkey} // 'eT2sPJnvXQ3IGF4yaaBLGkOXDVAsEqlo'; | ||
| 28 | my $now = time; | ||
| 29 | my $expire = $opt->{expire} // 120; | ||
| 30 | $expire = $now + $expire; | ||
| 31 | my $rand = int rand 1000000; | ||
| 32 | |||
| 33 | my $fileid = Mojo::Util::url_escape("/$appid/$bucket/$name"); | ||
| 34 | $fileid=~s/%2F/\//g; | ||
| 35 | my $orignal = "a=$appid&b=$bucket&k=$secretid&e=$expire&t=$now&r=$rand&f=$fileid"; | ||
| 36 | my $signtemp = Digest::SHA::hmac_sha1($orignal,$secretkey); | ||
| 37 | my $sign = Mojo::Util::b64_encode($signtemp . $orignal,""); | ||
| 38 | |||
| 39 | my $json = $client->http_post("http://web.file.myqcloud.com/files/v1/$appid/$bucket/$name", | ||
| 40 | { Authorization=>$sign, json=>1 ,ua_debug_req_body=>0}, | ||
| 41 | form=>{ | ||
| 42 | op=>'upload', | ||
| 43 | insertOnly=>1, | ||
| 44 | filecontent=>{filename=>$name,content=>$data}, | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | if(not defined $json){ | ||
| 48 | $client->warn("二维码图片上传云存储失败: 响应数据异常"); | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | elsif(defined $json and $json->{code} != 0 ){ | ||
| 52 | $client->warn("二维码图片上传云存储失败: " . $json->{message}); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $url = $json->{data}{source_url}; | ||
| 57 | $url=~s/(^https?:\/\/)([^\/]+)(.*)/$1$mydomain$3/ if (defined $url and defined $mydomain); | ||
| 58 | if(not defined $url){ | ||
| 59 | $client->warn("二维码图片上传云存储失败:未获取到有效地址"); | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | return $url; | ||
| 63 | } | ||
| 64 | 1; |