chart.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. (new function () {
  2. this.app = angular.module("michat", []).run(callback);
  3. angular.bootstrap(document.getElementById(this.app.name), [this.app.name]);
  4. function callback($rootScope) {
  5. $rootScope.text = '';
  6. $rootScope.user = {};
  7. $rootScope.list = [];
  8. // 数字位数处理
  9. function toNum(value, fixed) {
  10. while (("" + value).length < fixed) value = '0' + value;
  11. return value;
  12. }
  13. // 显示时间内容
  14. $rootScope.showDatetime = function (date) {
  15. var md, td, tm, my = new Date();
  16. md = my.getFullYear() + '-' + toNum(my.getMonth() + 1, 2) + '-' + toNum(my.getDate(), 2);
  17. td = date.getFullYear() + '-' + toNum(date.getMonth() + 1, 2) + '-' + toNum(date.getDate(), 2);
  18. tm = toNum(date.getHours(), 2) + ':' + toNum(date.getMinutes(), 2) + ':' + toNum(date.getSeconds(), 2);
  19. return md === td ? tm : (td + tm);
  20. };
  21. for (let i = 10; i <= 99; i++) $rootScope.list.push({
  22. active: false,
  23. headimg: 'https://demo.thinkadmin.top/upload/decb0fe26fa3f486/b3f6521bf29403c8.png',
  24. username: 'nickname_a_' + i,
  25. nickname: 'NickName_A_B_C' + i,
  26. message: [
  27. {
  28. type: 'text',
  29. float: 'left',
  30. nickname: 'Nickname_A_' + i,
  31. headimg: 'https://demo.thinkadmin.top/upload/decb0fe26fa3f486/b3f6521bf29403c8.png',
  32. content: '消息内容_1_' + i,
  33. datetime: new Date()
  34. },
  35. {
  36. type: 'text',
  37. float: 'right',
  38. username: 'think_admin',
  39. nickname: 'ThinkAdmin',
  40. headimg: 'https://demo.thinkadmin.top/upload/decb0fe26fa3f486/b3f6521bf29403c8.png',
  41. content: '消息内容_2_' + i,
  42. datetime: new Date()
  43. },
  44. {
  45. type: 'text',
  46. float: 'right',
  47. username: 'think_admin',
  48. nickname: 'ThinkAdmin',
  49. headimg: 'https://demo.thinkadmin.top/upload/decb0fe26fa3f486/b3f6521bf29403c8.png',
  50. content: '消息内容_2_' + i,
  51. datetime: new Date()
  52. },
  53. {
  54. type: 'text',
  55. float: 'left',
  56. username: 'nickname_a_' + i,
  57. nickname: 'Nickname_A_' + i,
  58. headimg: 'https://demo.thinkadmin.top/upload/decb0fe26fa3f486/b3f6521bf29403c8.png',
  59. content: '消息内容_2_' + i,
  60. datetime: new Date()
  61. }
  62. ]
  63. });
  64. // 移除当前用户
  65. $rootScope.removeUser = function (user, temp) {
  66. temp = [];
  67. for (let i in $rootScope.list) {
  68. if ($rootScope.list[i].username !== user.username) {
  69. temp.push($rootScope.list[i]);
  70. }
  71. }
  72. $rootScope.list = temp;
  73. };
  74. // 聊天内容底部
  75. $rootScope.scrollBottom = function () {
  76. setTimeout(function (div) {
  77. div = document.querySelector('.michat-right-list');
  78. div.scrollTop = div.scrollHeight;
  79. }, 10);
  80. };
  81. // 切换当前用户
  82. $rootScope.switchUser = function (user) {
  83. for (let i in $rootScope.list) {
  84. if ($rootScope.list[i].username === user.username) {
  85. $rootScope.list[i].active = true;
  86. $rootScope.user = user;
  87. } else {
  88. $rootScope.list[i].active = false;
  89. }
  90. }
  91. this.scrollBottom();
  92. };
  93. // 回复消息内容
  94. $rootScope.replyUser = function () {
  95. if ($rootScope.text.length < 1) {
  96. alert('请输入内容');
  97. }
  98. $rootScope.user.message.push({
  99. type: 'text',
  100. float: 'right',
  101. username: 'NICKNAME_A',
  102. headimg: 'https://demo.thinkadmin.top/upload/decb0fe26fa3f486/b3f6521bf29403c8.png',
  103. content: $rootScope.text,
  104. datetime: new Date()
  105. });
  106. $rootScope.text = '';
  107. $rootScope.scrollBottom();
  108. };
  109. // 默认选择会话
  110. $rootScope.switchUser($rootScope.list[2]);
  111. }
  112. });