trend.vue 11 KB

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