/* LVSWeb 2024 API version 1.0.0 powered by Doomeye Inc. */ class API { constructor(apiHost, token) { this.version = 'eyJ2ZW5kb3IiOiJEb29tZXllIEluYy4iLCJuYW1lIjoiTFZTV2ViIDIwMjQgQVBJIiwidmVyc2lvbiI6IjEuMC4wIn0='; this.apiHost = this.getCookie('apiHost'); this.token = this.getCookie('token'); if (this.apiHost==undefined) { this.apiHost=apiHost; } if (this.token==undefined) { this.token=token; } this.fafChars = []; for (let i=48; i<=57; i++) this.fafChars.push(String.fromCharCode(i)); for (let i=65; i<=90; i++) this.fafChars.push(String.fromCharCode(i)); for (let i=97; i<=112; i++) this.fafChars.push(String.fromCharCode(i)); this.idDB = this._iniJSONSessionStorageItem('fafIDDB', {}); this.fafDB = this._iniJSONSessionStorageItem('fafDB', {}); this.fafQueue = this._iniJSONSessionStorageItem('fafQueue', []); } setCookie(e,t,i){ var o=new Date;o.setTime(o.getTime()+1e3*i); var r="expires="+o.toUTCString(); document.cookie=e+"="+t+";"+r+";path=/" } removeCookie(e){setCookie(e,"",-1)} getCookie(e){ for(var t=e+"=",i=decodeURIComponent(document.cookie).split(";"),o=0;ok.indexOf('apiCache.')==0).forEach(k=>window.sessionStorage.removeItem(k)); } createFafRecordId() { return this._randomId('R_', 12); } getRealId(id) { if (id == null) throw 'id is null'; if (typeof id == 'number') return id; if (typeof id != 'string') throw 'id is not string'; let fafRecordId = id.trim(); if (fafRecordId.length == 0) throw 'id is empty'; let nid = this.idDB[fafRecordId]; if (nid == null) return null; if (typeof nid != 'number') throw 'id db is corrupted'; return nid; } async awaitRealId(id, callback) { let nid = this.getRealId(id); if (nid != null) { if (callback != null) callback(nid, id); return; } const ins = this; setTimeout(() => { ins.awaitRealId(id, callback); }, 5000); } async _setFafBatchByResponse(resp) { let data = await this.getResponseData(resp); if (data.fafId == undefined) throw data; if (data.batchId == undefined) throw data; this.fafDB[data.fafId] = data.batchId; this._setJSONSessionStorageItem('fafDB', this.fafDB); let index = this.fafQueue.indexOf(data.fafId); if (index < 0) return; if (index >= this.fafQueue.length) return; this.fafQueue.splice(index, 1); this._setJSONSessionStorageItem('fafQueue', this.fafQueue); } _randomId(prefix, length) { let id = prefix; for (let i=0; i{ ins._syncRealId(fafRecordId);}, 1000); return; } let data = await this.getResponseData(await fetch(`${this.apiHost}/batch/record/i/${batchId + '-' + fafRecordId}`)); if (data.id == null) { setTimeout(()=>{ ins._syncRealId(fafRecordId);}, 10000); return; } this.idDB[fafRecordId] = data.id; this._setJSONSessionStorageItem('fafIDDB', this.idDB); } /** * GET /version */ async jsondata_version() { return await this.getResponseData(await this._version()); } async _version() { var url = `${this.apiHost}/version`; console.log('fetch '+url); var pairs=[]; if (pairs.length > 0) { url += '?'; } url += pairs.join('&'); return fetch(url); } /** * GET /sdk/compatible * @param (required) sdkVersion */ async jsondata_sdk_compatible(sdkVersion) { return await this.getResponseData(await this._sdk_compatible(sdkVersion)); } async _sdk_compatible(sdkVersion) { var url = `${this.apiHost}/sdk/compatible`; console.log('fetch '+url); if (sdkVersion == null) throw 'requires sdkVersion'; url += '/'+sdkVersion; var pairs=[]; if (pairs.length > 0) { url += '?'; } url += pairs.join('&'); return fetch(url); } /** * POST /form/contact * @param (optional) name * @param (optional) email * @param (optional) phone * @param (optional) lineId * @param (optional) fbURL * @param (optional) sex * @param (optional) age * @param (optional) city * @param (optional) remark */ async jsondata_form_contact(name, email, phone, lineId, fbURL, sex, age, city, remark) { return await this.getResponseData(await this._form_contact(name, email, phone, lineId, fbURL, sex, age, city, remark)); } async _form_contact(name, email, phone, lineId, fbURL, sex, age, city, remark) { var url = `${this.apiHost}/form/contact`; console.log('fetch '+url); var bodyData = new FormData(); if (name != null) { bodyData.append('name', name); } if (email != null) { bodyData.append('email', email); } if (phone != null) { bodyData.append('phone', phone); } if (lineId != null) { bodyData.append('lineId', lineId); } if (fbURL != null) { bodyData.append('fbURL', fbURL); } if (sex != null) { bodyData.append('sex', sex); } if (age != null) { bodyData.append('age', age); } if (city != null) { bodyData.append('city', city); } if (remark != null) { bodyData.append('remark', remark); } return fetch(url, {method:'POST', body: bodyData}); } }