comparison.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <!-- 对应页面:注册用户统计-平台对比 -->
  3. <view class="fix-top-window">
  4. <view class="uni-header">
  5. <uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
  6. <view class="uni-group hide-on-phone">
  7. <view class="uni-sub-title">多个指标在不同平台数据的占比,可以直观看出各个平台引流的效果</view>
  8. </view>
  9. </view>
  10. <view class="uni-container">
  11. <view class="uni-stat--x flex mb-m" style="padding: 0 15px;">
  12. <view class="uni-stat--app-select">
  13. <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" />
  14. <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" />
  15. </view>
  16. <view class="flex" style="flex: 1;">
  17. <view class="ml-m label-text hide-on-phone">日期选择:</view>
  18. <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}" />
  19. </view>
  20. </view>
  21. <view class="dispaly-grid">
  22. <view v-for="(item,index) in chartsData" :key="index" class="uni-stat--x uni-charts-box1">
  23. <view class="label-text" style="margin: 5px 0 20px 0;">{{chartsData[index].title}}</view>
  24. <qiun-data-charts type="ring" :chartData="chartsData[index]" echartsH5 echartsApp />
  25. </view>
  26. </view>
  27. </view>
  28. <!-- #ifndef H5 -->
  29. <fix-window />
  30. <!-- #endif -->
  31. </view>
  32. </template>
  33. <script>
  34. import {
  35. mapfields,
  36. stringifyQuery,
  37. getTimeOfSomeDayAgo,
  38. division,
  39. format,
  40. debounce
  41. } from '@/js_sdk/uni-stat/util.js'
  42. export default {
  43. data() {
  44. return {
  45. query: {
  46. dimension: "day",
  47. appid: '',
  48. version_id: '',
  49. // start_time: new Date().getTime(),
  50. start_time: getTimeOfSomeDayAgo(0),
  51. },
  52. platforms: [],
  53. dayChartsData: [],
  54. monChartsData: []
  55. }
  56. },
  57. created() {
  58. this.debounceGet = debounce(() => {
  59. this.getChartData(this.query)
  60. this.getRangeCountData(this.query, 'month')
  61. }, 300);
  62. },
  63. watch: {
  64. query: {
  65. deep: true,
  66. handler(val) {
  67. this.debounceGet()
  68. }
  69. }
  70. },
  71. computed: {
  72. chartsData() {
  73. return [...this.dayChartsData, ...this.monChartsData]
  74. },
  75. versionQuery() {
  76. const {
  77. appid
  78. } = this.query
  79. const query = stringifyQuery({
  80. appid
  81. })
  82. return query
  83. }
  84. },
  85. methods: {
  86. getChartData(query, type = 'day') {
  87. query = JSON.parse(JSON.stringify(query))
  88. const today = getTimeOfSomeDayAgo(0)
  89. if (query.start_time >= today) {
  90. const now = new Date().getTime()
  91. query.start_time = [today, now]
  92. query = stringifyQuery(query, true)
  93. } else {
  94. query = stringifyQuery(query)
  95. }
  96. const db = uniCloud.database()
  97. db.collection('uni-stat-result')
  98. .where(query)
  99. .field(
  100. `active_user_count,new_user_count,total_users,platform_id`
  101. )
  102. .groupBy(`platform_id`)
  103. .groupField(
  104. `sum(active_user_count) as ${type}_active_user_count, sum(new_user_count) as ${type}_new_user_count, max(total_users) as ${type}_total_users`
  105. )
  106. .get()
  107. .then(res => {
  108. const data = res.result.data
  109. this.initChartOption(data, 'dayChartsData')
  110. })
  111. },
  112. getRangeCountData(query, type) {
  113. query = stringifyQuery(query)
  114. const db = uniCloud.database()
  115. const sub = db.collection('uni-stat-result')
  116. .where(query)
  117. .field(
  118. `active_user_count, new_user_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`
  119. )
  120. .groupBy(`year, ${type ? type + ',' : ''}platform_id`)
  121. .groupField(
  122. `sum(active_user_count) as ${type}_active_user_count, sum(new_user_count) as ${type}_new_user_count`
  123. )
  124. .orderBy(`year asc, ${type} asc`)
  125. .get()
  126. .then(res => {
  127. const data = res.result.data
  128. this.initChartOption(data, 'monChartsData', 'month')
  129. })
  130. },
  131. initChartOption(data, goal, type = 'day') {
  132. const db = uniCloud.database()
  133. db.collection('uni-stat-app-platforms').get().then(res => {
  134. const options = [{
  135. field: `${type}_new_user_count`,
  136. title: `${type === 'day' ? '日' : '月'}新增用户对比`,
  137. series: [{
  138. data: []
  139. }]
  140. }, {
  141. field: `${type}_active_user_count`,
  142. title: `${type === 'day' ? '日' : '月'}活跃用户对比`,
  143. series: [{
  144. data: []
  145. }]
  146. }]
  147. if (type === 'day') {
  148. options.unshift({
  149. field: `day_total_users`,
  150. title: `总用户数对比`,
  151. series: [{
  152. data: [],
  153. }]
  154. })
  155. }
  156. this[goal] = options
  157. const platformsData = res.result.data
  158. const platforms = {}
  159. platformsData.forEach(p => {
  160. platforms[p._id] = p.name
  161. })
  162. for (const chart of this[goal]) {
  163. const pie = chart.series[0].data
  164. const p = JSON.parse(JSON.stringify(platforms))
  165. for (const item of data) {
  166. for (const key in item) {
  167. if (chart.field === key) {
  168. const id = item.platform_id
  169. const slice = {
  170. name: p[id],
  171. value: item[key]
  172. }
  173. pie.push(slice)
  174. delete p[id]
  175. }
  176. }
  177. }
  178. for (const key in p) {
  179. const slice = {
  180. name: p[key],
  181. value: 0
  182. }
  183. pie.push(slice)
  184. }
  185. }
  186. })
  187. }
  188. }
  189. }
  190. </script>
  191. <style lang="scss">
  192. .uni-charts-box1 {
  193. padding: 10px;
  194. height: 420px;
  195. }
  196. </style>