comparison.vue 5.5 KB

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