channel.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <template>
  2. <!-- 对应页面:渠道(app) -->
  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. <uni-link href="https://ask.dcloud.net.cn/article/35974"
  9. text="支持Android App多渠道统计。设置App渠道包的方法,请参考 https://ask.dcloud.net.cn/article/35974。"></uni-link>
  10. </view>
  11. </view>
  12. </view>
  13. <view class="uni-container">
  14. <view class="uni-stat--x flex">
  15. <uni-data-select collection="opendb-app-list" field="appid as value, name as text" orderby="text asc"
  16. :defItem="1" label="应用选择" v-model="query.appid" :clear="false" />
  17. <uni-data-select collection="opendb-app-versions" :storage="false" :where="versionQuery"
  18. field="_id as value, version as text" orderby="text asc" label="版本选择" v-model="query.version_id" />
  19. <uni-stat-tabs label="平台选择" type="boldLine" mode="platform-channel" :all="false"
  20. v-model="query.platform_id" @change="changePlatform" />
  21. <view class="flex">
  22. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" @change="changeTimeRange" />
  23. <uni-datetime-picker type="daterange" :end="new Date().getTime()" v-model="query.start_time"
  24. returnType="timestamp" :clearIcon="false" class="uni-stat-datetime-picker"
  25. :class="{'uni-stat__actived': currentDateTab < 0 && !!query.start_time.length}"
  26. @change="useDatetimePicker" />
  27. </view>
  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">
  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. <view class="mb-m">
  39. <uni-link color="" href="https://ask.dcloud.net.cn/article/35974" text="如何自定义渠道包?"></uni-link>
  40. </view>
  41. <uni-table :loading="loading" border stripe :emptyText="$t('common.empty')">
  42. <uni-tr>
  43. <template v-for="(mapper, index) in fieldsMap.slice(0, fieldsMap.length-1)">
  44. <uni-th v-if="mapper.title" :key="index" align="center">
  45. {{mapper.title}}
  46. </uni-th>
  47. </template>
  48. </uni-tr>
  49. <uni-tr v-for="(item ,i) in tableData" :key="i">
  50. <template v-for="(mapper, index) in fieldsMap.slice(0, fieldsMap.length-1)">
  51. <uni-td v-if="mapper.title && index === 1" :key="mapper.field" class="uni-stat-edit--x">
  52. {{item[mapper.field] ? item[mapper.field] : '-'}}
  53. <uni-icons type="compose" color="#2979ff" size="25" class="uni-stat-edit--btn"
  54. @click="inputDialogToggle(item.channel_code, item.channel_name)" />
  55. </uni-td>
  56. <uni-td v-else="mapper.title" :key="mapper.field" align="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="paginationOptions.pageSize"
  64. :current="paginationOptions.pageCurrent" :total="paginationOptions.total"
  65. @change="changePageCurrent" @pageSizeChange="changePageSize" />
  66. </view>
  67. </view>
  68. </view>
  69. <uni-popup ref="inputDialog" type="dialog" :maskClick="true">
  70. <uni-popup-dialog ref="inputClose" mode="input" title="请编辑名称" v-model="updateValue" placeholder="请输入内容"
  71. @confirm="editName"></uni-popup-dialog>
  72. </uni-popup>
  73. <!-- #ifndef H5 -->
  74. <fix-window />
  75. <!-- #endif -->
  76. </view>
  77. </template>
  78. <script>
  79. import {
  80. mapfields,
  81. stringifyQuery,
  82. stringifyField,
  83. stringifyGroupField,
  84. maxDeltaDay,
  85. getTimeOfSomeDayAgo,
  86. division,
  87. format,
  88. formatDate,
  89. getFieldTotal,
  90. debounce
  91. } from '@/js_sdk/uni-stat/util.js'
  92. import fieldsMap from './fieldsMap.js'
  93. export default {
  94. data() {
  95. return {
  96. // 字段映射表
  97. fieldsMap,
  98. // 查询参数
  99. query: {
  100. // 统计范围 day:按天统计,hour:按小时统计
  101. dimension: "day",
  102. // 应用id
  103. appid: '',
  104. // 平台
  105. uni_platform: 'android',
  106. // 平台id
  107. platform_id: '',
  108. // 版本号
  109. version_id: '',
  110. // 开始时间
  111. start_time: [],
  112. },
  113. // 分页数据
  114. paginationOptions: {
  115. pageSize: 20,
  116. pageCurrent: 1, // 当前页
  117. total: 0, // 数据总量
  118. },
  119. // 加载状态
  120. loading: false,
  121. // 日期选择索引
  122. currentDateTab: 1,
  123. days: 0,
  124. // 表格数据
  125. tableData: [],
  126. panelData: fieldsMap.filter(f => f.hasOwnProperty('value')),
  127. chartData: {},
  128. chartTab: 'new_device_count',
  129. queryId: '',
  130. updateValue: ''
  131. }
  132. },
  133. computed: {
  134. chartTabs() {
  135. const tabs = []
  136. fieldsMap.forEach(item => {
  137. const {
  138. field: _id,
  139. title: name
  140. } = item
  141. const isTab = item.hasOwnProperty('value')
  142. if (_id && name && isTab) {
  143. tabs.push({
  144. _id,
  145. name
  146. })
  147. }
  148. })
  149. return tabs
  150. },
  151. queryStr() {
  152. return stringifyQuery(this.query, true)
  153. },
  154. dimension() {
  155. if (maxDeltaDay(this.query.start_time, 1)) {
  156. return 'hour'
  157. } else {
  158. return 'day'
  159. }
  160. },
  161. versionQuery() {
  162. const {
  163. appid,
  164. uni_platform
  165. } = this.query
  166. const query = stringifyQuery({
  167. appid,
  168. uni_platform,
  169. type: 'native_app'
  170. })
  171. return query
  172. }
  173. },
  174. created() {
  175. this.debounceGet = debounce(() => this.getAllData())
  176. },
  177. watch: {
  178. query: {
  179. deep: true,
  180. handler(val) {
  181. this.paginationOptions.pageCurrent = 1 // 重置分页
  182. this.debounceGet()
  183. }
  184. }
  185. },
  186. methods: {
  187. useDatetimePicker() {
  188. this.currentDateTab = -1
  189. },
  190. changePlatform(id, index, name, item) {
  191. this.query.version_id = 0
  192. console.log(item.code);
  193. this.query.uni_platform = item.code
  194. },
  195. changeTimeRange(id, index) {
  196. this.currentDateTab = index
  197. const day = 24 * 60 * 60 * 1000
  198. let start, end
  199. start = getTimeOfSomeDayAgo(id)
  200. if (!id) {
  201. end = getTimeOfSomeDayAgo(0) + day - 1
  202. } else {
  203. end = getTimeOfSomeDayAgo(0) - 1
  204. }
  205. this.query.start_time = [start, end]
  206. },
  207. changePageCurrent(e) {
  208. this.paginationOptions.pageCurrent = e.current
  209. this.getTableData()
  210. },
  211. changePageSize(pageSize) {
  212. this.paginationOptions.pageSize = pageSize
  213. this.paginationOptions.pageCurrent = 1 // 重置分页
  214. this.getTableData()
  215. },
  216. changeChartTab(id, index, name) {
  217. this.getChartData(id, name)
  218. },
  219. getAllData(query) {
  220. this.getPanelData()
  221. this.getChartData()
  222. this.getTableData()
  223. },
  224. getChartData(field = this.chartTab) {
  225. // this.chartData = {}
  226. let querystr = stringifyQuery(this.query, false, ['uni_platform'])
  227. const {
  228. pageCurrent
  229. } = this.paginationOptions
  230. const db = uniCloud.database()
  231. db.collection('uni-stat-result')
  232. .where(querystr)
  233. .field(`${stringifyField(fieldsMap, field)}, start_time, channel_id`)
  234. .groupBy('channel_id,start_time')
  235. .groupField(stringifyGroupField(fieldsMap, field))
  236. .orderBy('start_time', 'asc')
  237. .get({
  238. getCount: true
  239. })
  240. .then(res => {
  241. const {
  242. count,
  243. data
  244. } = res.result
  245. const options = {
  246. categories: [],
  247. series: [{
  248. name: '暂无数据',
  249. data: []
  250. }]
  251. }
  252. const xAxis = options.categories
  253. if (this.dimension === 'hour') {
  254. for (let i = 0; i < 24; ++i) {
  255. const hour = i < 10 ? '0' + i : i
  256. const x = `${hour}:00 ~ ${hour}:59`
  257. xAxis.push(x)
  258. }
  259. }
  260. const hasChannels = []
  261. console.log('data----', data);
  262. data.forEach(item => {
  263. if (hasChannels.indexOf(item.channel_id) < 0) {
  264. hasChannels.push(item.channel_id)
  265. }
  266. })
  267. // 请求所有渠道数据,与 hasChannels 匹配得出 channel_name
  268. let allChannels = []
  269. this.getChannels().then(res => {
  270. allChannels = res.result.data
  271. }).finally(() => {
  272. hasChannels.forEach((channel, index) => {
  273. // TODO 需要做个排序,暂时排序还是有问题的
  274. // allChannels = allChannels.sort((a,b)=>{ return a.channel_code.localeCompare(b.channel_code)})
  275. const c = allChannels.find(item => item._id === channel)
  276. const line = options.series[index] = {
  277. name: c && c.channel_name || '未知',
  278. data: []
  279. }
  280. if (this.dimension === 'hour') {
  281. for (let i = 0; i < 24; ++i) {
  282. line.data[i] = 0
  283. }
  284. }
  285. let mapper = fieldsMap.filter(f => f.field === field)
  286. mapper = JSON.parse(JSON.stringify(mapper))
  287. delete mapper[0].value
  288. mapper[0].formatter = ''
  289. for (const item of data) {
  290. // 将 item 根据 mapper 计算、格式化
  291. mapfields(mapper, item, item)
  292. let date = item.start_time
  293. const x = formatDate(date, this.dimension)
  294. let y = item[field]
  295. const dateIndex = xAxis.indexOf(x)
  296. if (channel === item.channel_id) {
  297. if (dateIndex < 0) {
  298. xAxis.push(x)
  299. line.data.push(y)
  300. } else {
  301. line.data[dateIndex] = y
  302. }
  303. }
  304. }
  305. })
  306. console.log(options);
  307. options.series = options.series.sort((a, b) => {
  308. return a.name.localeCompare(b.name)
  309. })
  310. this.chartData = options
  311. })
  312. }).catch((err) => {
  313. console.error(err)
  314. // err.message 错误信息
  315. // err.code 错误码
  316. }).finally(() => {})
  317. },
  318. getChannels() {
  319. const db = uniCloud.database()
  320. console.log(this.query);
  321. return db.collection('uni-stat-app-channels').where(stringifyQuery({
  322. appid: this.query.appid,
  323. platform_id: this.query.platform_id
  324. })).get()
  325. },
  326. getTableData() {
  327. const query = stringifyQuery(this.query, false, ['uni_platform'])
  328. const {
  329. pageCurrent
  330. } = this.paginationOptions
  331. this.loading = true
  332. const db = uniCloud.database()
  333. db.collection('uni-stat-result')
  334. .where(query)
  335. .field(`${stringifyField(fieldsMap)},appid, channel_id`)
  336. .groupBy('appid, channel_id')
  337. .groupField(stringifyGroupField(fieldsMap))
  338. .orderBy('new_device_count', 'desc')
  339. .skip((pageCurrent - 1) * this.paginationOptions.pageSize)
  340. .limit(this.paginationOptions.pageSize)
  341. .get({
  342. getCount: true
  343. })
  344. .then(res => {
  345. const {
  346. count,
  347. data
  348. } = res.result
  349. this.getChannels().then(res => {
  350. const channels = res.result.data
  351. for (const item of data) {
  352. channels.forEach(channel => {
  353. if (item.channel_id === channel._id) {
  354. item.channel_code = channel.channel_code
  355. item.channel_name = channel.channel_name
  356. }
  357. })
  358. }
  359. }).finally(() => {
  360. for (const item of data) {
  361. mapfields(fieldsMap, item, item, 'total_')
  362. }
  363. this.tableData = []
  364. this.paginationOptions.total = count
  365. this.tableData = data
  366. this.loading = false
  367. })
  368. }).catch((err) => {
  369. console.error(err)
  370. // err.message 错误信息
  371. // err.code 错误码
  372. this.loading = false
  373. })
  374. },
  375. createStr(maps, fn, prefix = 'total_') {
  376. const strArr = []
  377. maps.forEach(mapper => {
  378. if (field.hasOwnProperty('value')) {
  379. const fieldName = mapper.field
  380. strArr.push(`${fn}(${fieldName}) as ${prefix + fieldName}`)
  381. }
  382. })
  383. return strArr.join()
  384. },
  385. getPanelData() {
  386. let query = JSON.parse(JSON.stringify(this.query))
  387. query.dimension = 'day'
  388. // let query = stringifyQuery(cloneQuery)
  389. let querystr = stringifyQuery(query, false, ['uni_platform'])
  390. console.log('channel --:', querystr);
  391. const db = uniCloud.database()
  392. const subTable = db.collection('uni-stat-result')
  393. .where(querystr)
  394. .field(stringifyField(fieldsMap))
  395. .groupBy('appid')
  396. .groupField(stringifyGroupField(fieldsMap))
  397. .orderBy('start_time', 'desc')
  398. .get()
  399. .then(res => {
  400. const item = res.result.data[0]
  401. item && (item.total_devices = 0)
  402. getFieldTotal.call(this, query)
  403. this.panelData = []
  404. this.panelData = mapfields(fieldsMap, item)
  405. })
  406. },
  407. inputDialogToggle(queryId, updateValue) {
  408. this.queryId = queryId
  409. this.updateValue = updateValue
  410. this.$refs.inputDialog.open()
  411. },
  412. editName(value) {
  413. // 使用 clientDB 提交数据
  414. const db = uniCloud.database()
  415. db.collection('uni-stat-app-channels')
  416. .where({
  417. channel_code: this.queryId
  418. })
  419. .update({
  420. channel_name: value
  421. })
  422. .then((res) => {
  423. uni.showToast({
  424. title: '修改成功'
  425. })
  426. this.getTableData()
  427. }).catch((err) => {
  428. uni.showModal({
  429. content: err.message || '请求服务失败',
  430. showCancel: false
  431. })
  432. }).finally(() => {
  433. uni.hideLoading()
  434. })
  435. }
  436. }
  437. }
  438. </script>
  439. <style>
  440. .uni-stat-panel {
  441. box-shadow: unset;
  442. border-bottom: 1px solid #eee;
  443. padding: 0;
  444. margin: 0 15px;
  445. }
  446. .uni-stat-edit--x {
  447. display: flex;
  448. justify-content: space-between;
  449. }
  450. .uni-stat-edit--btn {
  451. cursor: pointer;
  452. }
  453. </style>