nim

云信聊天组件

1.初始化

根目录下依次执行

  1. weexplus plugin add https://github.com/farwolf2010/NIMKit
  1. weexplus plugin add https://github.com/farwolf2010/hik

2.配置和使用

申请appkey https://app.yunxin.163.com/index#/create nim - 图1 获取到appkey之后使用appkey和appsecret在云信那注册账号 以下为服务端注册代码(java)

  1. public static void main(String[] args) throws Exception{
  2. DefaultHttpClient httpClient = new DefaultHttpClient();
  3. String url = "https://api.netease.im/nimserver/user/create.action";
  4. HttpPost httpPost = new HttpPost(url);
  5. String appKey = "你的appkey";
  6. String appSecret = "你的appSecret";
  7. String nonce = "z11";
  8. String curTime = String.valueOf((new Date()).getTime() / 1000L);
  9. String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码
  10. // 设置请求的header
  11. httpPost.addHeader("AppKey", appKey);
  12. httpPost.addHeader("Nonce", nonce);
  13. httpPost.addHeader("CurTime", curTime);
  14. httpPost.addHeader("CheckSum", checkSum);
  15. httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  16. // 设置请求的参数
  17. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  18. nvps.add(new BasicNameValuePair("accid", "336"));//这个就是云信的账户id,打开对话靠的就是这个336
  19. httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
  20. // 执行请求
  21. HttpResponse response = httpClient.execute(httpPost);
  22. // 打印执行结果
  23. System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
  24. }

前端代码 打开某一用户的对话界面

  1. 登录并打开对话界面
  2. gotoChart(item){
  3. let nim=weex.requireModule('nim')
  4. nim.regist({appKey:你的云信appkey})
  5. nim.login({account:accid,token:注册云信id后返回的token},(res)=>{
  6. if(res.err==0){
  7. nim.openP2P({account:目标用户的accid, navBarBgColor:'#000000',theme:'white'})
  8. }else{
  9. this.toast('云信登录失败!'+res.err)
  10. }
  11. })
  12. }
  1. 最近联系人列表
  2. nim.recent((items)=>{
  3. })

api

  1. regist({appKey:你的云信appKey})
  2. login({account:注册的云信idaccid), token:云信token})
  3. recent((items)=>{
  4. //返回最近联系人列表
  5. })
  6. openP2P({account:目标用户的accid,navBarBgColor:标题栏背景颜色,theme:标题栏文字颜色('white','black'可选)})