/** * version 1.1.7 */ import Config from '../config/config' import $ from '../helpers/util' import store from "../store"; class apiconfig { constructor(config = {}) { var alt = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]", "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]", "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]", "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]", "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]", "[来]", "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]", "[钟]", "[话筒]", "[蜡烛]", "[蛋糕]"]; let arr = {}; alt.forEach(function(item, index){ arr[item] = 'https://media-mall-prod-1259811287.cos.ap-guangzhou.myqcloud.com/static/xcxfile/appicon/im/face/'+ index + '.gif'; }); //表情库 this.faces = arr; } ///把存聊天缓存放主文件里面 可全局调用 cacheid 缓存的对象的id cachetype对象类型(user,group) cacheMessage(val, cachetype, cacheid, tempmy) { console.info('cacheMessage'); ////聊天记录存入缓存 let msg = val; uni.getStorage({ key: 'chatim', fail(res) { if (res.data == '' || typeof (res.data) == 'undefined') { ///如果没有缓存 let tempcache = new Object(); let chatlog = new Object(); let temparr = new Array(); temparr.push(msg); if (cachetype == 'group') { chatlog['group'.concat(cacheid)] = temparr; } else { chatlog['friend'.concat(cacheid)] = temparr; } tempcache[tempmy.id] = {chatlog: chatlog}; uni.setStorage({ key: 'chatim', data: tempcache }) } }, success(res) { console.log(res.data) if (typeof (res.data[tempmy.id]) == 'undefined') { ///处理同一台设备登录过多个账户 res.data[tempmy.id] = new Object(); } ///如果有 //1判断有没有对应该用户的聊天记录 有没有都添加 if (typeof (res.data[tempmy.id]['chatlog']) == 'undefined') { res.data[tempmy.id]['chatlog'] = new Object(); if (cachetype == 'group') { res.data[tempmy.id]['chatlog']['group' + cacheid] = new Array(); res.data[tempmy.id]['chatlog']['group' + cacheid].push(msg) res.data[tempmy.id]['chatlog']['group' + cacheid] = res.data[tempmy.id]['chatlog']['group' + cacheid].slice(-10); } else { res.data[tempmy.id]['chatlog']['friend' + cacheid] = new Array(); res.data[tempmy.id]['chatlog']['friend' + cacheid].push(msg) res.data[tempmy.id]['chatlog']['friend' + cacheid] = res.data[tempmy.id]['chatlog']['friend' + cacheid].slice(-10); } } else { if (cachetype == 'group') { if (typeof (res.data[tempmy.id]['chatlog']['group' + cacheid]) == 'undefined') { res.data[tempmy.id]['chatlog']['group' + cacheid] = new Array(); } res.data[tempmy.id]['chatlog']['group' + cacheid].push(msg) res.data[tempmy.id]['chatlog']['group' + cacheid] = res.data[tempmy.id]['chatlog']['group' + cacheid].slice(-10);////只存10条记录 } else { if (typeof (res.data[tempmy.id]['chatlog']['friend' + cacheid]) == 'undefined') { res.data[tempmy.id]['chatlog']['friend' + cacheid] = new Array(); } res.data[tempmy.id]['chatlog']['friend' + cacheid].push(msg) res.data[tempmy.id]['chatlog']['friend' + cacheid] = res.data[tempmy.id]['chatlog']['friend' + cacheid].slice(-10); } } uni.setStorage({ key: 'chatim', data: res.data }) } }) } getrecentmsg_f(params, callback) { var that = this // $.request({ // url: Config.URL.user.msg_chat_lists, // data: params, // loading:false, // success: function (data, status, msg, code) { // if (status == 200) // { // //$.alert(Lang.__("已关注!")) // callback && callback(data, status, msg, code) // } // else // { // //$.alert(msg) // callback && callback(data, status, msg, code) // } // } // }) return true; } getlevemsg_f(config) { return request.post(this.pathconfig.getlevemsg, config); } updateuserinfo_f(config) { return request.post(this.pathconfig.updateuserinfo, config); } createGroup_f(config) { return request.post(this.pathconfig.createGroup, config); } handlefirend_f(config) { return request.post(this.pathconfig.handlefirend, config); } getMsgbox_f(config) { return request.post(this.pathconfig.getMsgbox, config); } sendInvite_f(config) { return request.post(this.pathconfig.sendInvite, config); } getUinfoByid_f(config) { return request.post(this.pathconfig.getUinfoByid, config); } searchuser_f(config) { return request.post(this.pathconfig.searchuser, config); } login_f(config) { return request.post(this.pathconfig.login, config); } regist_f(config) { return request.post(this.pathconfig.regist, config); } checklogout_f(config) { return request.post(this.pathconfig.checklogout, config); } registClientid_f(config) { return request.post(this.pathconfig.registClientid, config); } initInfo_f(config) { return request.post(this.pathconfig.initInfo, config); } sendMessage_f(params, callback) { console.info(params); var that = this // if (store.state.userInfo) // { // if(params.type === 'group'){ // var url = Config.URL.user.zonemsg_add_msg; // } else { // var url = Config.URL.user.msg_add; // } // $.request({ // type: 'post', // url: url, // data: params, // dataType: 'json', // loading:false, // success: (data, status, msg, code) => { // if (status == 200) // { // callback && callback(data, status, msg, code) // } // else // { // callback && callback(data, status, msg, code) // } // } // }); // } return true; } setMsgRead(msg_id, callback) { var that = this; let params = {message_id:msg_id}; $.request({ type: 'post', url: Config.URL.user.msg_set_read, data: params, dataType: 'json', success: (data, status, msg, code) => { if (status == 200) { callback && callback(data, status, msg, code) } else { callback && callback(data, status, msg, code) } } }); return true; } getMessagelog_f(config) { return request.post(this.pathconfig.getMessagelog, config); } im_decode(content){ let that = this; //支持的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, '"') //XSS .replace(/@(\S+)(\s+?|$)/g, '@$1$2') //转义@ .replace(/face\[([^\s\[\]]+?)\]/g, function(face){ //转义表情 var alt = face.replace(/^face/g, ''); return ''+ alt +''; }) .replace(/img\[([^\s]+?)\]/g, function(img){ //转义图片 //return ''; return '
 
'; }) .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 $.__('不支持该格式:file'); // #ifdef H5 return ''+ $.sprintf($.__('不支持该格式,请复制网址到浏览器打开 %s'), href) +''; // #endif // #ifndef H5 return ''+ $.sprintf($.__('不支持该格式,请复制网址到浏览器打开 %s'), href) +''; // #endif }) .replace(/audio\[([^\s]+?)\]/g, function(audio){ //转义音频 return $.__('不支持该格式:audio'); return '

音频消息

'; }) .replace(/video\[([^\s]+?)\]/g, function(video){ //转义音频 return $.__('不支持该格式:video'); return '
'; }) .replace(/\[pre([\s\S]*)\]([\s\S]*)\[\/pre\]/g, function(video){ //转义音频 return $.__('不支持该格式:code'); return '
'; }) .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 ''+ (text||href) +''; }) //.replace(html(), '\<$1 $2\>').replace(html('/'), '\') //转移HTML代码 //.replace(/\n/g, '
') //转义换行 return content; } im_decode_msg(msg){ let that = this; if (typeof msg.content.text != 'undefined') { let tmp_str = msg.content.text+''; let tmp_href = ''; //判断是否发送文件 if (tmp_href = (tmp_str.match(/file\(([\s\S]+?)\)\[/)||[])[1]) { msg.content.text = that.im_decode(tmp_str); } else if (tmp_href = (tmp_str.match(/audio\[([^\s]+?)\]/g))) { //msg.type = 'voice'; //msg.content.url = tmp_href; msg.content.text = that.im_decode(tmp_str); } else if (tmp_href = (tmp_str.match(/video\[([^\s]+?)\]/g))) { msg.content.text = that.im_decode(tmp_str); } else { msg.content.text = that.im_decode(msg.content.text+''); } } else { if (typeof msg.content != 'undefined') { let tmp_str = msg.content+''; let tmp_href = ''; //判断是否发送文件 if (tmp_href = (tmp_str.match(/file\(([\s\S]+?)\)\[/)||[])[1]) { msg.content = that.im_decode(tmp_str); } else if (tmp_href = (tmp_str.match(/audio\[([^\s]+?)\]/g))) { msg.content = that.im_decode(tmp_str); } else if (tmp_href = (tmp_str.match(/video\[([^\s]+?)\]/g))) { msg.content = that.im_decode(tmp_str); } else { msg.content = that.im_decode(tmp_str); } } } return msg; } } if (!global.$apiconfig) { global.$apiconfig = new apiconfig(); } export default global.$apiconfig;