scene.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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">
  8. 小程序平台有效。用户打开小程序时的场景,如通过扫描二维码打开小程序,场景为二维码。注意:部分平台可能获取不到场景值,如支付宝小程序</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" :storage="false" :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-scene" :all="false"
  27. v-model="query.platform_id" @change="changePlatform" />
  28. </view>
  29. <view class="uni-stat--x" style="padding: 15px 0;">
  30. <uni-stat-panel :items="panelData" class="uni-stat-panel" />
  31. <uni-stat-tabs type="box" v-model="chartTab" :tabs="chartTabs" class="mb-l" @change="changeChartTab" />
  32. <view class="uni-charts-box" style="height: 400px;">
  33. <qiun-data-charts type="area" :chartData="chartData" echartsH5 echartsApp
  34. tooltipFormat="tooltipCustom" />
  35. </view>
  36. </view>
  37. <view class="uni-stat--x p-m">
  38. <uni-stat-table :data="tableData" :filedsMap="fieldsMap.slice(0, fieldsMap.length-1)"
  39. :loading="loading" />
  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. maxDeltaDay,
  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. start_time: [],
  78. },
  79. options: {
  80. pageSize: 20,
  81. pageCurrent: 1, // 当前页
  82. total: 0, // 数据总量
  83. },
  84. loading: false,
  85. currentDateTab: 1,
  86. tableData: [],
  87. panelData: fieldsMap.filter(f => f.hasOwnProperty('value')),
  88. chartData: {},
  89. chartTab: 'new_device_count',
  90. }
  91. },
  92. computed: {
  93. chartTabs() {
  94. const tabs = []
  95. fieldsMap.forEach(item => {
  96. const {
  97. field: _id,
  98. title: name
  99. } = item
  100. const isTab = item.hasOwnProperty('value')
  101. if (_id && name && isTab) {
  102. tabs.push({
  103. _id,
  104. name
  105. })
  106. }
  107. })
  108. return tabs
  109. },
  110. queryStr() {
  111. return stringifyQuery(this.query, true)
  112. },
  113. dimension() {
  114. if (maxDeltaDay(this.query.start_time, 1)) {
  115. return 'hour'
  116. } else {
  117. return 'day'
  118. }
  119. },
  120. versionQuery() {
  121. const {
  122. appid,
  123. uni_platform
  124. } = this.query
  125. const query = stringifyQuery({
  126. appid,
  127. uni_platform,
  128. // type: 'native_app'
  129. })
  130. return query
  131. }
  132. },
  133. created() {
  134. this.debounceGet = debounce(() => this.getAllData())
  135. },
  136. watch: {
  137. query: {
  138. deep: true,
  139. handler(val) {
  140. this.options.pageCurrent = 1 // 重置分页
  141. this.debounceGet()
  142. }
  143. }
  144. },
  145. methods: {
  146. useDatetimePicker() {
  147. this.currentDateTab = -1
  148. },
  149. changePlatform(id, index, name, item) {
  150. this.query.version_id = 0
  151. this.query.uni_platform = item.code
  152. },
  153. changeTimeRange(id, index) {
  154. this.currentDateTab = index
  155. const start = getTimeOfSomeDayAgo(id),
  156. end = getTimeOfSomeDayAgo(0) - 1
  157. this.query.start_time = [start, end]
  158. },
  159. changePageCurrent(e) {
  160. this.options.pageCurrent = e.current
  161. this.getTabelData(this.queryStr)
  162. },
  163. changePageSize(pageSize) {
  164. this.options.pageSize = pageSize
  165. this.options.pageCurrent = 1 // 重置分页
  166. this.getTabelData(this.queryStr)
  167. },
  168. changeChartTab(id, index, name) {
  169. this.getChartData(this.queryStr, id, name)
  170. },
  171. getAllData(query) {
  172. this.getPanelData(query)
  173. this.getChartData(query)
  174. this.getTabelData(query)
  175. },
  176. getChartData(query, field = this.chartTab) {
  177. // this.chartData = {}
  178. const {
  179. pageCurrent
  180. } = this.options
  181. query = JSON.parse(JSON.stringify(this.query))
  182. query.dimension = 'day'
  183. let querystr = stringifyQuery(query, false, ['uni_platform'])
  184. console.log('querystr', querystr);
  185. const db = uniCloud.database()
  186. db.collection('uni-stat-result')
  187. .where(querystr)
  188. .field(`${stringifyField(fieldsMap, field)},start_time,channel_id`)
  189. .groupBy('channel_id,start_time')
  190. .groupField(stringifyGroupField(fieldsMap, field))
  191. .orderBy('start_time', 'asc')
  192. .get({
  193. getCount: true
  194. })
  195. .then(res => {
  196. const {
  197. count,
  198. data
  199. } = res.result
  200. const options = {
  201. categories: [],
  202. series: [{
  203. name: '暂无数据',
  204. data: []
  205. }]
  206. }
  207. const xAxis = options.categories
  208. if (this.dimension === 'hour') {
  209. for (let i = 0; i < 24; ++i) {
  210. const hour = i < 10 ? '0' + i : i
  211. const x = `${hour}:00 ~ ${hour}:59`
  212. xAxis.push(x)
  213. }
  214. }
  215. // 将数据中渠道 id 去重
  216. const hasChannels = []
  217. data.forEach(item => {
  218. if (hasChannels.indexOf(item.channel_id) < 0) {
  219. hasChannels.push(item.channel_id)
  220. }
  221. })
  222. // 请求所有渠道数据,与 hasChannels 匹配得出 channel_name
  223. let allChannels = []
  224. this.getChannels().then(res => {
  225. allChannels = res.result.data
  226. }).finally(() => {
  227. hasChannels.forEach((channel, index) => {
  228. const c = allChannels.find(item => item._id === channel)
  229. const line = options.series[index] = {
  230. name: c && c.channel_name || '未知',
  231. data: []
  232. }
  233. if (this.dimension === 'hour') {
  234. for (let i = 0; i < 24; ++i) {
  235. line.data[i] = 0
  236. }
  237. }
  238. let mapper = fieldsMap.filter(f => f.field === field)
  239. mapper = JSON.parse(JSON.stringify(mapper))
  240. delete mapper[0].value
  241. mapper[0].formatter = ''
  242. for (const item of data) {
  243. // 将 item 根据 mapper 计算、格式化
  244. mapfields(mapper, item, item)
  245. let date = item.start_time
  246. const x = formatDate(date, this.dimension)
  247. let y = item[field]
  248. const dateIndex = xAxis.indexOf(x)
  249. if (channel === item.channel_id) {
  250. if (dateIndex < 0) {
  251. xAxis.push(x)
  252. line.data.push(y)
  253. } else {
  254. line.data[dateIndex] = y
  255. }
  256. }
  257. }
  258. })
  259. this.chartData = options
  260. })
  261. }).catch((err) => {
  262. console.error(err)
  263. // err.message 错误信息
  264. // err.code 错误码
  265. }).finally(() => {
  266. this.loading = false
  267. })
  268. },
  269. getChannels() {
  270. const db = uniCloud.database()
  271. return db.collection('uni-stat-app-channels')
  272. .get()
  273. },
  274. getTabelData(query) {
  275. const {
  276. pageCurrent
  277. } = this.options
  278. this.loading = true
  279. let querystr = stringifyQuery(this.query, false, ['uni_platform'])
  280. const db = uniCloud.database()
  281. db.collection('uni-stat-result')
  282. .where(querystr)
  283. .field(`${stringifyField(fieldsMap)},appid, channel_id`)
  284. .groupBy('appid, channel_id')
  285. .groupField(stringifyGroupField(fieldsMap))
  286. .orderBy('new_device_count', 'desc')
  287. .skip((pageCurrent - 1) * this.options.pageSize)
  288. .limit(this.options.pageSize)
  289. .get({
  290. getCount: true
  291. })
  292. .then(res => {
  293. const {
  294. count,
  295. data
  296. } = res.result
  297. this.getChannels().then(res => {
  298. const channels = res.result.data
  299. for (const item of data) {
  300. channels.forEach(channel => {
  301. if (item.channel_id === channel._id) {
  302. item.channel_code = channel.channel_code
  303. item.channel_name = channel.channel_name
  304. }
  305. })
  306. }
  307. }).finally(() => {
  308. for (const item of data) {
  309. mapfields(fieldsMap, item, item)
  310. }
  311. this.tableData = []
  312. this.options.total = count
  313. this.tableData = data
  314. })
  315. }).catch((err) => {
  316. console.error(err)
  317. // err.message 错误信息
  318. // err.code 错误码
  319. }).finally(() => {
  320. this.loading = false
  321. })
  322. },
  323. getPanelData() {
  324. // let cloneQuery = JSON.parse(JSON.stringify(this.query))
  325. // cloneQuery.dimension = 'day'
  326. // let query = stringifyQuery(cloneQuery)
  327. let query = JSON.parse(JSON.stringify(this.query))
  328. query.dimension = 'day'
  329. let querystr = stringifyQuery(query, false, ['uni_platform'])
  330. console.log('---- getPanelData', querystr);
  331. const db = uniCloud.database()
  332. const subTable = db.collection('uni-stat-result')
  333. .where(querystr)
  334. .field(stringifyField(fieldsMap))
  335. .groupBy('appid')
  336. .groupField(stringifyGroupField(fieldsMap))
  337. .orderBy('start_time', 'desc')
  338. .get()
  339. .then(res => {
  340. const item = res.result.data[0]
  341. item && (item.total_devices = 0)
  342. getFieldTotal.call(this, query)
  343. this.panelData = []
  344. this.panelData = mapfields(fieldsMap, item)
  345. })
  346. },
  347. navTo(id) {
  348. const url = `/pages/uni-stat/overview/overview?id=${id}`
  349. uni.navigateTo({
  350. url
  351. })
  352. }
  353. }
  354. }
  355. </script>
  356. <style>
  357. .uni-stat-panel {
  358. box-shadow: unset;
  359. border-bottom: 1px solid #eee;
  360. padding: 0;
  361. margin: 0 15px;
  362. }
  363. </style>