activity.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. defaultChart: {
  93. field: 'new_user_count',
  94. name: '新增用户'
  95. },
  96. channelData: []
  97. }
  98. },
  99. computed: {
  100. chartTabs() {
  101. const tabs = [{
  102. _id: 'day',
  103. name: '日活'
  104. }, {
  105. _id: 'week',
  106. name: '周活'
  107. }, {
  108. _id: 'month',
  109. name: '月活'
  110. }]
  111. if (maxDeltaDay(this.query.start_time, 7)) {
  112. tabs.forEach((tab, index) => {
  113. if (tab._id === 'month') {
  114. tab.disabled = true
  115. } else {
  116. tab.disabled = false
  117. }
  118. })
  119. }
  120. return tabs
  121. },
  122. channelQuery() {
  123. const platform_id = this.query.platform_id
  124. return stringifyQuery({
  125. platform_id
  126. })
  127. },
  128. versionQuery() {
  129. const {
  130. appid,
  131. uni_platform
  132. } = this.query
  133. const query = stringifyQuery({
  134. appid,
  135. uni_platform
  136. })
  137. return query
  138. }
  139. },
  140. created() {
  141. this.debounceGet = debounce(() => this.getAllData(this.query))
  142. this.getChannelData()
  143. },
  144. watch: {
  145. query: {
  146. deep: true,
  147. handler(val) {
  148. this.options.pageCurrent = 1 // 重置分页
  149. this.debounceGet()
  150. }
  151. }
  152. },
  153. methods: {
  154. useDatetimePicker() {
  155. this.currentDateTab = -1
  156. },
  157. changeAppid(id) {
  158. this.getChannelData(id, false)
  159. },
  160. changePlatform(id, index, name, item) {
  161. this.getChannelData(null, id)
  162. this.query.version_id = 0
  163. this.query.uni_platform = item.code
  164. },
  165. changeTimeRange(id, index) {
  166. this.currentDateTab = index
  167. const day = 24 * 60 * 60 * 1000
  168. let start, end
  169. start = getTimeOfSomeDayAgo(id)
  170. if (!id) {
  171. end = getTimeOfSomeDayAgo(0) + day - 1
  172. } else {
  173. end = getTimeOfSomeDayAgo(0) - 1
  174. }
  175. this.query.start_time = [start, end]
  176. },
  177. changePageCurrent(e) {
  178. this.options.pageCurrent = e.current
  179. this.getTabelData(this.query)
  180. },
  181. changePageSize(pageSize) {
  182. this.options.pageSize = pageSize
  183. this.options.pageCurrent = 1 // 重置分页
  184. this.getTabelData(this.query)
  185. },
  186. changeChartTab(type, index, name) {
  187. this.currentChartTab = type
  188. this.getChartData(this.query, type, name)
  189. },
  190. getAllData(query) {
  191. this.getChartData(query, this.currentChartTab)
  192. this.getTabelData(query)
  193. },
  194. getChartData(query, type, name = '日活', field = 'active_user_count') {
  195. // this.chartData = {}
  196. const options = {
  197. categories: [],
  198. series: [{
  199. name,
  200. data: []
  201. }]
  202. }
  203. query = stringifyQuery(query, null, ['uni_platform'])
  204. const db = uniCloud.database()
  205. if (type === 'day') {
  206. db.collection(this.tableName)
  207. .where(query)
  208. .field(`${stringifyField(fieldsMap, field)}, start_time`)
  209. .groupBy('start_time')
  210. .groupField(stringifyGroupField(fieldsMap, field))
  211. .orderBy('start_time', 'asc')
  212. .get({
  213. getCount: true
  214. })
  215. .then(res => {
  216. const {
  217. count,
  218. data
  219. } = res.result
  220. this.chartData = []
  221. for (const item of data) {
  222. const x = formatDate(item.start_time, 'day')
  223. const y = item[field]
  224. options.series[0].data.push(y)
  225. options.categories.push(x)
  226. }
  227. this.chartData = options
  228. }).catch((err) => {
  229. console.error(err)
  230. })
  231. } else {
  232. // 获取周活、月活
  233. this.getRangeCountData(query, type).then(res => {
  234. const oldType = type
  235. if (type === 'week') type = 'isoWeek'
  236. const {
  237. count,
  238. data
  239. } = res.result
  240. this.chartData = []
  241. const wunWeekTime = 7 * 24 * 60 * 60 * 1000
  242. for (const item of data) {
  243. const date = +new Date(item.year, 0) + (Number(item[type]) * wunWeekTime - 1)
  244. const x = formatDate(date, oldType)
  245. const y = item[type + '_' + field]
  246. if (y) {
  247. options.series[0].data.push(y)
  248. options.categories.push(x)
  249. }
  250. }
  251. this.chartData = options
  252. })
  253. }
  254. },
  255. getTabelData(query, field = 'active_user_count') {
  256. const {
  257. pageCurrent
  258. } = this.options
  259. query = stringifyQuery(query, null, ['uni_platform'])
  260. this.loading = true
  261. const db = uniCloud.database()
  262. db.collection(this.tableName)
  263. .where(query)
  264. .field(`${stringifyField(fieldsMap, field)}, start_time`)
  265. .groupBy('start_time')
  266. .groupField(stringifyGroupField(fieldsMap, field))
  267. .orderBy('start_time', 'desc')
  268. .skip((pageCurrent - 1) * this.options.pageSize)
  269. .limit(this.options.pageSize)
  270. .get({
  271. getCount: true
  272. })
  273. .then(res => {
  274. const {
  275. count,
  276. data
  277. } = res.result
  278. let daysData = data,
  279. daysCount = count,
  280. weeks = [],
  281. months = []
  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_user_count') {
  312. console.log('getRangeCountData', query);
  313. if (type === 'week') type = 'isoWeek'
  314. const {
  315. pageCurrent
  316. } = this.options
  317. const db = uniCloud.database()
  318. return db.collection(this.tableName)
  319. .where(query)
  320. .field(
  321. `${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`
  322. )
  323. .groupBy(`year, ${type}`)
  324. .groupField(`sum(${field}) as ${type}_${field}`)
  325. .orderBy(`year asc, ${type} asc`)
  326. .get({
  327. getCount: true
  328. })
  329. },
  330. // 周、月范围的处理
  331. mapWithWeekAndMonth(data, weeks, months, field = 'active_user_count') {
  332. for (const item of data) {
  333. const date = new Date(item.start_time)
  334. const year = date.getUTCFullYear()
  335. const month = date.getMonth() + 1
  336. const week = this.getWeekNumber(date)
  337. for (const w of weeks) {
  338. if (w.isoWeek === week && w.year === year) {
  339. item[`isoWeek_${field}`] = w[`week_${field}`]
  340. }
  341. }
  342. for (const m of months) {
  343. if (m.month === month && m.year === year) {
  344. item[`month_${field}`] = m[`month_${field}`]
  345. }
  346. }
  347. }
  348. return data
  349. },
  350. //日期所在的周(一年中的第几周)
  351. getWeekNumber(d) {
  352. // Copy date so don't modify original
  353. d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
  354. // Set to nearest Thursday: current date + 4 - current day number
  355. // Make Sunday's day number 7
  356. d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
  357. // Get first day of year
  358. var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
  359. // Calculate full weeks to nearest Thursday
  360. var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
  361. // Return array of year and week number
  362. return weekNo;
  363. },
  364. //获取渠道信息
  365. getChannelData(appid, platform_id) {
  366. this.query.channel_id = ''
  367. const db = uniCloud.database()
  368. const condition = {}
  369. //对应应用
  370. appid = appid ? appid : this.query.appid
  371. if (appid) {
  372. condition.appid = appid
  373. }
  374. //对应平台
  375. platform_id = platform_id ? platform_id : this.query.platform_id
  376. if (platform_id) {
  377. condition.platform_id = platform_id
  378. }
  379. let platformTemp = db.collection('uni-stat-app-platforms')
  380. .field('_id, name')
  381. .getTemp()
  382. let channelTemp = db.collection('uni-stat-app-channels')
  383. .where(condition)
  384. .field('_id, channel_name, create_time, platform_id')
  385. .getTemp()
  386. db.collection(channelTemp, platformTemp)
  387. .orderBy('platform_id', 'asc')
  388. .get()
  389. .then(res => {
  390. let data = res.result.data
  391. let channels = []
  392. if (data.length > 0) {
  393. let channelName
  394. for (let i in data) {
  395. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  396. if (data[i].platform_id.length > 0) {
  397. channelName = data[i].platform_id[0].name + '-' + channelName
  398. }
  399. channels.push({
  400. value: data[i]._id,
  401. text: channelName
  402. })
  403. }
  404. }
  405. this.channelData = channels
  406. })
  407. .catch((err) => {
  408. console.error(err)
  409. // err.message 错误信息
  410. // err.code 错误码
  411. }).finally(() => {})
  412. }
  413. }
  414. }
  415. </script>
  416. <style>
  417. </style>