私聊功能-回复陌生人问题 进入私聊页面



API



/**

* 以管理员身份发送消息

* 文档地址:http://docs-im.easemob.com/im/server/basics/messages#%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF

*

* @param targetUserName 发送目标的用户名

* @param huanXinMessageType 消息类型

* @param msg

* @return

*/

Boolean sendMsgFromAdmin(String targetUserName, HuanXinMessageType huanXinMessageType, String msg);




=============================================================




enums



/**

* 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息

*/

public enum HuanXinMessageType {

TXT("txt"), IMG("img"), LOC("loc"), AUDIO("audio"), VIDEO("video"), FILE("file");

String type;

HuanXinMessageType(String type) {

this.type = type;

}

public String getType() {

return type;

}

}




============================================================



API IMPL


@Override

public Boolean sendMsgFromAdmin(String targetUserName, HuanXinMessageType huanXinMessageType, String msg) {

String targetUrl = this.huanXinConfig.getUrl()

+ this.huanXinConfig.getOrgName() + "/"

+ this.huanXinConfig.getAppName() + "/messages";

try {

//{"target_type": "users","target": ["user2","user3"],"msg": {"type": "txt","msg": "testmessage"},"from": "user1"}

String body = JSONUtil.createObj()

.set("target_type", "users")

.set("target", JSONUtil.createArray().set(targetUserName))

.set("msg", JSONUtil.createObj()

.set("type", huanXinMessageType.getType())

.set("msg", msg)).toString();

//表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败

// .set("from", "")

return this.requestService.execute(targetUrl, body, Method.POST).isOk();

} catch (Exception e) {

log.error("发送消息失败~ targetUserName = " + targetUserName+", type = " + huanXinMessageType.getType()+", msg = " + msg, e);

}

return false;

}




==================================================================




controller




/**

* 回复陌生人问题

*

* @return

*/

@PostMapping("strangerQuestions")

public ResponseEntity replyQuestion(@RequestBody Map param) {

try {

Long userId = Long.valueOf(param.get("userId").toString());

String reply = param.get("reply").toString();

Boolean result = this.tanHuaService.replyQuestion(userId, reply);

if (result) {

return ResponseEntity.ok(null);

}

} catch (Exception e) {

e.printStackTrace();

}

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

}




===========================================================






service





@Reference(version = "1.0.0")

private HuanXinApi huanXinApi;

public Boolean replyQuestion(Long userId, String reply) {

User user = UserThreadLocal.get();

UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(user.getId());

//构建消息内容

Map msg = new HashMap<>();

msg.put("userId", user.getId());

msg.put("huanXinId", "HX_" + user.getId());

msg.put("nickname", userInfo.getNickName());

msg.put("strangerQuestion", this.queryQuestion(userId));

msg.put("reply", reply);

//发送环信消息

return this.huanXinApi.sendMsgFromAdmin("HX_" + userId,

HuanXinMessageType.TXT, JSONUtil.toJsonStr(msg));

}

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章