Commit 918131e1a279c160c4efb8d92c5c7e851cf9460f

Authored by 王琳
1 parent 0d324f23

添加获取token与鉴权接口

IMLib/build.gradle
... ... @@ -21,7 +21,7 @@ android {
21 21  
22 22 buildTypes {
23 23 release {
24   - minifyEnabled true
  24 + minifyEnabled false
25 25 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26 26 }
27 27 }
... ... @@ -31,8 +31,9 @@ android {
31 31 }
32 32  
33 33 dependencies {
34   - compile fileTree(dir: 'libs', include: ['*.jar'])
  34 + compile fileTree(include: ['*.jar'], dir: 'libs')
35 35 compile 'com.android.support:support-v4:21.0.0'
  36 + compile files('libs/libcnliveutil.jar')
36 37 }
37 38  
38 39 ext {
... ...
IMLib/build/intermediates/incremental/mergeDebugAndroidTestResources/compile-file-map.properties
1   -#Wed Mar 01 16:14:59 CST 2017
  1 +#Thu Mar 02 15:05:54 CST 2017
... ...
IMLib/build/intermediates/incremental/mergeDebugResources/compile-file-map.properties
1   -#Wed Mar 01 16:14:58 CST 2017
  1 +#Thu Mar 02 15:05:52 CST 2017
... ...
IMLib/build/intermediates/incremental/packageDebugResources/compile-file-map.properties
1   -#Wed Mar 01 16:14:59 CST 2017
  1 +#Thu Mar 02 15:05:54 CST 2017
... ...
IMLib/build/intermediates/incremental/packageReleaseResources/compile-file-map.properties
1   -#Wed Mar 01 16:15:00 CST 2017
  1 +#Thu Mar 02 15:05:54 CST 2017
... ...
IMLib/build/intermediates/res/resources-debug-androidTest.ap_
No preview for this file type
IMLib/build/outputs/aar/IMLib-debug.aar
No preview for this file type
IMLib/build/outputs/aar/IMLib-release.aar
No preview for this file type
IMLib/libs/libcnliveutil.jar 0 → 100644
No preview for this file type
IMLib/src/main/java/io.rong.imlib/Chat.java
... ... @@ -3,10 +3,19 @@ package io.rong.imlib;
3 3 import android.content.Context;
4 4 import android.net.Uri;
5 5 import android.telephony.TelephonyManager;
  6 +import android.text.TextUtils;
  7 +import android.util.Log;
6 8  
  9 +import com.cnlive.libs.util.Config;
  10 +import com.cnlive.libs.util.NetUtil;
  11 +import com.cnlive.libs.util.data.okhttp3.Call;
  12 +import com.cnlive.libs.util.data.okhttpUtil.callback.GenericsCallback;
  13 +
  14 +import org.apache.http.HttpResponse;
7 15 import org.json.JSONException;
8 16 import org.json.JSONObject;
9 17  
  18 +import java.lang.annotation.Target;
10 19 import java.util.ArrayList;
11 20 import java.util.List;
12 21 import java.util.UUID;
... ... @@ -16,9 +25,11 @@ import io.rong.imlib.model.CNMessage;
16 25 import io.rong.imlib.model.CNUserInfo;
17 26 import io.rong.imlib.model.ConnectionStatus;
18 27 import io.rong.imlib.model.Conversation;
  28 +import io.rong.imlib.model.DataEntity;
19 29 import io.rong.imlib.model.Message;
20 30 import io.rong.imlib.model.MessageContent;
21 31 import io.rong.imlib.model.UserInfo;
  32 +import io.rong.imlib.network.HttpRequest;
22 33 import io.rong.imlib.network.HttpUtil;
23 34 import io.rong.message.TextMessage;
24 35  
... ... @@ -52,6 +63,11 @@ public class Chat implements IChat {
52 63 private OnResultListener mOnResultListener;
53 64 private OnLatestMessageListener mOnLatestMessageListener;
54 65  
  66 + private OnErrorListener mOnErrorListener;
  67 + private OnInfoListener mOnInfoListener;
  68 +
  69 + private String sid;
  70 +
55 71 private static String APP_KEY = "e5t4ouvpe5qea";
56 72 private static String APP_SECRET = "N16VJFIPE22p";
57 73  
... ... @@ -96,29 +112,48 @@ public class Chat implements IChat {
96 112 headUrl = cnUserInfo.getHeadUrl() == null ? "" : cnUserInfo.getHeadUrl();
97 113 currentUser = new UserInfo(userId, userName, Uri.parse(headUrl));
98 114 }
99   - getToken(cnUserInfo, new HttpUtil.OnResponse() {
  115 + sid = currentUser.getUserId();
  116 + getToken(cnUserInfo);
  117 + }
  118 +
  119 +// //获得token
  120 +// private static void getToken(CNUserInfo user, HttpUtil.OnResponse callback) {
  121 +// final String HTTP_GET_TOKEN = "https://api.cn.ronghub.com/user/getToken.json";
  122 +// HttpUtil.Header header = HttpUtil.getRcHeader(APP_KEY, APP_SECRET);
  123 +// String body = "userId=" + user.getUserId() + "&name=" + user.getUserName() + "&portraitUri=" + Uri.parse(user.getHeadUrl());
  124 +// HttpUtil httpUtil = new HttpUtil();
  125 +// httpUtil.post(HTTP_GET_TOKEN, header, body, callback);
  126 +// }
  127 +
  128 + private void getToken(CNUserInfo userInfo) {
  129 + Log.e("DemoView", userInfo.getUserId() + " : " + userInfo.getUserName() + " : " + userInfo.getHeadUrl());
  130 + HttpRequest.getToken(userInfo, new GenericsCallback<DataEntity>() {
100 131 @Override
101   - public void onResponse(int code, String body) {
102   - if (code != 200) return;
103   - try {
104   - JSONObject jsonObject = new JSONObject(body);
105   - token = jsonObject.getString("token");
106   - } catch (JSONException e) {
107   - e.printStackTrace();
108   - return;
  132 + public void onError(Call call, Exception e) {
  133 + if (!NetUtil.checkNetwork(Config.getContext())) {
  134 + if (mOnErrorListener != null) mOnErrorListener.onError(-0x1009, "网络连接失败");
  135 + } else {
  136 + if (mOnErrorListener != null) mOnErrorListener.onError(-0x1010, "内部错误");
109 137 }
110   - connect(token);
  138 + if (e != null) Log.e("Chat", "error ", e);
  139 +
111 140 }
112   - });
113   - }
114 141  
115   - //获得token
116   - private static void getToken(CNUserInfo user, HttpUtil.OnResponse callback) {
117   - final String HTTP_GET_TOKEN = "https://api.cn.ronghub.com/user/getToken.json";
118   - HttpUtil.Header header = HttpUtil.getRcHeader(APP_KEY, APP_SECRET);
119   - String body = "userId=" + user.getUserId() + "&name=" + user.getUserName() + "&portraitUri=" + Uri.parse(user.getHeadUrl());
120   - HttpUtil httpUtil = new HttpUtil();
121   - httpUtil.post(HTTP_GET_TOKEN, header, body, callback);
  142 + @Override
  143 + public void onResponse(DataEntity dataEntity, Exception e) {
  144 + if (dataEntity != null && "0".equals(dataEntity.getErrorCode())) {
  145 + if (mOnInfoListener != null) mOnInfoListener.onInfo(0x2001, "获取token成功");
  146 + token = dataEntity.getData().getToken();
  147 + Log.e("DemoView", token);
  148 + connect(token);
  149 + } else if (dataEntity != null && !TextUtils.isEmpty(dataEntity.getErrorCode())) {
  150 + if (mOnErrorListener != null)
  151 + mOnErrorListener.onError(-0x1008, dataEntity.getErrorMessage());
  152 + } else {
  153 + onError(null, e);
  154 + }
  155 + }
  156 + });
122 157 }
123 158  
124 159 //连接融云服务器
... ... @@ -151,8 +186,66 @@ public class Chat implements IChat {
151 186 else RongIMClient.getInstance().logout();
152 187 }
153 188  
  189 + //用户鉴权成功后加入已经存在聊天室
  190 + public void joinExistChatRoom(final String roomId, final int defMessageCount) {
  191 + HttpRequest.getRoomAuthCheck(roomId, sid, new GenericsCallback<DataEntity>() {
  192 + @Override
  193 + public void onError(Call call, Exception e) {
  194 + if (!NetUtil.checkNetwork(Config.getContext())) {
  195 + if (mOnErrorListener != null) mOnErrorListener.onError(-0x1009, "网络连接失败");
  196 + } else {
  197 + if (mOnErrorListener != null) mOnErrorListener.onError(-0x1010, "内部错误");
  198 + }
  199 + if (e != null) Log.e("Chat", "error ", e);
  200 + }
  201 +
  202 + @Override
  203 + public void onResponse(DataEntity dataEntity, Exception e) {
  204 + if (dataEntity != null && "0".equals(dataEntity.getErrorCode())) {
  205 + if (mOnInfoListener != null)
  206 + mOnInfoListener.onInfo(0x2001, dataEntity.getErrorMessage());
  207 + joinExistChatroom(roomId, defMessageCount);
  208 + } else if (dataEntity != null && !TextUtils.isEmpty(dataEntity.getErrorCode())) {
  209 + if (mOnErrorListener != null)
  210 + mOnErrorListener.onError(-0x1008, dataEntity.getErrorMessage());
  211 + } else {
  212 + onError(null, e);
  213 + }
  214 + }
  215 + });
  216 + }
  217 +
  218 + //用户鉴权成功后加入已经存在聊天室
  219 + public void joinChatRoom(final String roomId, final int defMessageCount) {
  220 + HttpRequest.getRoomAuthCheck(roomId, sid, new GenericsCallback<DataEntity>() {
  221 + @Override
  222 + public void onError(Call call, Exception e) {
  223 + if (!NetUtil.checkNetwork(Config.getContext())) {
  224 + if (mOnErrorListener != null) mOnErrorListener.onError(-0x1009, "网络连接失败");
  225 + } else {
  226 + if (mOnErrorListener != null) mOnErrorListener.onError(-0x1010, "内部错误");
  227 + }
  228 + if (e != null) Log.e("Chat", "error ", e);
  229 + }
  230 +
  231 + @Override
  232 + public void onResponse(DataEntity dataEntity, Exception e) {
  233 + if (dataEntity != null && "0".equals(dataEntity.getErrorCode())) {
  234 + if (mOnInfoListener != null)
  235 + mOnInfoListener.onInfo(0x2001, dataEntity.getErrorMessage());
  236 + joinChatroom(roomId, defMessageCount);
  237 + } else if (dataEntity != null && !TextUtils.isEmpty(dataEntity.getErrorCode())) {
  238 + if (mOnErrorListener != null)
  239 + mOnErrorListener.onError(-0x1008, dataEntity.getErrorMessage());
  240 + } else {
  241 + onError(null, e);
  242 + }
  243 + }
  244 + });
  245 + }
  246 +
154 247 //加入聊天室,如果存在则加入,如果不存在,则创建
155   - private void joinChatRoom(String roomId, int messageCount) {
  248 + private void joinChatroom(String roomId, int messageCount) {
156 249 defMessageCount = messageCount;
157 250 currentRoomId = roomId;
158 251 RongIMClient.getInstance().joinChatRoom(currentRoomId, defMessageCount, new RongIMClient.OperationCallback() {
... ... @@ -171,7 +264,7 @@ public class Chat implements IChat {
171 264 }
172 265  
173 266 //加入聊天室,如果存在则加入,如果不存在则加入失败
174   - public void joinExistChatRoom(String roomId, int messageCount) {
  267 + private void joinExistChatroom(String roomId, int messageCount) {
175 268 defMessageCount = messageCount;
176 269 currentRoomId = roomId;
177 270 roomIdList.add(currentRoomId);
... ... @@ -229,14 +322,6 @@ public class Chat implements IChat {
229 322 return connectionStatus;
230 323 }
231 324  
232   - //切换用户
233   - public void exchangeUser(CNUserInfo userInfo) {
234   - disconnect(false);
235   - connect(userInfo);
236   - //TODO 在token获取成功时做连接操作,连接成功之后做加入聊天室操作
237   - joinChatRoom(currentRoomId, -1);
238   - }
239   -
240 325 private void sendChatRoomMessage(String targetId, final MessageContent msgContent) {
241 326 if (currentUser == null) {
242 327 throw new RuntimeException("currentUser should not be null.");
... ... @@ -458,6 +543,14 @@ public class Chat implements IChat {
458 543 this.mOnLatestMessageListener = listener;
459 544 }
460 545  
  546 + public void setOnErrorListener(OnErrorListener listener) {
  547 + this.mOnErrorListener = listener;
  548 + }
  549 +
  550 + public void setOnInfoListener(OnInfoListener listener) {
  551 + this.mOnInfoListener = listener;
  552 + }
  553 +
461 554 private UserInfo getCurrentUser() {
462 555 return currentUser;
463 556 }
... ... @@ -476,4 +569,12 @@ public class Chat implements IChat {
476 569 currentUser.setPortraitUri(Uri.parse(headUrl));
477 570 setCurrentUser(currentUser);
478 571 }
  572 +
  573 + //切换用户
  574 + public void exchangeUser(CNUserInfo userInfo) {
  575 + disconnect(false);
  576 + connect(userInfo);
  577 + //TODO 在token获取成功时做连接操作,连接成功之后做加入聊天室操作
  578 + joinExistChatRoom(currentRoomId, -1);
  579 + }
479 580 }
... ...
IMLib/src/main/java/io.rong.imlib/ChatUtil.java
... ... @@ -80,6 +80,14 @@ public class ChatUtil {
80 80 Chat.getInstance().setOnLatestMessageListener(listener);
81 81 }
82 82  
  83 + public static void setOnErrorListener(IChat.OnErrorListener listener) {
  84 + Chat.getInstance().setOnErrorListener(listener);
  85 + }
  86 +
  87 + public static void setOnInfoListener(IChat.OnInfoListener listener) {
  88 + Chat.getInstance().setOnInfoListener(listener);
  89 + }
  90 +
83 91 public static void modifyUser(String userName, String headUrl) {
84 92 Chat.getInstance().modifyUser(userName, headUrl);
85 93 }
... ...
IMLib/src/main/java/io.rong.imlib/base/IChat.java
... ... @@ -99,6 +99,10 @@ public interface IChat {
99 99 //从本地获取私聊历史消息
100 100 void getLatestMessages(String targetId, int count);
101 101  
  102 + void setOnErrorListener(OnErrorListener listener);
  103 +
  104 + void setOnInfoListener(OnInfoListener listener);
  105 +
102 106 //获取历史消息监听
103 107 void setOnLatestMessageListener(OnLatestMessageListener listener);
104 108  
... ... @@ -152,4 +156,12 @@ public interface IChat {
152 156 void onError(int what, String extra);
153 157 }
154 158  
  159 + interface OnErrorListener {
  160 + void onError(int what, String extra);
  161 + }
  162 +
  163 + interface OnInfoListener {
  164 + void onInfo(int what, String extra);
  165 + }
  166 +
155 167 }
... ...
IMLib/src/main/java/io.rong.imlib/model/DataEntity.java 0 → 100644
  1 +package io.rong.imlib.model;
  2 +
  3 +/**
  4 + * Created by Lynn on 3/2/2017.
  5 + */
  6 +
  7 +public class DataEntity extends ErrorMessage {
  8 +
  9 + private TokenData data;
  10 +
  11 + public TokenData getData() {
  12 + return data;
  13 + }
  14 +
  15 + public void setData(TokenData data) {
  16 + this.data = data;
  17 + }
  18 +
  19 + public class TokenData {
  20 + private String sid;
  21 + private String token;
  22 +
  23 + public String getSid() {
  24 + return sid;
  25 + }
  26 +
  27 + public void setSid(String sid) {
  28 + this.sid = sid;
  29 + }
  30 +
  31 + public String getToken() {
  32 + return token;
  33 + }
  34 +
  35 + public void setToken(String token) {
  36 + this.token = token;
  37 + }
  38 + }
  39 +}
... ...
IMLib/src/main/java/io.rong.imlib/model/ErrorMessage.java 0 → 100644
  1 +package io.rong.imlib.model;
  2 +
  3 +import com.cnlive.libs.util.model.BaseData;
  4 +
  5 +/**
  6 + * Created by Lynn on 3/2/2017.
  7 + */
  8 +
  9 +public class ErrorMessage extends BaseData {
  10 + private String errorCode;
  11 + private String errorMessage;
  12 +
  13 + public String getErrorCode() {
  14 + return errorCode;
  15 + }
  16 +
  17 + public void setErrorCode(String errorCode) {
  18 + this.errorCode = errorCode;
  19 + }
  20 +
  21 + public String getErrorMessage() {
  22 + return errorMessage;
  23 + }
  24 +
  25 + public void setErrorMessage(String errorMessage) {
  26 + this.errorMessage = errorMessage;
  27 + }
  28 +}
... ...
IMLib/src/main/java/io.rong.imlib/network/HttpRequest.java 0 → 100644
  1 +package io.rong.imlib.network;
  2 +
  3 +import android.util.Log;
  4 +
  5 +import com.cnlive.libs.util.Config;
  6 +import com.cnlive.libs.util.HttpUtils;
  7 +import com.cnlive.libs.util.data.okhttpUtil.callback.GenericsCallback;
  8 +
  9 +import java.util.HashMap;
  10 +import java.util.Map;
  11 +
  12 +import io.rong.imlib.model.CNUserInfo;
  13 +import io.rong.imlib.model.DataEntity;
  14 +
  15 +/**
  16 + * Created by Lynn on 3/2/2017.
  17 + */
  18 +
  19 +public class HttpRequest {
  20 + private static final String BASE_URL = "https://api.cnlive.com/open/api2";
  21 + private static final String URL_GET_TOKEN = BASE_URL + "/im_chat/getToken";
  22 + private static final String URL_ROOM_AUTH_CHECK = "/im_chat/user/checkOnline";
  23 +
  24 + public static void getToken(CNUserInfo userInfo, GenericsCallback<DataEntity> callback) {
  25 + Map<String, String> params = new HashMap<>();
  26 + params.put("appId", Config.getAppId());
  27 + params.put("sid", userInfo.getUserId());
  28 + params.put("nickName", userInfo.getUserName());
  29 + params.put("faceUrl", userInfo.getHeadUrl());
  30 + HttpUtils.doPostAsyn(URL_GET_TOKEN, params, callback);
  31 + }
  32 +
  33 + public static void getRoomAuthCheck(String chatroomId, String sid, GenericsCallback<DataEntity> callback) {
  34 + Map<String, String> params = new HashMap<>();
  35 + params.put("appId", Config.getAppId());
  36 + params.put("chatroomId", chatroomId);
  37 + params.put("sid", sid);
  38 + HttpUtils.doPostAsyn(URL_ROOM_AUTH_CHECK, params, callback);
  39 + }
  40 +}
... ...
app/src/main/java/com/cnlive/chat/CNLiveApplication.java
... ... @@ -2,6 +2,8 @@ package com.cnlive.chat;
2 2  
3 3 import android.app.Application;
4 4  
  5 +import com.cnlive.libs.util.Config;
  6 +
5 7 import io.rong.imlib.ChatUtil;
6 8  
7 9 /**
... ... @@ -13,6 +15,7 @@ public class CNLiveApplication extends Application {
13 15 @Override
14 16 public void onCreate() {
15 17 super.onCreate();
  18 + Config.init(this, "60_irn9cdak77", "a425987ebc86e352b3373c25d5bd082d0cc248b4043028");
16 19 ChatUtil.init(this);
17 20 }
18 21 }
... ...
app/src/main/java/com/cnlive/chat/ui/ChatRoomActivity.java
... ... @@ -57,6 +57,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
57 57 // ChatUtil.connect(null);
58 58  
59 59 mHandler = new Handler(this.getMainLooper());
  60 + ChatUtil.setOnErrorListener(onErrorListener);
  61 + ChatUtil.setOnInfoListener(onInfoListener);
60 62 ChatUtil.setOnSendMessageListener(onSendMessageListener);
61 63 ChatUtil.setOnOperationListener(onOperationListener);
62 64 ChatUtil.setOnResultListener(onResultListener);
... ... @@ -284,6 +286,20 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
284 286 }
285 287 };
286 288  
  289 + private IChat.OnErrorListener onErrorListener = new IChat.OnErrorListener() {
  290 + @Override
  291 + public void onError(int what, String extra) {
  292 + Log.e("DemoView", what + " : " + extra);
  293 + }
  294 + };
  295 +
  296 + private IChat.OnInfoListener onInfoListener = new IChat.OnInfoListener() {
  297 + @Override
  298 + public void onInfo(int what, String extra) {
  299 + Log.e("DemoView", what + " : " + extra);
  300 + }
  301 + };
  302 +
287 303 @Override
288 304 protected void onDestroy() {
289 305 super.onDestroy();
... ...