12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324 |
- /**
- @Name:layim v3.9.1 Pro 商用版
- @Author:贤心
- @Site:http://layim.layui.com
- @License:LGPL
-
- */
- layui.define(['layer', 'laytpl', 'upload'], function(exports) {
- var v = '3.9.1';
- var $ = layui.$;
- var layer = layui.layer;
- var laytpl = layui.laytpl;
- var device = layui.device();
- var SHOW = 'layui-show',
- THIS = 'layim-this',
- MAX_ITEM = 20;
- //回调
- var call = {};
- //对外API
- var LAYIM = function() {
- this.v = v;
- $('body').on('click', '*[layim-event]', function(e) {
- var othis = $(this),
- methid = othis.attr('layim-event');
- events[methid] ? events[methid].call(this, othis, e) : '';
- });
- };
- //基础配置
- LAYIM.prototype.config = function(options) {
- var skin = [];
- layui.each(Array(5), function(index) {
- skin.push(layui.cache.dir + 'css/modules/layim/skin/' + (index + 1) + '.jpg')
- });
- options = options || {};
- options.skin = options.skin || [];
- layui.each(options.skin, function(index, item) {
- skin.unshift(item);
- });
- options.skin = skin;
- options = $.extend({
- isfriend: !0,
- isgroup: !0,
- voice: 'default.mp3'
- }, options);
- if (!window.JSON || !window.JSON.parse) return;
- init(options);
- return this;
- };
- //监听事件
- LAYIM.prototype.on = function(events, callback) {
- if (typeof callback === 'function') {
- call[events] ? call[events].push(callback) : call[events] = [callback];
- }
- return this;
- };
- //获取所有缓存数据
- LAYIM.prototype.cache = function() {
- return cache;
- };
- //打开一个自定义的会话界面
- LAYIM.prototype.chat = function(data) {
- if (!window.JSON || !window.JSON.parse) return;
- return popchat(data), this;
- };
- //设置聊天界面最小化
- LAYIM.prototype.setChatMin = function() {
- return setChatMin(), this;
- };
- //设置当前会话状态
- LAYIM.prototype.setChatStatus = function(str) {
- var thatChat = thisChat();
- if (!thatChat) return;
- var status = thatChat.elem.find('.layim-chat-status');
- return status.html(str), this;
- };
- //接受消息
- LAYIM.prototype.getMessage = function(data) {
- return getMessage(data), this;
- };
- //桌面消息通知
- LAYIM.prototype.notice = function(data) {
- return notice(data), this;
- };
- //打开添加好友/群组面板
- LAYIM.prototype.add = function(data) {
- return popAdd(data), this;
- };
- //好友分组面板
- LAYIM.prototype.setFriendGroup = function(data) {
- return popAdd(data, 'setGroup'), this;
- };
- //消息盒子的提醒
- LAYIM.prototype.msgbox = function(nums) {
- return msgbox(nums), this;
- };
- //添加好友/群
- LAYIM.prototype.addList = function(data) {
- return addList(data), this;
- };
- //删除好友/群
- LAYIM.prototype.removeList = function(data) {
- return removeList(data), this;
- };
- //设置好友在线/离线状态
- LAYIM.prototype.setFriendStatus = function(id, type) {
- var list = $('.layim-friend' + id);
- list[type === 'online' ? 'removeClass' : 'addClass']('layim-list-gray');
- };
- //解析聊天内容
- LAYIM.prototype.content = function(content) {
- return layui.data.content(content);
- };
- //主模板
- var listTpl = function(options) {
- var nodata = {
- friend: "该分组下暂无好友",
- group: "暂无群组",
- history: "暂无历史会话"
- };
- options = options || {};
- options.item = options.item || ('d.' + options.type);
- return ['{{# var length = 0; layui.each(' + options.item + ', function(i, data){ length++; }}',
- '<li layim-event="chat" data-type="' + options.type + '" data-index="{{ ' + (options
- .index || 'i') + ' }}" class="layim-' + (options.type === 'history' ? '{{i}}' : options
- .type + '{{data.id}}') +
- ' {{ data.status === "offline" ? "layim-list-gray" : "" }}"><img src="{{ data.avatar }}"><span>{{ data.username||data.groupname||data.name||"佚名" }}</span><p>{{ data.remark||data.sign||"" }}</p><span class="layim-msg-status">new</span></li>',
- '{{# }); if(length === 0){ }}', '<li class="layim-null">' + (nodata[options.type] ||
- "暂无数据") + '</li>', '{{# } }}'
- ].join('');
- };
- var elemTpl = ['<div class="layui-layim-main">', '<div class="layui-layim-info">',
- '<div class="layui-layim-user">{{ d.mine.username }}</div>', '<div class="layui-layim-status">',
- '{{# if(d.mine.status === "online"){ }}',
- '<span class="layui-icon layim-status-online" layim-event="status" lay-type="show"></span>',
- '{{# } else if(d.mine.status === "hide") { }}',
- '<span class="layui-icon layim-status-hide" layim-event="status" lay-type="show"></span>',
- '{{# } }}', '<ul class="layui-anim layim-menu-box">',
- '<li {{d.mine.status === "online" ? "class=layim-this" : ""}} layim-event="status" lay-type="online"><i class="layui-icon"></i><cite class="layui-icon layim-status-online"></cite>在线</li>',
- '<li {{d.mine.status === "hide" ? "class=layim-this" : ""}} layim-event="status" lay-type="hide"><i class="layui-icon"></i><cite class="layui-icon layim-status-hide"></cite>隐身</li>',
- '</ul>', '</div>',
- '<input class="layui-layim-remark" placeholder="编辑签名" value="{{ d.mine.remark||d.mine.sign||"" }}">',
- '</div>',
- '<ul class="layui-unselect layui-layim-tab{{# if(!d.base.isfriend || !d.base.isgroup){ }}',
- ' layim-tab-two', '{{# } }}">', '<li class="layui-icon', '{{# if(!d.base.isfriend){ }}',
- ' layim-hide', '{{# } else { }}', ' layim-this', '{{# } }}',
- '" title="联系人" layim-event="tab" lay-type="friend"></li>', '<li class="layui-icon',
- '{{# if(!d.base.isgroup){ }}', ' layim-hide', '{{# } else if(!d.base.isfriend) { }}', ' layim-this',
- '{{# } }}', '" title="群组" layim-event="tab" lay-type="group"></li>',
- '<li class="layui-icon" title="历史会话" layim-event="tab" lay-type="history"></li>', '</ul>',
- '<ul class="layui-unselect layim-tab-content {{# if(d.base.isfriend){ }}layui-show{{# } }} layim-list-friend">',
- '{{# layui.each(d.friend, function(index, item){ var spread = d.local["spread"+index]; }}', '<li>',
- '<h5 layim-event="spread" lay-type="{{ spread }}" data-ind="{{index}}"><i class="layui-icon">{{# if(spread === "true"){ }}{{# } else { }}{{# } }}</i><span>{{ item.groupname||"未命名分组"+index }}</span><em>(<cite class="layim-count" style="color:#000;"> {{ (item.list||[]).length }}</cite>)</em></h5>',
- '<ul class="layui-layim-list {{# if(spread === "true"){ }}', ' layui-show', '{{# } }}">', listTpl({
- type: "friend",
- item: "item.list",
- index: "index"
- }), '</ul>', '</li>', '{{# }); if(d.friend.length === 0){ }}',
- '<li><ul class="layui-layim-list layui-show"><li class="layim-null">暂无联系人</li></ul>', '{{# } }}',
- '</ul>',
- '<ul class="layui-unselect layim-tab-content {{# if(!d.base.isfriend && d.base.isgroup){ }}layui-show{{# } }}">',
- '<li>', '<ul class="layui-layim-list layui-show layim-list-group">', listTpl({
- type: 'group'
- }), '</ul>', '</li>', '</ul>',
- '<ul class="layui-unselect layim-tab-content {{# if(!d.base.isfriend && !d.base.isgroup){ }}layui-show{{# } }}">',
- '<li>', '<ul class="layui-layim-list layui-show layim-list-history">', listTpl({
- type: 'history'
- }), '</ul>', '</li>', '</ul>', '<ul class="layui-unselect layim-tab-content">', '<li>',
- '<ul class="layui-layim-list layui-show" id="layui-layim-search"></ul>', '</li>', '</ul>',
- '<ul class="layui-unselect layui-layim-tool">',
- '<li class="layui-icon layim-tool-search" layim-event="search" title="搜索"></li>',
- '{{# if(d.base.msgbox){ }}',
- '<li class="layui-icon layim-tool-msgbox" layim-event="msgbox" title="消息盒子"><span class="layui-anim"></span></li>',
- '{{# } }}', '{{# if(d.base.find){ }}',
- '<li class="layui-icon layim-tool-find" layim-event="find" title="查找"></li>', '{{# } }}',
- '<li class="layui-icon layim-tool-skin" layim-event="skin" title="更换背景"></li>',
- '{{# if(!d.base.copyright){ }}',
- '<li class="layui-icon layim-tool-about" layim-event="about" title="关于"></li>', '{{# } }}',
- '</ul>',
- '<div class="layui-layim-search"><input><label class="layui-icon" layim-event="closeSearch">ဇ</label></div>',
- '</div>'
- ].join('');
- //换肤模版
- var elemSkinTpl = ['<ul class="layui-layim-skin">', '{{# layui.each(d.skin, function(index, item){ }}',
- '<li><img layim-event="setSkin" src="{{ item }}"></li>', '{{# }); }}',
- '<li layim-event="setSkin"><cite>简约</cite></li>', '</ul>'
- ].join('');
- //聊天主模板
- var elemChatTpl = ['<div class="layim-chat layim-chat-{{d.data.type}}{{d.first ? " layui-show" : ""}}">',
- '<div class="layui-unselect layim-chat-title">', '<div class="layim-chat-other">',
- '<img class="layim-{{ d.data.type }}{{ d.data.id }}" src="{{ d.data.avatar }}"><span class="layim-chat-username" layim-event="{{ d.data.type==="group" ? \"groupMembers\" : \"\" }}">{{ d.data.name||"佚名" }} {{d.data.temporary ? "<cite>临时会话</cite>" : ""}} {{# if(d.data.type==="group"){ }} <em class="layim-chat-members"></em><i class="layui-icon"></i> {{# } }}</span>',
- '<p class="layim-chat-status"></p>', '</div>', '</div>', '<div class="layim-chat-main">',
- '<ul></ul>', '</div>', '<div class="layim-chat-footer">',
- '<div class="layui-unselect layim-chat-tool" data-json="{{encodeURIComponent(JSON.stringify(d.data))}}">',
- '<span class="layui-icon layim-tool-face" title="选择表情" layim-event="face"></span>',
- '{{# if(d.base && d.base.uploadImage){ }}',
- '<span class="layui-icon layim-tool-image" title="上传图片" layim-event="image"><input type="file" name="file"></span>',
- '{{# }; }}', '{{# if(d.base && d.base.uploadFile){ }}',
- '<span class="layui-icon layim-tool-image" title="发送文件" layim-event="image" data-type="file"><input type="file" name="file"></span>',
- '{{# }; }}', '{{# if(d.base && d.base.isAudio){ }}',
- '<span class="layui-icon layim-tool-audio" title="发送网络音频" layim-event="media" data-type="audio"></span>',
- '{{# }; }}', '{{# if(d.base && d.base.isVideo){ }}',
- '<span class="layui-icon layim-tool-video" title="发送网络视频" layim-event="media" data-type="video"></span>',
- '{{# }; }}', '{{# layui.each(d.base.tool, function(index, item){ }}',
- '<span class="layui-icon layim-tool-{{item.alias}}" title="{{item.title}}" layim-event="extend" lay-filter="{{ item.alias }}">{{item.icon}}</span>',
- '{{# }); }}', '{{# if(d.base && d.base.chatLog){ }}',
- '<span class="layim-tool-log" layim-event="chatLog"><i class="layui-icon"></i>聊天记录</span>',
- '{{# }; }}', '</div>', '<div class="layim-chat-textarea"><textarea></textarea></div>',
- '<div class="layim-chat-bottom">', '<div class="layim-chat-send">', '{{# if(!d.base.brief){ }}',
- '<span class="layim-send-close" layim-event="closeThisChat">关闭</span>', '{{# } }}',
- '<span class="layim-send-btn" layim-event="send">发送</span>',
- '<span class="layim-send-set" layim-event="setSend" lay-type="show"><em class="layui-edge"></em></span>',
- '<ul class="layui-anim layim-menu-box">',
- '<li {{d.local.sendHotKey !== "Ctrl+Enter" ? "class=layim-this" : ""}} layim-event="setSend" lay-type="Enter"><i class="layui-icon"></i>按Enter键发送消息</li>',
- '<li {{d.local.sendHotKey === "Ctrl+Enter" ? "class=layim-this" : ""}} layim-event="setSend" lay-type="Ctrl+Enter"><i class="layui-icon"></i>按Ctrl+Enter键发送消息</li>',
- '</ul>', '</div>', '</div>', '</div>', '</div>'
- ].join('');
- //添加好友群组模版
- var elemAddTpl = ['<div class="layim-add-box">',
- '<div class="layim-add-img"><img class="layui-circle" src="{{ d.data.avatar }}"><p>{{ d.data.name||"" }}</p></div>',
- '<div class="layim-add-remark">', '{{# if(d.data.type === "friend" && d.type === "setGroup"){ }}',
- '<p>选择分组</p>', '{{# } if(d.data.type === "friend"){ }}',
- '<select class="layui-select" id="LAY_layimGroup">',
- '{{# layui.each(d.data.group, function(index, item){ }}',
- '<option value="{{ item.id }}">{{ item.groupname }}</option>', '{{# }); }}', '</select>',
- '{{# } }}', '{{# if(d.data.type === "group"){ }}', '<p>请输入验证信息</p>',
- '{{# } if(d.type !== "setGroup"){ }}',
- '<textarea id="LAY_layimRemark" placeholder="验证信息" class="layui-textarea"></textarea>', '{{# } }}',
- '</div>', '</div>'
- ].join('');
- //聊天内容列表模版
- var elemChatMain = [
- '<li {{ d.mine ? "class=layim-chat-mine" : "" }} {{# if(d.cid){ }}data-cid="{{d.cid}}"{{# } }}>',
- '<div class="layim-chat-user"><img src="{{ d.avatar }}"><cite>', '{{# if(d.mine){ }}',
- '<i style="color:#ccc;">{{ layui.data.date(d.timestamp) }}</i>{{ d.username||"佚名" }}', '{{# } else { }}',
- '{{ d.username||"佚名" }}<i style="color:#ccc;">{{ layui.data.date(d.timestamp) }}</i>', '{{# } }}', '</cite></div>',
- '<div class="layim-chat-text">{{ layui.data.content(d.content||" ") }}</div>', '</li>'
- ].join('');
- var elemChatList =
- '<li class="layim-{{ d.data.type }}{{ d.data.id }} layim-chatlist-{{ d.data.type }}{{ d.data.id }} layim-this" layim-event="tabChat"><img src="{{ d.data.avatar }}"><span>{{ d.data.name||"佚名" }}</span>{{# if(!d.base.brief){ }}<i class="layui-icon" layim-event="closeChat">ဇ</i>{{# } }}</li>';
- //补齐数位
- var digit = function(num) {
- return num < 10 ? '0' + (num | 0) : num;
- };
- //转换时间
- layui.data.date = function(timestamp) {
- var d = new Date(timestamp || new Date());
- return d.getFullYear() + '-' + digit(d.getMonth() + 1) + '-' + digit(d.getDate()) +
- ' ' + digit(d.getHours()) + ':' + digit(d.getMinutes()) + ':' + digit(d.getSeconds());
- };
- //转换内容
- layui.data.content = function(content) {
- //支持的html标签
- var html = function(end) {
- return new RegExp('\\n*\\[' + (end || '') +
- '(code|pre|div|span|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*',
- 'g');
- };
- content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&')
- .replace(/</g, '<').replace(/>/g, '>').replace(/'/g, ''').replace(/"/g,
- '"') //XSS
- .replace(/@(\S+)(\s+?|$)/g, '@<a href="javascript:;">$1</a>$2') //转义@
- .replace(/face\[([^\s\[\]]+?)\]/g, function(face) { //转义表情
- var alt = face.replace(/^face/g, '');
- return '<img alt="' + alt + '" title="' + alt + '" src="' + faces[alt] + '">';
- })
- .replace(/img\[([^\s]+?)\]/g, function(img) { //转义图片
- return '<img class="layui-layim-photos" src="' + img.replace(/(^img\[)|(\]$)/g, '') +
- '">';
- })
- .replace(/file\([\s\S]+?\)\[[\s\S]*?\]/g, function(str) { //转义文件
- var href = (str.match(/file\(([\s\S]+?)\)\[/) || [])[1];
- var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
- if (!href) return str;
- return '<a class="layui-layim-file" href="' + href +
- '" download target="_blank"><i class="layui-icon"></i><cite>' + (text ||
- href) + '</cite></a>';
- })
- .replace(/audio\[([^\s]+?)\]/g, function(audio) { //转义音频
- return '<div class="layui-unselect layui-layim-audio" layim-event="playAudio" data-src="' +
- audio.replace(/(^audio\[)|(\]$)/g, '') +
- '"><i class="layui-icon"></i><p>音频消息</p></div>';
- })
- .replace(/video\[([^\s]+?)\]/g, function(video) { //转义音频
- return '<div class="layui-unselect layui-layim-video" layim-event="playVideo" data-src="' +
- video.replace(/(^video\[)|(\]$)/g, '') +
- '"><i class="layui-icon"></i></div>';
- })
- .replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g, function(str) { //转义链接
- var href = (str.match(/a\(([\s\S]+?)\)\[/) || [])[1];
- var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
- if (!href) return str;
- return '<a href="' + href + '" target="_blank">' + (text || href) + '</a>';
- }).replace(html(), '\<$1 $2\>').replace(html('/'), '\</$1\>') //转移HTML代码
- .replace(/\n/g, '<br>') //转义换行
- return content;
- };
- //Ajax
- var post = function(options, callback, tips) {
- options = options || {};
- return $.ajax({
- url: options.url,
- type: options.type || 'get',
- data: options.data,
- dataType: options.dataType || 'json',
- cache: false,
- success: function(res) {
- res.code == 0 ?
- callback && callback(res.data || {}) :
- layer.msg(res.msg || ((tips || 'Error') + ': LAYIM_NOT_GET_DATA'), {
- time: 5000
- });
- },
- error: function(err, msg) {
- window.console && console.log && console.error('LAYIM_DATE_ERROR:' + msg);
- }
- });
- };
- let typesData = "";
- var groupIdLi=[];
- var getallName=function(){
- var userId=localStorage.getItem("user_id")||getCookie("user_id");
- $.post("/friendTypeController/getFriendType",{"userId":userId},function (res) {
- let data = eval("("+res+")");
- $.each(data,function(k,v){
- typesData+=v.typeName+",";
- });
- })
- $.post("/api/qun/getGroupByUserId",{"userId":userId},function (res) {
- var data = eval("("+res+")");
- $.each(data,function (k,v) {
- groupIdLi.push({"groupId":v.id,"groupMaster":v.userId});
- })
- })
- }
- getallName();
- //处理初始化信息
- var cache = {
- message: {},
- chat: []
- },
- init = function(options) {
- var init = options.init || {}
- mine = init.mine || {}, local = layui.data('layim')[mine.id] || {}, obj = {
- base: options,
- local: local,
- mine: mine,
- history: local.history || {}
- }, create = function(data) {
- var mine = data.mine || {};
- var local = layui.data('layim')[mine.id] || {},
- obj = {
- base: options //基础配置信息
- ,
- local: local //本地数据
- ,
- mine: mine //我的用户信息
- ,
- friend: data.friend || [] //联系人信息
- ,
- group: data.group || [] //群组信息
- ,
- history: local.history || {} //历史会话信息
- ,
- allName:typesData
- ,
- allGroupId:groupIdLi
- };
- cache = $.extend(cache, obj);
- popim(laytpl(elemTpl).render(obj));
- if (local.close || options.min) {
- popmin();
- }
- layui.each(call.ready, function(index, item) {
- item && item(obj);
- });
- };
- cache = $.extend(cache, obj);
- if (options.brief) {
- return layui.each(call.ready, function(index, item) {
- item && item(obj);
- });
- };
- init.url ? post(init, create, 'INIT') : create(init);
- };
-
- //显示主面板
- var layimMain, popim = function(content) {
- return layer.open({
- type: 1,
- area: ['260px', '520px'],
- skin: 'layui-box layui-layim',
- title: '​',
- offset: 'rb',
- id: 'layui-layim',
- shade: false,
- anim: 2,
- resize: false,
- content: content,
- success: function(layero) {
- layimMain = layero;
- setSkin(layero);
- if (cache.base.right) {
- layero.css('margin-left', '-' + cache.base.right);
- }
- if (layimClose) {
- layer.close(layimClose.attr('times'));
- }
- //按最新会话重新排列
- var arr = [],
- historyElem = layero.find('.layim-list-history');
- historyElem.find('li').each(function() {
- arr.push($(this).prop('outerHTML'))
- });
- if (arr.length > 0) {
- arr.reverse();
- historyElem.html(arr.join(''));
- }
- banRightMenu();
- events.sign();
- },
- cancel: function(index) {
- popmin();
- var local = layui.data('layim')[cache.mine.id] || {};
- local.close = true;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- return false;
- }
- });
- };
- //屏蔽主面板右键菜单
- var banRightMenu = function() {
- layimMain.on('contextmenu', function(event) {
- event.cancelBubble = true
- event.returnValue = false;
- return false;
- });
- var hide = function() {
- layer.closeAll('tips');
- };
- //自定义历史会话右键菜单
- layimMain.find('.layim-list-history').on('contextmenu', 'li', function(e) {
- var othis = $(this);
- var html = '<ul data-id="' + othis[0].id + '" data-index="' + othis.data('index') +
- '"><li layim-event="menuHistory" data-type="one">移除该会话</li><li layim-event="menuHistory" data-type="all">清空全部会话列表</li></ul>';
- if (othis.hasClass('layim-null')) return;
- layer.tips(html, this, {
- tips: 1,
- time: 0,
- anim: 5,
- fixed: true,
- skin: 'layui-box layui-layim-contextmenu',
- success: function(layero) {
- var stopmp = function(e) {
- stope(e);
- };
- layero.off('mousedown', stopmp).on('mousedown', stopmp);
- }
- });
- $(document).off('mousedown', hide).on('mousedown', hide);
- $(window).off('resize', hide).on('resize', hide);
- });
- //自定义好友列表会话右键菜单
- layimMain.find('.layim-list-friend .layui-layim-list>li').on('contextmenu', '', function(e) {
- var othis = $(this);
- let oind = othis.index();
- var html = '<ul data-id="' + othis[0].id + '" data-index="' + othis.data('index') +
- '" data-yzind="' + oind +
- '"><li layim-event="chat" data-type="friend">发送消息</li><li layim-event="chatLog" data-type="friend">消息记录</li><li layim-event="menuFriend" data-type="delfriend">删除好友</li></ul>';
- if (othis.hasClass('layim-null')) return;
- layer.tips(html, this, {
- tips: 1,
- time: 0,
- anim: 5,
- fixed: true,
- skin: 'layui-box layui-layim-contextmenu',
- success: function(layero) {
- var stopmp = function(e) {
- stope(e);
- };
- layero.off('mousedown', stopmp).on('mousedown', stopmp);
- }
- });
- $(document).off('mousedown', hide).on('mousedown', hide);
- $(window).off('resize', hide).on('resize', hide);
- });
- //自定义好友列表会话右键菜单
- layimMain.find('.layim-list-friend>li').on('contextmenu', 'h5', function(e) {
-
- var othis = $(this);
- var html = '<ul data-id="' + othis[0].id + '" data-index="' + othis.data('ind')+'"><li layim-event="menuFriend" data-type="resetname">重命名</li><li layim-event="menuFriend" data-type="delete">删除分组</li><li layim-event="menuFriend" data-type="newadd">新建分组</li></ul>';
- if (othis.hasClass('layim-null')) return;
- layer.tips(html, this, {
- tips: 1,
- time: 0,
- anim: 5,
- fixed: true,
- skin: 'layui-box layui-layim-contextmenu',
- success: function(layero) {
- var stopmp = function(e) {
- stope(e);
- };
- layero.off('mousedown', stopmp).on('mousedown', stopmp);
- }
- });
- $(document).off('mousedown', hide).on('mousedown', hide);
- $(window).off('resize', hide).on('resize', hide);
-
- });
- //
- //自定义群列表会话右键菜单
- layimMain.find('.layim-list-group').on('contextmenu', 'li', function(e) {
- var othis = $(this);
- let oind = othis.index();
- let _obj=cache.allGroupId;
- let _id=cache["group"][oind].id;
- var userId = cache.mine.id;
- var html =`<ul data-id="${othis[0].id }" data-index="${othis.data('index')}" data-yzgroup="${oind }">
- <li layim-event="chat" data-type="group">发送消息</li>
- <li layim-event="chatLog" data-type="group">消息记录</li>
- <li layim-event="menuGroup" data-type="newadd">新建一个群</li>
- <li layim-event="menuGroup" data-type="dropout">退出群</li></ul>`;
- for(var i=0;i<_obj.length;i++){
- if(_id==_obj[i].groupId){
- if(userId==_obj[i].groupMaster){
- html=`<ul data-id="${othis[0].id }" data-index="${othis.data('index')}" data-yzgroup="${oind }">
- <li layim-event="chat" data-type="group">发送消息</li>
- <li layim-event="chatLog" data-type="group">消息记录</li>
- <li layim-event="menuGroup" data-type="newadd">新建一个群</li>
- <li layim-event="menuGroup" data-type="editinfo">修改群信息</li>
- <li layim-event="menuGroup" data-type="dissolve">解散群</li></ul>`;
- break;
- }
- }
- }
- if (othis.hasClass('layim-null')) return;
- layer.tips(html, this, {
- tips: 1,
- time: 0,
- anim: 5,
- fixed: true,
- skin: 'layui-box layui-layim-contextmenu',
- success: function(layero) {
- var stopmp = function(e) {
- stope(e);
- };
- layero.off('mousedown', stopmp).on('mousedown', stopmp);
- }
- });
- $(document).off('mousedown', hide).on('mousedown', hide);
- $(window).off('resize', hide).on('resize', hide);
- });
- }
- //主面板最小化状态
- var layimClose, popmin = function(content) {
- if (layimClose) {
- layer.close(layimClose.attr('times'));
- }
- if (layimMain) {
- layimMain.hide();
- }
- cache.mine = cache.mine || {};
- return layer.open({
- type: 1,
- title: false,
- id: 'layui-layim-close',
- skin: 'layui-box layui-layim-min layui-layim-close',
- shade: false,
- closeBtn: false,
- anim: 2,
- offset: 'rb',
- resize: false,
- content: '<img src="' + (cache.mine.avatar || (layui.cache
- .dir + 'css/pc/layim/skin/logo.jpg')) + '"><span>' + (content || cache.base
- .title || '我的LayIM') + '</span>',
- move: '#layui-layim-close img',
- success: function(layero, index) {
- layimClose = layero;
- if (cache.base.right) {
- layero.css('margin-left', '-' + cache.base.right);
- }
- layero.on('click', function() {
- layer.close(index);
- layimMain.show();
- var local = layui.data('layim')[cache.mine.id] || {};
- delete local.close;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- });
- }
- });
- };
- //显示聊天面板
- var layimChat, layimMin, chatIndex, To = {},
- popchat = function(data) {
- data = data || {};
- var chat = $('#layui-layim-chat'),
- render = {
- data: data,
- base: cache.base,
- local: cache.local
- };
- if (!data.id) {
- return layer.msg('非法用户');
- }
- if (chat[0]) {
- var list = layimChat.find('.layim-chat-list');
- var listThat = list.find('.layim-chatlist-' + data.type + data.id);
- var hasFull = layimChat.find('.layui-layer-max').hasClass('layui-layer-maxmin');
- var chatBox = chat.children('.layim-chat-box');
- //如果是最小化,则还原窗口
- if (layimChat.css('display') === 'none') {
- layimChat.show();
- }
- if (layimMin) {
- layer.close(layimMin.attr('times'));
- }
- //如果出现多个聊天面板
- if (list.find('li').length === 1 && !listThat[0]) {
- hasFull || layimChat.css('width', 800);
- list.css({
- height: layimChat.height()
- }).show();
- chatBox.css('margin-left', '200px');
- }
- //打开的是非当前聊天面板,则新增面板
- if (!listThat[0]) {
- list.append(laytpl(elemChatList).render(render));
- chatBox.append(laytpl(elemChatTpl).render(render));
- syncGray(data);
- resizeChat();
- }
- changeChat(list.find('.layim-chatlist-' + data.type + data.id));
- listThat[0] || viewChatlog();
- setHistory(data);
- hotkeySend();
- return chatIndex;
- }
- render.first = !0;
- var index = chatIndex = layer.open({
- type: 1,
- area: '600px',
- skin: 'layui-box layui-layim-chat',
- id: 'layui-layim-chat',
- title: '​',
- shade: false,
- maxmin: true,
- offset: data.offset || 'auto',
- anim: data.anim || 0,
- closeBtn: cache.base.brief ? false : 1,
- content: laytpl('<ul class="layui-unselect layim-chat-list">' + elemChatList +
- '</ul><div class="layim-chat-box">' + elemChatTpl + '</div>').render(render),
- success: function(layero) {
- layimChat = layero;
- layero.css({
- 'min-width': '500px',
- 'min-height': '420px'
- });
- syncGray(data);
- typeof data.success === 'function' && data.success(layero);
- hotkeySend();
- setSkin(layero);
- setHistory(data);
- viewChatlog();
- showOffMessage();
- //聊天窗口的切换监听
- layui.each(call.chatChange, function(index, item) {
- item && item(thisChat());
- });
- //查看大图
- layero.on('dblclick', '.layui-layim-photos', function() {
- var src = this.src;
- layer.close(popchat.photosIndex);
- layer.photos({
- photos: {
- data: [{
- "alt": "大图模式",
- "src": src
- }]
- },
- shade: 0.01,
- closeBtn: 2,
- anim: 0,
- resize: false,
- success: function(layero, index) {
- popchat.photosIndex = index;
- }
- });
- });
- },
- full: function(layero) {
- layer.style(index, {
- width: '100%',
- height: '100%'
- }, true);
- resizeChat();
- },
- resizing: resizeChat,
- restore: resizeChat,
- min: function() {
- setChatMin();
- return false;
- },
- end: function() {
- layer.closeAll('tips');
- layimChat = null;
- }
- });
- return index;
- };
- //同步置灰状态
- var syncGray = function(data) {
- $('.layim-' + data.type + data.id).each(function() {
- if ($(this).hasClass('layim-list-gray')) {
- layui.layim.setFriendStatus(data.id, 'offline');
- }
- });
- };
- //重置聊天窗口大小
- var resizeChat = function() {
- var list = layimChat.find('.layim-chat-list'),
- chatMain = layimChat.find('.layim-chat-main'),
- chatHeight = layimChat.height();
- list.css({
- height: chatHeight
- });
- chatMain.css({
- height: chatHeight - 20 - 80 - 158
- })
- };
- //设置聊天窗口最小化 & 新消息提醒
- var setChatMin = function(newMsg) {
- var thatChat = newMsg || thisChat().data,
- base = layui.layim.cache().base;
- if (layimChat && !newMsg) {
- layimChat.hide();
- }
- layer.close(setChatMin.index);
- setChatMin.index = layer.open({
- type: 1,
- title: false,
- skin: 'layui-box layui-layim-min',
- shade: false,
- closeBtn: false,
- anim: thatChat.anim || 2,
- offset: 'b',
- move: '#layui-layim-min',
- resize: false,
- area: ['182px', '50px'],
- content: '<img id="layui-layim-min" src="' + thatChat.avatar + '"><span>' + thatChat
- .name + '</span>',
- success: function(layero, index) {
- if (!newMsg) layimMin = layero;
- if (base.minRight) {
- layer.style(index, {
- left: $(window).width() - layero.outerWidth() - parseFloat(base
- .minRight)
- });
- }
- layero.find('.layui-layer-content span').on('click', function() {
- layer.close(index);
- newMsg ? layui.each(cache.chat, function(i, item) {
- popchat(item);
- }) : layimChat.show();
- if (newMsg) {
- cache.chat = [];
- chatListMore();
- }
- });
- layero.find('.layui-layer-content img').on('click', function(e) {
- stope(e);
- });
- }
- });
- };
- //打开添加好友、群组面板、好友分组面板
- var popAdd = function(data, type) {
- data = data || {};
- layer.close(popAdd.index);
- return popAdd.index = layer.open({
- type: 1,
- area: '430px',
- title: {
- friend: '添加好友',
- group: '加入群组'
- } [data.type] || '',
- shade: false,
- resize: false,
- btn: type ? ['确认', '取消'] : ['发送申请', '关闭'],
- content: laytpl(elemAddTpl).render({
- data: {
- name: data.username || data.groupname,
- avatar: data.avatar,
- group: data.group || parent.layui.layim.cache().friend || [],
- type: data.type
- },
- type: type
- }),
- yes: function(index, layero) {
- var groupElem = layero.find('#LAY_layimGroup'),
- remarkElem = layero.find('#LAY_layimRemark')
- if (type) {
- data.submit && data.submit(groupElem.val(), index);
- } else {
- data.submit && data.submit(groupElem.val(), remarkElem.val(), index);
- }
- }
- });
- };
- //切换聊天
- var changeChat = function(elem, del) {
- elem = elem || $('.layim-chat-list .' + THIS);
- var index = elem.index() === -1 ? 0 : elem.index();
- var str = '.layim-chat',
- cont = layimChat.find(str).eq(index);
- var hasFull = layimChat.find('.layui-layer-max').hasClass('layui-layer-maxmin');
- if (del) {
- //如果关闭的是当前聊天,则切换聊天焦点
- if (elem.hasClass(THIS)) {
- changeChat(index === 0 ? elem.next() : elem.prev());
- }
- var length = layimChat.find(str).length;
- //关闭聊天界面
- if (length === 1) {
- return layer.close(chatIndex);
- }
- elem.remove();
- cont.remove();
- //只剩下1个列表,隐藏左侧区块
- if (length === 2) {
- layimChat.find('.layim-chat-list').hide();
- if (!hasFull) {
- layimChat.css('width', '600px');
- }
- layimChat.find('.layim-chat-box').css('margin-left', 0);
- }
- return false;
- }
- elem.addClass(THIS).siblings().removeClass(THIS);
- cont.addClass(SHOW).siblings(str).removeClass(SHOW);
- cont.find('textarea').focus();
- //聊天窗口的切换监听
- layui.each(call.chatChange, function(index, item) {
- item && item(thisChat());
- });
- showOffMessage();
- };
- //展示存在队列中的消息
- var showOffMessage = function() {
- var thatChat = thisChat();
- var message = cache.message[thatChat.data.type + thatChat.data.id];
- if (message) {
- //展现后,删除队列中消息
- delete cache.message[thatChat.data.type + thatChat.data.id];
- }
- };
- //获取当前聊天面板
- var thisChat = LAYIM.prototype.thisChat = function() {
- if (!layimChat) return;
- var index = $('.layim-chat-list .' + THIS).index();
- var cont = layimChat.find('.layim-chat').eq(index);
- var to = JSON.parse(decodeURIComponent(cont.find('.layim-chat-tool').data('json')));
- return {
- elem: cont,
- data: to,
- textarea: cont.find('textarea')
- };
- };
- //记录初始背景
- var setSkin = function(layero) {
- var local = layui.data('layim')[cache.mine.id] || {},
- skin = local.skin;
- layero.css({
- 'background-image': skin ? 'url(' + skin + ')' : function() {
- return cache.base.initSkin ?
- 'url(' + (layui.cache.dir + 'css/modules/layim/skin/' + cache.base
- .initSkin) + ')' :
- 'none';
- }()
- });
- };
- //记录历史会话
- var setHistory = function(data) {
- var local = layui.data('layim')[cache.mine.id] || {};
- var obj = {},
- history = local.history || {};
- var is = history[data.type + data.id];
- if (!layimMain) return;
- var historyElem = layimMain.find('.layim-list-history');
- data.historyTime = new Date().getTime();
- history[data.type + data.id] = data;
- local.history = history;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- if (is) return;
- obj[data.type + data.id] = data;
- var historyList = laytpl(listTpl({
- type: 'history',
- item: 'd.data'
- })).render({
- data: obj
- });
- historyElem.prepend(historyList);
- historyElem.find('.layim-null').remove();
- };
- //发送消息
- var sendMessage = function() {
- var data = {
- username: cache.mine ? cache.mine.username : '访客',
- avatar: cache.mine ? cache.mine.avatar : (layui.cache.dir + 'css/pc/layim/skin/logo.jpg'),
- id: cache.mine ? cache.mine.id : null,
- mine: true
- };
- var thatChat = thisChat(),
- ul = thatChat.elem.find('.layim-chat-main ul');
- var maxLength = cache.base.maxLength || 3000;
- data.content = thatChat.textarea.val();
- if (data.content.replace(/\s/g, '') !== '') {
- if (data.content.length > maxLength) {
- return layer.msg('内容最长不能超过' + maxLength + '个字符')
- }
- ul.append(laytpl(elemChatMain).render(data));
- var param = {
- mine: data,
- to: thatChat.data
- },
- message = {
- username: param.mine.username,
- avatar: param.mine.avatar,
- id: param.to.id,
- type: param.to.type,
- content: param.mine.content,
- timestamp: new Date().getTime(),
- mine: true
- };
- pushChatlog(message);
- layui.each(call.sendMessage, function(index, item) {
- item && item(param);
- });
- }
- chatListMore();
- thatChat.textarea.val('').focus();
- };
- //桌面消息提醒
- var notice = function(data) {
- data = data || {};
- if (window.Notification) {
- if (Notification.permission === 'granted') {
- var notification = new Notification(data.title || '', {
- body: data.content || '',
- icon: data.avatar || 'http://tp2.sinaimg.cn/5488749285/50/5719808192/1'
- });
- } else {
- Notification.requestPermission();
- };
- }
- };
- //消息声音提醒
- var voice = function() {
- if (device.ie && device.ie < 9) return;
- var audio = document.createElement("audio");
- audio.src = layui.cache.dir + 'css/modules/layim/voice/' + cache.base.voice;
- audio.play();
- };
- //接受消息
- var messageNew = {},
- getMessage = function(data) {
- data = data || {};
- var elem = $('.layim-chatlist-' + data.type + data.id);
- var group = {},
- index = elem.index();
- data.timestamp = data.timestamp || new Date().getTime();
- if (data.fromid == cache.mine.id) {
- data.mine = true;
- }
- data.system || pushChatlog(data);
- messageNew = JSON.parse(JSON.stringify(data));
- if (cache.base.voice) {
- voice();
- }
- if ((!layimChat && data.content) || index === -1) {
- if (cache.message[data.type + data.id]) {
- cache.message[data.type + data.id].push(data)
- } else {
- cache.message[data.type + data.id] = [data];
- //记录聊天面板队列
- if (data.type === 'friend') {
- var friend;
- layui.each(cache.friend, function(index1, item1) {
- layui.each(item1.list, function(index, item) {
- if (item.id == data.id) {
- item.type = 'friend';
- item.name = item.username;
- cache.chat.push(item);
- return friend = true;
- }
- });
- if (friend) return true;
- });
- if (!friend) {
- data.name = data.username;
- data.temporary = true; //临时会话
- cache.chat.push(data);
- }
- } else if (data.type === 'group') {
- var isgroup;
- layui.each(cache.group, function(index, item) {
- if (item.id == data.id) {
- item.type = 'group';
- item.name = item.groupname;
- cache.chat.push(item);
- return isgroup = true;
- }
- });
- if (!isgroup) {
- data.name = data.groupname;
- cache.chat.push(data);
- }
- } else {
- data.name = data.name || data.username || data.groupname;
- cache.chat.push(data);
- }
- }
- if (data.type === 'group') {
- layui.each(cache.group, function(index, item) {
- if (item.id == data.id) {
- group.avatar = item.avatar;
- return true;
- }
- });
- }
- if (!data.system) {
- if (cache.base.notice) {
- notice({
- title: '来自 ' + data.username + ' 的消息',
- content: data.content,
- avatar: group.avatar || data.avatar
- });
- }
- return setChatMin({
- name: '收到新消息',
- avatar: group.avatar || data.avatar,
- anim: 6
- });
- }
- }
- if (!layimChat) return;
- //接受到的消息不在当前Tab
- var thatChat = thisChat();
- if (thatChat.data.type + thatChat.data.id !== data.type + data.id) {
- elem.addClass('layui-anim layer-anim-06');
- setTimeout(function() {
- elem.removeClass('layui-anim layer-anim-06')
- }, 300);
- }
- var cont = layimChat.find('.layim-chat').eq(index);
- var ul = cont.find('.layim-chat-main ul');
- //系统消息
- if (data.system) {
- if (index !== -1) {
- ul.append('<li class="layim-chat-system"><span>' + data.content + '</span></li>');
- }
- } else if (data.content.replace(/\s/g, '') !== '') {
- ul.append(laytpl(elemChatMain).render(data));
- }
- chatListMore();
- };
- //消息盒子的提醒
- var ANIM_MSG = 'layui-anim-loop layer-anim-05',
- msgbox = function(num) {
- var msgboxElem = layimMain.find('.layim-tool-msgbox');
- msgboxElem.find('span').addClass(ANIM_MSG).html(num);
- };
- //存储最近MAX_ITEM条聊天记录到本地
- var pushChatlog = function(message) {
- var local = layui.data('layim')[cache.mine.id] || {};
- local.chatlog = local.chatlog || {};
- var thisChatlog = local.chatlog[message.type + message.id];
- if (thisChatlog) {
- //避免浏览器多窗口时聊天记录重复保存
- var nosame;
- layui.each(thisChatlog, function(index, item) {
- if ((item.timestamp === message.timestamp &&
- item.type === message.type &&
- item.id === message.id &&
- item.content === message.content)) {
- nosame = true;
- }
- });
- if (!(nosame || message.fromid == cache.mine.id)) {
- thisChatlog.push(message);
- }
- if (thisChatlog.length > MAX_ITEM) {
- thisChatlog.shift();
- }
- } else {
- local.chatlog[message.type + message.id] = [message];
- }
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- };
- //渲染本地最新聊天记录到相应面板
- var viewChatlog = function() {
- var local = layui.data('layim')[cache.mine.id] || {},
- thatChat = thisChat(),
- chatlog = local.chatlog || {},
- ul = thatChat.elem.find('.layim-chat-main ul');
- layui.each(chatlog[thatChat.data.type + thatChat.data.id], function(index, item) {
- ul.append(laytpl(elemChatMain).render(item));
- });
- chatListMore();
- };
- //添加好友或群
- var addList = function(data) {
- var obj = {},
- has, listElem = layimMain.find('.layim-list-' + data.type);
- if (cache[data.type]) {
- if (data.type === 'friend') {
- layui.each(cache.friend, function(index, item) {
- if (data.groupid == item.id) {
- //检查好友是否已经在列表中
- layui.each(cache.friend[index].list, function(idx, itm) {
- if (itm.id == data.id) {
- return has = true
- }
- });
- if (has) return layer.msg('好友 [' + (data.username || '') + '] 已经存在列表中', {
- anim: 6
- });
- cache.friend[index].list = cache.friend[index].list || [];
- obj[cache.friend[index].list.length] = data;
- data.groupIndex = index;
- cache.friend[index].list.push(data); //在cache的friend里面也增加好友
- return true;
- }
- });
- } else if (data.type === 'group') {
- //检查群组是否已经在列表中
- layui.each(cache.group, function(idx, itm) {
- if (itm.id == data.id) {
- return has = true
- }
- });
- if (has) return layer.msg('您已是 [' + (data.groupname || '') + '] 的群成员', {
- anim: 6
- });
- obj[cache.group.length] = data;
- cache.group.push(data);
- }
- }
- if (has) return;
- var list = laytpl(listTpl({
- type: data.type,
- item: 'd.data',
- index: data.type === 'friend' ? 'data.groupIndex' : null
- })).render({
- data: obj
- });
- if (data.type === 'friend') {
- var li = listElem.find('>li').eq(data.groupIndex);
- li.find('.layui-layim-list').append(list);
- li.find('.layim-count').html(cache.friend[data.groupIndex].list.length); //刷新好友数量
- //如果初始没有好友
- if (li.find('.layim-null')[0]) {
- li.find('.layim-null').remove();
- }
- } else if (data.type === 'group') {
- listElem.append(list);
- //如果初始没有群组
- if (listElem.find('.layim-null')[0]) {
- listElem.find('.layim-null').remove();
- }
- }
- };
- //移出好友或群
- var removeList = function(data) {
- var listElem = layimMain.find('.layim-list-' + data.type);
- var obj = {};
- if (cache[data.type]) {
- if (data.type === 'friend') {
- layui.each(cache.friend, function(index1, item1) {
- layui.each(item1.list, function(index, item) {
- if (data.id == item.id) {
- var li = listElem.find('>li').eq(index1);
- var list = li.find('.layui-layim-list>li');
- li.find('.layui-layim-list>li').eq(index).remove();
- cache.friend[index1].list.splice(index,
- 1); //从cache的friend里面也删除掉好友
- li.find('.layim-count').html(cache.friend[index1].list
- .length); //刷新好友数量
- //如果一个好友都没了
- if (cache.friend[index1].list.length === 0) {
- li.find('.layui-layim-list').html(
- '<li class="layim-null">该分组下已无好友了</li>');
- }
- return true;
- }
- });
- });
- } else if (data.type === 'group') {
- layui.each(cache.group, function(index, item) {
- if (data.id == item.id) {
- listElem.find('>li').eq(index).remove();
- cache.group.splice(index, 1); //从cache的group里面也删除掉数据
- //如果一个群组都没了
- if (cache.group.length === 0) {
- listElem.html('<li class="layim-null">暂无群组</li>');
- }
- return true;
- }
- });
- }
- }
- };
- //查看更多记录
- var chatListMore = function() {
- var thatChat = thisChat(),
- chatMain = thatChat.elem.find('.layim-chat-main');
- var ul = chatMain.find('ul');
- var length = ul.find('li').length;
- if (length >= MAX_ITEM) {
- var first = ul.find('li').eq(0);
- if (!ul.prev().hasClass('layim-chat-system')) {
- ul.before('<div class="layim-chat-system"><span layim-event="chatLog">查看更多记录</span></div>');
- }
- if (length > MAX_ITEM) {
- first.remove();
- }
- }
- chatMain.scrollTop(chatMain[0].scrollHeight + 1000);
- chatMain.find('ul li:last').find('img').load(function() {
- chatMain.scrollTop(chatMain[0].scrollHeight + 1000);
- });
- };
- //快捷键发送
- var hotkeySend = function() {
- var thatChat = thisChat(),
- textarea = thatChat.textarea;
- textarea.focus();
- textarea.off('keydown').on('keydown', function(e) {
- var local = layui.data('layim')[cache.mine.id] || {};
- var keyCode = e.keyCode;
- if (local.sendHotKey === 'Ctrl+Enter') {
- if (e.ctrlKey && keyCode === 13) {
- sendMessage();
- }
- return;
- }
- if (keyCode === 13) {
- if (e.ctrlKey) {
- return textarea.val(textarea.val() + '\n');
- }
- if (e.shiftKey) return;
- e.preventDefault();
- sendMessage();
- }
- });
- };
- //表情库
- var faces = function() {
- var alt = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]",
- "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]",
- "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]",
- "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]",
- "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]",
- "[来]", "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]",
- "[钟]", "[话筒]", "[蜡烛]", "[蛋糕]"
- ],
- arr = {};
- layui.each(alt, function(index, item) {
- arr[item] = layui.cache.dir + 'images/face/' + index + '.gif';
- });
- return arr;
- }();
- var stope = layui.stope; //组件事件冒泡
- //在焦点处插入内容
- var focusInsert = function(obj, str) {
- var result, val = obj.value;
- obj.focus();
- if (document.selection) { //ie
- result = document.selection.createRange();
- document.selection.empty();
- result.text = str;
- } else {
- result = [val.substring(0, obj.selectionStart), str, val.substr(obj.selectionEnd)];
- obj.focus();
- obj.value = result.join('');
- }
- };
- //事件
- var anim = 'layui-anim-upbit',
- events = {
- //在线状态
- status: function(othis, e) {
- var hide = function() {
- othis.next().hide().removeClass(anim);
- };
- var type = othis.attr('lay-type');
- if (type === 'show') {
- stope(e);
- othis.next().show().addClass(anim);
- $(document).off('click', hide).on('click', hide);
- } else {
- var prev = othis.parent().prev();
- othis.addClass(THIS).siblings().removeClass(THIS);
- prev.html(othis.find('cite').html());
- prev.removeClass('layim-status-' + (type === 'online' ? 'hide' : 'online'))
- .addClass('layim-status-' + type);
- layui.each(call.online, function(index, item) {
- item && item(type);
- });
- }
- }
- //编辑签名
- ,
- sign: function() {
- var input = layimMain.find('.layui-layim-remark');
- input.on('change', function() {
- var value = this.value;
- layui.each(call.sign, function(index, item) {
- item && item(value);
- });
- });
- input.on('keyup', function(e) {
- var keyCode = e.keyCode;
- if (keyCode === 13) {
- this.blur();
- }
- });
- }
- //大分组切换
- ,
- tab: function(othis) {
- var index, main = '.layim-tab-content';
- var tabs = layimMain.find('.layui-layim-tab>li');
- typeof othis === 'number' ? (
- index = othis, othis = tabs.eq(index)
- ) : (
- index = othis.index()
- );
- index > 2 ? tabs.removeClass(THIS) : (
- events.tab.index = index, othis.addClass(THIS).siblings().removeClass(THIS)
- )
- layimMain.find(main).eq(index).addClass(SHOW).siblings(main).removeClass(SHOW);
- }
- //展开联系人分组
- ,
- spread: function(othis) {
- var type = othis.attr('lay-type');
- var spread = type === 'true' ? 'false' : 'true';
- var local = layui.data('layim')[cache.mine.id] || {};
- othis.next()[type === 'true' ? 'removeClass' : 'addClass'](SHOW);
- local['spread' + othis.parent().index()] = spread;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- othis.attr('lay-type', spread);
- othis.find('.layui-icon').html(spread === 'true' ? '' : '');
- }
- //搜索
- ,
- search: function(othis) {
- var search = layimMain.find('.layui-layim-search');
- var main = layimMain.find('#layui-layim-search');
- var input = search.find('input'),
- find = function(e) {
- var val = input.val().replace(/\s/);
- if (val === '') {
- events.tab(events.tab.index | 0);
- } else {
- var data = [],
- friend = cache.friend || [];
- var group = cache.group || [],
- html = '';
- for (var i = 0; i < friend.length; i++) {
- for (var k = 0; k < (friend[i].list || []).length; k++) {
- if (friend[i].list[k].username.indexOf(val) !== -1) {
- friend[i].list[k].type = 'friend';
- friend[i].list[k].index = i;
- friend[i].list[k].list = k;
- data.push(friend[i].list[k]);
- }
- }
- }
- for (var j = 0; j < group.length; j++) {
- if (group[j].groupname.indexOf(val) !== -1) {
- group[j].type = 'group';
- group[j].index = j;
- group[j].list = j;
- data.push(group[j]);
- }
- }
- if (data.length > 0) {
- for (var l = 0; l < data.length; l++) {
- html += '<li layim-event="chat" data-type="' + data[l].type +
- '" data-index="' + data[l].index + '" data-list="' + data[l].list +
- '"><img src="' + data[l].avatar + '"><span>' + (data[l].username ||
- data[l].groupname || '佚名') + '</span><p>' + (data[l].remark ||
- data[l].sign || '') + '</p></li>';
- }
- main.html(html);
- } else {
- getInpVal(val,function(_data){
- if(_data.length){
- html = '<li class="layim-null">无本地搜索结果</li>'+
- '<li class="layim-null" style="text-align:left;padding:20px 10px 4px;color:#000;">陌生人</li>';
- $.each(_data,function (k,v) {
- html += '<li style="height:60px" data-uid="'+v.id+'">'+
- '<img src="' + v.avatar + '" style="top:16px;">'+
- '<span name="nick">昵称:' + (v.nickName ||v.groupname || '佚名') + '</span>'+
- '<p name="user">账号:' + (v.userName ||'') + '</p>'+
- '<p name="sign">' + (v.remark ||v.sign || '') + '</p>'+
- ' <button type="button" class="layui-btn layui-btn-xs searchlibtn" layim-event="yzSearch">'+
- ' <i class="layui-icon" style="margin:0;"></i>'+
- '</button></li>';
- })
-
- }else{
- html = '<li class="layim-null">无搜索结果</li>'
- }
- main.html(html);
- });
- }
- events.tab(3);
- }
- };
- if (!cache.base.isfriend && cache.base.isgroup) {
- events.tab.index = 1;
- } else if (!cache.base.isfriend && !cache.base.isgroup) {
- events.tab.index = 2;
- }
- search.show();
- input.focus();
- input.off('keyup', find).on('keyup', find);
- }
- //关闭搜索
- ,
- closeSearch: function(othis) {
- othis.parent().hide();
- events.tab(events.tab.index | 0);
- },
- yzSearch:function(othis){
- let paren=othis.parent();
- let _id=paren.data("uid");
- let _img=paren.find('img').attr("src");
- let _nick=paren.find('span[name="nick"]').text();
- let _user=paren.find('p[name="user"]').text();
- let _sign=paren.find('p[name="sign"]').text();
- let _html='<ul class="layui-layim-list" style="border:1px solid #ccc;border-radius:4px;"><li style="height:60px" data-uid="'+v.id+'">'+
- '<img src="' + _img + '" style="top:16px;">'+
- '<span name="nick">昵称:' + _nick + '</span>'+
- '<p name="user">账号:' + _user + '</p>'+
- '<p name="sign">' + _sign + '</p>'+
- '</li></ul>'+
- '<textarea type="text" id="yanzremark" style="border:1px solid #ccc;border-radius:4px;width:97%;padding:4px;margin-top:10px;" rows="5" placeholder="请输入验证信息"></textarea>';
- let index14 = layer.open({
- type: 1,
- title: '添加好友',
- offset: 'auto',
- area: ["30%", "auto"],
- id: 'layerDemo2',
- content: `<div style="padding:14px">
- ${_html}
- </div>`,
- btn: ['添加', '取消'],
- yes: function() {
- let _remark=$("#yanzremark").val();
- addNewFriend(_remark,_id,function(){layer.close(index14);},2);
- },
- no: function() {
- layer.close(index14);
- }
- })
- },
- //消息盒子
- msgbox: function() {
- var msgboxElem = layimMain.find('.layim-tool-msgbox');
- layer.close(events.msgbox.index);
- msgboxElem.find('span').removeClass(ANIM_MSG).html('');
- return events.msgbox.index = layer.open({
- type: 2,
- title: '消息盒子',
- shade: false,
- maxmin: true,
- area: ['600px', '520px'],
- skin: 'layui-box layui-layer-border',
- resize: false,
- content: cache.base.msgbox
- });
- }
- //弹出查找页面
- ,
- find: function() {
- layer.close(events.find.index);
- return events.find.index = layer.open({
- type: 2,
- title: '系统管理',
- shade: false,
- maxmin: true,
- area: ['1000px', '520px'],
- skin: 'layui-box layui-layer-border',
- resize: false,
- content: cache.base.find
- });
- }
- //弹出更换背景
- ,
- skin: function() {
- layer.open({
- type: 1,
- title: '更换背景',
- shade: false,
- area: '300px',
- skin: 'layui-box layui-layer-border',
- id: 'layui-layim-skin',
- zIndex: 66666666,
- resize: false,
- content: laytpl(elemSkinTpl).render({
- skin: cache.base.skin
- })
- });
- }
- //关于
- ,
- about: function() {
- layer.alert('版权所有: ' + "易站科技服务有限公司" +
- '<br>易站科技官网:<a href="http:www.enstation.com" target="_blank">http:www.enstation.com</a>', {
- title: '关于易聊',
- shade: false
- });
- }
- //生成换肤
- ,
- setSkin: function(othis) {
- var src = othis.attr('src');
- var local = layui.data('layim')[cache.mine.id] || {};
- local.skin = src;
- if (!src) delete local.skin;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- try {
- layimMain.css({
- 'background-image': src ? 'url(' + src + ')' : 'none'
- });
- layimChat.css({
- 'background-image': src ? 'url(' + src + ')' : 'none'
- });
- } catch (e) {}
- layui.each(call.setSkin, function(index, item) {
- var filename = (src || '').replace(layui.cache.dir + 'css/modules/layim/skin/',
- '');
- item && item(filename, src);
- });
- }
- //弹出聊天面板
- ,
- chat: function(othis) {
- var local = layui.data('layim')[cache.mine.id] || {};
- var type = othis.data('type'),
- index = othis.data('index');
- var list = othis.attr('data-list') || othis.index(),
- data = {};
- let parind = othis.parent();
- if (parind.data('yzind') != undefined) {
- list = parind.data('yzind');
- index = parind.data('index');
- }
- if(parind.data('yzgroup') != undefined){
- list = parind.data('yzgroup');
- index = parind.data('index');
- }
- console.log(list)
- if (type === 'friend') {
- data = cache[type][index].list[list];
- } else if (type === 'group') {
- data = cache[type][list];
- } else if (type === 'history') {
- data = (local.history || {})[index] || {};
- }
- data.name = data.name || data.username || data.groupname;
- if (type !== 'history') {
- data.type = type;
- }
- popchat(data);
- }
- //切换聊天
- ,
- tabChat: function(othis) {
- changeChat(othis);
- }
- //关闭聊天列表
- ,
- closeChat: function(othis, e) {
- changeChat(othis.parent(), 1);
- stope(e);
- },
- closeThisChat: function() {
- changeChat(null, 1);
- }
- //展开群组成员
- ,
- groupMembers: function(othis, e) {
- var icon = othis.find('.layui-icon'),
- hide = function() {
- icon.html('');
- othis.data('down', null);
- layer.close(events.groupMembers.index);
- },
- stopmp = function(e) {
- stope(e)
- };
- if (othis.data('down')) {
- hide();
- } else {
- icon.html('');
- othis.data('down', true);
- events.groupMembers.index = layer.tips('<ul class="layim-members-list"></ul>', othis, {
- tips: 3,
- time: 0,
- anim: 5,
- fixed: true,
- skin: 'layui-box layui-layim-members',
- success: function(layero) {
- var members = cache.base.members || {},
- thatChat = thisChat(),
- ul = layero.find('.layim-members-list'),
- li = '',
- membersCache = {},
- hasFull = layimChat.find('.layui-layer-max').hasClass(
- 'layui-layer-maxmin'),
- listNone = layimChat.find('.layim-chat-list').css('display') ===
- 'none';
- if (hasFull) {
- ul.css({
- width: $(window).width() - 22 - (listNone || 200)
- });
- }
- members.data = $.extend(members.data, {
- id: thatChat.data.id
- });
- post(members, function(res) {
- layui.each(res.list, function(index, item) {
- li += '<li data-uid="' + item.id +
- '"><a href="javascript:;"><img src="' +
- item.avatar + '"><cite style="color:#000;">' + item
- .username + '</cite></a></li>';
- membersCache[item.id] = item;
- });
- ul.html(li);
- //获取群员
- othis.find('.layim-chat-members').html(res.members || (
- res.list || []).length + '人');
- //私聊
- ul.find('li').on('click', function() {
- var uid = $(this).data('uid'),
- info = membersCache[uid]
- popchat({
- name: info.username,
- type: 'friend',
- avatar: info.avatar,
- id: info.id
- });
- hide();
- });
- layui.each(call.members, function(index, item) {
- item && item(res);
- });
- });
- layero.on('mousedown', function(e) {
- stope(e);
- });
- }
- });
- $(document).off('mousedown', hide).on('mousedown', hide);
- $(window).off('resize', hide).on('resize', hide);
- othis.off('mousedown', stopmp).on('mousedown', stopmp);
- }
- }
- //发送聊天内容
- ,
- send: function() {
- sendMessage();
- }
- //设置发送聊天快捷键
- ,
- setSend: function(othis, e) {
- var box = events.setSend.box = othis.siblings('.layim-menu-box'),
- type = othis.attr('lay-type');
- if (type === 'show') {
- stope(e);
- box.show().addClass(anim);
- $(document).off('click', events.setSendHide).on('click', events.setSendHide);
- } else {
- othis.addClass(THIS).siblings().removeClass(THIS);
- var local = layui.data('layim')[cache.mine.id] || {};
- local.sendHotKey = type;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- events.setSendHide(e, othis.parent());
- }
- },
- setSendHide: function(e, box) {
- (box || events.setSend.box).hide().removeClass(anim);
- }
- //表情
- ,
- face: function(othis, e) {
- var content = '',
- thatChat = thisChat();
- for (var key in faces) {
- content += '<li title="' + key + '"><img src="' + faces[key] + '"></li>';
- }
- content = '<ul class="layui-clear layim-face-list">' + content + '</ul>';
- events.face.index = layer.tips(content, othis, {
- tips: 1,
- time: 0,
- fixed: true,
- skin: 'layui-box layui-layim-face',
- success: function(layero) {
- layero.find('.layim-face-list>li').on('mousedown', function(e) {
- stope(e);
- }).on('click', function() {
- focusInsert(thatChat.textarea[0], 'face' + this.title +
- ' ');
- layer.close(events.face.index);
- });
- }
- });
- $(document).off('mousedown', events.faceHide).on('mousedown', events.faceHide);
- $(window).off('resize', events.faceHide).on('resize', events.faceHide);
- stope(e);
- },
- faceHide: function() {
- layer.close(events.face.index);
- }
- //图片或一般文件
- ,
- image: function(othis) {
- var type = othis.data('type') || 'images',
- api = {
- images: 'uploadImage',
- file: 'uploadFile'
- },
- thatChat = thisChat(),
- conf = cache.base[api[type]] || {};
- layui.upload.render({
- url: conf.url || '',
- method: conf.type,
- elem: othis.find('input')[0],
- accept: type,
- done: function(res) {
- if (res.code == 0) {
- res.data = res.data || {};
- if (type === 'images') {
- focusInsert(thatChat.textarea[0], 'img[' + (res.data.src ||
- '') + ']');
- } else if (type === 'file') {
- focusInsert(thatChat.textarea[0], 'file(' + (res.data.src ||
- '') + ')[' + (res.data.name || '下载文件') + ']');
- }
- sendMessage();
- } else {
- layer.msg(res.msg || '上传失败');
- }
- }
- });
- }
- //音频和视频
- ,
- media: function(othis) {
- var type = othis.data('type'),
- text = {
- audio: '音频',
- video: '视频'
- },
- thatChat = thisChat()
- layer.prompt({
- title: '请输入网络' + text[type] + '地址',
- shade: false,
- offset: [
- othis.offset().top - $(window).scrollTop() - 158 + 'px', othis.offset()
- .left + 'px'
- ]
- }, function(src, index) {
- focusInsert(thatChat.textarea[0], type + '[' + src + ']');
- sendMessage();
- layer.close(index);
- });
- }
- //扩展工具栏
- ,
- extend: function(othis) {
- var filter = othis.attr('lay-filter'),
- thatChat = thisChat();
- layui.each(call['tool(' + filter + ')'], function(index, item) {
- item && item.call(othis, function(content) {
- focusInsert(thatChat.textarea[0], content);
- }, sendMessage, thatChat);
- });
- }
- //播放音频
- ,
- playAudio: function(othis) {
- var audioData = othis.data('audio'),
- audio = audioData || document.createElement('audio'),
- pause = function() {
- audio.pause();
- othis.removeAttr('status');
- othis.find('i').html('');
- };
- if (othis.data('error')) {
- return layer.msg('播放音频源异常');
- }
- if (!audio.play) {
- return layer.msg('您的浏览器不支持audio');
- }
- if (othis.attr('status')) {
- pause();
- } else {
- audioData || (audio.src = othis.data('src'));
- audio.play();
- othis.attr('status', 'pause');
- othis.data('audio', audio);
- othis.find('i').html('');
- //播放结束
- audio.onended = function() {
- pause();
- };
- //播放异常
- audio.onerror = function() {
- layer.msg('播放音频源异常');
- othis.data('error', true);
- pause();
- };
- }
- }
- //播放视频
- ,
- playVideo: function(othis) {
- var videoData = othis.data('src'),
- video = document.createElement('video');
- if (!video.play) {
- return layer.msg('您的浏览器不支持video');
- }
- layer.close(events.playVideo.index);
- events.playVideo.index = layer.open({
- type: 1,
- title: '播放视频',
- area: ['460px', '300px'],
- maxmin: true,
- shade: false,
- content: '<div style="background-color: #000; height: 100%;"><video style="position: absolute; width: 100%; height: 100%;" src="' +
- videoData + '" loop="loop" autoplay="autoplay"></video></div>'
- });
- }
- //聊天记录
- ,
- chatLog: function(othis) {
- var thatChat = thisChat();
- let parind = othis.parent();
- var type = othis.data('type')
- if (parind.data("yzind") != undefined) {
- var list = parind.data('yzind');
- var index = parind.data('index');
- var _str = cache[type][index].list[list];
- _str.name = _str.username;
- _str.type = type;
- thatChat = {
- data: _str
- };
- }
- if(parind.data("yzgroup") != undefined){
- var list = parind.data('yzgroup');
- var index = parind.data('index');
- var _str = cache[type][index];
- _str.name = _str.groupname;
- _str.type = type;
- thatChat = {
- data: _str
- };
- }
- if (!cache.base.chatLog) {
- return layer.msg('未开启更多聊天记录');
- }
- layer.close(events.chatLog.index);
- return events.chatLog.index = layer.open({
- type: 2,
- maxmin: true,
- title: '与 ' + thatChat.data.name + ' 的聊天记录',
- area: ['450px', '100%'],
- shade: false,
- offset: 'rb',
- skin: 'layui-box',
- anim: 2,
- id: 'layui-layim-chatlog',
- content: cache.base.chatLog + '?id=' + thatChat.data.id + '&type=' + thatChat
- .data.type
- });
- }
- //历史会话右键菜单操作
- ,
- menuHistory: function(othis, e) {
- var local = layui.data('layim')[cache.mine.id] || {};
- var parent = othis.parent(),
- type = othis.data('type');
- var hisElem = layimMain.find('.layim-list-history');
- var none = '<li class="layim-null">暂无历史会话</li>'
- if (type === 'one') {
- var history = local.history;
- delete history[parent.data('index')];
- local.history = history;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- //删除 DOM
- $('.layim-list-history li.layim-' + parent.data('index')).remove();
- if (hisElem.find('li').length === 0) {
- hisElem.html(none);
- }
- } else if (type === 'all') {
- delete local.history;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- hisElem.html(none);
- }
- layer.closeAll('tips');
- }
- //好友列表右键菜单操作
- ,
- menuFriend: function(othis, e) {
- var local = layui.data('layim')[cache.mine.id] || {};
- var parent = othis.parent(),
- type = othis.data('type');
- var list = parent.data('yzind');
- var index = parent.data('index');
- var userId = cache.mine.id;
- var hisElem = layimMain.find('.layim-list-friend');
- var none = '<li class="layim-null">暂无好友</li>'
- if(type=="delfriend"){
- var friendId = cache["friend"][index].list[list].id;
- let index1 = layer.open({
- type: 1,
- title: '删除好友',
- offset: 'auto',
- area: ["30%", "auto"],
- id: 'layerDemo2',
- content: '<div style="text-align:center;padding:14px">删除后你也将从对方联系人列表中消失,且以后不再接收此人的会话消息。</div>',
- btn: ['确定', '取消'],
- yes: function() {
- $.post('/friendController/delFriend', {
- "userId": userId,
- "friendId": friendId
- }, function(result) {
- if (result > 0) {
- layim.removeList({
- type: 'friend',
- id: friendId
- });
- var local = layui.data('layim')[cache.mine.id] || {};
- var hisElem = layimMain.find('.layim-list-history');
- var history = local.history;
- delete history["friend"+friendId];
- local.history = history;
- layui.data('layim', {
- key: cache.mine.id,
- value: local
- });
- //删除 DOM
- $('.layim-list-history li.layim-' + parent.data('index')).remove();
-
- if (hisElem.find('li').length === 0) {
- hisElem.html('<li class="layim-null">暂无历史会话</li>');
- }
- layer.msg('删除成功!');
- } else {
- layer.msg('删除失败!');
- }
- });
- layer.close(index1);
- },
- no: function() {
- layer.close(index1);
- }
- })
- }else if(type=="resetname"){
- var tId = cache["friend"][index].id;
- let index2 = layer.open({
- type: 1,
- title: '分组重命名',
- offset: 'auto',
- area: ["30%", "auto"],
- id: 'layerDemo2',
- content: '<div style="text-align:center;padding:14px"><input type="text" style="width:73%;padding: 10px;border:1px solid #ccc;border-radius:4px;" id="addname" placeholder="请输入新的分组名"></div>',
- btn: ['确定', '取消'],
- yes: function() {
- let _key=$("#addname").val();
- resetName(_key,tId,cache.allName,function callback(){layer.close(index2)});
- },
- no: function() {
- layer.close(index2);
- }
- })
- }else if(type=="delete"){
- var tId = cache["friend"][index].id;
- let index1 = layer.open({
- type: 1,
- title: '删除分组',
- offset: 'auto',
- area: ["30%", "auto"],
- id: 'layerDemo2',
- content: '<div style="text-align:center;padding:14px">删除该分组后,其下好友将移到默认[好友]分组,确定要删除吗?</div>',
- btn: ['删除', '取消'],
- yes: function() {
- $.post("/friendTypeController/deleteById",{"id":tId},function(result) {
- if(result>0){
- window.location.reload();
- layer.msg('删除成功!');
- }else{
- layer.msg('删除失败!');
- }
- })
- layer.close(index1);
- },
- no: function() {
- layer.close(index1);
- }
- })
-
- }else if(type=="newadd"){
- let index3 = layer.open({
- type: 1,
- title: '添加分组',
- offset: 'auto',
- area: ["30%", "auto"],
- id: 'layerDemo2',
- content: '<div style="text-align:center;padding:14px"><input type="text" style="width:73%;padding:10px;border:1px solid #ccc;border-radius:4px;" id="newname" placeholder="请输入新的分组名"></div>',
- btn: ['添加', '取消'],
- yes: function() {
- let _key=$("#newname").val().trim();
- addNewName(_key,cache.allName,function callback(){layer.close(index3)});
- },
- no: function() {
- layer.close(index3);
- }
- })
- }
- //删除 DOM
- if (hisElem.find('li').length === 0) {
- hisElem.html(none);
- }
- layer.closeAll('tips');
- }
- //群列表右键菜单操作
- ,menuGroup: function(othis, e) {
- var local = layui.data('layim')[cache.mine.id] || {};
- var type = othis.data("type");
- var parent = othis.parent();
- var index = parent.data('index');
- var _str = cache["group"][index];
- var userId = cache.mine.id;
- var none = '<li class="layim-null">暂无好友</li>'
- if (type === 'newadd') {
- let _url=cache.base.find.replace("myFriends","myGroups");
- let index1 = layer.open({
- type: 2,
- title: '系统管理',
- shade: false,
- maxmin: true,
- area: ['1000px', '520px'],
- skin: 'layui-box layui-layer-border',
- resize: false,
- content: _url
- });
- }else if(type === 'dissolve'){
- if(confirm("确定要解散该群吗?")){
- $.post(baseUrl+"/groupController/delete",{"groupId":_str.id},function(result) {
- //alert(result);
- if(result>0){
- window.location.reload();
- }else{
- layer.msg('删除失败!');
- }
- })
- }else{
- return;
- }
- }else if(type === 'dropout'){
- if(confirm("确定要退出群吗?")){
- $.post(baseUrl+'/groupController/quit',{"groupId":_str.id,"userId":userId},function(result){
- if(result>0){
- window.location.reload();
- }else{
- layer.msg('退出失败!');
- }
- });
- }else{
- return;
- }
- }else if(type==="editinfo"){
- let index4 = layer.open({
- type: 1,
- title: '修改群信息',
- offset: 'auto',
- area: ["30%", "auto"],
- id: 'layerDemo2',
- content: `<div style="text-align:center;padding:14px">
- <div style="margin-bottom:10px">
- <span>群名称:</span><input type="text" style="width:60%;padding:8px;border:1px solid #ccc;border-radius:4px;" id="newgrname" placeholder="请输入新的群名">
- </div>
- <div>
- <span>群简介:</span><textarea id="newgrremark" style="resize:none;width:60%;padding:8px;border:1px solid #ccc;border-radius:4px;" rows="3"></textarea>
- </div>
- </div>`,
- btn: ['添加', '取消'],
- yes: function() {
- let _key=$("#newgrname").val();
- let _remark=$("#newgrremark").val();
- resetgroupname(_str.id,_key,_remark,function(){layer.close(index4);});
- },
- no: function() {
- layer.close(index4);
- }
- })
- }
-
- layer.closeAll('tips');
- }
-
- };
- //暴露接口
- exports('layim', new LAYIM());
- }).addcss(
- 'modules/layim/layim.css?v=3.9.1', 'skinlayimcss'
- );
|