page-res.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 v-if="index === 1" :key="mapper.field" class="uni-stat-edit--x">
  57. {{item[mapper.field] !== undefined ? item[mapper.field] : '-'}}
  58. <uni-icons type="compose" color="#2979ff" size="25" class="uni-stat-edit--btn"
  59. @click="inputDialogToggle(item.path, item.title)" />
  60. </uni-td>
  61. <uni-td v-else :key="mapper.field" :align="index === 0 ? 'left' : 'center'">
  62. {{item[mapper.field] !== undefined ? item[mapper.field] : '-'}}
  63. </uni-td>
  64. </template>
  65. </uni-tr>
  66. </uni-table>
  67. <view class="uni-pagination-box">
  68. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  69. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  70. @pageSizeChange="changePageSize" />
  71. </view>
  72. </view>
  73. </view>
  74. <uni-popup ref="inputDialog" type="dialog" :maskClick="true">
  75. <uni-popup-dialog ref="inputClose" mode="input" title="请编辑名称" v-model="updateValue" placeholder="请输入内容"
  76. @confirm="editName"></uni-popup-dialog>
  77. </uni-popup>
  78. <!-- #ifndef H5 -->
  79. <fix-window />
  80. <!-- #endif -->
  81. </view>
  82. </template>
  83. <script>
  84. import {
  85. mapfields,
  86. stringifyQuery,
  87. stringifyField,
  88. stringifyGroupField,
  89. getTimeOfSomeDayAgo,
  90. division,
  91. format,
  92. debounce
  93. } from '@/js_sdk/uni-stat/util.js'
  94. import fieldsMap from './fieldsMap.js'
  95. export default {
  96. data() {
  97. return {
  98. fieldsMap,
  99. query: {
  100. dimension: "day",
  101. appid: '',
  102. platform_id: '',
  103. uni_platform: '',
  104. version_id: '',
  105. channel_id: '',
  106. start_time: [],
  107. },
  108. options: {
  109. pageSize: 20,
  110. pageCurrent: 1, // 当前页
  111. total: 0, // 数据总量
  112. },
  113. loading: false,
  114. currentDateTab: 1,
  115. tableData: [],
  116. panelData: fieldsMap.filter(f => f.hasOwnProperty('value')),
  117. queryId: '',
  118. updateValue: '',
  119. channelData: []
  120. }
  121. },
  122. computed: {
  123. channelQuery() {
  124. const platform_id = this.query.platform_id
  125. return stringifyQuery({
  126. platform_id
  127. })
  128. },
  129. versionQuery() {
  130. const {
  131. appid,
  132. uni_platform
  133. } = this.query
  134. const query = stringifyQuery({
  135. appid,
  136. uni_platform
  137. })
  138. return query
  139. }
  140. },
  141. created() {
  142. this.debounceGet = debounce(() => this.getAllData())
  143. this.getChannelData()
  144. },
  145. watch: {
  146. query: {
  147. deep: true,
  148. handler(val) {
  149. this.options.pageCurrent = 1 // 重置分页
  150. this.debounceGet()
  151. }
  152. }
  153. },
  154. methods: {
  155. useDatetimePicker() {
  156. this.currentDateTab = -1
  157. },
  158. changeAppid(id) {
  159. this.getChannelData(id, false)
  160. },
  161. changePlatform(id, index, name, item) {
  162. this.getChannelData(null, id)
  163. this.query.version_id = 0
  164. this.query.uni_platform = item.code
  165. },
  166. changeTimeRange(id, index) {
  167. this.currentDateTab = index
  168. const start = getTimeOfSomeDayAgo(id),
  169. end = getTimeOfSomeDayAgo(0) - 1
  170. this.query.start_time = [start, end]
  171. },
  172. changePageCurrent(e) {
  173. this.options.pageCurrent = e.current
  174. this.getTableData(this.query)
  175. },
  176. changePageSize(pageSize) {
  177. this.options.pageSize = pageSize
  178. this.options.pageCurrent = 1 // 重置分页
  179. this.getTableData()
  180. },
  181. getAllData() {
  182. this.getPanelData()
  183. this.getTableData()
  184. },
  185. getTableData(query) {
  186. query = stringifyQuery(this.query, null, ['uni_platform'])
  187. const {
  188. pageCurrent
  189. } = this.options
  190. this.loading = true
  191. const db = uniCloud.database()
  192. const filterAppid = stringifyQuery({
  193. appid: this.query.appid
  194. })
  195. const mainTableTemp = db.collection('uni-stat-pages')
  196. .where(filterAppid)
  197. .field('_id, title, path')
  198. .getTemp()
  199. const subTableTemp = db.collection('uni-stat-page-result')
  200. .where(query)
  201. .getTemp()
  202. db.collection(subTableTemp, mainTableTemp)
  203. .field(
  204. `${stringifyField(fieldsMap)}, stat_date, page_id`
  205. )
  206. .groupBy("page_id")
  207. .groupField(stringifyGroupField(fieldsMap))
  208. .orderBy('visit_times', 'desc')
  209. .skip((pageCurrent - 1) * this.options.pageSize)
  210. .limit(this.options.pageSize)
  211. .get({
  212. getCount: true
  213. })
  214. .then(res => {
  215. const {
  216. count,
  217. data
  218. } = res.result
  219. this.options.total = count
  220. this.tableData = []
  221. for (const item of data) {
  222. const lines = item.page_id
  223. if (Array.isArray(lines)) {
  224. delete(item.page_id)
  225. const line = lines[0]
  226. if (line && Object.keys(line).length) {
  227. for (const key in line) {
  228. if (key !== '_id') {
  229. item[key] = line[key]
  230. }
  231. }
  232. }
  233. }
  234. mapfields(fieldsMap, item, item)
  235. this.tableData.push(item)
  236. }
  237. }).catch((err) => {
  238. console.error(err)
  239. // err.message 错误信息
  240. // err.code 错误码
  241. }).finally(() => {
  242. this.loading = false
  243. })
  244. },
  245. getPanelData(query = stringifyQuery(this.query, null, ['uni_platform'])) {
  246. const db = uniCloud.database()
  247. const subTable = db.collection('uni-stat-page-result')
  248. .where(query)
  249. .field(stringifyField(fieldsMap))
  250. .groupBy('appid')
  251. .groupField(stringifyGroupField(fieldsMap))
  252. .orderBy('start_time', 'desc ')
  253. .get()
  254. .then(res => {
  255. const items = res.result.data[0]
  256. this.panelData = []
  257. this.panelData = mapfields(fieldsMap, items)
  258. })
  259. },
  260. inputDialogToggle(url, title) {
  261. this.queryId = url
  262. this.updateValue = title
  263. this.$refs.inputDialog.open()
  264. },
  265. editName(value) {
  266. // 使用 clientDB 提交数据
  267. const db = uniCloud.database()
  268. db.collection('uni-stat-pages')
  269. .where({
  270. url: this.queryId
  271. })
  272. .update({
  273. title: value
  274. })
  275. .then((res) => {
  276. uni.showToast({
  277. title: '修改成功'
  278. })
  279. this.getTableData()
  280. }).catch((err) => {
  281. uni.showModal({
  282. content: err.message || '请求服务失败',
  283. showCancel: false
  284. })
  285. }).finally(() => {
  286. uni.hideLoading()
  287. })
  288. },
  289. getChannelData(appid, platform_id) {
  290. this.query.channel_id = ''
  291. const db = uniCloud.database()
  292. const condition = {}
  293. //对应应用
  294. appid = appid ? appid : this.query.appid
  295. if (appid) {
  296. condition.appid = appid
  297. }
  298. //对应平台
  299. platform_id = platform_id ? platform_id : this.query.platform_id
  300. if (platform_id) {
  301. condition.platform_id = platform_id
  302. }
  303. let platformTemp = db.collection('uni-stat-app-platforms')
  304. .field('_id, name')
  305. .getTemp()
  306. let channelTemp = db.collection('uni-stat-app-channels')
  307. .where(condition)
  308. .field('_id, channel_name, create_time, platform_id')
  309. .getTemp()
  310. db.collection(channelTemp, platformTemp)
  311. .orderBy('platform_id', 'asc')
  312. .get()
  313. .then(res => {
  314. let data = res.result.data
  315. let channels = []
  316. if (data.length > 0) {
  317. let channelName
  318. for (let i in data) {
  319. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  320. if (data[i].platform_id.length > 0) {
  321. channelName = data[i].platform_id[0].name + '-' + channelName
  322. }
  323. channels.push({
  324. value: data[i]._id,
  325. text: channelName
  326. })
  327. }
  328. }
  329. this.channelData = channels
  330. })
  331. .catch((err) => {
  332. console.error(err)
  333. // err.message 错误信息
  334. // err.code 错误码
  335. }).finally(() => {})
  336. }
  337. }
  338. }
  339. </script>
  340. <style>
  341. .uni-stat-edit--x {
  342. display: flex;
  343. justify-content: space-between;
  344. }
  345. .uni-stat-edit--btn {
  346. cursor: pointer;
  347. }
  348. </style>