page-ent.vue 9.2 KB

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