3
3
import android .app .Activity ;
4
4
import android .graphics .Bitmap ;
5
5
import android .os .Bundle ;
6
- import android .support .annotation .NonNull ;
7
6
import android .support .annotation .Nullable ;
8
7
import android .text .TextUtils ;
9
8
import android .view .View ;
26
25
import com .rae .cnblogs .widget .webclient .RaeWebViewClient ;
27
26
import com .tencent .bugly .crashreport .CrashReport ;
28
27
29
- import io .reactivex .Observable ;
30
28
import io .reactivex .ObservableSource ;
31
- import io .reactivex .android .schedulers .AndroidSchedulers ;
32
- import io .reactivex .functions .Consumer ;
33
29
import io .reactivex .functions .Function ;
34
- import io .reactivex .schedulers .Schedulers ;
35
30
36
31
/**
37
32
* 网页登录
38
33
* Created by ChenRui on 2017/2/3 0003 12:01.
39
34
*/
40
35
public class WebLoginFragment extends WebViewFragment {
41
36
42
-
43
- /**
44
- * 网页登录回调
45
- */
46
- public interface WebLoginListener {
47
-
48
- /**
49
- * 当网页加载完毕触发
50
- */
51
- void onWebLoadingFinish ();
52
-
53
- /**
54
- * 当验证码发生错误的时候触发
55
- *
56
- * @param url 新的验证码图片地址
57
- */
58
- void onLoginVerifyCodeError (String url );
59
-
60
- /**
61
- * 当网页需要验证码的时候触发
62
- *
63
- * @param url 验证码图片地址
64
- */
65
- void onNeedVerifyCode (String url );
66
-
67
- /**
68
- * 登录错误时候触发
69
- *
70
- * @param msg 错误消息
71
- */
72
- void onLoginError (String msg );
73
-
74
- /**
75
- * 登录中
76
- *
77
- * @param msg 消息
78
- */
79
- void onLoggingIn (String msg );
80
-
81
- /**
82
- * 登录成功
83
- *
84
- * @param data 用户信息
85
- */
86
- void onLoginSuccess (UserInfoBean data );
87
- }
88
-
89
-
90
37
private String mBlogApp ;
91
38
92
39
public static WebLoginFragment newInstance (String url ) {
@@ -100,10 +47,6 @@ public static WebLoginFragment newInstance(String url) {
100
47
private IUserApi mUserApi ;
101
48
102
49
private PlaceholderView mPlaceholderView ;
103
- private WebLoginListener mWebLoginListener ; // 登录回调
104
-
105
- // 网页是否加载完毕
106
- private boolean mIsLoadFinish ;
107
50
108
51
@ Override
109
52
public void onCreate (@ Nullable Bundle savedInstanceState ) {
@@ -118,9 +61,6 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
118
61
@ Override
119
62
public void onActivityCreated (@ Nullable Bundle savedInstanceState ) {
120
63
super .onActivityCreated (savedInstanceState );
121
- if (getActivity () instanceof WebLoginListener ) {
122
- mWebLoginListener = (WebLoginListener ) getActivity ();
123
- }
124
64
}
125
65
126
66
@ Override
@@ -139,71 +79,22 @@ public void onClick(View view) {
139
79
parent .addView (mPlaceholderView );
140
80
}
141
81
142
-
143
82
@ Override
144
83
public void onDestroy () {
145
84
RxObservable .dispose ("user" );
146
85
super .onDestroy ();
147
86
}
148
87
88
+ private void perfromLogin () {
89
+ // 同步COOKIE
90
+ UserProvider .getInstance ().syncFormWebview ();
149
91
150
- /**
151
- * 执行登录
152
- *
153
- * @param username 用户名
154
- * @param password 密码
155
- * @param verifyCode 验证码
156
- */
157
- public void performLogin (@ NonNull final String username , @ NonNull final String password , @ Nullable final String verifyCode ) {
158
-
159
-
160
- Observable .just (1 )
161
- .subscribeOn (Schedulers .newThread ())
162
- .map (new Function <Integer , Integer >() {
163
- @ Override
164
- public Integer apply (Integer integer ) throws Exception {
165
- long startTimeMillis = System .currentTimeMillis ();
166
- while (!mIsLoadFinish ) {
167
- // 网页加载超时
168
- if ((System .currentTimeMillis () - startTimeMillis ) > 15 * 1000 )
169
- return -1 ;
170
- }
171
-
172
- return 1 ;
173
- }
174
- })
175
- .observeOn (AndroidSchedulers .mainThread ())
176
- .subscribe (new Consumer <Integer >() {
177
- @ Override
178
- public void accept (Integer integer ) throws Exception {
179
- if (mWebLoginListener == null ) return ;
180
- if (integer == -1 ) {
181
- mWebLoginListener .onLoginError ("网页登录加载超时,请检查网络连接" );
182
- return ;
183
- }
184
-
185
-
186
- String js = "javascript:rae.login('@u','@p','@c')"
187
- .replace ("@u" , username )
188
- .replace ("@p" , password )
189
- .replace ("@c" , verifyCode == null ? "" : verifyCode );
190
-
191
- mWebView .loadUrl (js );
192
- }
193
- });
92
+ // 获取用户信息
93
+ loadUserInfo ();
194
94
}
195
95
196
- /**
197
- * 加载用户信息
198
- */
199
96
private void loadUserInfo () {
200
-
201
- String msg = getString (R .string .loading_user_info );
202
- mPlaceholderView .loading (msg );
203
-
204
- if (mWebLoginListener != null )
205
- mWebLoginListener .onLoggingIn (msg );
206
-
97
+ mPlaceholderView .loading (getString (R .string .loading_user_info ));
207
98
RxObservable .create (mUserApi .getUserBlogAppInfo (), "user" )
208
99
.flatMap (new Function <UserInfoBean , ObservableSource <UserInfoBean >>() {
209
100
@ Override
@@ -229,20 +120,14 @@ private String getLog(Throwable e) {
229
120
230
121
@ Override
231
122
protected void onError (String message ) {
232
- notifyLoginError (message );
233
- }
234
-
235
- private void notifyLoginError (String message ) {
236
123
mPlaceholderView .retry (message );
237
- if (mWebLoginListener != null )
238
- mWebLoginListener .onLoginError (message );
239
124
}
240
125
241
126
@ Override
242
127
protected void accept (UserInfoBean data ) {
243
128
mPlaceholderView .dismiss ();
244
129
if (TextUtils .isEmpty (data .getUserId ())) {
245
- notifyLoginError ("获取用户信息失败,该用户没有用户ID " );
130
+ mPlaceholderView . retry ("获取用户信息失败" );
246
131
AppMobclickAgent .onLoginEvent (getContext (), "ERROR" , false , "没有获取到用户ID" );
247
132
return ;
248
133
}
@@ -254,12 +139,6 @@ protected void accept(UserInfoBean data) {
254
139
255
140
UserProvider .getInstance ().setLoginUserInfo (data );
256
141
AppUI .success (getContext (), R .string .login_success );
257
-
258
- // 通知成功
259
- if (mWebLoginListener != null ) {
260
- mWebLoginListener .onLoginSuccess (data );
261
- }
262
-
263
142
getActivity ().setResult (Activity .RESULT_OK );
264
143
getActivity ().finish ();
265
144
}
@@ -273,7 +152,6 @@ public WebViewClient getWebViewClient() {
273
152
@ Override
274
153
public void onPageStarted (WebView view , String url , Bitmap favicon ) {
275
154
super .onPageStarted (view , url , favicon );
276
- mIsLoadFinish = false ;
277
155
if (!TextUtils .isEmpty (url ) && url .contains ("home.cnblogs.com" )) {
278
156
mPlaceholderView .loading (getString (R .string .loading_user_info ));
279
157
}
@@ -282,18 +160,15 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
282
160
@ Override
283
161
public void onPageFinished (WebView view , String url ) {
284
162
super .onPageFinished (view , url );
285
- injectJavascriptFromAssets (view , "js/rae-login.js" );
286
- mIsLoadFinish = true ;
287
163
String cookie = CookieManager .getInstance ().getCookie (url );
288
164
289
165
// 登录成功
290
166
if (cookie != null && cookie .contains (".CNBlogsCookie" )) {
291
- // 同步COOKIE
292
- UserProvider .getInstance ().syncFormWebview ();
293
- loadUserInfo ();
167
+ perfromLogin ();
294
168
}
295
169
}
296
170
171
+
297
172
};
298
173
}
299
174
}
0 commit comments