import { reportUrl } from '@/utils/conf.js'
import { getJsonHead } from '@/utils/header.js'
import request from '@/utils/request.js'
import Taro,{getCurrentInstance} from '@tarojs/taro'
const maxlength = 1
const MEMBER_VALIDATION = true;
let statArr = []
function checkMinmum() {
return maxlength === 1
}
function padStart(target, targetLength, padString) {
targetLength = targetLength >> 0
padString = String(typeof padString !== 'undefined' ? padString : ' ')
if (target.length > targetLength) {
return String(target)
} else {
targetLength = targetLength - target.length
if (targetLength > padString.length) {
const repeatLength = targetLength / padString.length
for (let i = 0; i < repeatLength; i++) {
padString += padString
}
}
return padString.slice(0, targetLength) + String(target)
}
}
const fillZero = (num) => {
try {
return num ? padStart(num.toString(), 2, '0') : ''
} catch (e) {
return ''
}
}
const filterData = (data) => {
const memberId = Taro.getStorageSync('memberId')
let params = {
uid: memberId ? memberId : null
}
let paramsData = {}
for (let [k, v] of Object.entries(data)) {
if (v) {
paramsData[k] = k === 'index' ? fillZero(v) : v
}
}
paramsData = Object.assign(params, paramsData)
Object.keys(paramsData).forEach((i) => !paramsData[i] && delete paramsData[i])
return paramsData
}
function getStorageDataStatus(data) {
const paramsData = filterData(data);
statArr = Taro.getStorageSync('statArr') || []
statArr.push(paramsData)
if (statArr.length <= maxlength) {
Taro.setStorageSync('statArr', statArr)
}
return statArr.length === maxlength
}
function validateMemberSwitch() {
if (!MEMBER_VALIDATION) return true;
const userInfo = Taro.getStorageSync('memberInfo') || null
const isRegistered = userInfo && userInfo.openid && userInfo.mobile;
if (isRegistered) {
return true;
}
return false;
}
export const statReport = ({ url = '', immediate = false } = {} , params = {}) => {
if (!url) { return false }
try {
params.uuid = uuid(32, 64)
params.timestamp = new Date().getTime()
const isValid = validateMemberSwitch();
if (!isValid) {
return false
}
const isEvery = checkMinmum()
if (isEvery) {
const data = filterData(params);
requestReport(data, { url })
} else {
const isValid = !immediate ? getStorageDataStatus(params) : true
if (isValid) {
const reportData = Taro.getStorageSync('statArr')
Taro.removeStorageSync('statArr')
if (Array.isArray(reportData)) {
requestReport(reportData, { url })
}
}
}
} catch (e) {
console.info(e)
}
}
function requestReport(reportData, { url = '' } = {}) {
console.info(reportData, 333);
if (reportData && reportData.sourceFormScene) {
let launchOptions = Taro.getStorageSync('launchOptions')
launchOptions.isReport = true
Taro.setStorageSync('launchOptions', launchOptions)
delete reportData.sourceFormScene
}
request({
header: getJsonHead(),
url: `${reportUrl}/data/watch/${url}`,
data: reportData,
success: function (res) {},
fail: function (err) {
console.info(`%c stat:fail`, 'color: #EE00EE')
}
}).catch(e => {
})
}
function uuid(len, radix) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
var uuid = [], i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
} else {
var r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random()*16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
}