chat.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. require "autoload.php";
  3. $name = $_REQUEST['account'];
  4. $pas = $_REQUEST['paw'];
  5. $type = !empty($_REQUEST['type']) ? $_REQUEST['type'] : ZERO; // 1 注册 2 登录
  6. if (!empty($name) && !empty($pas) && isset($_REQUEST['login2'])) {
  7. $_SESSION['account'] = $name;
  8. $_SESSION['password'] = $pas;
  9. $account = $_SESSION['account'];
  10. $password = $_SESSION['password'];
  11. $data = [
  12. 'account' => $account,
  13. 'password' => $password
  14. ];
  15. $pactdata = Utils::senFormatData('login', $data);
  16. } else {
  17. echo "<script>alert('账号密码不能为空!');
  18. window.location.href='login.php';</script>";
  19. }
  20. /*$ChatModel = new ChatModel($type);
  21. $ChatModel->userLogin();
  22. if (!empty($name) && !empty($pas)) {
  23. log_message::info(3);
  24. $data = $ChatModel->userVerif();
  25. $data = is_string($data) ? json_decode($data, true) : $data;
  26. $rs = [
  27. 'msg' => '用户名密码ok',
  28. 'data' => $data
  29. ];
  30. log_message::info(4);
  31. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  32. // 校验用户名
  33. }*/
  34. $time = null;
  35. $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组
  36. $time = date('Y-m-d') . " 星期" . $weekarray[date("w")] . ' ' . date("H:i:s");
  37. //$pactdata = Utils::senFormatData('Intopic', $data);
  38. ?>
  39. <!DOCTYPE html>
  40. <html lang="en">
  41. <head>
  42. <style>
  43. * {
  44. margin: 0px;
  45. padding: 0px;
  46. }
  47. </style>
  48. </head>
  49. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  50. <script type="text/javascript" src="http://localhost:8055/chatroom/res/js/jquery.cookie.js"></script>
  51. <script>
  52. $(function () {
  53. $("#quit").click(function () {
  54. window.history.back(-1);
  55. });
  56. });
  57. </script>
  58. <body>
  59. <div id="user_id"></div>
  60. <div id="roole_id" style="display: none"></div>
  61. <input type="button" name="quit" id='quit' value="退出"/>
  62. <input type="button" name="close" value="清理记录" onclick="close_session('userinfo_data')">
  63. <div>
  64. <marquee onMouseOut="this.start()" onMouseOver="this.stop()" id="bulletin">
  65. </marquee>
  66. </div>
  67. <div id="friend" style="border:1px solid;width: 600px;height: 100px;">
  68. <table id="friendtables">
  69. </table>
  70. </div>
  71. <div style="margin-left:400px">
  72. <p id="byfriend"></p>
  73. <div style="border:1px solid;width: 600px;height: 500px;">
  74. <div id="msgArea"
  75. style="width:100%;height: 100%;text-align:start;resize: none;font-family: 微软雅黑;font-size: 20px;overflow-y: scroll"></div>
  76. </div>
  77. <div style="border:1px solid;width: 600px;height: 200px;">
  78. <div style="width:100%;height: 100%;">
  79. <textarea id="userMsg"
  80. style="width:100%;height: 100%;text-align:start;resize: none;font-family: 微软雅黑;font-size: 20px;"></textarea>
  81. </div>
  82. </div>
  83. <div style="border:1px solid;width: 600px;height: 25px;">
  84. <button style="float: right;" onclick="sendMsg()">发送</button>
  85. </div>
  86. </div>
  87. </body>
  88. <p></p>
  89. <form id="ByFrom">
  90. <input type="hidden" id="userid" name="userid"/>
  91. <input type="hidden" id="nickname" name="nickname"/>
  92. </form>
  93. </html>
  94. <script>
  95. //修改备注
  96. var ws;
  97. var userinfo = {};
  98. var friends_info = {};
  99. var strings = <?php echo $pactdata; ?>;
  100. var time = "<?php echo $time;?>";
  101. var timestamp = (new Date()).getTime();
  102. var friend_realtime = {};
  103. var storage = window.sessionStorage;
  104. var Interval = {};
  105. $(document).on("click", "#friendtables tr td", function () {
  106. $('#byfriend').html('');
  107. var _this = $(this);
  108. var userinfo = _this.text();
  109. var id = $(this).attr("data-name");
  110. $('#byfriend').append(userinfo + "(" + id + ")");
  111. $('#ByFrom #userid').text(id);
  112. // 展示消息
  113. var in_data_json = getJson();
  114. setinfo(in_data_json, id);
  115. if (Interval.id) {
  116. clearInterval(Interval.id);
  117. }
  118. });
  119. $(document).on("reload", "#friendtables tr td", function () {
  120. $("#friendtables").each(function () {
  121. var _this = $(this);
  122. var text = null;
  123. var userinfo = _this.text();
  124. var value = $(_this).parent().parent().find('[data-name=' + roole + ']').text();
  125. });
  126. });
  127. $("#friendtables tr").each(function () {
  128. var _this = $(this);
  129. var text = null;
  130. var userinfo = _this.text();
  131. if (userinfo) {
  132. // 设置
  133. }
  134. });
  135. function client_info(roole) {
  136. $("#friendtables").each(function () {
  137. //alert(2222);
  138. var _this = $(this);
  139. var text = null;
  140. var userinfo = _this.text();
  141. var value = $(_this).parent().parent().find('[data-name=' + roole + ']').text();
  142. //var itemName = $("table tr").attr("data-name");
  143. //console.log("clicen_info"+itemName);
  144. //var v1 = _this.getAttribute('data-name');
  145. /* if (userinfo == roole) {
  146. // 设置
  147. alert(roole);
  148. }
  149. else {
  150. alert('and info:' + userinfo);
  151. }*/
  152. });
  153. }
  154. $(function () {
  155. link();
  156. })
  157. function link(accountid) {
  158. ws = new WebSocket("ws://192.168.0.183:9501");//连接服务器
  159. ws.onopen = function (event) {
  160. console.log(event);
  161. // login
  162. var data_message = JSON.stringify(strings);
  163. // login
  164. ws.send(data_message);
  165. // get userInfo
  166. alert('连接了');
  167. };
  168. ws.onmessage = function (event) {
  169. // 这里去处理解析服务api的相关接口
  170. var obj = JSON.parse(event.data);
  171. var serdat = obj.msg;
  172. var status = obj.status;
  173. var pact = obj.pact;
  174. // 回调成功
  175. if (status == 1) {
  176. //login
  177. switch (pact) {
  178. case 'login':
  179. // 获取用户信息
  180. var nick_name = obj.msg.nick_name;
  181. $('#ByFrom #nickname').text(nick_name);
  182. var id = obj.msg.id;
  183. // 获取用户好友信息bulletin
  184. $("#bulletin").append("Welcome back " + nick_name + "!!");
  185. $("#user_id").append("ID :: " + id);
  186. $("#roole_id").append(id);
  187. var friendinfo = obj.msg.friendinfo
  188. // 展示好好友信息列表
  189. //$("#friend").append("<table id='friendtables'>");
  190. var html = '';
  191. for (var p in friendinfo) {//遍历json数组时,这么写p为索引,0,1
  192. var nickname = friendinfo[p].nick_name;
  193. var id = friendinfo[p].id;
  194. var status = friendinfo[p].status;
  195. status = (nickname != "" && status == 1
  196. )
  197. ? '在线' : '离线'
  198. html += "<tr><td data-name=" + id + ">" + nickname + status + "</td></tr>";
  199. }
  200. // data = {"pact": "sende_message","data": {"time": time, "msg": msg, "friendid": friendid, "user_id": userid}};
  201. //var data = JSON.stringify(data);
  202. $('#friendtables').append(html);
  203. //$("#friend").append("</table>");
  204. break;
  205. case 'sende_message':
  206. var friend_nickname = obj.msg.friend_info.nick_name;
  207. var friend_id = parseInt(obj.msg.friend_info.id);
  208. var friend_msg = obj.msg.info;
  209. var timestamp = (new Date()).getTime();
  210. friend_realtime.friend_id = friend_id;
  211. var info = "<p style='color: #0a789b'>" + "(" + friend_nickname + ")" + time + "</p>" + "<p>" + obj.msg.info + "</p>";
  212. /*var info = "<p>" + "(" + friend_nickname + ")" + obj.msg.info + "</p>";*/
  213. // 这里设置更改为提示在用户列表中闪烁灯 吧接收的消息存放缓存里
  214. // 后面当点击的时候 获取缓存的消息数据并且展现到列表上面
  215. $("#friendtables tr td").each(function (obj) {
  216. //alert("friend_id:" + friend_id);
  217. var _this = $(this);
  218. var userinfo = _this.text();
  219. // alert(userinfo);
  220. var dataname = $(_this).attr("data-name");
  221. //alert(dataname);
  222. // exportRaw('userinfo.txt', friend_nickname + ":" + friend_msg);//
  223. //localStorage
  224. //var data =
  225. // 如果列表有用户
  226. if (dataname == friend_id) {
  227. $.cookie("roolestatus", friend_id);
  228. // 根据消息的用户id存放到数组追加最后 当用户不在线的时候存放到服务端吧消息
  229. // 如果在线的时候直接发过来存放session data
  230. // 后面当用户点击消息闪动的时候读取状态未能读取的内容 并且设置session 的状态
  231. var data = {
  232. "friend_msg": friend_msg,
  233. "friendid": friend_id,
  234. "friend_nickname": friend_nickname,
  235. 'create_at': time,
  236. 'status': 1
  237. };
  238. //var data = JSON.stringify(datad);
  239. var dd = $('#byfriend').text();
  240. // 并且已经非存在的对话框泽
  241. Interval.id = setInterval("changeColor()", 200);//每0.2秒变换一种颜色
  242. // 展示消息
  243. var in_data_json = setJson(data);
  244. }
  245. });
  246. //$("#msgArea").append(info);
  247. break;
  248. case 'intopic':
  249. // set json
  250. // set info
  251. var in_data_json = serdat;
  252. if (in_data_json != false) {
  253. setinfo(in_data_json, friend_realtime.friend_id);
  254. }
  255. break;
  256. default:
  257. alert('无效的协议');
  258. break;
  259. }
  260. } else {
  261. alert(obj.msg);
  262. window.history.back(-1);
  263. }
  264. //var msg = "<p>" + event.data + "</p>";
  265. //$("#msgArea").append(obj.status);
  266. //$("#msgArea").append(msg);
  267. }
  268. ws.onclose = function (event) {
  269. alert("已经与服务器断开连接\r\n当前连接状态:" + this.readyState);
  270. };
  271. ws.onerror = function (event) {
  272. alert("WebSocket异常!");
  273. };
  274. }
  275. var data = [];
  276. data = undefined;
  277. /***
  278. * 客户端发消息
  279. */
  280. function sendMsg() {
  281. var msg = $("#userMsg").val();
  282. var friendid = $('#ByFrom #userid').text();
  283. if (friendid == "" || friendid == null || friendid == false || friendid == "undefined") {
  284. alert("请选择用户在进行发消息");
  285. return false;
  286. }
  287. var username = $('#ByFrom #nickname').text();
  288. var userid = $("#roole_id").text();
  289. data = {"pact": "sende_message", "data": {"msg": msg, "friendid": friendid, "user_id": userid}};
  290. var data = JSON.stringify(data);
  291. var info = "<p>" + "(" + username + ")" + time + "</p>" + "<p>" + msg + "</p>";
  292. $("#msgArea").append(info);
  293. ws.send(data);
  294. }
  295. function changeColor() {
  296. // 目前是单个 后期改为多个 通过设置cookie来进行设置
  297. var roolestatus = $.cookie("roolestatus");
  298. if (roolestatus != "undefined" && roolestatus != null && roolestatus != null) {
  299. $("#friendtables tr td").each(function () {
  300. var _this = $(this);
  301. var userinfo = _this.text();
  302. // alert(userinfo);
  303. var dataname = $(_this).attr("data-name");
  304. if (dataname == roolestatus) {
  305. var color = "#f00|#0f0|#00f|#880|#808|#088|yellow|green|blue|gray";
  306. color = color.split("|");
  307. var rand_color = color[parseInt(Math.random() * color.length)];
  308. $(_this).parent().css("color", rand_color);
  309. }
  310. });
  311. }
  312. //$(obj).parent().css("color", rand_color);
  313. }
  314. function fakeClick(obj) {
  315. var ev = document.createEvent("MouseEvents");
  316. ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  317. obj.dispatchEvent(ev);
  318. }
  319. function exportRaw(name, data) {
  320. var urlObject = window.URL || window.webkitURL || window;
  321. var export_blob = new Blob([data]);
  322. var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
  323. save_link.href = urlObject.createObjectURL(export_blob);
  324. save_link.download = name;
  325. fakeClick(save_link);
  326. }
  327. function extend(des, src, override) {
  328. if (src instanceof Array) {
  329. for (var i = 0, len = src.length; i < len; i++)
  330. extend(des, src[i], override);
  331. }
  332. for (var i in src) {
  333. if (override || !(i in des)) {
  334. des[i] = src[i];
  335. }
  336. }
  337. return des;
  338. }
  339. function setinfo(data, uid) {
  340. uid = uid ? parseInt(uid) : '';
  341. var dat = [];
  342. var indata = JSON.parse(data);
  343. var usertitle = $('#byfriend').text();
  344. //var clien_uid = $('#ByFrom #userid').text();
  345. var i = 0;
  346. if (uid != '') {
  347. $.each(indata, function (infoIndex, info) {
  348. var msg = info["friend_msg"];
  349. if (msg != undefined) {
  350. var msg = info["friend_msg"];
  351. var friendid = parseInt(info["friendid"]);
  352. var friend_nickname = info["friend_nickname"];
  353. var status = info["status"];
  354. var userinfo = null;
  355. // 如果用户打开对话框口 并且状态没有获取 直接显示
  356. if (friendid === uid && status == 1 && usertitle != "") {
  357. info["status"] = 0;
  358. // 设置data的status 为1
  359. // userinfo = "(" + friend_nickname + ")" + time + "<br>" + "\n" + msg;
  360. userinfo = "<p>" + "(" + friend_nickname + ")" + time + "</p>" + "<p>" + msg + "</p>";
  361. $("#msgArea").append(userinfo);
  362. }
  363. }
  364. indata[infoIndex] = info;
  365. });
  366. var stringdat = JSON.stringify(indata);
  367. storage.setItem('userinfo_data', stringdat);
  368. // set session
  369. }
  370. }
  371. /**
  372. * @param data 要录入的json str
  373. * @returns {*} bool and obj
  374. */
  375. function setJson(data = null, uid = null) {
  376. var sum_obj = null;
  377. var obj_str = null;
  378. var userinfo_data = storage.getItem('userinfo_data');
  379. var dacc = data;
  380. // 如果存在准备录入合并
  381. if (data != null && userinfo_data !== "" && userinfo_data != "null" && userinfo_data != null && userinfo_data != {} && userinfo_data != "[object JSON]") {
  382. //storage.setItem('userinfo_data', null);
  383. var receive_info = JSON.parse(userinfo_data);
  384. var send_dat = {'pact': 'intopic', 'data': {"history_receive_info": receive_info, "receive_info": data}};
  385. var indata = JSON.stringify(send_dat);
  386. ws.send(indata);
  387. /* //var sum_obj = extend({}, [userinfo_obj, data]);
  388. var sum_obj = extend({}, [userinfo_obj, data]);
  389. var in_sum_data = JSON.stringify(sum_obj);
  390. storage.setItem('userinfo_data', in_sum_data);
  391. return storage.getItem('userinfo_data');*/
  392. } else if (data != null && (userinfo_data === "" || userinfo_data === null || userinfo_data === {} || userinfo_data == "[object JSON]")) {
  393. var in_str_data = JSON.stringify(data);
  394. storage.setItem('userinfo_data', in_str_data);
  395. return data;
  396. }
  397. return false;
  398. }
  399. /****
  400. *
  401. * @param data 要录入的json str
  402. * @returns {*}
  403. */
  404. function getJson() {
  405. var userinfo_data = storage.getItem('userinfo_data');
  406. if (userinfo_data !== "" && userinfo_data != null && userinfo_data != {} && userinfo_data != "[object JSON]") {
  407. return userinfo_data;
  408. }
  409. }
  410. function close_session(key = null) {
  411. var sessiontor = window.sessionStorage;
  412. if (key && key != null) {
  413. var msg = sessiontor.getItem(key);
  414. var d = sessiontor.removeItem(msg);
  415. if (d) {
  416. alert('清理成功' + key);
  417. return;
  418. }
  419. alert('清理失败' + key);
  420. }
  421. }
  422. function concat(jsonbject1, jsonbject2) {
  423. var resultJsonObject = {};
  424. for (var attr in jsonbject1) {
  425. resultJsonObject[attr] = jsonbject1[attr];
  426. }
  427. for (var attr in jsonbject2) {
  428. resultJsonObject[attr] = jsonbject2[attr];
  429. }
  430. return resultJsonObject;
  431. }
  432. /**
  433. *
  434. * @param old_data
  435. * @param now_data
  436. * @constructor
  437. */
  438. function InServerTopicInfo(old_data, now_data) {
  439. $.ajax({
  440. type: 'POST',
  441. url: '/active/loadserver',
  442. dataType: 'json',
  443. success: function (result) {
  444. if (result.errcode == 0) {
  445. var zTree;
  446. $.fn.zTree.init($("#tree"), setting, result.msg);
  447. zTree = $.fn.zTree.getZTreeObj("tree");
  448. zTree.expandAll(true);
  449. }
  450. }
  451. });
  452. }
  453. </script>