New file |
| | |
| | | package com.stylefeng.guns.modular.system.util.rongyun; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.system.util.rongyun.model.CloudRecordingCallback; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.*; |
| | | import java.net.*; |
| | | import java.security.MessageDigest; |
| | | |
| | | /** |
| | | * 融云及时通讯工具类 |
| | | */ |
| | | @Component |
| | | public class RongYunUtil { |
| | | |
| | | // public static String app_key = "bmdehs6pbnozs"; |
| | | // public static String app_secret = "RovVwtABN1"; |
| | | |
| | | @Value("${rongyun.app_key}") |
| | | private String app_key; |
| | | |
| | | @Value("${rongyun.app_secret}") |
| | | private String app_secret; |
| | | /** |
| | | * 注册获取token |
| | | * @param userId |
| | | * @param name |
| | | * @param headUrl |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public String getToken(String userId, String name, String headUrl) throws Exception { |
| | | String url = "http://api-cn.ronghub.com/user/getToken.json"; |
| | | String data = "userId=" + userId + "&name=" + name + "&portraitUri=" + headUrl; |
| | | String s = this.pushHttp(url, data); |
| | | return s; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 刷新用户信息 |
| | | * @param userId |
| | | * @param name |
| | | * @param headUrl |
| | | * @throws Exception |
| | | */ |
| | | public void refresh(String userId, String name, String headUrl) throws Exception { |
| | | String url = "http://api-cn.ronghub.com/user/refresh.json"; |
| | | String data = "userId=" + userId + "&name=" + name + "&portraitUri=" + headUrl; |
| | | String s = this.pushHttp(url, data); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检查用户在线状态 |
| | | * @param userId |
| | | * @return 1:在线,0:离线 |
| | | * @throws Exception |
| | | */ |
| | | public Integer checkOnline(String userId) throws Exception { |
| | | String url = "http://api-cn.ronghub.com/user/checkOnline.json"; |
| | | String data = "userId=" + userId; |
| | | String s = this.pushHttp(url, data); |
| | | JSONObject jsonObject = JSON.parseObject(s); |
| | | if(jsonObject.getIntValue("code") == 200){ |
| | | return jsonObject.getIntValue("status"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送普通消息 |
| | | * @param fromUserId |
| | | * @param toUserId |
| | | * @param objectName |
| | | * @param content |
| | | * @return |
| | | */ |
| | | public String sendSms(String fromUserId, String toUserId, String objectName, String content){ |
| | | try { |
| | | String url = "https://api-cn.ronghub.com/message/private/publish.json"; |
| | | String data = "fromUserId=" + URLEncoder.encode(fromUserId, "UTF-8") + "&toUserId=" + URLEncoder.encode(toUserId, "UTF-8") + "&objectName=" + |
| | | URLEncoder.encode(objectName, "UTF-8") + "&content=" + URLEncoder.encode(content, "UTF-8") + "&disablePush=0&isIncludeSender=1"; |
| | | String s = this.pushHttp(url, data); |
| | | return s; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 创建 IM 聊天室 |
| | | * @param id 房间id长度不超过 64 字节 |
| | | * @param name 聊天室名称 |
| | | * @return |
| | | */ |
| | | public Integer ChatRoomCreate(String id, String name){ |
| | | String url = "https://api-cn.ronghub.com/chatroom/create.json"; |
| | | String data = "chatroom[" + id + "]=" + name; |
| | | String s = this.pushHttp(url, data); |
| | | JSONObject jsonObject = JSON.parseObject(s); |
| | | if(jsonObject.getIntValue("code") == 200){ |
| | | return jsonObject.getIntValue("status"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 销毁聊天室 |
| | | * @param chatroomId 聊天室id |
| | | * @return |
| | | */ |
| | | public Integer ChatRoomDestroy(String chatroomId){ |
| | | String url = "https://api-cn.ronghub.com/chatroom/destroy.json"; |
| | | String data = "chatroomId=" + chatroomId; |
| | | String s = this.pushHttp(url, data); |
| | | JSONObject jsonObject = JSON.parseObject(s); |
| | | if(jsonObject.getIntValue("code") == 200){ |
| | | return jsonObject.getIntValue("status"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 聊天室发送文本消息 |
| | | * @param userId 发送用户id |
| | | * @param chatroomId 聊天室id |
| | | * @param content 消息文本内容 |
| | | * @return |
| | | */ |
| | | public Integer ChatRoomPushTxtMessage(String userId, String chatroomId, String content){ |
| | | String url = "https://api-cn.ronghub.com/chatroom/destroy.json"; |
| | | String data = "fromUserId=" + userId + "&toChatroomId=" + chatroomId + "&objectName=RC:TxtMsg&content=" + content; |
| | | String s = this.pushHttp(url, data); |
| | | JSONObject jsonObject = JSON.parseObject(s); |
| | | if(jsonObject.getIntValue("code") == 200){ |
| | | return jsonObject.getIntValue("status"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 请求接口 |
| | | * @param path |
| | | * @param json |
| | | * @return |
| | | */ |
| | | public String pushHttp(String path, String json){ |
| | | String nonce = String.valueOf(Double.valueOf(Math.random() * 1000000.0D).intValue()); |
| | | String timeMillis = String.valueOf(System.currentTimeMillis() / 1000); |
| | | String signature = getSha1(app_secret + nonce + timeMillis); |
| | | |
| | | URL url = null; |
| | | try { |
| | | url = new URL(path); |
| | | HttpURLConnection connection = (HttpURLConnection)url.openConnection(); |
| | | connection.setRequestMethod("POST"); |
| | | connection.setConnectTimeout(1000 * 30); |
| | | connection.setRequestProperty("Host", "api-cn.ronghub.com"); |
| | | connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| | | connection.setRequestProperty("App-Key", app_key); |
| | | connection.setRequestProperty("Nonce", nonce); |
| | | connection.setRequestProperty("Timestamp", timeMillis); |
| | | connection.setRequestProperty("Signature", signature); |
| | | connection.setDoOutput(true); |
| | | connection.setDoInput(true); |
| | | connection.setUseCaches(false); |
| | | connection.setInstanceFollowRedirects(true); |
| | | connection.connect(); |
| | | DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); |
| | | outputStream.write(json.getBytes("UTF-8")); |
| | | outputStream.flush(); |
| | | outputStream.close(); |
| | | |
| | | int responseCode = connection.getResponseCode(); |
| | | InputStream inputStream = null; |
| | | if(responseCode == 403){ |
| | | inputStream = connection.getErrorStream(); |
| | | } |
| | | if(responseCode == 200){ |
| | | inputStream = connection.getInputStream(); |
| | | } |
| | | DataInputStream dataInputStream = new DataInputStream(inputStream); |
| | | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| | | int len; |
| | | byte[] bytes = new byte[1024]; |
| | | while ((len = dataInputStream.read(bytes)) != -1){ |
| | | stream.write(bytes, 0, len); |
| | | } |
| | | return stream.toString(); |
| | | } catch (MalformedURLException e) { |
| | | e.printStackTrace(); |
| | | } catch (ProtocolException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | public static String getSha1(String str) { |
| | | char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| | | 'a', 'b', 'c', 'd', 'e', 'f' }; |
| | | try { |
| | | MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); |
| | | mdTemp.update(str.getBytes("UTF-8")); |
| | | byte[] md = mdTemp.digest(); |
| | | int j = md.length; |
| | | char buf[] = new char[j * 2]; |
| | | int k = 0; |
| | | for (int i = 0; i < j; i++) { |
| | | byte byte0 = md[i]; |
| | | buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; |
| | | buf[k++] = hexDigits[byte0 & 0xf]; |
| | | } |
| | | return new String(buf); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 云端录制状态回调 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static CloudRecordingCallback cloudRecordingCallback(HttpServletRequest request){ |
| | | String param = null; |
| | | try { |
| | | param = getParam(request); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(ToolUtil.isNotEmpty(param)){ |
| | | CloudRecordingCallback cloudRecordingCallback = JSON.parseObject(param, CloudRecordingCallback.class); |
| | | return cloudRecordingCallback; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | private static String getParam(HttpServletRequest request) throws IOException { |
| | | // 读取参数 |
| | | InputStream inputStream; |
| | | StringBuilder sb = new StringBuilder(); |
| | | inputStream = request.getInputStream(); |
| | | String s; |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); |
| | | while ((s = in.readLine()) != null) { |
| | | sb.append(s); |
| | | } |
| | | in.close(); |
| | | inputStream.close(); |
| | | return sb.toString(); |
| | | } |
| | | } |