scene.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 p-1015">
  13. <view class="uni-stat--app-select">
  14. <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" />
  15. <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" />
  16. </view>
  17. </view>
  18. <view class="uni-stat--x flex">
  19. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" @change="changeTimeRange" />
  20. <uni-datetime-picker type="datetimerange" :end="new Date().getTime()" v-model="query.start_time"
  21. returnType="timestamp" :clearIcon="false" class="uni-stat-datetime-picker"
  22. :class="{'uni-stat__actived': currentDateTab < 0 && !!query.start_time.length}"
  23. @change="useDatetimePicker" />
  24. </view>
  25. <view class="uni-stat--x">
  26. <uni-stat-tabs label="平台选择" type="boldLine" mode="platform-scene" :all="false" v-model="query.platform_id" @change="changePlatform" />
  27. </view>
  28. <view class="uni-stat--x" style="padding: 15px 0;">
  29. <uni-stat-panel :items="panelData" class="uni-stat-panel" />
  30. <uni-stat-tabs type="box" v-model="chartTab" :tabs="chartTabs" class="mb-l" @change="changeChartTab" />
  31. <view class="uni-charts-box" style="height: 400px;">
  32. <qiun-data-charts type="area" :chartData="chartData" echartsH5 echartsApp tooltipFormat="tooltipCustom" :errorMessage="errorMessage"/>
  33. </view>
  34. </view>
  35. <view class="uni-stat--x p-m">
  36. <uni-stat-table :data="tableData" :filedsMap="fieldsMap.slice(0, fieldsMap.length-1)"
  37. :loading="loading" />
  38. <view class="uni-pagination-box">
  39. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  40. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  41. @pageSizeChange="changePageSize" />
  42. </view>
  43. </view>
  44. </view>
  45. <!-- #ifndef H5 -->
  46. <fix-window />
  47. <!-- #endif -->
  48. </view>
  49. </template>
  50. <script>
  51. import {
  52. mapfields,
  53. stringifyQuery,
  54. stringifyField,
  55. stringifyGroupField,
  56. maxDeltaDay,
  57. getTimeOfSomeDayAgo,
  58. division,
  59. format,
  60. formatDate,
  61. getFieldTotal,
  62. debounce
  63. } from '@/js_sdk/uni-stat/util.js'
  64. import fieldsMap from './fieldsMap.js'
  65. export default {
  66. data() {
  67. return {
  68. fieldsMap,
  69. query: {
  70. dimension: "hour",
  71. appid: '',
  72. platform_id: '',
  73. uni_platform: '',
  74. version_id: '',
  75. start_time: [],
  76. },
  77. options: {
  78. pageSize: 20,
  79. pageCurrent: 1, // 当前页
  80. total: 0, // 数据总量
  81. },
  82. loading: false,
  83. currentDateTab: 1,
  84. tableData: [],
  85. panelData: fieldsMap.filter(f => f.hasOwnProperty('value')),
  86. chartData: {},
  87. chartTab: 'new_device_count',
  88. errorMessage: "",
  89. }
  90. },
  91. computed: {
  92. chartTabs() {
  93. const tabs = []
  94. fieldsMap.forEach(item => {
  95. const {
  96. field: _id,
  97. title: name
  98. } = item
  99. const isTab = item.hasOwnProperty('value')
  100. if (_id && name && isTab) {
  101. tabs.push({
  102. _id,
  103. name
  104. })
  105. }
  106. })
  107. return tabs
  108. },
  109. queryStr() {
  110. return stringifyQuery(this.query, true)
  111. },
  112. dimension() {
  113. if (maxDeltaDay(this.query.start_time, 1)) {
  114. return 'hour'
  115. } else {
  116. return 'day'
  117. }
  118. },
  119. versionQuery() {
  120. const {
  121. appid,
  122. uni_platform
  123. } = this.query
  124. const query = stringifyQuery({
  125. appid,
  126. uni_platform,
  127. // type: 'native_app'
  128. })
  129. return query
  130. }
  131. },
  132. created() {
  133. this.debounceGet = debounce(() => {
  134. this.getAllData(this.queryStr);
  135. }, 300);
  136. },
  137. watch: {
  138. query: {
  139. deep: true,
  140. handler(val) {
  141. this.options.pageCurrent = 1 // 重置分页
  142. this.debounceGet()
  143. }
  144. }
  145. },
  146. methods: {
  147. useDatetimePicker() {
  148. this.currentDateTab = -1
  149. },
  150. changePlatform(id, index, name, item) {
  151. this.query.version_id = 0
  152. this.query.uni_platform = item.code
  153. },
  154. changeTimeRange(id, index) {
  155. this.currentDateTab = index
  156. const start = getTimeOfSomeDayAgo(id),
  157. end = getTimeOfSomeDayAgo(0) - 1
  158. this.query.start_time = [start, end]
  159. },
  160. changePageCurrent(e) {
  161. this.options.pageCurrent = e.current
  162. this.getTabelData(this.queryStr)
  163. },
  164. changePageSize(pageSize) {
  165. this.options.pageSize = pageSize
  166. this.options.pageCurrent = 1 // 重置分页
  167. this.getTabelData(this.queryStr)
  168. },
  169. changeChartTab(id, index, name) {
  170. this.getChartData(this.queryStr, id, name)
  171. },
  172. getAllData(query) {
  173. if (query.indexOf("appid") === -1) {
  174. this.errorMessage = "请先选择应用";
  175. return; // 如果appid为空,则不进行查询
  176. }
  177. this.errorMessage = "";
  178. this.getPanelData(query)
  179. this.getChartData(query)
  180. this.getTabelData(query)
  181. },
  182. getChartData(query, field = this.chartTab) {
  183. // this.chartData = {}
  184. const {
  185. pageCurrent
  186. } = this.options
  187. query = JSON.parse(JSON.stringify(this.query))
  188. query.dimension = 'day'
  189. let querystr = stringifyQuery(query, false, ['uni_platform'])
  190. const db = uniCloud.database()
  191. db.collection('uni-stat-result')
  192. .where(querystr)
  193. .field(`${stringifyField(fieldsMap, field)},start_time,channel_id`)
  194. .groupBy('channel_id,start_time')
  195. .groupField(stringifyGroupField(fieldsMap, field))
  196. .orderBy('start_time', 'asc')
  197. .get({
  198. getCount: true
  199. })
  200. .then(res => {
  201. const {
  202. count,
  203. data
  204. } = res.result
  205. const options = {
  206. categories: [],
  207. series: [{
  208. name: '暂无数据',
  209. data: []
  210. }]
  211. }
  212. const xAxis = options.categories
  213. if (this.dimension === 'hour') {
  214. for (let i = 0; i < 24; ++i) {
  215. const hour = i < 10 ? '0' + i : i
  216. const x = `${hour}:00 ~ ${hour}:59`
  217. xAxis.push(x)
  218. }
  219. }
  220. // 将数据中渠道 id 去重
  221. const hasChannels = []
  222. data.forEach(item => {
  223. if (hasChannels.indexOf(item.channel_id) < 0) {
  224. hasChannels.push(item.channel_id)
  225. }
  226. })
  227. // 请求所有渠道数据,与 hasChannels 匹配得出 channel_name
  228. let allChannels = []
  229. this.getChannels().then(res => {
  230. allChannels = res.result.data
  231. }).finally(() => {
  232. hasChannels.forEach((channel, index) => {
  233. const c = allChannels.find(item => item._id === channel)
  234. const line = options.series[index] = {
  235. name: c && c.channel_name || '未知',
  236. data: []
  237. }
  238. if (this.dimension === 'hour') {
  239. for (let i = 0; i < 24; ++i) {
  240. line.data[i] = 0
  241. }
  242. }
  243. let mapper = fieldsMap.filter(f => f.field === field)
  244. mapper = JSON.parse(JSON.stringify(mapper))
  245. delete mapper[0].value
  246. mapper[0].formatter = ''
  247. for (const item of data) {
  248. // 将 item 根据 mapper 计算、格式化
  249. mapfields(mapper, item, item)
  250. let date = item.start_time
  251. const x = formatDate(date, this.dimension)
  252. let y = item[field]
  253. const dateIndex = xAxis.indexOf(x)
  254. if (channel === item.channel_id) {
  255. if (dateIndex < 0) {
  256. xAxis.push(x)
  257. line.data.push(y)
  258. } else {
  259. line.data[dateIndex] = y
  260. }
  261. }
  262. }
  263. })
  264. this.chartData = options
  265. })
  266. }).catch((err) => {
  267. console.error(err)
  268. // err.message 错误信息
  269. // err.code 错误码
  270. }).finally(() => {
  271. this.loading = false
  272. })
  273. },
  274. getChannels() {
  275. const db = uniCloud.database()
  276. return db.collection('uni-stat-app-channels')
  277. .get()
  278. },
  279. getTabelData(query) {
  280. const {
  281. pageCurrent
  282. } = this.options
  283. this.loading = true
  284. let querystr = stringifyQuery(this.query, false, ['uni_platform'])
  285. const db = uniCloud.database()
  286. db.collection('uni-stat-result')
  287. .where(querystr)
  288. .field(`${stringifyField(fieldsMap)},appid, channel_id`)
  289. .groupBy('appid, channel_id')
  290. .groupField(stringifyGroupField(fieldsMap))
  291. .orderBy('new_device_count', 'desc')
  292. .skip((pageCurrent - 1) * this.options.pageSize)
  293. .limit(this.options.pageSize)
  294. .get({
  295. getCount: true
  296. })
  297. .then(res => {
  298. const {
  299. count,
  300. data
  301. } = res.result
  302. this.getChannels().then(res => {
  303. const channels = res.result.data
  304. for (const item of data) {
  305. channels.forEach(channel => {
  306. if (item.channel_id === channel._id) {
  307. item.channel_code = channel.channel_code
  308. item.channel_name = channel.channel_name
  309. }
  310. })
  311. }
  312. }).finally(() => {
  313. for (const item of data) {
  314. mapfields(fieldsMap, item, item)
  315. }
  316. this.tableData = []
  317. this.options.total = count
  318. this.tableData = data
  319. })
  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)
  332. let query = JSON.parse(JSON.stringify(this.query))
  333. query.dimension = 'day'
  334. let querystr = stringifyQuery(query, false, ['uni_platform'])
  335. const db = uniCloud.database()
  336. const subTable = db.collection('uni-stat-result')
  337. .where(querystr)
  338. .field(stringifyField(fieldsMap))
  339. .groupBy('appid')
  340. .groupField(stringifyGroupField(fieldsMap))
  341. .orderBy('start_time', 'desc')
  342. .get()
  343. .then(res => {
  344. const item = res.result.data[0]
  345. item && (item.total_devices = 0)
  346. getFieldTotal.call(this, query)
  347. this.panelData = []
  348. this.panelData = mapfields(fieldsMap, item)
  349. })
  350. },
  351. navTo(id) {
  352. const url = `/pages/uni-stat/overview/overview?id=${id}`
  353. uni.navigateTo({
  354. url
  355. })
  356. }
  357. }
  358. }
  359. </script>
  360. <style>
  361. .uni-stat-panel {
  362. box-shadow: unset;
  363. border-bottom: 1px solid #eee;
  364. padding: 0;
  365. margin: 0 15px;
  366. }
  367. </style>