123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- import action from "./action.js"
-
- class chat {
- push(res,fromway){
-
- if(fromway!=uni.getStorageSync('storeykey')) return false;
- var data={ id: res.data.cache_key, msg: res.data}
-
- let key = prefix(data.id);
-
- var msg_list_key = msglist_key();
- var statesource = cache(key) ? cache(key) : [];
- // console.log(statesource[statesource.length-1].msg_id,res.data.msg_id)
- if(statesource.length>0){
- if(statesource[statesource.length-1].msg_id==res.data.msg_id) return false;
- }
-
- if (data.msg.sender_id != uni.getStorageSync('access_token')) data.msg.unread = 1;
- else data.msg.unread = 0;
-
- var statelist = [];
- var msglist = cache(msg_list_key) || [];
- statesource.push(data.msg);
- // console.log(data.msg.message);
- if (data.msg.message.type != 'tips' || data.msg.message.msg_type == 'show') {
- //更新消息列表
-
- if (data.msg.isloading == 0 || data.msg.message.msg_type == 'show') {
-
- let msgindex = msglist.findIndex(v => v.key == key);
- let unreadCount = 0;
- var readtime=0;
- if (msgindex >= 0) {
- let tmp = msglist.splice(msgindex, 1);
- unreadCount = tmp[0].msg.unread || 0;
- readtime=tmp[0].msg.readtime || 0;
- }
- // console.log(data.msg);
- // console.log(data.msg.cache_key , uni.getStorageSync('cache_key'))
- if (data.msg.self != 1 && data.msg.cache_key != uni.getStorageSync('cache_key')) {
- var no_tip = 0;
- var msgnotip = uni.getStorageSync('msgnotip');
- if (!msgnotip) msgnotip = [];
- msgnotip.map(item => {
- if (item == data.msg.cache_key) {
- no_tip = 1;
- }
-
- })
- try {
- data.msg.message.content = JSON.parse(data.msg.message.content);
-
- } catch (e) {
-
- }
- if (data.msg.message.content.type == 'remind' && data.msg.message.content.remind.id == uni.getStorageSync('access_token')) {
- no_tip = 0;
- data.msg.message.content.content = data.msg.message.content.content.replace('@{' + data.msg
- .message.content.remind.id + '}', '有人@我');
- cache('remid_' + key, data.msg);
-
- }
-
- if (no_tip == 0) {
- try{
- let userinfo = cache('userInfo');
-
- if(parseInt(data.msg.group_id)>0){
- if (parseInt(userinfo.note1) != 0) action.playVoice('/static/voice/chat.mp3');
- if (parseInt(userinfo.note2) != 0) uni.vibrateLong();
- }
- else{
- if (parseInt(userinfo.note3) != 0) action.playVoice('/static/voice/friend.mp3');
- if (parseInt(userinfo.note4) != 0) uni.vibrateLong();
- }
- }catch(e){
- //TODO handle the exception
- }
-
- }
- }
- //console.log(data.msg);
- if (cache('remid_' + key)) var datamsg = cache('remid_' + key)
- else
- var datamsg = data.msg
- delete datamsg['receiver']
-
- msglist.push({
- key,
- msg: {
- ...datamsg,
- unread: data.msg.self ? unreadCount : unreadCount + 1,
- readtime:0,
- istop: 0
- }
- });
-
- uni.setStorageSync(msg_list_key, msglist);
-
-
- }
-
- // console.log(data.msg);
-
- } else {
-
- let list1 = statesource;
-
-
- //测回消息
- if (data.msg.message.msg_type == 'chat_back') {
- // #ifdef APP-PLUS
- /* plus.push.clear(); */
- // #endif
- try{
- var msg_id = data.msg.message.data.msg_id;
- }catch(e){
- //TODO handle the exception
- var msg_id = data.msg.msg_id;
- }
-
- var list = [];
- if (msg_id > 0) {
- var unread = 0;
- var tempdata = '';
- var num=0;
- for(var i=0;i<statesource.length;i++){
- var item=statesource[i];
- if (parseInt(item.msg_id) == parseInt(msg_id) ) {
-
-
- }
- else{
- list.push(item);
- if (item.message.type != 'tips') {
- tempdata = item;
- unread = unread + item.unread;
- }
-
- }
- }
-
-
- statesource= list;
-
-
- let msgindex = msglist.findIndex(v => v.key == key);
- let unreadCount = 0;
- if (msgindex >= 0) {
- let tmp = msglist.splice(msgindex, 1);
- unreadCount = tmp[0].msg.unread || 0;
- }
- msglist.push({
- key,
- msg: {
- ...tempdata,
- unread: unread,
- istop: set_msgtop(data.msg.cache_key)
- }
- });
-
- cache(msg_list_key, msglist);
-
- }
-
- }else{
- // console.log(data);
- }
-
- }
-
- uni.setStorageSync(key, statesource);
- return {'source':statesource,'msglist':msglist};
- }
-
- send(data){
-
-
- console.log(data);
- }
-
- //最近聊天记录
- msglist() {
- let tmp = [];
-
- var msg_list_key = msglist_key();
-
- let lists11 = cache(msg_list_key) ? cache(msg_list_key) : [];
- let lists = [];
- var userid=uni.getStorageSync('access_token') ;
- for (var i = 0; i < lists11.length; i++) {
- try {
- // console.log(lists11[i]['key'],uni.getStorageSync('access_token'));
- if (lists11[i]['key'].indexOf(userid + '_chat_') > -1) lists.push(lists11[i]);
- } catch (e) {
-
- }
-
- }
- lists.forEach(v => {
-
- v.msg = Object.assign(v.msg, (+v.msg.group_id > 1) ? getGroupInfoById(v.msg.group_id) :
- getUserInfo(v.msg));
-
- try {
- v.msg.message.content = JSON.parse(v.msg.message.content);
-
- } catch (e) {
-
- }
-
- if (v.msg.message.content.type == 'remind') {
- if(v.msg.message.content.remind.id == uni.getStorageSync('access_token')){
-
- v.msg.message.content.content = v.msg.message.content.content.replace('@{' + v.msg.message.content.remind.id + '}', "有人@我");
- }else{
-
- var mem = getGroupmemebrs(v.msg.group_id, v.msg.message.content.remind.id);
- if (mem.id==v.msg.message.content.remind.id) {
- var atname = mem.nickname;
- } else var atname = v.msg.message.content.remind.nickname;
- v.msg.message.content.content = v.msg.message.content.content.replace('@{' + v.msg.message.content.remind.id + '}', '@' + atname);
- }
-
-
- }
-
- // if (!v.msg.group_id) console.log(getUserInfoById(v.msg.id))
- if (!v.msg.none) tmp.push(v.msg);
-
-
- });
- let temp1 = [];
- let temp2 = [];
- tmp.map(item => {
- if (item.istop > 0) temp1.push(item)
- else temp2.push(item)
- })
- temp1.sort((a, b) => b.istop - a.istop);
- temp2.sort((a, b) => b.timestamp - a.timestamp);
- // tmp.sort((a, b) => b.timestamp - a.timestamp);
-
- action.setStatusTips();
- var res= temp1.concat(temp2);
-
- return res;
- }
-
-
- }
-
- export default new chat();
- const cache = function(key, value) {
-
- try {
- if (arguments.length == 1) {
- // console.log(key);
- return uni.getStorageSync(key)
- }
- if (arguments.length > 1) {
- if (value === null) {
- uni.removeStorageSync(key);
- } else {
- uni.setStorageSync(key, value);
- }
- }
- } catch (e) {
- uni.showToast({
- icon: 'none',
- title: '程序发生错误'
- });
- }
- };
- const parseDate = (str) => new Date(str.replace(/(-|年|月)/g, '/').replace('日', ''));
- const dateToTime = (date) => date.getTime() / 1000;
- const cutTimeStr = (str) => str.substr(0, 11);
-
- function timestampToTime(timestamp) {
- var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
- var Y = date.getFullYear();
- var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
- var D = date.getDate();
- var h = date.getHours();
- var m = date.getMinutes();
- var s = date.getSeconds();
- if (D < 10) D = '0' + D;
- if (h < 10) h = '0' + h;
- if (m < 10) m = '0' + m;
- if (s < 10) s = '0' + s;
- return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
- }
-
- function setTimeShow(timestamp) {
- var time = timestampToTime(timestamp);
-
- var now = new Date();
- var Year = now.getFullYear();
- var month = now.getMonth() + 1;
- if (month < 10) month = '0' + month;
- var day = now.getDate();
- if (day < 10) day = '0' + day;
- var today = Year + '-' + month + '-' + day;
- if (time.substr(0, 10) == today) return time.substr(11, 8);
- if (time.substr(0, 4) == Year) return time.substr(5, 14);
- return time;
- }
-
- function prefix(id) {
-
- return uni.getStorageSync('access_token') + '_chat_' + id;
- }
-
- function prefixIndex(id) {
- return uni.getStorageSync('access_token') + prefix(id) + '_index';
- }
-
- function msglist_key() {
- return prefix('msglist');
- }
-
- function set_msgtop(key) {
- var msgtop = cache('msgtop');
- var istop = 0;
- for (var i = 0; i < msgtop.length; i++) {
- if ( msgtop[i]== key) {
- return true;
- }
- }
- return false;
- }
-
-
- function set_senderlist(sender) {
- var list = uni.getStorageSync(prefix('senderlist'));
-
- var isin = 0;
- if (list.length > 0) {
-
- for (var i = 0; i < list.length; i++) {
- if (parseInt(list[i].id) == parseInt(sender.id) && isin == 0) {
- list[i].nickname = sender.nickname;
- list[i].avatar = sender.avatar;
- isin = 1;
- }
-
- }
-
- } else var list = Array();
- if (isin == 0) list.push(sender);
-
-
- uni.setStorageSync(prefix('senderlist'), list);
-
- }
-
- function getUserInfoById(id) {
- if (id > 0) {
-
- var result = {
- id,
- avatar: config.imgUri + '/uploads/avatar.jpg',
- nickname: '未知用户',
- };
- let list = uni.getStorageSync(uni.getStorageSync('access_token') + '_frienddata');
- if (!list) list = [];
- list.forEach(v => {
- if (v.id == id) {
- result.nickname = v.nickname;
-
- if (v.avatar.indexOf('http') > -1) {
-
- } else v.avatar = config.imgUri + v.avatar
-
- result.avatar = v.avatar;
- }
-
- })
- } else {
- var system = uni.getStorageSync('system');
-
- var result = {
- id,
- avatar: config.imgUri + system['admin_logo'],
- nickname: system['admin_nickname'],
- };
- }
-
-
-
-
-
-
- return result;
- }
-
- function getGroupInfoById(id) {
- var groups = uni.getStorageSync(uni.getStorageSync('access_token') + '_groups');
- for (var i = 0; i < groups.length; i++) {
- if (groups[i].id == id) {
- var avatar = groups[i].avatar;
- if (avatar !== null && avatar.indexOf('http') > -1) {
-
- } else avatar = config.imgUri + avatar
- var result = {
- id: id,
- avatar: avatar,
- nickname: groups[i].nickname,
- none: false
- };
-
-
- return result;
-
- }
- }
-
- return {
- id,
- avatar: config.imgUri + '/uploads/group.jpg',
- nickname: '未知群组',
- none: false
- };
-
-
- }
-
- function getGroupmemebrs(group_id, userid) {
- var members = uni.getStorageSync(group_id + '_group_members');
- if (!members) members = [];
- let result = [];
- members.forEach(item => {
- if (item.id == userid) result = item;
-
- })
- return result;
- }
-
- function get_chat_list(id) {
-
- try{
- var temp = cache(prefix(id)) ? cache(prefix(id)) : [];
-
- return temp;
- }catch(e){
- //TODO handle the exception
- return [];
- }
- }
-
- function get_chatold_list(id) {
- try{
- var temp = cache(prefix('old' + id)) ? cache(prefix('old' + id)) : [];
- return lists;
- }catch(e){
- //TODO handle the exception
- return [];
- }
-
- }
-
- function msg_vocice(data) {
- if (data.msg.self != 1 && data.msg.cache_key != uni.getStorageSync('cache_key')) {
- var no_tip = 0;
- var msgnotip = uni.getStorageSync('msgnotip');
- if (!msgnotip) msgnotip = [];
- msgnotip.map(item => {
- if (item.storekey == data.msg.cache_key) {
- no_tip = 1;
-
- }
-
- })
- if (data.msg.message.content.type == 'remind' && data.msg.message.content.remind.id == uni.getStorageSync(
- 'access_token')) {
- no_tip = 0;
- data.msg.message.content.content = data.msg.message.content.content.replace('@{' + data.msg.message.content
- .remind.id + '}', '有人@我');
- cache('remid_' + key, data.msg);
-
- }
-
- if (no_tip == 0) {
- let userinfo = cache('userInfo');
-
- // console.log(userinfo);
- if (parseInt(userinfo.note2) != 0) uni.vibrateLong();
- if (parseInt(userinfo.note1) != 0) action.playVoice('/static/voice/chat.mp3');
-
- }
- }
-
- return data;
-
-
- }
-
- function httpString(str) {
-
- var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
- // console.log(str);
-
- try{
-
- var s = str.match(reg);
- if (s != null) {
- return s;
- } else {
- return false;
- }
- }catch(e){
- return false;
- }
-
-
-
- }
-
- function get_senderinfo(v) {
-
-
- try {
- if (v.group_id > 0) {
- var mem = getGroupmemebrs(v.group_id, v.sender_id);
- if (mem.id > 0) v.sender = mem;
- // console.log(mem);
- }
- var avatar = v.sender.avatar;
- if (avatar.indexOf('http') > -1) {
-
- } else avatar = config.imgUri + avatar
- var obj = {
- id: v.sender_id,
- avatar: avatar,
- nickname: v.sender.nickname,
- };
- } catch (e) {
- // console.log(v);
- var obj = {
- id: v.sender_id,
- avatar: '',
- nickname: v.nickname,
- };
- }
-
- return obj;
- }
-
- function message_format(v) {
-
- try {
- v.message.content = JSON.parse(v.message.content);
- var type = v.message.content.type;
-
- } catch (e) {
- //TODO handle the exception
-
- var type = v.message.content.type;
- }
-
-
- if (type == 'remind') {
-
- var mem = getGroupmemebrs(v.group_id, v.message.content.remind.id);
- if (mem.id==v.message.content.remind.id ) {
- var atname = mem.nickname;
- } else
- var atname = v.message.content.remind.nickname;
-
- v.message.content.content = v.message.content.content.replace('@{' + v.message.content.remind.id + '}', '@' +
- atname);
-
- }
- if (v.message.type == 'text' && v.message.content.type != 'emotion') {
-
- if (type == 'remind')
- var content = v.message.content.content;
- else var content = v.message.content;
-
- var res = httpString(content);
- if (res != false) {
- v.message.type = 'url';
- v.message.content = {};
- v.message.content.content = content;
- v.message.content.urls = res;
- }
- }
-
- return v;
- }
|