activity.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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">
  7. <view class="uni-sub-title hide-on-phone">用户活跃度分析</view>
  8. </view>
  9. </view>
  10. <view class="uni-container">
  11. <view class="uni-stat--x flex p-1015">
  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="应用选择" @change="changeAppid" 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>
  17. <view class="uni-stat--x flex">
  18. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" :yesterday="false" @change="changeTimeRange" />
  19. <uni-datetime-picker type="datetimerange" :end="new Date().getTime()" v-model="query.start_time" returnType="timestamp" :clearIcon="false" class="uni-stat-datetime-picker" :class="{'uni-stat__actived': currentDateTab < 0 && !!query.start_time.length}" @change="useDatetimePicker" />
  20. </view>
  21. <view class="uni-stat--x">
  22. <uni-stat-tabs label="平台选择" type="boldLine" mode="platform" v-model="query.platform_id" @change="changePlatform" />
  23. <uni-data-select ref="version-select" v-if="query.platform_id && query.platform_id.indexOf('==') === -1" collection="uni-stat-app-channels" :where="channelQuery" class="p-channel" field="_id as value, channel_name as text" orderby="text asc" label="渠道/场景值选择" v-model="query.channel_id" />
  24. </view>
  25. <view class="uni-stat--x p-m">
  26. <view class="label-text mb-l">
  27. 趋势图
  28. </view>
  29. <uni-stat-tabs type="box" :tabs="chartTabs" class="mb-l" @change="changeChartTab" />
  30. <view class="uni-charts-box">
  31. <qiun-data-charts type="area" :chartData="chartData" echartsH5 echartsApp :errorMessage="errorMessage"/>
  32. </view>
  33. </view>
  34. <view class="uni-stat--x p-m">
  35. <uni-stat-table :data="tableData" :filedsMap="fieldsMap" :loading="loading" tooltip />
  36. <view class="uni-pagination-box">
  37. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  38. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  39. @pageSizeChange="changePageSize" />
  40. </view>
  41. </view>
  42. </view>
  43. <!-- #ifndef H5 -->
  44. <fix-window />
  45. <!-- #endif -->
  46. </view>
  47. </template>
  48. <script>
  49. import {
  50. mapfields,
  51. stringifyQuery,
  52. stringifyField,
  53. stringifyGroupField,
  54. getTimeOfSomeDayAgo,
  55. division,
  56. format,
  57. formatDate,
  58. maxDeltaDay,
  59. debounce,
  60. } from '@/js_sdk/uni-stat/util.js'
  61. import fieldsMap from './fieldsMap.js'
  62. export default {
  63. data() {
  64. return {
  65. tableName: 'uni-stat-result',
  66. fieldsMap,
  67. query: {
  68. dimension: "day",
  69. appid: '',
  70. platform_id: '',
  71. uni_platform: '',
  72. version_id: '',
  73. channel_id: '',
  74. start_time: [],
  75. },
  76. options: {
  77. pageSize: 20,
  78. pageCurrent: 1, // 当前页
  79. total: 0, // 数据总量
  80. },
  81. loading: false,
  82. currentDateTab: 0,
  83. currentChartTab: 'day',
  84. tableData: [],
  85. chartData: {},
  86. channelData: [],
  87. tabName: '日活',
  88. errorMessage: "",
  89. }
  90. },
  91. computed: {
  92. chartTabs() {
  93. const tabs = [{
  94. _id: 'day',
  95. name: '日活'
  96. }, {
  97. _id: 'week',
  98. name: '周活'
  99. }, {
  100. _id: 'month',
  101. name: '月活'
  102. }]
  103. if (maxDeltaDay(this.query.start_time, 7)) {
  104. tabs.forEach((tab, index) => {
  105. if (tab._id === 'month') {
  106. tab.disabled = true
  107. } else {
  108. tab.disabled = false
  109. }
  110. })
  111. }
  112. return tabs
  113. },
  114. channelQuery() {
  115. const platform_id = this.query.platform_id
  116. return stringifyQuery({
  117. platform_id
  118. })
  119. },
  120. versionQuery() {
  121. const {
  122. appid,
  123. uni_platform
  124. } = this.query
  125. const query = stringifyQuery({
  126. appid,
  127. uni_platform,
  128. })
  129. return query
  130. },
  131. },
  132. created() {
  133. this.debounceGet = debounce(() => {
  134. this.getAllData(this.query);
  135. }, 300);
  136. this.getChannelData()
  137. },
  138. watch: {
  139. query: {
  140. deep: true,
  141. handler(val) {
  142. this.options.pageCurrent = 1 // 重置分页
  143. this.debounceGet()
  144. }
  145. }
  146. },
  147. methods: {
  148. useDatetimePicker() {
  149. this.currentDateTab = -1
  150. },
  151. changeAppid(id) {
  152. this.getChannelData(id, false)
  153. },
  154. changePlatform(id, index, name, item) {
  155. this.getChannelData(null, id)
  156. this.query.version_id = 0
  157. this.query.uni_platform = item.code
  158. },
  159. changeTimeRange(id, index) {
  160. this.currentDateTab = index
  161. const day = 24 * 60 * 60 * 1000
  162. let start, end
  163. start = getTimeOfSomeDayAgo(id)
  164. if (!id) {
  165. end = getTimeOfSomeDayAgo(0) + day - 1
  166. } else {
  167. end = getTimeOfSomeDayAgo(0) - 1
  168. }
  169. this.query.start_time = [start, end]
  170. },
  171. changePageCurrent(e) {
  172. this.options.pageCurrent = e.current
  173. this.getTabelData(this.query)
  174. },
  175. changePageSize(pageSize) {
  176. this.options.pageSize = pageSize
  177. this.options.pageCurrent = 1 // 重置分页
  178. this.getTabelData(this.query)
  179. },
  180. changeChartTab(type, index, name) {
  181. this.currentChartTab = type
  182. this.tabName = name
  183. this.getChartData(this.query, type, name)
  184. },
  185. getAllData(query) {
  186. if (!query.appid) {
  187. this.errorMessage = "请先选择应用";
  188. return; // 如果appid为空,则不进行查询
  189. }
  190. this.errorMessage = "";
  191. this.getChartData(query, this.currentChartTab, this.tabName)
  192. this.getTabelData(query)
  193. },
  194. getChartData(query, type, name = '日活', field = 'active_device_count') {
  195. this.chartData = {}
  196. const options = {
  197. categories: [],
  198. series: [{
  199. name,
  200. data: []
  201. }]
  202. }
  203. query = stringifyQuery(query, false, ['uni_platform'])
  204. console.log('query: ', query)
  205. const db = uniCloud.database()
  206. if (type === 'day') {
  207. db.collection(this.tableName)
  208. .where(query)
  209. .field(`${stringifyField(fieldsMap, field)}, start_time`)
  210. .groupBy('start_time')
  211. .groupField(stringifyGroupField(fieldsMap, field))
  212. .orderBy('start_time', 'asc')
  213. .get({
  214. getCount: true
  215. })
  216. .then(res => {
  217. const {
  218. count,
  219. data
  220. } = res.result
  221. this.chartData = []
  222. for (const item of data) {
  223. const x = formatDate(item.start_time, 'day')
  224. const y = item[field]
  225. options.series[0].data.push(y)
  226. options.categories.push(x)
  227. }
  228. this.chartData = options
  229. }).catch((err) => {
  230. console.error(err)
  231. })
  232. } else {
  233. // 周、月范围的处理
  234. this.getRangeCountData(query, type).then(res => {
  235. const oldType = type
  236. if (type === 'week') type = 'isoWeek'
  237. const {
  238. count,
  239. data
  240. } = res.result
  241. this.chartData = []
  242. const wunWeekTime = 7 * 24 * 60 * 60 * 1000
  243. for (const item of data) {
  244. const date = +new Date(item.year, 0) + (Number(item[type]) * wunWeekTime - 1)
  245. const x = formatDate(date, oldType)
  246. const y = item[type + '_' + field]
  247. if (y) {
  248. options.series[0].data.push(y)
  249. options.categories.push(x)
  250. }
  251. }
  252. this.chartData = options
  253. })
  254. }
  255. },
  256. getTabelData(queryData, field = 'active_device_count') {
  257. const {
  258. pageCurrent
  259. } = this.options
  260. let query = stringifyQuery(queryData)
  261. this.loading = true
  262. const db = uniCloud.database()
  263. db.collection(this.tableName)
  264. .where(query)
  265. .field(`${stringifyField(fieldsMap, field)}, start_time`)
  266. .groupBy('start_time')
  267. .groupField(stringifyGroupField(fieldsMap, field))
  268. .orderBy('start_time', 'desc')
  269. .skip((pageCurrent - 1) * this.options.pageSize)
  270. .limit(this.options.pageSize)
  271. .get({
  272. getCount: true
  273. })
  274. .then(res => {
  275. const {
  276. count,
  277. data
  278. } = res.result
  279. let daysData = data,
  280. daysCount = count,
  281. weeks = [],
  282. months = []
  283. // 获取周活、月活
  284. // stringifyQuery(queryData)
  285. let query = JSON.parse(JSON.stringify(queryData))
  286. query.dimension = 'week'
  287. this.getRangeCountData(stringifyQuery(query), 'week').then(res => {
  288. const {
  289. count,
  290. data
  291. } = res.result
  292. weeks = data
  293. // queryData.dimension = 'month'
  294. let query = JSON.parse(JSON.stringify(queryData))
  295. query.dimension = 'month'
  296. this.getRangeCountData(stringifyQuery(query), 'month').then(res => {
  297. const {
  298. count,
  299. data
  300. } = res.result
  301. months = data
  302. const allData = this.mapWithWeekAndMonth(daysData, weeks, months)
  303. for (const item of allData) {
  304. mapfields(fieldsMap, item, item)
  305. }
  306. this.tableData = []
  307. this.options.total = daysCount
  308. this.tableData = allData
  309. }).finally(() => {
  310. this.loading = false
  311. })
  312. })
  313. }).catch((err) => {
  314. console.error(err)
  315. // err.message 错误信息
  316. // err.code 错误码
  317. })
  318. },
  319. getRangeCountData(query, type, field = 'active_device_count') {
  320. if (type === 'week') type = 'isoWeek'
  321. const {
  322. pageCurrent
  323. } = this.options
  324. const db = uniCloud.database()
  325. return db.collection(this.tableName)
  326. .where(query)
  327. .field(
  328. `${field}, start_time, ${type}(add(new Date(0),start_time), "Asia/Shanghai") as ${type},year(add(new Date(0),start_time), "Asia/Shanghai") as year`
  329. )
  330. .groupBy(`year, ${type}`)
  331. .groupField(`sum(${field}) as ${type}_${field}`)
  332. .orderBy(`year asc, ${type} asc`)
  333. .get({
  334. getCount: true
  335. })
  336. },
  337. // 匹配数据日期所在的周活、月活
  338. mapWithWeekAndMonth(data, weeks, months, field = 'active_device_count') {
  339. for (const item of data) {
  340. const date = new Date(item.start_time)
  341. const year = date.getUTCFullYear()
  342. const month = date.getMonth() + 1
  343. const week = this.getWeekNumber(date)
  344. for (const w of weeks) {
  345. if (w.isoWeek === week && w.year === year) {
  346. item[`week_${field}`] = w[`isoWeek_${field}`]
  347. }
  348. }
  349. for (const m of months) {
  350. if (m.month === month && m.year === year) {
  351. item[`month_${field}`] = m[`month_${field}`]
  352. }
  353. }
  354. }
  355. return data
  356. },
  357. //日期所在的周(一年中的第几周)
  358. getWeekNumber(d) {
  359. // Copy date so don't modify original
  360. d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
  361. // Set to nearest Thursday: current date + 4 - current day number
  362. // Make Sunday's day number 7
  363. d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
  364. // Get first day of year
  365. const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
  366. // Calculate full weeks to nearest Thursday
  367. return Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
  368. },
  369. //获取渠道信息
  370. getChannelData(appid, platform_id) {
  371. this.query.channel_id = ''
  372. const db = uniCloud.database()
  373. const condition = {}
  374. //对应应用
  375. appid = appid ? appid : this.query.appid
  376. if (appid) {
  377. condition.appid = appid
  378. }
  379. //对应平台
  380. platform_id = platform_id ? platform_id : this.query.platform_id
  381. if (platform_id) {
  382. condition.platform_id = platform_id
  383. }
  384. let platformTemp = db.collection('uni-stat-app-platforms')
  385. .field('_id, name')
  386. .getTemp()
  387. let channelTemp = db.collection('uni-stat-app-channels')
  388. .where(condition)
  389. .field('_id, channel_name, create_time, platform_id')
  390. .getTemp()
  391. db.collection(channelTemp, platformTemp)
  392. .orderBy('platform_id', 'asc')
  393. .get()
  394. .then(res => {
  395. let data = res.result.data
  396. let channels = []
  397. if (data.length > 0) {
  398. let channelName
  399. for (let i in data) {
  400. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  401. if (data[i].platform_id.length > 0) {
  402. channelName = data[i].platform_id[0].name + '-' + channelName
  403. }
  404. channels.push({
  405. value: data[i]._id,
  406. text: channelName
  407. })
  408. }
  409. }
  410. this.channelData = channels
  411. })
  412. .catch((err) => {
  413. console.error(err)
  414. // err.message 错误信息
  415. // err.code 错误码
  416. }).finally(() => {})
  417. }
  418. }
  419. }
  420. </script>
  421. <style>
  422. </style>