@ -6,12 +6,15 @@ import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException ;
import com.taobao.api.DefaultTaobaoClient ;
import com.taobao.api.TaobaoClient ;
import com.taobao.api.domain.OpenImUser ;
import com.taobao.api.domain.Userinfos ;
import com.taobao.api.request.OpenimCustmsgPushRequest ;
import com.taobao.api.request.OpenimRelationsGetRequest ;
import com.taobao.api.request.OpenimUsersAddRequest ;
import com.taobao.api.request.OpenimUsersGetRequest ;
import com.taobao.api.request.OpenimUsersUpdateRequest ;
import com.taobao.api.response.OpenimCustmsgPushResponse ;
import com.taobao.api.response.OpenimRelationsGetResponse ;
import com.taobao.api.response.OpenimUsersAddResponse ;
import com.taobao.api.response.OpenimUsersGetResponse ;
import com.taobao.api.response.OpenimUsersUpdateResponse ;
@ -29,14 +32,16 @@ import java.util.List;
@Service
public class OpenimClient {
Logger logger = LoggerFactory . getLogger ( getClass ( ) ) ;
private final String appkey = "24960261" ;
private final String secret = "7639427973bd671be15c9d0c1e9c90b4" ;
// private final String appkey = "24960261";
// private final String secret = "7639427973bd671be15c9d0c1e9c90b4";
private final String appkey = "24962653" ;
private final String secret = "1c1a1320c4e6f24df24e3fe41c3fc00a" ;
private final String url = "https://eco.taobao.com/router/rest" ;
public JSONObject getUser ( String userId ) {
public JSONObject getUser ( String userId ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimUsersGetRequest req = new OpenimUsersGetRequest ( ) ;
// 批量为userid逗号分隔字符串
// 批量为userid逗号分隔字符串
req . setUserids ( userId ) ;
OpenimUsersGetResponse rsp = null ;
try {
@ -47,8 +52,6 @@ public class OpenimClient {
return JSONObject . parseObject ( rsp . getBody ( ) ) ;
}
public void addUser ( List < Userinfos > users ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimUsersAddRequest req = new OpenimUsersAddRequest ( ) ;
@ -59,9 +62,10 @@ public class OpenimClient {
} catch ( ApiException e ) {
logger . info ( "openim add user fail" , e ) ;
}
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 ( ) ) ;
JSONObject result = JSONObject . parseObject ( rsp . getBody ( ) ) ;
if ( result . getJSONObject ( "openim_users_add_response" ) . getJSONObject ( "fail_msg" ) . size ( ) > 0 ) {
logger . info ( "openim add user fail reason:"
+ result . getJSONObject ( "openim_users_add_response" ) . getJSONObject ( "fail_msg" ) . getJSONArray ( "string" ) . toJSONString ( ) ) ;
throw new ServerErrorException ( "System error" ) ;
}
}
@ -76,14 +80,15 @@ public class OpenimClient {
} catch ( ApiException e ) {
logger . info ( "openim update user fail" , e ) ;
}
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 ( ) ) ;
JSONObject result = JSONObject . parseObject ( rsp . getBody ( ) ) ;
if ( result . getJSONObject ( "openim_users_update_response" ) . getJSONObject ( "fail_msg" ) . size ( ) > 0 ) {
logger . info ( "openim update user fail reason:"
+ result . getJSONObject ( "openim_users_update_response" ) . getJSONObject ( "fail_msg" ) . getJSONArray ( "string" ) . toJSONString ( ) ) ;
throw new ServerErrorException ( "System error" ) ;
}
}
public JSONObject pushMsg ( OpenimCustmsgPushRequest . CustMsg msg ) {
public void pushMsg ( OpenimCustmsgPushRequest . CustMsg msg ) {
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimCustmsgPushRequest req = new OpenimCustmsgPushRequest ( ) ;
req . setCustmsg ( msg ) ;
@ -93,55 +98,33 @@ public class OpenimClient {
} catch ( ApiException e ) {
logger . info ( "openim push msg fail" , e ) ;
}
return JSONObject . parseObject ( rsp . getBody ( ) ) ;
JSONObject result = JSONObject . parseObject ( rsp . getBody ( ) ) ;
if ( result . getJSONObject ( "openim_custmsg_push_response" ) . getJSONObject ( "fail_msg" ) . size ( ) > 0 ) {
logger . info ( "openim push message fail reason:"
+ result . getJSONObject ( "openim_custmsg_push_response" ) . getJSONObject ( "fail_msg" ) . getJSONArray ( "string" ) . toJSONString ( ) ) ;
throw new ServerErrorException ( "System error" ) ;
}
}
// 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);
// }
public static void main ( String [ ] args ) {
String appkey = "24960261" ;
String secret = "7639427973bd671be15c9d0c1e9c90b4" ;
String url = "https://eco.taobao.com/router/rest" ;
TaobaoClient client = new DefaultTaobaoClient ( url , appkey , secret ) ;
OpenimRelationsGetRequest req = new OpenimRelationsGetRequest ( ) ;
req . setBegDate ( "20180701" ) ;
req . setEndDate ( "20180711" ) ;
OpenImUser obj1 = new OpenImUser ( ) ;
obj1 . setUid ( "imuser123" ) ;
obj1 . setTaobaoAccount ( false ) ;
req . setUser ( obj1 ) ;
OpenimRelationsGetResponse rsp = null ;
try {
rsp = client . execute ( req ) ;
} catch ( ApiException e ) {
e . printStackTrace ( ) ;
}
System . out . println ( rsp . getBody ( ) ) ;
}
}