message.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import config from '../config.js'
  2. const parseDate = (str) => new Date(str.replace(/(-|年|月)/g, '/').replace('日', ''));
  3. const dateToTime = (date) => date.getTime() / 1000;
  4. const cutTimeStr = (str) => str.substr(0,11);
  5. class Message {
  6. //注册
  7. list(){
  8. var msg_list_key=uni.getStorageSync('access_token')+'_chat_msglist';
  9. let tmp = [];
  10. let lists11 =uni.getStorageSync(msg_list_key)?uni.getStorageSync(msg_list_key):[];
  11. let list= [];
  12. let lists=[];
  13. for(var i=0;i<lists11.length;i++){
  14. try{
  15. console.log(lists11[i]['key'],uni.getStorageSync('access_token'));
  16. if(lists11[i]['key'].indexOf(uni.getStorageSync('access_token')+'_chat_')>-1) lists.push(lists11[i]);
  17. }catch(e){
  18. }
  19. }
  20. uni.setStorageSync(msg_list_key,lists);
  21. lists.forEach(v => {
  22. if(v.msg.group_id>0){
  23. }
  24. else{
  25. var avatar=v.msg.avatar;
  26. if(avatar!==null && avatar.indexOf('http')>-1) {
  27. }else avatar=config.imgUri+avatar
  28. var userinfo = {
  29. avatar:avatar,
  30. nickname:v.msg.nickname,
  31. };
  32. }
  33. v.msg = Object.assign(v.msg, (+v.msg.group_id > 0) ? getGroupInfoById(v.msg.receiver) :userinfo);
  34. if(!v.msg.none) tmp.push(v.msg);
  35. // console.log(JSON.stringify(v));
  36. });
  37. tmp.sort((a, b) => dateToTime(parseDate(b.time)) - dateToTime(parseDate(a.time)));
  38. //console.log(tmp)
  39. return tmp;
  40. }
  41. }
  42. export default new Message()