ae838ed4414201bfee3a05e042c846ff1c8f1014.svn-base
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* @author 郎咸智
* 上午11:48:18
*/
package com.ivt.android.me.model.main;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.ivt.android.me.api.UserApiBean;
import com.ivt.android.me.bean.BaseBean;
import com.ivt.android.me.bean.ChatDBEntity;
import com.ivt.android.me.bean.UserBean;
import com.ivt.android.me.bean.UserEntity;
import com.ivt.android.me.bean.WeiBoEntity;
import com.ivt.android.me.bean.evenbus.CodeBean;
import com.ivt.android.me.constant.BaseInfo;
import com.ivt.android.me.constant.CodeUtils;
import com.ivt.android.me.utils.httputil.HttpUtils;
import com.ivt.android.me.utils.httputil.HttpUtils.HttpCallback;
import com.ivt.android.me.utils.httputil.JsonUtils;
import com.ivt.android.me.utils.publics.AuthLinstener;
import com.ivt.android.me.utils.publics.DateUtils;
import com.ivt.android.me.utils.publics.DbUtil;
import com.ivt.android.me.utils.publics.LocationUserUtil;
import com.ivt.android.me.utils.publics.LogUtil;
import com.ivt.android.me.utils.publics.MyToastUtils;
import com.ivt.android.me.utils.publics.PhoneNumUtils;
import com.ivt.android.me.utils.publics.SharePreferenceUtil;
import com.ivt.android.me.utils.publics.StringUtils;
import com.ivt.android.me.utils.xmpp.XmppConnectionTool;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.client.HttpRequest;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;
import com.tencent.connect.common.Constants;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
public class ModleLogin implements Ilogin {
private Context context;
/** 获取验证码成功 */
private final int GETCODESUCCESS = 1000;
/** 获取验证码失败 */
private final int GETCODEFail = -1;
/** 手机号登陆成功 */
private final int PHONELOGINSUCCESS = 1001;
/** 手机号登陆失败 */
private final int PHONELOGINFAIL = -2;
/** 登录中 */
private final int LOGINING = 1002;
/** 判定手机号 */
private final int BANGPHONENUM = 1003;
/** 登录XMPP成功 */
private final int LOGINXMPPSUCCESS = 1008;
/** 登录XMPP失败 */
private final int LOGINXMPPFAIL = 1009;
private Message msg;
private Tencent mTencent;
private String openId;
/** 注意:SsoHandler 仅当 SDK 支持 SSO 时有效 */
private SsoHandler mSsoHandler;
private AuthInfo mAuthInfo;
// 微信登录
private IWXAPI api;
private Handler h;
public ModleLogin(Context context, Handler h,SsoHandler mSsoHandler) {
// TODO Auto-generated constructor stub
this.context = context;
this.h = h;
this.mSsoHandler=mSsoHandler;
// 配置IM
api = WXAPIFactory.createWXAPI(this.context, BaseInfo.WEIXIN_APP_ID,
false);
api.registerApp(BaseInfo.WEIXIN_APP_ID);
}
@Override
public void LoginXmpp(String userId, final String Captcha,
final UserEntity user) {
// TODO Auto-generated method stub
new Thread() {
public void run() {
if (user.getCaptcha().length() <= 0) {
user.setCaptcha(Captcha);
}
Boolean b = XmppConnectionTool.getInstance().intent(user);
if (b) {
h.sendEmptyMessage(LOGINXMPPSUCCESS);
} else {
h.sendEmptyMessage(LOGINXMPPFAIL);
}
};
}.start();
}
@Override
public void LoginMe(final String PhoneNum, final String Captcha) {
// TODO Auto-generated method stub
if (PhoneNumUtils.judgePhoneNums(PhoneNum)) {
if (PhoneNumUtils.isYanZM(Captcha, 4, context)) {
String url = UserApiBean.Login(PhoneNum, Captcha);
HttpUtils.getbase(url, new HttpCallback() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
h.sendEmptyMessage(LOGINING);
}
@Override
public void onError(String msg) {
// TODO Auto-generated method stub
super.onError(msg);
h.sendEmptyMessage(PHONELOGINFAIL);
}
@Override
public void onSuccess(String data) {
// TODO Auto-generated method stub
UserBean bean = JsonUtils.deserialize(data,
UserBean.class);
int code = bean.getCode();
if (code == 0) {
UserEntity user = bean.getData();
user.setCaptcha(Captcha);
user.setMsisdn(PhoneNum);
BaseInfo.timeDiff = user.getServertime()
- DateUtils.getCurrentTime();
// user.setToken(MD5Util.GetMD5Code(user.getServertime()));
SaveInfo(user);
Bundle b = new Bundle();
b.putSerializable("user", user);
msg = new Message();
msg.what = PHONELOGINSUCCESS;
msg.obj = b;
h.sendMessage(msg);
} else {
h.sendEmptyMessage(PHONELOGINFAIL);
}
}
});
} else {
MyToastUtils.showToast(context, "请输入有效的验证码");
}
}
}
private void SaveInfo(UserEntity user) {
// 是否需要清除数据库
checkChatSql(user.getId());
BaseInfo.Captcha = user.getCaptcha();
BaseInfo.UserId = user.getId() + "";
BaseInfo.base_user = user;
LocationUserUtil.SaveUser(user);
SharePreferenceUtil.setValue(context, "XmppDbId", user.getId());
}
@Override
public void GetCode(String PhoneNum) {
// TODO Auto-generated method stub
String url = UserApiBean.sendsms(PhoneNum);
HttpUtils.getbase(url, new HttpCallback() {
@Override
public void onSuccess(String data) {
// TODO Auto-generated method stub
BaseBean base = JsonUtils.deserialize(data, BaseBean.class);
LogUtil.e(data);
int code = base.getCode();
if (code == 0) {
h.sendEmptyMessage(GETCODESUCCESS);
} else {
h.sendEmptyMessage(GETCODEFail);
}
}
@Override
public void onError(String msg) {
// TODO Auto-generated method stub
super.onError(msg);
h.sendEmptyMessage(GETCODEFail);
}
});
}
@Override
public void iSBangThird(final String value, final String thirdPartyAccount) {
// TODO Auto-generated method stub
String requestUrl = UserApiBean
.thridpartLogin(value, thirdPartyAccount);
HttpUtils.get(requestUrl, new HttpCallback() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
h.sendEmptyMessage(LOGINING);
}
@Override
public void onError(String msg) {
// TODO Auto-generated method stub
super.onError(msg);
h.sendEmptyMessage(PHONELOGINFAIL);
}
@Override
public void onSuccess(String data) {
// TODO Auto-generated method stub
UserBean bean = JsonUtils.deserialize(data, UserBean.class);
int code = bean.getCode();
if (code == 1) {
msg = new Message();
Bundle b = new Bundle();
b.putString("thirdPartyAccount", thirdPartyAccount);
b.putString("value", value);
msg.obj = b;
msg.what = BANGPHONENUM;
h.sendMessage(msg);
// MainApplication.finishActivity();
} else if (code == 0) {
UserEntity user = bean.getData();
Bundle b = new Bundle();
b.putSerializable("user", user);
BaseInfo.timeDiff = user.getServertime()
- DateUtils.getCurrentTime();
SaveInfo(user);
msg = new Message();
msg.what = PHONELOGINSUCCESS;
msg.obj = b;
h.sendMessage(msg);
}
}
});
}
@SuppressWarnings("deprecation")
@Override
public void GetQQInfo(Intent intent) {
mTencent.handleLoginData(intent, iulin);
}
@Override
public void CallThirdLogin(int value) {
// TODO Auto-generated method stub
switch (value) {
case 1:
h.sendEmptyMessage(LOGINING);
mTencent = Tencent.createInstance(BaseInfo.QQ_mAppid, context);
mTencent.login((Activity) context, "all", iulin);
break;
case 2:
// h.sendEmptyMessage(LOGINING);
SendAuth.Req req = new SendAuth.Req();
req.scope = "snsapi_userinfo";
req.state = "wechat_sdk_demo_test";
api.sendReq(req);
break;
case 3:
AuthLinstener auth = new AuthLinstener(h, new HttpCallback() {
@Override
public void onSuccess(String data) {
// TODO Auto-generated method stub
WeiBoEntity userb = JsonUtils.deserialize(data,WeiBoEntity.class);
openId = userb.getId() + "";
iSBangThird("SINA", openId);
}
});
mSsoHandler.authorize(auth);
break;
}
}
/**************** QQ监听 *******************/
IUiListener iulin = new IUiListener() {
@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(Object response) {
// TODO Auto-generated method stub
if (null == response) {
return;
}
JSONObject jsonResponse = (JSONObject) response;
if (null != jsonResponse && jsonResponse.length() == 0) {
return;
}
try {
String token = jsonResponse
.getString(Constants.PARAM_ACCESS_TOKEN);
String expires = jsonResponse
.getString(Constants.PARAM_EXPIRES_IN);
openId = jsonResponse.getString(Constants.PARAM_OPEN_ID);
if (!StringUtils.isEmpty(token)
&& !StringUtils.isEmpty(expires)
&& !StringUtils.isEmpty(openId)) {
mTencent.setAccessToken(token, expires);
mTencent.setOpenId(openId);
iSBangThird("QQ", openId);
// updateUserInfo(mTencent, openId, h);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
h.sendEmptyMessage(PHONELOGINFAIL);
}
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
h.sendEmptyMessage(PHONELOGINFAIL);
}
};
public void onEventMainThread(CodeBean code) {
switch (code.getCode()) {
case CodeUtils.LOGINWEIXINSU:
openId = (String) code.getOther();
iSBangThird("WECHAT", openId);
break;
case CodeUtils.LOGINWEIXINFA:
h.sendEmptyMessage(PHONELOGINFAIL);
break;
default:
break;
}
}
/**
* 检查是否应该清除聊天记录
*
* @param userId
* 当前登录账号的 id
*/
public void checkChatSql(int userId) {
int oldId = SharePreferenceUtil.getInt(context, "XmppDbId", 0);
if (oldId != userId) {
DbUtil db = new DbUtil(context);
DbUtils dbUtils = db.getDbUtils();
try {
dbUtils.deleteAll(ChatDBEntity.class);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}