Weixin/Plugin/PostQRcode.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 53 CC 7 SCAR 32.9
CC
7
Cov
0%
call
42.0
BEGIN
6.0
BEGIN
2.0
CRAP 26.8
1 package Mojo::Weixin::Plugin::PostQRcode;
2 our $PRIORITY = 0;
3 our $CALL_ON_LOAD = 1;
4 use MIME::Base64;
5 BEGIN{
6 our $has_mime_lite = 0;
7 eval{require MIME::Lite;};
8 $has_mime_lite = 1 if not $@;
9 }
10 sub call{
11 my $client = shift;
12 my $data = shift;
13 $client->die("插件[". __PACKAGE__ ."]依赖模块 MIME::Lite,请先确认该模块已经正确安装") if not $has_mime_lite;
14 $data->{max} = 10 if not defined $data->{max};
15 #$data->{charset} = "UTF-8" if not defined $data->{charset};
16 my $count = 0;
17 $client->on(login=>sub{$count = 0});
18 $client->on(input_qrcode=>sub{
19 my($client,$filename) = @_;
20 if($count > $data->{max}){
21 $client->fatal("等待扫描二维码超时");
22 $client->stop();
23 return
24 }
25 $data->{subject} = "微信扫描二维码" if not defined $data->{subject};
26 my $mime = MIME::Lite->new(
27 Type => 'multipart/mixed',
28 From => $data->{from},
29 To => $data->{to},
30 );
31 $mime->add("Subject"=>"=?UTF-8?B?" . MIME::Base64::encode_base64($data->{subject},"") . "?=");
32 $mime->attach(
33 Type =>"text/plain; charset=UTF-8",
34 Data =>"请使用手机微信扫描附件中的二维码",
35 );
36 $mime->attach(
37 Path => $filename,
38 Disposition => 'attachment',
39 Type => 'image/jpg',
40 );
41 $data->{data} = $mime->as_string;
42 my($is_success,$err) = $client->mail(%$data);
43 if(not $is_success){
44 $client->error("插件[".__PACKAGE__."]邮件发送失败: $err");
45 }
46 else{
47 $client->info("登录二维码已经发送到邮箱: $data->{to}");
48 }
49 $count++;
50 });
51 }
52 1;