trend.vue 11 KB

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