blib/lib/Circle/User.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Circle::User;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use Circle::Common qw(load_config build_url_template http_json_post http_json_get);
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 our $VERSION = '0.08';
13 our @EXPORT = qw(
14 send_register_verify_code
15 register
16 send_verify_code
17 login
18 logout
19 send_pay_verify_code
20 set_pay_password
21 have_pay_password
22 send_reset_password_verify_code
23 reset_password
24 add_contacts
25 list_contacts
26 save_or_update_user_info
27 get_user_info
28 );
29  
30 sub _build_url_template {
31 my ($path) = @_;
32 return build_url_template( "user", $path );
33 }
34  
35 sub send_register_verify_code {
36 my ($req) = @_;
37 my $url = _build_url_template("sendRegisterVerifyCode");
38 return http_json_post( $url, $req, 0 );
39 }
40  
41 sub register {
42 my ($req) = @_;
43 my $url = _build_url_template("register");
44 return http_json_post( $url, $req, 0 );
45 }
46  
47 sub send_verify_code {
48 my ($req) = @_;
49 my $url = _build_url_template("sendVerifyCode");
50 return http_json_post( $url, $req, 0 );
51 }
52  
53 sub login {
54 my ($req) = @_;
55 my $url = _build_url_template("login");
56 my $response = http_json_post( $url, $req, 0 );
57 if ( $response->{status} == 200 ) {
58 my $login_data = $response->{data};
59 _save_session_data( $req, $login_data );
60 }
61  
62 return $response;
63 }
64  
65 sub _save_session_data {
66 my ( $req, $login_data ) = @_;
67 my $user_type = defined $req->{email} ? 1 : 0;
68 my $user_key = defined $req->{email} ? 'email' : 'phone';
69 my $user_value = defined $req->{email} ? $req->{email} : $req->{phone};
70 my $user_id = $login_data->{userId};
71 my $session_key = $login_data->{sessionKey};
72 my $config = load_config();
73 my $session_path = $config->{user}->{sessionPath};
74 my $full_path = $ENV{HOME} . '/' . $session_path;
75  
76 if ( not -f $full_path ) {
77 my $dirname = dirname($full_path);
78 make_path($dirname);
79 }
80 my $content = qq(
81 ##### user login information
82 userType=$user_type
83 userId=$user_id
84 $user_key=$user_value
85 sessionKey=$session_key
86 );
87 open my $fd, ">:encoding(utf8)", $full_path;
88  
89 print $fd $content;
90 close($fd);
91 chmod 0600, $full_path;
92 }
93  
94 sub logout {
95 my $url = _build_url_template("logout");
96 return http_json_post( $url, {} );
97 }
98  
99 sub send_pay_verify_code {
100 my ($req) = @_;
101 my $url = _build_url_template("sendPayVerifyCode");
102 return http_json_post( $url, $req );
103 }
104  
105 sub set_pay_password {
106 my ($req) = @_;
107 my $url = _build_url_template("setPayPassword");
108 return http_json_post( $url, $req );
109 }
110  
111 sub have_pay_password {
112 my $url = _build_url_template("havePayPassword");
113 return http_json_get($url);
114 }
115  
116 sub send_reset_password_verify_code {
117 my ($req) = @_;
118 my $url = _build_url_template("sendResetPasswordVerifyCode");
119 return http_json_post( $url, $req, 0 );
120 }
121  
122 sub reset_password {
123 my ($req) = @_;
124 my $url = _build_url_template("resetPassword");
125 return http_json_post( $url, $req, 0);
126 }
127  
128 sub add_contacts {
129 my ($req) = @_;
130 my $url = _build_url_template("addContacts");
131 return http_json_post( $url, $req );
132 }
133  
134 sub list_contacts {
135 my $url = _build_url_template("listContacts");
136 return http_json_get($url);
137 }
138  
139 sub save_or_update_user_info {
140 my ($req) = @_;
141 my $url = _build_url_template("saveOrUpdateUserInfo");
142 return http_json_post( $url, $req );
143 }
144  
145 sub get_user_info {
146 my $url = _build_url_template("userInfo");
147 return http_json_get($url);
148 }
149  
150 1;
151  
152 __END__
↑ Summary