jiuyiUniapp/shop/utils/gio-minp/components/gio-banner/gio-banner.js

2 lines
6.7 KiB
JavaScript
Raw Normal View History

2024-12-18 15:46:27 +08:00
if(!gioGlobal.__growing__)throw"未加载 gio SDK";const gio=gioGlobal.__growing__.gio,growingio=gioGlobal.__growing__.growingio,gioEmitter=gioGlobal.__growing__.gioEmitter,userStorage=gioGlobal.__growing__.userStorage,isCDP="cdp"===gioGlobal.__growing__.entry;if(!growingio.info)throw"gio SDK 未初始化";function log(...e){growingio.vdsConfig.debug&&console.log(...e)}let baseUrl;isCDP?(growingio.vdsConfig.gtouchHost||log("如果您需要使用触达功能,请配置 gtouchHost"),baseUrl=`${growingio.vdsConfig.scheme||"https://"}${growingio.vdsConfig.gtouchHost}/marketing/automation/v3/${growingio.vdsConfig.projectId}/notifications`):baseUrl=`https://messages.growingio.com/v3/${growingio.vdsConfig.projectId}/notifications`;const bannerStore={namespace:"gio-banner",key(e,r){return`#${this.namespace}#${e}#${r||""}`},get(e,r){const t=growingio.info.getStorageSync(this.key(e,r));return t?JSON.parse(t):void 0},set(e,r,t){return growingio.info.setStorageSync(this.key(e,r),t)}},defaultMessage={id:"",content:"",name:"",rule:{target:""}};class GioBanner{constructor(){log("gio-banner 代码已引入,请继续使用<gio-banner />的方式引入组件"),this.bannerShowMap={},this.timer=null,this.listener=null,this.bannerKey=null,this.placeholderDrawable=null,this.errorReplaceDrawable=null,this.refetchPushMessage=this.refetchPushMessage.bind(this)}static getInstance(){return this.gioBanner||(this.gioBanner=new GioBanner),this.gioBanner}activite(e){this.disabled(),log("gio-banner 激活成功,初始化中…"),this.addEventListener(),this.bannerKey=e.bannerKey,this.placeholderDrawable=e.placeholderDrawable,this.errorReplaceDrawable=e.errorReplaceDrawable,this.refetchPushMessage()}disabled(){this.resetState(),this.removeEventListener(),this.removeComponentListener()}resetState(){this.timer=null,this.bannerKey=null,this.placeholderDrawable=null,this.errorReplaceDrawable=null}addEventListener(){gioEmitter.on("cs1Refresh",this.refetchPushMessage)}removeEventListener(){gioEmitter.off("cs1Refresh",this.refetchPushMessage)}addComponentListener(e){this.listener=e}removeComponentListener(){this.listener=null}triggerListener(e,r=!0){e&&this.listener&&this.listener(e,r)}formatUrl(){const e=userStorage.get("bu"),r=userStorage.get("bcs"),t=growingio.info.uid,a=gio("getUserId");let s=`${baseUrl}?url_scheme=${growingio.vdsConfig.appId}`;return s+=e?`&bu=${e}`:t?`&u=${t}`:"",s+=r?`&bcs=${r}`:a?`&cs=${a}`:""}fetchPushMessage(e){const r=this.formatUrl();return new Promise((t,a)=>{if(!e||"string"!=typeof e)return a("请设置正确的bannerKey后重试");growingio.info.request({url:r,success:r=>{this.updateUserStorage(r);const a=this.formatResponse(r,e);t(a)},fail:e=>{a(e)}})})}updateUserStorage(e){e&&e.data&&e.data.idMappings&&(userStorage.set("bcs",e.data.idMappings.bcs),userStorage.set("bu",e.data.idMappings.bu))}formatResponse(e,r){if(!(e&&e.data&&e.data.banners&&e.data.banners.length))return{name:"",key:"",messages:[]};const t=e.data.banners.find(e=>e.key===r)||{},a=(t.messages||[]).filter(this.validTimeRange).sort(this.sortMessages);return{name:t.name||"",key:t.key||"",messages:this.filterSameIndex(a)}}validTimeRange(e){const r=Date.now();return r>=(e.rule.startAt||0)&&(e.rule.endAt||r+1)>r}sortMessages(e,r){return e.index!==r.index?e.index-r.index:e.priority!==r.priority?r.priority-e.priority:r.updateAt-e.updateAt}filterSameIndex(e){const r=[];return e.forEach(e=>{r.find(r=>r.index===e.index)||r.push(e)}),r}refetchPushMessage(){this.renderStoreMessages("default"),this.fetchPushMessage(this.bannerKey).then(e=>{const r=e.messages;r.length&&(this.updateStoreMessages(r),this.triggerListener(r))}).catch(e=>{console.error("banner数据请求失败",e),this.renderStoreMessages("error")}),this.timer=setTimeout(()=>{this.refetchPushMessage()},18e5)}updateStoreMessages(e){const r=gio("getUserId");bannerStore.set(this.bannerKey,r,e)}renderStoreMessages(e="default"){const r=userStorage.get("cs1"),t=bannerStore.get(this.bannerKey,r)||[],a={error:this.renderErrorMessages,default:this.renderDefaultMessages};t.length?this.triggerListener(t):a[e].cal