activity.vue 12 KB

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