michat.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. define(['angular', baseRoot + 'plugs/michat/mimc-min_1_0_2.js', 'css!' + baseRoot + 'plugs/michat/michat.css'], function () {
  2. return new function (that) {
  3. this.appid = '', this.appkey = '';
  4. this.secret = '', this.account = '';
  5. that = this, this.list = [], this.body = $('body');
  6. // 数据网络请求
  7. this.httpRequest = function (url, data) {
  8. this.xhr = new XMLHttpRequest();
  9. this.xhr.open('post', url, false);
  10. this.xhr.setRequestHeader('content-type', 'application/json');
  11. this.xhr.send(JSON.stringify(data));
  12. return JSON.parse(this.xhr.response);
  13. };
  14. // 显示消息图标
  15. this.showMessageIcon = function () {
  16. that.body.append('<a class="michat-message-icon layui-btn layui-btn-normal"><i class="layui-icon">&#xe667;</i><b class="michat-message-number">0</b></a>');
  17. that.body.on('click', '.michat-message-icon', function () {
  18. layui.$.get(baseRoot + 'plugs/michat/template.html', function (template) {
  19. layui.layer.open({
  20. type: 1, title: false, skin: 'michat', area: ['800px', '520px'],
  21. closeBtn: true, shadeClose: false, content: template, success: function () {
  22. layui.$.getScript(baseRoot + 'plugs/michat/chart.js')
  23. }
  24. });
  25. });
  26. });
  27. };
  28. this.showMessageIcon();
  29. // 给指定账号发送消息
  30. this.send = function (account, payload) {
  31. this.user.sendMessage(account, payload, false);
  32. };
  33. // SDK登录初始化
  34. this.login = function () {
  35. this.user = new MIMCUser(this.appid, this.account);
  36. this.user.registerP2PMsgHandler(function (message) {
  37. console.log("time: " + new Date(parseInt(message.getTimeStamp())));
  38. console.log('load:' + message.getPayload());
  39. that.list.push({
  40. type: message.getBizType || 'TEXT',
  41. text: message.getPayload(),
  42. date: new Date(parseInt(message.getTimeStamp()))
  43. });
  44. $('.michat-message-number').html(that.list.length);
  45. });
  46. this.user.registerGroupMsgHandler(function (message) {
  47. console.log(message);
  48. });
  49. this.user.registerFetchToken(function () {
  50. return that.httpRequest('https://mimc.chat.xiaomi.net/api/account/token', {
  51. appId: that.appid, appKey: that.appkey, appSecret: that.secret, appAccount: that.account
  52. });
  53. });
  54. this.user.registerStatusChange(function (bindResult, errType, errReason, errDesc) {
  55. if (bindResult) {
  56. console.log("login succeed");
  57. } else {
  58. console.log("login failed.errReason=" + errReason + ",errDesc=" + errDesc + ",errType=" + errType);
  59. }
  60. });
  61. this.user.registerServerAckHandler(function (packetId, sequence, timeStamp, errMsg) {
  62. console.log(packetId, sequence, timeStamp, errMsg);
  63. });
  64. this.user.registerDisconnHandler(function () {
  65. console.log('disconnect');
  66. });
  67. this.user.registerUCDismissHandler(function (topicId) {
  68. console.log("uc dismiss:" + topicId);
  69. });
  70. this.user.registerUCJoinRespHandler(function (topicId, code, msg, context) {
  71. console.log("uc join:" + topicId + ",code=" + code + ",msg=" + msg + ",context=" + context);
  72. });
  73. this.user.registerUCMsgHandler(function (groupMsg) {
  74. console.log(groupMsg)
  75. });
  76. this.user.registerUCQuitRespHandler(function (topicId, code, msg, context) {
  77. console.log("uc quit:" + topicId + ",code=" + code + ",msg=" + msg + ",context=" + context);
  78. });
  79. this.user.login();
  80. }
  81. };
  82. });