main.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //设置 cookie
  2. function setCookie(c_name,value,expiredays){ // 名称 值 时间
  3. var exdate=new Date()
  4. exdate.setDate(exdate.getDate()+expiredays)
  5. document.cookie=c_name+ "=" +escape(value)+
  6. ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
  7. }
  8. //获取 cookie
  9. function getCookie(c_name){ // 名称
  10. if (document.cookie.length>0)
  11. {
  12. c_start=document.cookie.indexOf(c_name + "=")
  13. if (c_start!=-1)
  14. {
  15. c_start=c_start + c_name.length+1
  16. c_end=document.cookie.indexOf(";",c_start)
  17. if (c_end==-1) c_end=document.cookie.length
  18. return unescape(document.cookie.substring(c_start,c_end))
  19. }
  20. }
  21. return ""
  22. }
  23. var socket='';
  24. Socket = function(){
  25. this.socket='';
  26. this.init();
  27. this.content();
  28. }
  29. Socket.prototype={
  30. init:function(){
  31. this.socket = io.connect();
  32. },
  33. content:function(){
  34. if(getCookie("name")!=''){
  35. this.socket.emit('login',{"name":getCookie("name"),"img":localStorage.getItem('img'),"id":getCookie("id")});
  36. }
  37. this.socket.on('login',function(r){
  38. setCookie('id',r.id,10);
  39. });
  40. this.socket.on('AllUser',function(r){
  41. document.querySelector(".chat_room .user_list .list").innerHTML='';
  42. var html='';
  43. for (let i = 0; i < r.AllUser.length; i++) {
  44. if(getCookie("id") == r.AllUser[i].id){
  45. var dd = "color:red;";
  46. }else{
  47. var dd = "";
  48. }
  49. html += `
  50. <div class="list_1" id="`+r.AllUser[i].id+`" title="`+r.AllUser[i].name+`">
  51. <div class="img" onclick="user_info(this,'`+r.AllUser[i].id+`')"><img src="`+r.AllUser[i].img+`" alt=""></div>
  52. <div class="name" style="`+dd+`padding-bottom: 9px; height: 21px;overflow: hidden;box-sizing: border-box;margin-top: 6px;margin-left: 49px;">`+r.AllUser[i].name+`</div>
  53. </div>`;
  54. }
  55. document.querySelector(".chat_room .user_list .list").innerHTML=html;
  56. });
  57. this.socket.on('send',function(r){//接受信息
  58. if(r.id == getCookie("id")){//判断发送消息的对象是谁
  59. var type = "me";
  60. }else{
  61. var type = "other";
  62. }
  63. if(r.accept=="all"){//判断是否为公共聊天室
  64. let content = document.querySelectorAll(".chat_room>.content");
  65. for (let i = 0; i < content.length; i++) {
  66. if(content[i].getAttribute("id") == r.accept){
  67. chat_information(type,r.id,r.img,r.name,r.content,r.date,content[i]);
  68. break;
  69. }
  70. }
  71. }else{//私聊
  72. var bool = true;
  73. let length = document.querySelectorAll(".chat_room>.list .list_1")
  74. for (let i = 0; i < length.length; i++) {
  75. if(length[i].getAttribute("id") == r.accept||length[i].getAttribute("id") == r.send){//判断是否有聊天用户列表
  76. bool=false;
  77. }
  78. }
  79. if(bool){
  80. private_chat('',0,r.name,r.img,r.id);//私聊操作 显示用户列表 聊天框
  81. }
  82. let content = document.querySelectorAll(".chat_room>.content");
  83. for (let i = 0; i < content.length; i++) {
  84. if(content[i].getAttribute("id") == r.accept||content[i].getAttribute("id") == r.send){//找到聊天框
  85. chat_information(type,r.id,r.img,r.name,r.content,r.date,content[i]);//显示消息内容
  86. break;
  87. }
  88. }
  89. }
  90. var list = document.querySelectorAll(".chat_room>.list>.list_1");
  91. for (let i = 0; i < list.length; i++) {
  92. if(list[i].getAttribute("id") == r.send||list[i].getAttribute("id") == r.accept){
  93. if(list[i].className!="list_1 on"){//接受信息的框如果不是在现在显示的 就加个红点提示有消息
  94. if(!list[i].querySelector(".prompt")){
  95. var prompt = document.createElement("div");
  96. prompt.className = "prompt";
  97. prompt.innerHTML = '.';
  98. list[i].appendChild(prompt);
  99. }
  100. }
  101. break;
  102. }
  103. }
  104. });
  105. }
  106. }
  107. if(getCookie("name")==''||localStorage.getItem('img')==''){//判断是否有设置名称和头像
  108. document.querySelector(".setting .img img").src=localStorage.getItem('img');
  109. document.querySelector(".setting .info .name").value=getCookie("name");
  110. document.querySelector(".setting").style.display='block';
  111. }else{
  112. document.querySelector("body").removeChild(document.querySelector(".setting"));
  113. socket = new Socket();//new 一个socket对象
  114. }
  115. var chant_id="all";//当前聊天室id
  116. changepic=function(This,img) {//头像上传后执行的函数
  117. var reads= new FileReader();
  118. f=This.files[0];
  119. reads.readAsDataURL(f);
  120. fileSize =This.files[0].size;
  121. var size = fileSize / 1024;
  122. var fileMaxSize = 1024;//设置头像大小不能超过300K
  123. if (size > fileMaxSize) {
  124. alert("文件大小不能大于1M!");
  125. This.value = "";
  126. return false;
  127. }else if (size <= 0) {
  128. alert("文件大小不能为0M!");
  129. }
  130. reads.onload=function (e) {
  131. img.src=this.result;
  132. }
  133. }
  134. // type 聊天类型id id 聊天id img 头像 name 名称 content 内容 date 时间 create_obj 创建在哪个div聊天框中
  135. chat_information=function(type,id,img,name,content,date,create_obj){//添加消息内容
  136. var div = document.createElement("div");
  137. div.className=type;
  138. div.innerHTML=`
  139. <div class="img" id="`+id+`" onclick="user_info(this,'`+id+`')">
  140. <img src="`+img+`" alt="">
  141. </div>
  142. <p class="name">`+name+`</p><br/><br/>
  143. <p class="content">
  144. `+decodeURI(content)+`
  145. <span class="date">`+date+`</span>
  146. </p>`;
  147. create_obj.appendChild(div);
  148. create_obj.scrollTop = create_obj.scrollHeight;//将聊天框滚动到最底部
  149. }
  150. issetting=function(){//判断是否有设置头像 名称
  151. if(document.querySelector(".ok")){
  152. document.querySelector(".ok").onclick=function(){
  153. var img = document.querySelector(".setting .img img").src;
  154. var name = document.querySelector(".setting_main .info .name").value;
  155. if(img==''||name==''){
  156. alert("不能为空,请重新输入!");
  157. return;
  158. }
  159. setCookie('name',name,10); //设置cookie
  160. localStorage.setItem('img',img);//将头像放置到 localStorage中
  161. alert("设置成功!");
  162. document.querySelector("body").removeChild(document.querySelector(".setting"));
  163. socket = new Socket();//new 一个socket对象
  164. }
  165. }
  166. }
  167. issetting();
  168. send=function(This){//发送消息
  169. if(This.parentNode.querySelector(".input_content").value.trim()==''){
  170. This.parentNode.querySelector(".input_content").value='';
  171. return false;
  172. }
  173. var type = chant_id;//获取当前聊天的id
  174. var img = localStorage.getItem('img');//获取头像
  175. var name = getCookie("name");//获取用户名
  176. var content = This.parentNode.querySelector(".input_content").value;//获取聊天的内容
  177. content = encodeURI(content);//将聊天的内容url编码
  178. var DATE =new Date();
  179. var date = DATE.getHours()+":"+DATE.getMinutes()+":"+DATE.getSeconds();//时间
  180. This.parentNode.querySelector(".input_content").value='';//清空聊天框内容
  181. socket.socket.emit("send",{"accept":type,"send":getCookie("id"),'id':getCookie("id"),"img":img,"name":name,"content":content})//发送给请求给服务器
  182. }
  183. user_info=function(This,id){//点击头像 会显示该用户的信息 可进行私聊
  184. if(getCookie("id")==id){//如果点击的是自己的头像 就不显示
  185. alert("自己");
  186. return;
  187. }
  188. var div = document.createElement("div");
  189. div.className="setting";
  190. div.style.display='block';
  191. var html=`
  192. <div class="setting_main" style="height: 250px;">
  193. <div class="img">
  194. <img src="`+This.querySelector("img").src+`"/>
  195. </div>
  196. <div onclick="document.querySelector('body').removeChild(document.querySelector('.setting'))" style="position: absolute;top: 9px;right: 11px;color: #757575; cursor: pointer;">X</div>
  197. <div style="text-align: center;margin-top:10px;">`+This.parentNode.querySelector(".name").innerHTML+`</div>
  198. <input type="button" value="私聊" class="ok" onclick="private_chat(this,1,'`+This.parentNode.querySelector(".name").innerHTML+`','`+This.querySelector("img").src+`','`+id+`')">
  199. </div>`;
  200. div.innerHTML=html;
  201. document.querySelector("body").appendChild(div);
  202. }
  203. private_chat=function(This,bool,name,img,id){
  204. //私聊操作 This 触发对象 bool (判断是私聊的 还是被私聊的 1是私聊的 0被私聊) name 名称 img 头像 id
  205. var qq = true;
  206. let list_1 = document.querySelectorAll(".chat_room>.list .list_1");
  207. for (let i = 0; i < list_1.length; i++) {
  208. if(list_1[i].getAttribute("id") == id){//判断聊天列表中 是否有该用户
  209. qq=false;
  210. }
  211. }
  212. if(qq){//聊天列表中没有改用户 创建聊天列表 创建聊天框
  213. var div = document.createElement("div");
  214. div.className = 'list_1';
  215. div.id = id;
  216. div.onclick=function(){char_list(this);};
  217. div.innerHTML=`
  218. <div class="img"><img src="`+img+`" alt=""></div>
  219. <div class="title">`+name+`</div>
  220. <div class="info"></div>`;
  221. var content_div = document.createElement("div");
  222. content_div.className = "content";
  223. content_div.id = id;
  224. content_div.style.display = "none";
  225. document.querySelector(".chat_room .list").appendChild(div);
  226. document.querySelector(".chat_room").appendChild(content_div);
  227. }
  228. if(bool==1){//如果是主动私聊的 就将跟该用户的聊天列表和聊天框显示出来
  229. chant_id = id;
  230. char_list_on(id)
  231. chat_room_content_on(id)
  232. }
  233. if(document.querySelector(".setting")){//删除查看用户信息的弹窗
  234. document.body.removeChild(document.querySelector(".setting"));
  235. }
  236. }
  237. char_list_on=function(id){//切换至某个用户列表显示
  238. let list_1 = document.querySelectorAll(".chat_room .list .list_1");
  239. for (let i = 0; i < list_1.length; i++) {
  240. list_1[i].className = "list_1";
  241. if(list_1[i].getAttribute("id") == id){
  242. list_1[i].className = "list_1 on";
  243. if(list_1[i].querySelector(".prompt")){
  244. list_1[i].removeChild(list_1[i].querySelector(".prompt"));
  245. }
  246. }
  247. }
  248. }
  249. chat_room_content_on=function(id){//切换至某个用户聊天框显示
  250. let content = document.querySelectorAll(".chat_room>.content");
  251. for (let i = 0; i < content.length; i++) {
  252. content[i].style.display = "none";
  253. if(content[i].getAttribute("id") == id){
  254. content[i].style.display = "block";
  255. }
  256. }
  257. }
  258. char_list=function(This){//列表点击切换 并切换聊天框
  259. chant_id=This.getAttribute("id");
  260. var length = document.querySelectorAll(".chat_room .list .list_1");
  261. for (let i = 0; i < length.length; i++) {
  262. length[i].className = "list_1";
  263. }
  264. This.className= "list_1 on";
  265. if(This.querySelector(".prompt")){
  266. This.removeChild(This.querySelector(".prompt"));
  267. }
  268. chat_room_content_on(This.getAttribute("id"));//切换至某个用户聊天框显示
  269. }
  270. document.querySelector(".input_content").onfocus=function(){//聊天输入框的回车事件
  271. document.onkeyup = function (e) {//按键信息对象以函数参数的形式传递进来了,就是那个e
  272. var code = e.charCode || e.keyCode; //取出按键信息中的按键代码(大部分浏览器通过keyCode属性获取按键代码,但少部分浏览器使用的却是charCode)
  273. if (code == 13) {//code 为 13 是回车
  274. document.querySelector(".chat_room .input input").click();
  275. }
  276. }
  277. }