Commit d177b23c99ccee93bae426c836f150f67531cdb1

Authored by 陶汉栋
2 parents e7ab7c8e a4167d2d

Merge branch 'developer' of http://git.shunzhi.net/taohd/parentwork into developer

app/src/main/java/com/shunzhi/parent/AppContext.java
1 1 package com.shunzhi.parent;
2 2  
  3 +import android.content.Context;
3 4 import android.content.Intent;
  5 +import android.content.SharedPreferences;
4 6 import android.util.Log;
5 7  
6 8 import com.amap.api.location.AMapLocation;
7 9 import com.amap.api.location.AMapLocationClient;
8 10 import com.amap.api.location.AMapLocationClientOption;
9 11 import com.amap.api.location.AMapLocationListener;
  12 +import com.netease.nimlib.sdk.NIMClient;
  13 +import com.netease.nimlib.sdk.SDKOptions;
  14 +import com.netease.nimlib.sdk.StatusBarNotificationConfig;
  15 +import com.netease.nimlib.sdk.auth.LoginInfo;
10 16 import com.share.mvpsdk.global.GlobalApplication;
11 17 import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
12 18  
... ... @@ -17,9 +23,9 @@ import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
17 23 public class AppContext extends GlobalApplication {
18 24 private static AppContext appContext;
19 25  
20   - public final static int LOCATION_CITYNAME=0x00;
  26 + public final static int LOCATION_CITYNAME = 0x00;
21 27  
22   - public String cityName="",district="";
  28 + public String cityName = "", district = "";
23 29  
24 30 public static AppContext getInstance() {
25 31 return appContext;
... ... @@ -29,14 +35,39 @@ public class AppContext extends GlobalApplication {
29 35 public AMapLocationClient mLocationClient = null;
30 36  
31 37 //声明AMapLocationClientOption对象
32   - public AMapLocationClientOption mLocationOption=null;
  38 + public AMapLocationClientOption mLocationOption = null;
33 39  
34 40 @Override
35 41 public void onCreate() {
36   - appContext=this;
  42 + appContext = this;
37 43 super.onCreate();
38 44 //开启地图地位
39 45 initMapLocal();
  46 + NIMClient.init(this, loginInfo(), options());
  47 + }
  48 +
  49 + private SDKOptions options() {
  50 + SDKOptions options = new SDKOptions();
  51 +
  52 + // 如果将新消息通知提醒托管给 SDK 完成,需要添加以下配置。否则无需设置。
  53 + StatusBarNotificationConfig config = new StatusBarNotificationConfig();
  54 + //config.notificationEntrance = MainActivity.class; // 点击通知栏跳转到该Activity
  55 + config.notificationSmallIconId = R.mipmap.ic_launcher;
  56 + options.sdkStorageRootPath = getExternalFilesDir(null).getPath() + "/nim";
  57 + options.statusBarNotificationConfig = config;
  58 + //options.preloadAttach=false;
  59 + return options;
  60 + }
  61 +
  62 +
  63 + private LoginInfo loginInfo() {
  64 + SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
  65 + String account = sp.getString("account", "");
  66 + String token = sp.getString("token", "");
  67 + if (account.length() > 0 && token.length() > 0) {
  68 + return new LoginInfo(account, token);
  69 + }
  70 + return null;
40 71 }
41 72  
42 73 private void initMapLocal() {
... ... @@ -66,47 +97,47 @@ public class AppContext extends GlobalApplication {
66 97 mLocationOption.setHttpTimeOut(20000);
67 98 //关闭缓存机制
68 99 mLocationOption.setLocationCacheEnable(false);
69   - if (null!=mLocationClient)
  100 + if (null != mLocationClient)
70 101 mLocationClient.setLocationOption(mLocationOption);
71 102 //启动定位
72 103 startLocation();
73 104  
74 105 }
75 106  
76   - public void startLocation(){
  107 + public void startLocation() {
77 108 //启动定位
78   - if (null!=mLocationClient){
  109 + if (null != mLocationClient) {
79 110 // mLocationClient.setLocationOption(mLocationOption);
80 111 //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
81 112 stopLocation();
82 113 mLocationClient.startLocation();
83   - }else initMapLocal();
  114 + } else initMapLocal();
84 115 }
85 116  
86   - public void stopLocation(){
87   - if (null!=mLocationClient)mLocationClient.stopLocation();
  117 + public void stopLocation() {
  118 + if (null != mLocationClient) mLocationClient.stopLocation();
88 119 }
89 120  
90   - public void destoryLocation(){
91   - if (null!=mLocationClient)mLocationClient.onDestroy();
  121 + public void destoryLocation() {
  122 + if (null != mLocationClient) mLocationClient.onDestroy();
92 123 }
93 124  
94 125 //声明定位回调监听器
95 126 public AMapLocationListener mLocationListener = new AMapLocationListener() {
96 127 @Override
97 128 public void onLocationChanged(AMapLocation aMapLocation) {
98   - if(null!=aMapLocation){
  129 + if (null != aMapLocation) {
99 130  
100   - if (aMapLocation.getErrorCode()==0){
101   - cityName=aMapLocation.getCity();
102   - district=aMapLocation.getDistrict();
  131 + if (aMapLocation.getErrorCode() == 0) {
  132 + cityName = aMapLocation.getCity();
  133 + district = aMapLocation.getDistrict();
103 134 aMapLocation.getCityCode();
104   - Intent intent=new Intent();
105   - intent.setAction(LOCATION_CITYNAME+"");
  135 + Intent intent = new Intent();
  136 + intent.setAction(LOCATION_CITYNAME + "");
106 137 sendBroadcast(intent);
107 138 stopLocation();
108   - }else {
109   - cityName="定位失败";
  139 + } else {
  140 + cityName = "定位失败";
110 141 // Log.d("mlocation:","errorCode="+aMapLocation.getErrorCode()+"errorInfo="+aMapLocation.getErrorInfo());
111 142 }
112 143  
... ...
app/src/main/java/com/shunzhi/parent/bean/NIMLoginResultBean.java 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +package com.shunzhi.parent.bean;
  2 +
  3 +/**
  4 + * Created by 10501 on 2018/3/19.
  5 + */
  6 +
  7 +public class NIMLoginResultBean {
  8 + public NIMLoginResultBean() {
  9 + }
  10 +
  11 + public NIMLoginResultBean(boolean isSuccess, int resultCode, Throwable throwable) {
  12 + this.isSuccess = isSuccess;
  13 + this.resultCode = resultCode;
  14 + this.throwable = throwable;
  15 + }
  16 +
  17 + private boolean isSuccess;
  18 + private int resultCode;
  19 + private Throwable throwable;
  20 +
  21 + public boolean isSuccess() {
  22 + return isSuccess;
  23 + }
  24 +
  25 + public void setSuccess(boolean success) {
  26 + isSuccess = success;
  27 + }
  28 +
  29 + public int getResultCode() {
  30 + return resultCode;
  31 + }
  32 +
  33 + public void setResultCode(int resultCode) {
  34 + this.resultCode = resultCode;
  35 + }
  36 +
  37 + public Throwable getThrowable() {
  38 + return throwable;
  39 + }
  40 +
  41 + public void setThrowable(Throwable throwable) {
  42 + this.throwable = throwable;
  43 + }
  44 +}
... ...
app/src/main/java/com/shunzhi/parent/contract/loginandregister/LoginAndRegisterContract.java
... ... @@ -4,6 +4,7 @@ import com.google.gson.JsonObject;
4 4 import com.share.mvpsdk.base.BasePresenter;
5 5 import com.share.mvpsdk.base.IBaseFragment;
6 6 import com.share.mvpsdk.base.IBaseModel;
  7 +import com.shunzhi.parent.bean.NIMLoginResultBean;
7 8 import com.shunzhi.parent.bean.UserInfo;
8 9  
9 10 import io.reactivex.Observable;
... ... @@ -22,6 +23,8 @@ public interface LoginAndRegisterContract {
22 23 public abstract void idCodeResult(String phoneNumber);
23 24  
24 25 public abstract void getUserInfo(String mobile,int school_id,String captcha);
  26 +
  27 + public abstract void nimLogin(String account,String password);
25 28 }
26 29  
27 30 interface ILoginModel extends IBaseModel{
... ... @@ -33,6 +36,8 @@ public interface LoginAndRegisterContract {
33 36  
34 37 //获取登录用户信息
35 38 Observable<UserInfo> getUserInfo(String mobile, int school_id, String captcha);
  39 +
  40 + Observable<NIMLoginResultBean> nimLoginResult(String account, String token);
36 41 }
37 42 interface ILoginView extends IBaseFragment {
38 43  
... ...
app/src/main/java/com/shunzhi/parent/manager/MessageManager.java
1 1 package com.shunzhi.parent.manager;
2 2  
  3 +import android.content.Context;
  4 +import android.content.Intent;
  5 +import android.content.SharedPreferences;
  6 +import android.support.annotation.NonNull;
  7 +
3 8 import com.netease.nimlib.sdk.NIMClient;
4 9 import com.netease.nimlib.sdk.Observer;
  10 +import com.netease.nimlib.sdk.RequestCallback;
  11 +import com.netease.nimlib.sdk.StatusCode;
5 12 import com.netease.nimlib.sdk.auth.AuthService;
  13 +import com.netease.nimlib.sdk.auth.AuthServiceObserver;
6 14 import com.netease.nimlib.sdk.auth.LoginInfo;
7 15 import com.netease.nimlib.sdk.msg.MsgServiceObserve;
8 16 import com.netease.nimlib.sdk.msg.model.CustomNotification;
9 17 import com.netease.nimlib.sdk.msg.model.IMMessage;
  18 +import com.share.mvpsdk.utils.ToastUtils;
  19 +import com.shunzhi.parent.AppContext;
  20 +import com.shunzhi.parent.bean.NIMLoginResultBean;
  21 +import com.shunzhi.parent.ui.activity.LoginAndRegistActivity;
10 22  
11 23 import java.util.List;
12 24  
  25 +import io.reactivex.Observable;
  26 +import io.reactivex.subjects.PublishSubject;
  27 +import timber.log.Timber;
  28 +
13 29 /**
14 30 * Created by 10501 on 2018/3/17.
15 31 */
... ... @@ -20,7 +36,7 @@ public class MessageManager {
20 36 static final MessageManager instance = new MessageManager();
21 37 }
22 38  
23   - public MessageManager getInstance() {
  39 + public static MessageManager getInstance() {
24 40 return InstanceHolder.instance;
25 41 }
26 42  
... ... @@ -41,6 +57,15 @@ public class MessageManager {
41 57 private MessageManager() {
42 58 NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, true);
43 59 NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, true);
  60 + NIMClient.getService(AuthServiceObserver.class).observeOnlineStatus(new Observer<StatusCode>() {
  61 + @Override
  62 + public void onEvent(StatusCode statusCode) {
  63 + if (statusCode==StatusCode.KICKOUT){
  64 + Intent i = new Intent(AppContext.getInstance(), LoginAndRegistActivity.class);
  65 + i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
  66 + }
  67 + }
  68 + },true);
44 69  
45 70 }
46 71  
... ... @@ -52,8 +77,36 @@ public class MessageManager {
52 77  
53 78 }
54 79  
55   - public void login(String account, String password) {
56   - NIMClient.getService(AuthService.class).login(new LoginInfo(account, password));
  80 + public Observable<NIMLoginResultBean> login(String account, String password) {
  81 + final PublishSubject<NIMLoginResultBean> subject = PublishSubject.create();
  82 + SharedPreferences sp = AppContext.getInstance().getApplicationContext().getSharedPreferences("info", Context.MODE_PRIVATE);
  83 + sp.edit().putString("account", account).apply();
  84 + sp.edit().putString("token", password).apply();
  85 + NIMClient.getService(AuthService.class)
  86 + .login(new LoginInfo(account, password))
  87 + .setCallback(new RequestCallback() {
  88 + @Override
  89 + public void onSuccess(Object o) {
  90 + NIMLoginResultBean bean = new NIMLoginResultBean(true, 200, null);
  91 + subject.onNext(bean);
  92 + }
  93 +
  94 + @Override
  95 + public void onFailed(int i) {
  96 + Timber.i("------nim login failed : %s", i);
  97 + ToastUtils.showToast("云信服务器登录失败:" + i);
  98 + NIMLoginResultBean bean = new NIMLoginResultBean(false, i, null);
  99 + subject.onNext(bean);
  100 + }
  101 +
  102 + @Override
  103 + public void onException(Throwable throwable) {
  104 + if (throwable != null) throwable.printStackTrace();
  105 + NIMLoginResultBean bean = new NIMLoginResultBean(false, 0, throwable);
  106 + subject.onNext(bean);
  107 + }
  108 + });
  109 + return subject;
57 110 }
58 111  
59 112  
... ... @@ -62,4 +115,8 @@ public class MessageManager {
62 115 NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, false);
63 116 NIMClient.getService(AuthService.class).logout();
64 117 }
  118 +
  119 + public interface NIMLoginCallback {
  120 + void onResult(boolean isSuccess, int resultCode, Throwable throwable);
  121 + }
65 122 }
... ...
app/src/main/java/com/shunzhi/parent/model/loginandregister/LoginAndRegisterModel.java
... ... @@ -5,8 +5,10 @@ import com.share.mvpsdk.base.BaseModel;
5 5 import com.share.mvpsdk.helper.RetrofitCreateHelper;
6 6 import com.share.mvpsdk.helper.RxHelper;
7 7 import com.shunzhi.parent.api.LoginRegisterApi;
  8 +import com.shunzhi.parent.bean.NIMLoginResultBean;
8 9 import com.shunzhi.parent.bean.UserInfo;
9 10 import com.shunzhi.parent.contract.loginandregister.LoginAndRegisterContract;
  11 +import com.shunzhi.parent.manager.MessageManager;
10 12  
11 13 import io.reactivex.Observable;
12 14  
... ... @@ -47,4 +49,9 @@ public class LoginAndRegisterModel extends BaseModel implements LoginAndRegiste
47 49 .compose(RxHelper.<UserInfo>rxSchedulerHelper());
48 50 }
49 51  
  52 + @Override
  53 + public Observable<NIMLoginResultBean> nimLoginResult(String account, String token) {
  54 + return MessageManager.getInstance().login(account, token);
  55 + }
  56 +
50 57 }
... ...
app/src/main/java/com/shunzhi/parent/presenter/loginandregister/LoginAndRegisterPresenter.java
... ... @@ -10,10 +10,13 @@ import com.share.mvpsdk.utils.ToastUtils;
10 10 import com.shunzhi.parent.AppConfig;
11 11 import com.shunzhi.parent.AppContext;
12 12 import com.shunzhi.parent.bean.CurrentBean;
  13 +import com.shunzhi.parent.bean.NIMLoginResultBean;
13 14 import com.shunzhi.parent.bean.UserInfo;
14 15 import com.shunzhi.parent.contract.loginandregister.LoginAndRegisterContract;
  16 +import com.shunzhi.parent.manager.MessageManager;
15 17 import com.shunzhi.parent.model.loginandregister.LoginAndRegisterModel;
16 18 import com.shunzhi.parent.ui.fragment.loginandregistfragment.LoginAndRegistFragment;
  19 +import com.shunzhi.parent.util.Utils;
17 20  
18 21 import java.util.regex.Matcher;
19 22 import java.util.regex.Pattern;
... ... @@ -125,12 +128,11 @@ public class LoginAndRegisterPresenter extends LoginAndRegisterContract.LoginPre
125 128 }
126 129  
127 130 @Override
128   -
129 131 public void getUserInfo(final String mobile, int school_id, String captcha) {
130 132 mRxManager.register(mIModel.getUserInfo(mobile, school_id, captcha).subscribe(new Consumer<UserInfo>() {
131 133 @Override
132 134 public void accept(UserInfo userInfo) throws Exception {
133   - Log.d("6666","userInfo="+userInfo.toString());
  135 + Log.d("6666", "userInfo=" + userInfo.toString());
134 136 if (userInfo != null) {
135 137 CurrentBean currentBean = userInfo.getData();
136 138 AppConfig.getAppConfig(AppContext.getInstance()).set(AppConfig.LOGIN_NAME, currentBean.getMobile());
... ... @@ -138,9 +140,10 @@ public class LoginAndRegisterPresenter extends LoginAndRegisterContract.LoginPre
138 140 AppConfig.getAppConfig(AppContext.getInstance()).set(AppConfig.USER_IMAGE, currentBean.getImage());
139 141 AppConfig.getAppConfig(AppContext.getInstance()).set(AppConfig.USER_SEX, String.valueOf(currentBean.getSex()));
140 142 AppConfig.getAppConfig(AppContext.getInstance()).set(AppConfig.PARENT_ID, String.valueOf(currentBean.getParentId()));
141   - AppConfig.getAppConfig(AppContext.getContext()).set(AppConfig.USER_ID,currentBean.getUserid());
142   - mIView.getUserInfo(0);
143   -
  143 + AppConfig.getAppConfig(AppContext.getContext()).set(AppConfig.USER_ID, currentBean.getUserid());
  144 + String account = currentBean.getUserid();
  145 + String token = Utils.MD5(account);
  146 + nimLogin(account, token);
144 147 } else {
145 148 LoginAndRegistFragment.progressDialog.dismiss();
146 149 ToastUtils.showToast("错误");
... ... @@ -155,6 +158,17 @@ public class LoginAndRegisterPresenter extends LoginAndRegisterContract.LoginPre
155 158 }));
156 159 }
157 160  
  161 + @Override
  162 + public void nimLogin(String account, String password) {
  163 + mRxManager.register(mIModel.nimLoginResult(account, password).subscribe(new Consumer<NIMLoginResultBean>() {
  164 + @Override
  165 + public void accept(NIMLoginResultBean bean) throws Exception {
  166 + if (bean.isSuccess())
  167 + mIView.getUserInfo(0);
  168 + }
  169 + }));
  170 + }
  171 +
158 172  
159 173 @Override
160 174 public LoginAndRegisterContract.ILoginModel getModel() {
... ...
app/src/main/java/com/shunzhi/parent/util/Utils.java 0 → 100644
... ... @@ -0,0 +1,36 @@
  1 +package com.shunzhi.parent.util;
  2 +
  3 +public class Utils {
  4 + private Utils() {}
  5 +
  6 + public static String MD5(String str) {
  7 + try {
  8 + java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
  9 + byte[] array = md.digest(str.getBytes("utf-8"));
  10 + StringBuilder sb = new StringBuilder();
  11 + for (int i = 0; i < array.length; ++i) {
  12 + sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
  13 + }
  14 + return sb.toString().toUpperCase();
  15 + } catch (Exception e) {
  16 + e.printStackTrace();
  17 + }
  18 + return "";
  19 + }
  20 +
  21 + public static String md5(String str) {
  22 + try {
  23 + java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
  24 + byte[] array = md.digest(str.getBytes("utf-8"));
  25 + StringBuilder sb = new StringBuilder();
  26 + for (int i = 0; i < array.length; ++i) {
  27 + sb.append(Integer.toHexString((array[i] & 0xff) | 0x100).substring(1, 3));
  28 + }
  29 + return sb.toString().toUpperCase();
  30 + } catch (Exception e) {
  31 + e.printStackTrace();
  32 + }
  33 + return "";
  34 + }
  35 +
  36 +}
... ...