page-ent.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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-title">入口页</view> -->
  8. <view class="uni-sub-title hide-on-phone">入口页数据分析</view>
  9. </view>
  10. </view>
  11. <view class="uni-container">
  12. <view class="uni-stat--x flex">
  13. <uni-data-select collection="opendb-app-list" field="appid as value, name as text" orderby="text asc"
  14. :defItem="1" label="应用选择" v-model="query.appid" :clear="false" />
  15. <uni-data-select collection="opendb-app-versions" :where="versionQuery"
  16. field="_id as value, version as text" orderby="text asc" label="版本选择" v-model="query.version_id" />
  17. <view class="flex">
  18. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" @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. <uni-stat-panel :items="panelData" />
  32. <view class="uni-stat--x p-m">
  33. <uni-table :loading="loading" border stripe :emptyText="$t('common.empty')">
  34. <uni-tr>
  35. <template v-for="(mapper, index) in fieldsMap">
  36. <uni-th v-if="mapper.title" :key="index" align="center">
  37. <!-- #ifdef MP -->
  38. {{mapper.title}}
  39. <!-- #endif -->
  40. <!-- #ifndef MP -->
  41. <uni-tooltip>
  42. {{mapper.title}}
  43. <uni-icons v-if="index === 0 && mapper.tooltip" type="help" color="#666" />
  44. <template v-if="index === 0 && mapper.tooltip" v-slot:content>
  45. <view class="uni-stat-tooltip-s">
  46. {{mapper.tooltip}}
  47. </view>
  48. </template>
  49. </uni-tooltip>
  50. <!-- #endif -->
  51. </uni-th>
  52. </template>
  53. </uni-tr>
  54. <uni-tr v-for="(item ,i) in tableData" :key="i">
  55. <template v-for="(mapper, index) in fieldsMap">
  56. <uni-td :align="index === 0 ? 'left' : 'center'">
  57. {{item[mapper.field] !== undefined ? item[mapper.field] : '-'}}
  58. </uni-td>
  59. </template>
  60. </uni-tr>
  61. </uni-table>
  62. <view class="uni-pagination-box">
  63. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  64. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  65. @pageSizeChange="changePageSize" />
  66. </view>
  67. </view>
  68. </view>
  69. <!-- #ifndef H5 -->
  70. <fix-window />
  71. <!-- #endif -->
  72. </view>
  73. </template>
  74. <script>
  75. import {
  76. mapfields,
  77. stringifyQuery,
  78. stringifyField,
  79. stringifyGroupField,
  80. getTimeOfSomeDayAgo,
  81. division,
  82. format,
  83. debounce
  84. } from '@/js_sdk/uni-stat/util.js'
  85. import fieldsMap from './fieldsMap.js'
  86. export default {
  87. data() {
  88. return {
  89. fieldsMap,
  90. query: {
  91. dimension: "day",
  92. appid: '',
  93. platform_id: '',
  94. uni_platform: '',
  95. version_id: '',
  96. channel_id: '',
  97. start_time: [],
  98. },
  99. options: {
  100. pageSize: 20,
  101. pageCurrent: 1, // 当前页
  102. total: 0, // 数据总量
  103. },
  104. loading: false,
  105. currentDateTab: 1,
  106. tableData: [],
  107. panelData: fieldsMap.filter(f => f.hasOwnProperty('value')),
  108. channelData: []
  109. }
  110. },
  111. computed: {
  112. channelQuery() {
  113. const platform_id = this.query.platform_id
  114. return stringifyQuery({
  115. platform_id
  116. })
  117. },
  118. versionQuery() {
  119. const {
  120. appid,
  121. uni_platform
  122. } = this.query
  123. const query = stringifyQuery({
  124. appid,
  125. uni_platform
  126. })
  127. return query
  128. }
  129. },
  130. created() {
  131. this.debounceGet = debounce(() => this.getAllData())
  132. this.getChannelData()
  133. },
  134. watch: {
  135. query: {
  136. deep: true,
  137. handler(val) {
  138. this.options.pageCurrent = 1 // 重置分页
  139. this.debounceGet()
  140. }
  141. }
  142. },
  143. methods: {
  144. useDatetimePicker() {
  145. this.currentDateTab = -1
  146. },
  147. changeAppid(id) {
  148. this.getChannelData(id, false)
  149. },
  150. changePlatform(id, index, name, item) {
  151. this.getChannelData(null, id)
  152. this.query.version_id = 0
  153. this.query.uni_platform = item.code
  154. },
  155. changeTimeRange(id, index) {
  156. this.currentDateTab = index
  157. const start = getTimeOfSomeDayAgo(id),
  158. end = getTimeOfSomeDayAgo(0) - 1
  159. this.query.start_time = [start, end]
  160. },
  161. changePageCurrent(e) {
  162. this.options.pageCurrent = e.current
  163. this.getTableData()
  164. },
  165. changePageSize(pageSize) {
  166. this.options.pageSize = pageSize
  167. this.options.pageCurrent = 1 // 重置分页
  168. this.getTableData()
  169. },
  170. getAllData() {
  171. this.getPanelData()
  172. this.getTableData()
  173. },
  174. getTableData(query) {
  175. query = stringifyQuery(this.query, null, ['uni_platform'])
  176. const {
  177. pageCurrent
  178. } = this.options
  179. this.loading = true
  180. const db = uniCloud.database()
  181. const filterAppid = stringifyQuery({
  182. appid: this.query.appid
  183. })
  184. const mainTableTemp = db.collection('uni-stat-pages')
  185. .where(filterAppid)
  186. .getTemp()
  187. const subTableTemp = db.collection('uni-stat-page-result')
  188. .where(query + ' && ' + 'entry_count > 0')
  189. .getTemp()
  190. db.collection(subTableTemp, mainTableTemp)
  191. .field(
  192. `${stringifyField(fieldsMap)}, stat_date, page_id`
  193. )
  194. .groupBy("page_id")
  195. .groupField(stringifyGroupField(fieldsMap))
  196. .orderBy('entry_count', 'desc')
  197. .skip((pageCurrent - 1) * this.options.pageSize)
  198. .limit(this.options.pageSize)
  199. .get({
  200. getCount: true
  201. })
  202. .then(res => {
  203. const {
  204. count,
  205. data
  206. } = res.result
  207. this.options.total = count
  208. this.tableData = []
  209. for (const item of data) {
  210. const lines = item.page_id
  211. if (Array.isArray(lines)) {
  212. delete(item.page_id)
  213. const line = lines[0]
  214. if (line && Object.keys(line).length) {
  215. for (const key in line) {
  216. if (key !== '_id') {
  217. item[key] = line[key]
  218. }
  219. }
  220. }
  221. }
  222. mapfields(fieldsMap, item, item)
  223. this.tableData.push(item)
  224. }
  225. }).catch((err) => {
  226. console.error(err)
  227. // err.message 错误信息
  228. // err.code 错误码
  229. }).finally(() => {
  230. this.loading = false
  231. })
  232. },
  233. getPanelData(query = stringifyQuery(this.query, null, ['uni_platform'])) {
  234. const db = uniCloud.database()
  235. const subTable = db.collection('uni-stat-page-result')
  236. .where(query)
  237. .field(stringifyField(fieldsMap))
  238. .groupBy('appid')
  239. .groupField(stringifyGroupField(fieldsMap))
  240. .orderBy('start_time', 'desc ')
  241. .get()
  242. .then(res => {
  243. const items = res.result.data[0]
  244. this.panelData = []
  245. this.panelData = mapfields(fieldsMap, items)
  246. })
  247. },
  248. navTo(id) {
  249. const url = `/pages/uni-stat/overview/overview?id=${id}`
  250. uni.navigateTo({
  251. url
  252. })
  253. },
  254. getChannelData(appid, platform_id) {
  255. this.query.channel_id = ''
  256. const db = uniCloud.database()
  257. const condition = {}
  258. //对应应用
  259. appid = appid ? appid : this.query.appid
  260. if (appid) {
  261. condition.appid = appid
  262. }
  263. //对应平台
  264. platform_id = platform_id ? platform_id : this.query.platform_id
  265. if (platform_id) {
  266. condition.platform_id = platform_id
  267. }
  268. let platformTemp = db.collection('uni-stat-app-platforms')
  269. .field('_id, name')
  270. .getTemp()
  271. let channelTemp = db.collection('uni-stat-app-channels')
  272. .where(condition)
  273. .field('_id, channel_name, create_time, platform_id')
  274. .getTemp()
  275. db.collection(channelTemp, platformTemp)
  276. .orderBy('platform_id', 'asc')
  277. .get()
  278. .then(res => {
  279. let data = res.result.data
  280. let channels = []
  281. if (data.length > 0) {
  282. let channelName
  283. for (let i in data) {
  284. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  285. if (data[i].platform_id.length > 0) {
  286. channelName = data[i].platform_id[0].name + '-' + channelName
  287. }
  288. channels.push({
  289. value: data[i]._id,
  290. text: channelName
  291. })
  292. }
  293. }
  294. this.channelData = channels
  295. })
  296. .catch((err) => {
  297. console.error(err)
  298. // err.message 错误信息
  299. // err.code 错误码
  300. }).finally(() => {})
  301. }
  302. }
  303. }
  304. </script>
  305. <style>
  306. .uni-stat-edit--x {
  307. display: flex;
  308. justify-content: space-between;
  309. }
  310. </style>