Java IM环信服务端集成之用户体系集成(二)

来源:互联网 发布:中国经济网数据库 编辑:程序博客网 时间:2024/06/01 10:39
移动端用户注册 VS 环信IM用户体系集成

由于业务我们涉及到即时通讯(IM)的原因,在多个第三方的选择后,选择了环信。业务的改变导致了接口的重构下面我就简述一下环信IM的用户体系集成的流程以及规则:

涉及到业务参数,请查看Java IM环信服务端集成之创建应用(一)  参数后边都要用到。

名词解释

当您申请了 AppKey 后,会得到一个 xxxx#xxxx 格式的字符串,这个就是您的 AppKey,下文中用到的:

  • org_name 对应#前面部分
  • app_name 对应#后面部分
  • client_id 和 client_secret 可以在环信管理后台的 APP 详情页面看到,在获取token时候要用到。

环信 ID 规则

在注册环信账户的时候,需要注意环信 ID 的规则:
  • 使用英文字母和(或)数字的组合
  • 不能使用中文
  • 不能使用 email 地址
  • 不能使用 UUID
  • 用户ID的长度在255字节以内
  • 中间不能有空格或者井号(#)等特殊字符
  • 允许的用户名正则 “[a-zA-Z0-9_-.]*”(a~z大小写字母/数字/下划线/横线/英文句号),其他都不允许 如果是大写字母会自动转成小写
  • 不区分大小写。系统忽略大小写,认为 AA、Aa、aa、aA 都是一样的。如果系统已经存在了环信 ID 为 AA 的用户,再试图使用 aa 作为环信 ID 注册新用户,系统返回用户名重复,以此类推。但是请注意:环信 ID 在数据上的表现形式还是用户最初注册的形式,注册时候使用的大写就保存大写,是小写就保存小写。即:使用 AA 注册,环信保存的 ID 就是 AA;使用 Aa 注册,环信保存的 ID 就是 Aa,以此类推。

数据结构




废话不多说了,上车。
package com.xunxin.controller.app.huanxin;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.xunxin.controller.system.BaseController;import com.xunxin.dao.sys.PlatformMutualityManagentDao;import com.xunxin.util.HttpUtils;import com.xunxin.util.HttpUtils.UHeader;import com.xunxin.vo.sys.PageData;import com.xunxin.vo.sys.PlatformMutualityManagent;import com.xunxin.web.api.bean.Response;import com.xunxin.web.api.bean.Router;/** * Copyright © 2017 noseparte(Libra) © Like the wind, like rain * @Author Noseparte * @Compile 2017年10月24日 -- 下午5:31:38 * @Version 1.0 * @Description 获取环信SDK的返回信息 */@Controller@RequestMapping(value=Router.PATH+Router.Easemob.PATH)public class EasemobController extends BaseController{private static final Logger log = Logger.getLogger(EasemobController.class);private static final String LINKED_URL = "http://www.easemob.com/";@Autowiredprivate PlatformMutualityManagentDao platformMutualityManagentDao; @RequestMapping(value=Router.Easemob.GET_TOKEN,method=RequestMethod.POST)@ResponseBodypublic Response getToken(PageData pd) {log.info("info-: get huanxin token begin");Response res = this.getReponse();try {PlatformMutualityManagent pm = platformMutualityManagentDao.findOne(pd);String app_key = pm.getApp_key();String org_name = app_key.split("#")[0];//组织名称String app_name = app_key.split("#")[1];//APP名称String client_id = app_key.split("#")[1];//客户端idString client_secret = app_key.split("#")[1];//客户端密钥//post请求路径String url = LINKED_URL + org_name + "/" + app_name + "/" + "token";//post请求参数 jsonStringBuffer sb = new StringBuffer();sb.append("grant_type:").append("client_credentials");sb.append("client_id:").append(client_id);sb.append("client_secret:").append(client_secret);String argJson = sb.toString();//请求头信息List headerList = new ArrayList();headerList.add(new UHeader("Content-Type", "application/json"));//获取环信tokenString resp = HttpUtils.getPostResponse(url, argJson, headerList);JSONObject obj = JSON.parseObject(resp);//TODO 判断response 状态码String access_token = obj.getString("access_token");  //token 值String expires_in = obj.getString("expires_in");  //token 有效时间,以秒为单位,在有效期内不需要重复获取String application = obj.getString("application");  //token 有效时间,以秒为单位,在有效期内不需要重复获取//获得返回值 并解析PageData tokenPd = new PageData<>();tokenPd.put("access_token", access_token);tokenPd.put("expires_in", expires_in);tokenPd.put("application", application);log.info("infoMsg:- get token success, ok");return res.success(tokenPd);} catch (Exception e) {log.error("errorMsg:- An error occurred when get token!");return res.failure(e.getMessage());}}}




原创粉丝点击