activity.vue 12 KB

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