Files
tk-mini-program/unpackage/dist/build/mp-weixin/TUIKit/components/TUISearch/utils.js
pengxiaolong c5042d08c3 优化页面
2025-06-09 19:18:38 +08:00

2 lines
4.8 KiB
JavaScript

"use strict";const e=require("../../../common/vendor.js"),t=require("./search-type-list.js"),n=require("../common/Toast/index.js"),o=require("./type.js"),s=require("../../utils/env.js"),r=require("../common/Toast/type.js"),i=(e,t)=>{var n;if(!e||!t||!t.length)return[{text:e||"",isHighlight:!1}];const o=[];for(let a=0;a<t.length;a++){const s=null==(n=t[a])?void 0:n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),r=new RegExp(s,"gi");let i;for(;null!==(i=r.exec(e));){const e=i.index,t=i.index+i[0].length-1;o.push([e,t])}}const s=[o[0]];if(o.length>1){o.sort(((e,t)=>e[0]-t[0]));for(let e=1;e<o.length;e++){const t=o[e],n=s[s.length-1];t[0]-1<=n[1]?n[1]=Math.max(n[1],t[1]):s.push(t)}}if(!s[0])return[{text:e,isHighlight:!1}];const r=[];let i=0;for(let a=0;a<s.length;a++){const t=e.substring(i,s[a][0]);t&&r.push({text:t,isHighlight:!1});const n=e.substring(s[a][0],s[a][1]+1);n&&r.push({text:n,isHighlight:!0}),i=s[a][1]+1}const l=e.substring(i);return l&&r.push({text:l,isHighlight:!1}),r};exports.debounce=(e,t)=>{let n=null;return(...o)=>{null!==n&&(clearTimeout(n),n=null),n=setTimeout((()=>e(...o)),t)}},exports.enterConversation=t=>{const o=(null==t?void 0:t.conversationID)||((null==t?void 0:t.groupID)?`GROUP${null==t?void 0:t.groupID}`:`C2C${null==t?void 0:t.userID}`);e.Xt.switchConversation(o).then((()=>{var t;e.Jt.update(e.o.SEARCH,"currentSearchingStatus",{isSearching:!1,searchType:"global"}),e.Jt.update(e.o.SEARCH,"currentSearchInputValue",{value:"",searchType:"global"}),s.isUniFrameWork&&(null==(t=e.i)||t.navigateTo({url:"/TUIKit/components/TUIChat/index"}))})).catch((t=>{console.warn("switch conversation failed:",null==t?void 0:t.message),n.Toast({message:e.Wt.t("TUISearch.进入会话失败"),type:r.TOAST_TYPE.ERROR,duration:1e3})}))},exports.generateSearchResultAvatar=t=>{var n,o,s,r;return t?t.ID?t.avatar||"https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png":t.groupID?t.avatar||`https://web.sdk.qcloud.com/im/assets/images/${null==t?void 0:t.type}.svg`:t.userID?t.avatar||"https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png":(null==(n=null==t?void 0:t.conversation)?void 0:n.conversationID)?"function"==typeof t.conversation.getAvatar?null==(o=t.conversation)?void 0:o.getAvatar():null==(r=null==(s=e.Jt.getConversationModel(t.conversation.conversationID))?void 0:s.getAvatar)?void 0:r.call(s):"":""},exports.generateSearchResultShowContent=(n,s,r,l=!0)=>{var a,u,g,c;if(null==n?void 0:n.groupID)return[{text:"groupID: ",isHighlight:!1},{text:n.groupID,isHighlight:!0}];if(null==n?void 0:n.userID)return[{text:"userID: ",isHighlight:!1},{text:n.userID,isHighlight:!0}];if((null==n?void 0:n.conversation)||(null==n?void 0:n.flow)){if(1===(null==n?void 0:n.messageCount)||(null==n?void 0:n.flow)){const t=(null==n?void 0:n.flow)?n:null==n?void 0:n.messageList[0],s=(null==(a=null==t?void 0:t.payload)?void 0:a.text)||(null==(u=null==t?void 0:t.payload)?void 0:u.fileName)||(null==(g=null==t?void 0:t.payload)?void 0:g.description),c=[];return(null==t?void 0:t.type)&&l&&t.type!==e.qt.TYPES.MSG_TEXT&&c.push({text:e.Wt.t(`TUISearch.${o.messageTypeAbstractMap[t.type]}`),isHighlight:!1}),c.push(...i(s,r)),c}return[{text:`${null==n?void 0:n.messageCount}${e.Wt.t("TUISearch.条相关")}${e.Wt.t(`TUISearch.${"allMessage"===s?"结果":null==(c=t.searchMessageTypeList[s])?void 0:c.label}`)}`,isHighlight:!1}]}return[]},exports.generateSearchResultShowName=(t,n)=>{var o,s,r;return t?t.ID?null==n?void 0:n.showName:t.groupID?t.name||t.groupID:t.userID?t.remark||t.nick||t.userID||"":(null==(o=t.conversation)?void 0:o.conversationID)?"function"==typeof t.conversation.getShowName?t.conversation.getShowName():(null==(r=null==(s=e.Jt.getConversationModel(t.conversation.conversationID))?void 0:s.getShowName)?void 0:r.call(s))||t.conversation.conversationID:"":""},exports.generateSearchResultTime=t=>{const n=(new Date).setHours(0,0,0,0),o=new Date((new Date).getFullYear(),0,1,0,0,0,0).getTime(),s=new Date(t),r=864e5,i=n-s.getTime();function l(e){return e<10?"0"+e:e.toString()}if(i<=0)return`${l(s.getHours())}:${l(s.getMinutes())}`;if(i<=r)return`${e.Wt.t("time.昨天")} ${l(s.getHours())}:${l(s.getMinutes())}`;if(i<=5184e5){const t=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"][s.getDay()];return`${e.Wt.t("time."+t)} ${l(s.getHours())}:${l(s.getMinutes())}`}return s.getTime()>=o?`${s.getMonth()+1}/${s.getDate()} ${l(s.getHours())}:${l(s.getMinutes())}`:`${s.getFullYear()}/${s.getMonth()+1}/${s.getDate()} ${l(s.getHours())}:${l(s.getMinutes())}`},exports.generateSearchResultYMD=e=>{const t=new Date(1e3*e);return`${t.getFullYear()}-${("0"+(t.getMonth()+1)).slice(-2)}-${("0"+t.getDate()).slice(-2)}`},exports.searchCloudMessages=t=>e.Qt.searchCloudMessages(t).then((e=>e)).catch((t=>(n.Toast({message:e.Wt.t("TUISearch.消息云端搜索失败:")+(null==t?void 0:t.message),type:r.TOAST_TYPE.ERROR,duration:3e3}),null)));