1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| import { getSystemType } from './axiosSet' import local from '@/Global/utils/localStorage' import store from '@/store' import axios from 'axios' import Handle from '@/Global/utils/handle' import CloneDeep from 'lodash.clonedeep' import { throttle } from 'throttle-debounce'
const DelayTime = 3 * 60 * 1000 const MaxNum = 50 const _Axios = axios.create({ timeout: 55000 }) const apiUrl = 'post /v1/stat-goods/report' let timer = 0
const DataReportFn = (params = {}, config = {}, otherConfig = {}) => { try { const token = local.get('accessToken') const user = store.state.userInfo if (!token || !user.member.id) { console.log('没有token或者没有用户信息') return } const systemInfo = local.get('systemParams') if (!systemInfo || Object.keys(systemInfo).length === 0) { console.log('没有初始化') return } if (+systemInfo.stat2_is_open !== 1) { console.log('stat2_is_open !==1 => 没有打开统计') return } if (otherConfig.headers) { otherConfig.headers.Authorization = `Bearer ${local.get('accessToken')}` } else { otherConfig.headers = { Authorization: `Bearer ${local.get('accessToken')}` } } const apiUrlArr = apiUrl.split(' ') if (apiUrlArr.length === 1) { apiUrlArr.unshift('get') }
if (!params || Object.keys(params).length === 0) { console.log('没有传入params,无法统计') return }
const { goodsIdArr, actionType } = params if (!goodsIdArr || goodsIdArr.length === 0 || !Handle.isArray(goodsIdArr)) { console.log('没有传入goodsIdArr,无法统计') return } if (!actionType) { console.log('没有传入actionType,无法统计') return }
let DataReportArr = local.get('DataReportArr') || []
let entry_type = 1 const share_from = Handle.getQueryByName(window.location.href, 'share_from') || null const _index = [] const _qrcode = ['store_img', 'poster', 'qr_code', 'goods_img'] const _link = ['store_link', 'link', 'goods_link']
if (share_from) { if (_qrcode.includes(share_from)) { entry_type = 2 } else if (_link.includes(share_from)) { entry_type = 3 } else { entry_type = 1 } } else { entry_type = 1 }
const defaultStatData = { entry_type: entry_type ? +entry_type : 1, credit_open_qty: 0, goods_id: 0, exposure_qty: 0, detail_qty: 0, share_qty: 0, confirm_qty: 0, cash_qty: 0, pay_qty: 0, cancel_qty: 0 }
const _goodsIdArr = Array.from(new Set(CloneDeep(goodsIdArr))) for (let item of _goodsIdArr) { if (DataReportArr.length === 0 || (DataReportArr && !DataReportArr.some(it => +it.goods_id === +item))) { const _defaultStatData = CloneDeep(defaultStatData) for (let key in _defaultStatData) { if (key === actionType) { _defaultStatData[key] = ++_defaultStatData[key] break } } DataReportArr.push({ ..._defaultStatData, goods_id: +item }) } else { for (let obj of DataReportArr) { if (+obj.goods_id === +item) { for (let key in obj) { if (key === actionType) { obj[key] = ++obj[key] break } } break } } } }
local.set('DataReportArr', DataReportArr)
const _submit = throttle(3000, true, function () { if (!local.get('DataReportArr')) { console.log('localStorage中无DataReportArr数据, 不执行上报, 退出') return } const _params = { systemType: getSystemType(), platform_type: getSystemType(), stat: JSON.stringify(local.get('DataReportArr')) } return _Axios({ method: apiUrlArr[0], url: `${systemInfo.stat2_domain_name}${apiUrlArr[1]}`, data: _params, params: apiUrlArr[0].toLowerCase() === 'get' ? _params : {}, ...otherConfig }).then((res) => { if (+res.data.code === 0) { local.remove('DataReportArr') return res.data.data } else { throw res.data } }).catch(err => { console.log(err) }) })
if (local.get('DataReportArr') && local.get('DataReportArr').length >= +MaxNum) { _submit() return }
!timer && (timer = setTimeout(() => { clearTimeout(timer) timer = 0 _submit() }, DelayTime)) } catch (err) { console.log(err) } }
export default DataReportFn
|