comparison.vue 5.9 KB
Newer Older
M
MicroMilo 已提交
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
<template>
	<!-- 对应页面:设备统计-平台对比  -->
	<view class="fix-top-window">
		<view class="uni-header">
			<uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
			<view class="uni-group hide-on-phone">
				<!-- <view class="uni-title">平台对比</view> -->
				<view class="uni-sub-title">多个指标在不同平台数据的占比,可以直观看出各个平台引流的效果</view>
			</view>
		</view>
		<view class="uni-container">
			<view class="uni-stat--x flex mb-m" style="padding: 0px 15px;">
				<uni-data-select collection="opendb-app-list" field="appid as value, name as text" orderby="text asc" :defItem="1" label="应用选择" v-model="query.appid" :clear="false" />
				<uni-data-select collection="opendb-app-versions" :where="versionQuery" class="ml-m" field="_id as value, version as text, uni_platform as label, create_date as date" format="{label} - {text}" orderby="date desc" label="版本选择" v-model="query.version_id" />
				<view class="flex">
					<view class="ml-m label-text hide-on-phone">日期选择:</view>
					<uni-datetime-picker type="date" v-model="query.start_time" returnType="timestamp"
						:clearIcon="false" class="uni-stat-datetime-picker"
						:class="{'uni-stat__actived': !!query.start_time}" />
				</view>
			</view>
			<view class="dispaly-grid">
				<view v-for="(item,index) in chartsData" :key="index" class="uni-stat--x uni-charts-box1">
					<view class="label-text" style="margin: 5px 0 20px 0;">{{chartsData[index].title}}</view>
					<qiun-data-charts type="ring" :chartData="chartsData[index]" echartsH5 echartsApp :errorMessage="errorMessage"/>
				</view>
			</view>

		</view>

		<!-- #ifndef H5 -->
		<fix-window />
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		mapfields,
		stringifyQuery,
		getTimeOfSomeDayAgo,
		division,
		format,
		debounce
	} from '@/js_sdk/uni-stat/util.js'
	export default {
		data() {
			return {
				query: {
					dimension: "day",
					appid: '',
					version_id: '',
					start_time: getTimeOfSomeDayAgo(0),
				},
				platforms: [],
				dayChartsData: [],
				monChartsData: [],
				errorMessage: "",
			}
		},
		created() {
			this.debounceGet = debounce(() => {
				this.getAllData(this.query);
			}, 300);
		},
		watch: {
			query: {
				deep: true,
				handler(val) {
					this.debounceGet()
				}
			}
		},
		computed: {
			chartsData() {
				return [...this.dayChartsData, ...this.monChartsData]
			},
			versionQuery() {
				const {
					appid
				} = this.query
				const query = stringifyQuery({
					appid
				})
				return query
			}
		},
		methods: {
			getAllData(query) {
				if (!query.appid) {
					this.errorMessage = "请先选择应用";
					return; // 如果appid为空,则不进行查询
				}
				this.errorMessage = "";
				this.getChartData(query)
				this.getRangeCountData(query, 'month')
			},
			// 获取天的数据
			getChartData(query, type = 'day') {
				query = JSON.parse(JSON.stringify(query))
				const today = getTimeOfSomeDayAgo(0)
				if (query.start_time >= today) {
					const now = new Date().getTime()
					query.start_time = [today, now]
					query = stringifyQuery(query, true)
				} else {
					query = stringifyQuery(query)
				}
				const db = uniCloud.database()
				db.collection('uni-stat-result')
					.where(query)
					.field(
						`active_device_count,new_device_count,total_devices,platform_id`
					)
					.groupBy(`platform_id`)
					.groupField(
						`sum(active_device_count) as ${type}_active_device_count, sum(new_device_count) as ${type}_new_device_count, max(total_devices) as ${type}_total_devices`
					)
					.get()
					.then(res => {
						const data = res.result.data
						this.initChartOption(data, 'dayChartsData')
					})
			},

			// 获取月的数据
			getRangeCountData(query, type) {
				query = stringifyQuery(query)
				const db = uniCloud.database()
				const sub = db.collection('uni-stat-result')
					.where(query)
					.field(
						`active_device_count, new_device_count, platform_id, ${type}(add(new Date(0),start_time), "Asia/Shanghai") as ${type},year(add(new Date(0),start_time), "Asia/Shanghai") as year`
					)
					.groupBy(`year, ${type ? type + ',' : ''}platform_id`)
					.groupField(
						`sum(active_device_count) as ${type}_active_device_count, sum(new_device_count) as ${type}_new_device_count`
					)
					.orderBy(`year asc, ${type} asc`)
					.get()
					.then(res => {
						const data = res.result.data
						this.initChartOption(data, 'monChartsData', 'month')
					})

			},

			initChartOption(data, goal, type = 'day') {
				const db = uniCloud.database()
				db.collection('uni-stat-app-platforms').get().then(res => {
					const options = [{
						field: `${type}_new_device_count`,
						title: `${type === 'day' ? '' : ''}新增设备对比`,
						series: [{
							data: []
						}]
					}, {
						field: `${type}_active_device_count`,
						title: `${type === 'day' ? '' : ''}活跃设备对比`,
						series: [{
							data: []
						}]
					}]

					if (type === 'day') {
						options.unshift({
							field: `day_total_devices`,
							title: `总设备数对比`,
							series: [{
								data: [],
							}]
						})
					}

					this[goal] = options
					const platformsData = res.result.data
					const platforms = {}
					platformsData.forEach(p => {
						platforms[p._id] = p.name
					})

					for (const chart of this[goal]) {
						const pie = chart.series[0].data
						const p = JSON.parse(JSON.stringify(platforms))
						for (const item of data) {
							for (const key in item) {
								if (chart.field === key) {
									const id = item.platform_id
									const slice = {
										name: p[id],
										value: item[key]
									}
									pie.push(slice)
									delete p[id]
								}
							}
						}
						for (const key in p) {
							const slice = {
								name: p[key],
								value: 0
							}
							pie.push(slice)
						}
					}
				})
			}

		}

	}
</script>

<style lang="scss">
	.uni-charts-box1 {
		padding: 10px;
		height: 420px;
	}
</style>