groupchat.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* ...霸屏事件.start */
  2. $(".J__wchatBp").on("click", function(){
  3. var bpidx = wcPop({
  4. skin: 'ios',
  5. content: $("#J__popupTmpl-Baping").html(),
  6. style: 'background-color: #f3f3f3; max-width: 320px; width: 90%;',
  7. shadeClose: false,
  8. btns: [
  9. {
  10. text: '我要霸屏',
  11. style: 'background:#00c1de;color:#fff;font-size:16px;',
  12. onTap() {
  13. alert("霸屏成功!");
  14. wcPop.close(bpidx);
  15. }
  16. }
  17. ]
  18. });
  19. });
  20. //霸屏时长picker4c9fd0901d433aa2
  21. $("body").on("click", ".wc__popupTmpl input[name='bpTimeline']", function(){
  22. var that = $(this);
  23. weui.picker([{
  24. label: '10s ¥8',
  25. value: 8
  26. },{
  27. label: '20s ¥16',
  28. value: 16
  29. },{
  30. label: '30s ¥24',
  31. value: 24
  32. },{
  33. label: '40s ¥32',
  34. value: 32
  35. },{
  36. label: '50s ¥40',
  37. value: 40
  38. },{
  39. label: '60s ¥48',
  40. value: 48
  41. }], {
  42. onChange: function(res){
  43. console.log(res);
  44. },
  45. onConfirm: function(res){
  46. console.log(res);
  47. that.val("¥" + res);
  48. }
  49. }
  50. );
  51. });
  52. /* ...霸屏事件.end */
  53. /* ...打赏事件.start */
  54. $(".J__wchatDs").on("click", function(){
  55. var bpidx = wcPop({
  56. skin: 'ios',
  57. content: $("#J__popupTmpl-Dashang").html(),
  58. style: 'background-color: #f3f3f3; max-width: 320px; width: 90%;',
  59. shadeClose: false,
  60. btns: [
  61. {
  62. text: '<span class="btn-dashang">打赏</span>',
  63. style: 'background:#00c1de;color:#fff;font-size:16px;',
  64. onTap() {
  65. alert("打赏成功!");
  66. wcPop.close(bpidx);
  67. }
  68. }
  69. ]
  70. });
  71. });
  72. //打赏节目picker
  73. $("body").on("click", ".wc__popupTmpl input[name='dschooseProgram']", function(){
  74. var that = $(this);
  75. weui.picker([{
  76. label: '小品:送礼',
  77. value: '小品:送礼'
  78. },{
  79. label: '歌曲:红日',
  80. value: '歌曲:红日'
  81. },{
  82. label: '相声:就服你',
  83. value: '相声:就服你'
  84. },{
  85. label: '歌曲:上海滩',
  86. value: '歌曲:上海滩'
  87. },{
  88. label: '小品:回家过年',
  89. value: '小品:回家过年'
  90. },{
  91. label: '相声:逗你玩',
  92. value: '相声:逗你玩'
  93. }], {
  94. onChange: function(res){
  95. console.log(res);
  96. },
  97. onConfirm: function(res){
  98. console.log(res);
  99. that.val(res);
  100. }
  101. }
  102. );
  103. });
  104. //选择礼物
  105. $("body").on("click", "#J__chooseGift .gift", function(){
  106. $(this).addClass("selected").siblings().removeClass("selected");
  107. $(".popui__panel-btn .btn-dashang").html('支付 <em class="ff-ar">¥<i class="moneyNum">'+ $(this).find(".amount em").text() +'</i></em> 打赏');
  108. console.log($(this).attr("data-gift"));
  109. });
  110. /* ...打赏事件.end */
  111. /* ...红包事件.start */
  112. $(".J__wchatHb").on("click", function(){
  113. $("#Hongbao2").show();
  114. $("#J__popupTmpl-Hongbao").show();
  115. $(".wc__choose-panel").hide();
  116. /*
  117. var bpidx = wcPop({
  118. skin: 'ios',
  119. content: $("#J__popupTmpl-Hongbao").html(),
  120. style: 'background-color: #f3f3f3; max-width: 320px; width: 90%;',
  121. shadeClose: false,
  122. btns: [
  123. {
  124. text: '塞钱进红包',
  125. style: 'background:#00c1de;color:#fff;font-size:16px;',
  126. onTap() {
  127. alert("塞钱成功!");
  128. wcPop.close(bpidx);
  129. }
  130. }
  131. ]
  132. });
  133. */
  134. });
  135. /* ...红包事件.end */
  136. // ...关闭
  137. $("body").on("click", ".wc__popupTmpl .wc-xclose", function(){
  138. wcPop.close();
  139. });
  140. /** __公共函数 */
  141. $(function(){
  142. // 禁止长按弹出系统菜单
  143. /**
  144. $(".wechat__panel").on("contextmenu", function(e){
  145. e.preventDefault();
  146. });
  147. **/
  148. });
  149. /** __自定函数 */
  150. $(function(){
  151. // ...滚动聊天区底部
  152. function wchat_ToBottom(){
  153. $(".wc__chatMsg-panel").animate({scrollTop: $("#pagecount2020").height()}, 0);
  154. //$(".wc__chatMsg-panel").animate({scrollTop: $("#J__chatMsgList").height()}, 0);
  155. }
  156. // ...点击聊天面板区域
  157. $(document).on("click", ".wc__chatMsg-panel", function(e){
  158. var _tapMenu = $(".wc__chatTapMenu");
  159. if(_tapMenu.length && e.target != _tapMenu && !$.contains(_tapMenu[0], e.target)){
  160. // 关闭长按菜单
  161. _tapMenu.hide();
  162. $(".wc__chatMsg-panel").find("li .msg").removeClass("taped");
  163. }
  164. //滚动到顶部
  165. window.scrollTo(0, 0);
  166. //滚动到底部
  167. window.scrollTo(0, document.documentElement.clientHeight);
  168. $(".wc__choose-panel").hide();
  169. });
  170. // ...表情、选择区切换
  171. $(".wc__editor-panel").on("click", ".btn", function(){
  172. var that = $(this);
  173. $(".wc__choose-panel").show();
  174. if (that.hasClass("btn-emotion")) {
  175. $(".wc__choose-panel .wrap-emotion").show();
  176. $(".wc__choose-panel .wrap-choose").hide();
  177. // 初始化swiper表情
  178. !emotionSwiper && $("#J__emotionFootTab ul li.cur").trigger("click");
  179. } else if (that.hasClass("btn-choose")) {
  180. $(".wc__choose-panel .wrap-emotion").hide();
  181. $(".wc__choose-panel .wrap-choose").show();
  182. }
  183. wchat_ToBottom();
  184. });
  185. // ...处理编辑器信息
  186. var $editor = $(".J__wcEditor"), _editor = $editor[0];
  187. function surrounds(){
  188. setTimeout(function () { //chrome
  189. var sel = window.getSelection();
  190. var anchorNode = sel.anchorNode;
  191. if (!anchorNode) return;
  192. if (sel.anchorNode === _editor ||
  193. (sel.anchorNode.nodeType === 3 && sel.anchorNode.parentNode === _editor)) {
  194. var range = sel.getRangeAt(0);
  195. var p = document.createElement("p");
  196. range.surroundContents(p);
  197. range.selectNodeContents(p);
  198. range.insertNode(document.createElement("br")); //chrome
  199. sel.collapse(p, 0);
  200. (function clearBr() {
  201. var elems = [].slice.call(_editor.children);
  202. for (var i = 0, len = elems.length; i < len; i++) {
  203. var el = elems[i];
  204. if (el.tagName.toLowerCase() == "br") {
  205. _editor.removeChild(el);
  206. }
  207. }
  208. elems.length = 0;
  209. })();
  210. }
  211. }, 10);
  212. }
  213. // 格式化编辑器包含标签
  214. _editor.addEventListener("click", function () {
  215. $(".wc__choose-panel").hide();
  216. }, true);
  217. _editor.addEventListener("focus", function(){
  218. surrounds();
  219. }, true);
  220. _editor.addEventListener("input", function(){
  221. surrounds();
  222. }, false);
  223. // 点击表情
  224. $("#J__swiperEmotion").on("click", ".face-list span img", function(){
  225. var that = $(this), range;
  226. if(that.hasClass("face")){ //小表情
  227. var img = that[0].cloneNode(true);
  228. _editor.focus();
  229. _editor.blur(); //输入表情时禁止输入法
  230. setTimeout(function(){
  231. if(document.selection && document.selection.createRange){
  232. document.selection.createRange().pasteHTML(img);
  233. }else if(window.getSelection && window.getSelection().getRangeAt){
  234. range = window.getSelection().getRangeAt(0);
  235. range.insertNode(img);
  236. range.collapse(false);
  237. var sel = window.getSelection();
  238. sel.removeAllRanges();
  239. sel.addRange(range);
  240. }
  241. }, 10);
  242. }else if(that.hasClass("del")){ //删除
  243. _editor.focus();
  244. _editor.blur(); //输入表情时禁止输入法
  245. setTimeout(function(){
  246. range = window.getSelection().getRangeAt(0);
  247. range.collapse(false);
  248. var sel = window.getSelection();
  249. sel.removeAllRanges();
  250. sel.addRange(range);
  251. document.execCommand("delete");
  252. }, 10);
  253. } else if(that.hasClass("lg-face")){ //大表情
  254. var _img = that.parent().html();
  255. result = _img.substring(_img.indexOf("#") + 1,_img.indexOf("&"));
  256. _img = '<img class="lg-face" src="'+ result +'">'
  257. console.log(result);
  258. //记录消息
  259. $.post("/message/index/group", { id: ljc_id, content: _img,fid:ljc_fid },
  260. function(data){
  261. //消息回调
  262. if(data=="s1"){
  263. Tmsg(jy_1,3);
  264. return false;
  265. }else if(data=="s2"){
  266. Tmsg(jy_2,3);
  267. window.location.href = '/';
  268. return false;
  269. }
  270. });
  271. var _tpl = [
  272. '<li class="me">\
  273. <div class="content">\
  274. <p class="author">'+ ljc_name +'</p>\
  275. <div class="msg lgface">'+ _img + '</div>\
  276. </div>\
  277. <a class="avatar" href="/index/index/fdinfo.html"><img src="'+ ljc_tx +'" /></a>\
  278. </li>'
  279. ].join("");
  280. $chatMsgList.append(_tpl);
  281. wchat_ToBottom();
  282. }
  283. });
  284. // 发送信息
  285. var $chatMsgList = $("#J__chatMsgList");
  286. function isEmpty(){
  287. var html = $editor.html();
  288. html = html.replace(/<br[\s\/]{0,2}>/ig, "\r\n");
  289. html = html.replace(/<[^img].*?>/ig, "");
  290. html = html.replace(/&nbsp;/ig, "");
  291. return html.replace(/\r\n|\n|\r/, "").replace(/(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g, "") == "";
  292. }
  293. $(".J__wchatSubmit").on("click", function(){
  294. // 判断内容是否为空
  295. if(isEmpty()) return;
  296. var html = $editor.html();
  297. //console.log(html);
  298. $(".J__wchatSubmit").css("background-color","");
  299. $(".J__wchatSubmit").css("color","");
  300. var reg =/(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|\&|-|\;|\:)+)/g;
  301. //var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|-)+)/g;
  302. html = html.replace(reg, "<a href='$1$2'>$1$2</a>");
  303. console.log(html);
  304. //记录消息
  305. $.post("/message/index/group", { id: ljc_id, content: html,fid:ljc_fid },
  306. function(data){
  307. console.log(data);
  308. //消息回调
  309. if(data=="s1"){
  310. Tmsg(jy_1,3);
  311. return false;
  312. }else if(data=="s2"){
  313. Tmsg(jy_2,3);
  314. window.location.href = '/';
  315. return false;
  316. }
  317. //判断是否禁言后,确定是否执行下面语句
  318. var msgTpl = [
  319. '<li class="me">\
  320. <div class="content">\
  321. <p class="author">'+ ljc_name +'</p>\
  322. <div class="msg">'+ html +'</div>\
  323. </div>\
  324. <a class="avatar" href="/index/index/fdinfo"><img src="'+ ljc_tx +'" /></a>\
  325. </li>'
  326. ].join("");
  327. $chatMsgList.append(msgTpl);
  328. //滚动到顶部
  329. window.scrollTo(0, 0);
  330. //滚动到底部
  331. window.scrollTo(0, document.documentElement.clientHeight);
  332. // 清空聊天框并获取焦点(处理输入法和表情 - 聚焦)
  333. if(!$(".wc__choose-panel").is(":hidden")){
  334. $editor.html("");
  335. }else{
  336. $editor.html("").focus().trigger("click");
  337. }
  338. wchat_ToBottom();
  339. });
  340. });
  341. // ...长按弹出菜单
  342. /**
  343. $("#J__chatMsgList").on("longTap", "li .msg", function(e){
  344. var that = $(this), menuTpl, menuNode = $("<div class='wc__chatTapMenu animated anim-fadeIn'></div>");
  345. that.addClass("taped");
  346. that.parents("li").siblings().find(".msg").removeClass("taped");
  347. var isRevoke = that.parents("li").hasClass("me");
  348. var _revoke = isRevoke ? "<a href='#'><i class='ico i4'></i>撤回</a>" : "";
  349. if(that.hasClass("picture")){
  350. console.log("图片长按");
  351. menuTpl = "<div class='menu menu-picture'><a href='#'><i class='ico i1'></i>复制</a><a href='#'><i class='ico i2'></i>收藏</a><a href='#'><i class='ico i3'></i>另存为</a>"+ _revoke +"<a href='#'><i class='ico i5'></i>删除</a></div>";
  352. }else if(that.hasClass("video")){
  353. console.log("视频长按");
  354. menuTpl = "<div class='menu menu-video'><a href='#'><i class='ico i3'></i>另存为</a>" + _revoke +"<a href='#'><i class='ico i5'></i>删除</a></div>";
  355. }else{
  356. console.log("文字长按");
  357. menuTpl = "<div class='menu menu-text'><a href='#'><i class='ico i1'></i>复制</a><a href='#'><i class='ico i2'></i>收藏</a>" + _revoke +"<a href='#'><i class='ico i5'></i>删除</a></div>";
  358. }
  359. if(!$(".wc__chatTapMenu").length){
  360. $(".wc__chatMsg-panel").append(menuNode.html(menuTpl));
  361. autoPos();
  362. }else{
  363. $(".wc__chatTapMenu").hide().html(menuTpl).fadeIn(250);
  364. autoPos();
  365. }
  366. function autoPos(){
  367. console.log(that.position().top)
  368. var _other = that.parents("li").hasClass("others");
  369. $(".wc__chatTapMenu").css({
  370. position: "absolute",
  371. left: that.position().left + parseInt(that.css("marginLeft")) + (_other ? 0 : that.outerWidth() - $(".wc__chatTapMenu").outerWidth()),
  372. top: that.position().top - $(".wc__chatTapMenu").outerHeight() - 8
  373. });
  374. }
  375. });
  376. **/
  377. // ...销毁长按弹窗
  378. $(".wc__chatMsg-panel").on("scroll", function(){
  379. $(".wc__chatTapMenu").hide();
  380. $(this).find("li .msg").removeClass("taped");
  381. });
  382. // 定位光标尾部
  383. function posCursorEnd(obj) {
  384. if (window.getSelection) {//ie11 10 9 ff safari
  385. obj.focus(); //解决ff不获取焦点无法定位问题
  386. var range = window.getSelection();//创建range
  387. range.selectAllChildren(obj);//range 选择obj下所有子内容
  388. range.collapseToEnd();//光标移至最后
  389. }
  390. else if (document.selection) {//ie10 9 8 7 6 5
  391. var range = document.selection.createRange();//创建选择对象
  392. //var range = document.body.createTextRange();
  393. range.moveToElementText(obj);//range定位到obj
  394. range.collapse(false);//光标移至最后
  395. range.select();
  396. }
  397. }
  398. // 长按@聊友longTap
  399. $("#J__chatMsgList").on("click", "li .avatar", function(e){
  400. var author = $(this).siblings(".content").find(".slt").text();
  401. var atidx = wcPop({
  402. skin: 'actionsheet',
  403. content: '<span style="color:#aaa;">'+author+'</span>',
  404. anim: 'footer',
  405. shadeClose: true,
  406. btns: [
  407. {
  408. text: '@他/她',
  409. style: 'color:#00c1de;',
  410. onTap() {
  411. $(".wc__choose-panel").hide();
  412. // @内容
  413. var _html = '<em class="onez-at" data-at="1233" data-at-uname="@'+author+'">@'+author+'</em>&nbsp;';
  414. $editor.html("").append(_html);
  415. posCursorEnd(_editor);
  416. wcPop.close(atidx);
  417. }
  418. },
  419. {
  420. text: '取消',
  421. onTap() {
  422. wcPop.close(atidx);
  423. }
  424. }
  425. ]
  426. });
  427. });
  428. });