@ -1,7 +1,8 @@
package au.com.royalpay.payment.manage.openim.core ;
import au.com.royalpay.payment. manage.openim.beans.OpenimUser ;
import au.com.royalpay.payment. tools.exceptions.ServerErrorException ;
import com.alibaba.fastjson.JSONObject ;
import com.taobao.api.ApiException ;
import com.taobao.api.DefaultTaobaoClient ;
import com.taobao.api.TaobaoClient ;
@ -19,8 +20,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory ;
import org.springframework.stereotype.Service ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
/ * *
@ -34,7 +33,7 @@ public class OpenimClient {
private final String secret = "7639427973bd671be15c9d0c1e9c90b4" ;
private final String url = "https://eco.taobao.com/router/rest" ;
public void getUser ( String userId ) {
public JSONObject getUser ( String userId ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimUsersGetRequest req = new OpenimUsersGetRequest ( ) ;
// 批量为userid逗号分隔字符串
@ -45,91 +44,104 @@ public class OpenimClient {
} catch ( ApiException e ) {
logger . info ( "openim get user fail" , e ) ;
}
System . out . println ( rsp . getBody ( ) ) ;
return JSONObject . parseObject ( rsp . getBody ( ) ) ;
}
public void addUser ( OpenimUser user ) {
public void addUser ( List< Userinfos > users ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimUsersAddRequest req = new OpenimUsersAddRequest ( ) ;
List < Userinfos > list2 = new ArrayList < > ( ) ;
Userinfos obj3 = new Userinfos ( ) ;
list2 . add ( obj3 ) ;
obj3 . setMobile ( "18600000000" ) ;
obj3 . setTaobaoid ( "tbnick123" ) ;
obj3 . setUserid ( "imuser123456" ) ;
obj3 . setPassword ( "xxxxxx" ) ;
req . setUserinfos ( list2 ) ;
req . setUserinfos ( users ) ;
OpenimUsersAddResponse rsp = null ;
try {
rsp = client . execute ( req ) ;
} catch ( ApiException e ) {
logger . info ( "openim add user fail" , e ) ;
}
System . out . println ( rsp . getBody ( ) ) ;
JSONObject result = JSONObject . parseObject ( rsp . getBody ( ) ) ;
if ( result . getJSONObject ( "openim_users_add_response" ) . getJSONObject ( "fail_msg" ) . size ( ) > 1 ) {
logger . info ( "openim add user fail reason:" + result . getJSONObject ( "openim_users_add_response" ) . getJSONObject ( "fail_msg" ) . getJSONArray ( "string" ) . toJSONString ( ) ) ;
throw new ServerErrorException ( "System error" ) ;
}
}
public void updateUser ( List < Userinfos > users ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimUsersUpdateRequest req = new OpenimUsersUpdateRequest ( ) ;
List < Userinfos > list2 = new ArrayList < Userinfos > ( ) ;
Userinfos obj3 = new Userinfos ( ) ;
list2 . add ( obj3 ) ;
obj3 . setNick ( "king" ) ;
obj3 . setIconUrl ( "http://xxx.com/xxx" ) ;
obj3 . setEmail ( "uid@taobao.com" ) ;
obj3 . setMobile ( "18600000000" ) ;
obj3 . setTaobaoid ( "tbnick123" ) ;
obj3 . setUserid ( "imuser12345" ) ;
obj3 . setPassword ( "xxxxxx" ) ;
obj3 . setRemark ( "demo" ) ;
obj3 . setExtra ( "{}" ) ;
obj3 . setCareer ( "demo" ) ;
obj3 . setVip ( "{}" ) ;
obj3 . setAddress ( "demo" ) ;
obj3 . setName ( "demo" ) ;
obj3 . setAge ( 123L ) ;
obj3 . setGender ( "M" ) ;
obj3 . setWechat ( "demo" ) ;
obj3 . setQq ( "demo" ) ;
obj3 . setWeibo ( "demo" ) ;
req . setUserinfos ( list2 ) ;
req . setUserinfos ( users ) ;
OpenimUsersUpdateResponse rsp = null ;
try {
rsp = client . execute ( req ) ;
} catch ( ApiException e ) {
logger . info ( "openim update user fail" , e ) ;
}
System . out . println ( rsp . getBody ( ) ) ;
JSONObject result = JSONObject . parseObject ( rsp . getBody ( ) ) ;
if ( result . getJSONObject ( "openim_users_update_response" ) . getJSONObject ( "fail_msg" ) . size ( ) > 1 ) {
logger . info ( "openim add user fail reason:" + result . getJSONObject ( "openim_users_add_response" ) . getJSONObject ( "fail_msg" ) . getJSONArray ( "string" ) . toJSONString ( ) ) ;
throw new ServerErrorException ( "System error" ) ;
}
}
public void pushMsg ( ) {
public JSONObject pushMsg ( OpenimCustmsgPushRequest . CustMsg msg ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimCustmsgPushRequest req = new OpenimCustmsgPushRequest ( ) ;
OpenimCustmsgPushRequest . CustMsg obj1 = new OpenimCustmsgPushRequest . CustMsg ( ) ;
obj1 . setFromUser ( "user_sender" ) ;
obj1 . setToAppkey ( "0" ) ;
obj1 . setToUsers ( Collections . singletonList ( "user1" ) ) ;
obj1 . setSummary ( "客户端最近消息里面显示的消息摘要" ) ;
obj1 . setData ( "push payload" ) ;
obj1 . setAps ( "{\"alert\":\"ios apns push\"}" ) ;
obj1 . setApnsParam ( "apns推送的附带数据" ) ;
obj1 . setInvisible ( 0 L ) ;
obj1 . setFromNick ( "sender_nick" ) ;
obj1 . setFromTaobao ( 0 L ) ;
req . setCustmsg ( obj1 ) ;
req . setCustmsg ( msg ) ;
OpenimCustmsgPushResponse rsp = null ;
try {
rsp = client . execute ( req ) ;
} catch ( ApiException e ) {
logger . info ( "openim push msg fail" , e ) ;
}
System . out . println ( rsp . getBody ( ) ) ;
return JSONObject . parseObject ( rsp . getBody ( ) ) ;
}
public static void main ( String [ ] args ) {
OpenimClient asd = new OpenimClient ( ) ;
asd . getUser ( "imuser123456" ) ;
}
// public JSONObject test(){
// TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
// OpenimUsersUpdateRequest req = new OpenimUsersUpdateRequest();
// List<Userinfos> list2 = new ArrayList<Userinfos>();
// Userinfos obj3 = new Userinfos();
// list2.add(obj3);
// obj3.setNick("king");
// obj3.setIconUrl("http://xxx.com/xxx");
// obj3.setEmail("uid@taobao.com");
// obj3.setMobile("18600000000");
// obj3.setTaobaoid("tbnick123");
// obj3.setUserid("imuserkcbknefe123");
// obj3.setPassword("xxxxxx");
// obj3.setRemark("demo");
// obj3.setExtra("{}");
// obj3.setCareer("demo");
// obj3.setVip("{}");
// obj3.setAddress("demo");
// obj3.setName("demo");
// obj3.setAge(123L);
// obj3.setGender("M");
// obj3.setWechat("demo");
// obj3.setQq("demo");
// obj3.setWeibo("demo");
// req.setUserinfos(list2);
// OpenimUsersUpdateResponse rsp = null;
// try {
// rsp = client.execute(req);
// } catch (ApiException e) {
// e.printStackTrace();
// }
// System.out.println(rsp.getBody());
// return JSONObject.parseObject(rsp.getBody());
//
// }
// public static void main(String[] args) {
// OpenimClient asd = new OpenimClient();
// JSONObject zxc = asd.test();
// System.out.println(zxc);
// System.out.println(zxc);
// System.out.println(zxc);
// JSONObject qwe = JSONObject.parseObject("{\"openim_users_add_response\":{\"uid_fail\":{\"string\":[\"imuser123asdasdasd\"]},\"uid_succ\":{},\"fail_msg\":{\"string\":[\"data exist\"]}}}");
//
// System.out.println(qwe.getJSONObject("openim_users_add_response").getJSONObject("fail_msg").getJSONArray("string").toJSONString());
// System.out.println(qwe);
// System.out.println(qwe);
// }
}