event.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. <uni-link href="https://ask.dcloud.net.cn/article/36304" text="自定义事件说明>>"></uni-link>
  9. </view>
  10. </view>
  11. </view>
  12. <view class="uni-container">
  13. <view class="uni-stat--x flex p-1015">
  14. <view class="uni-stat--app-select">
  15. <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" />
  16. <uni-data-select collection="opendb-app-versions" :where="versionQuery" class="ml-m" field="concat(version, '---',uni_platform) 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" @change="changeVersion"/>
  17. </view>
  18. </view>
  19. <view class="uni-stat--x flex">
  20. <uni-stat-tabs label="日期选择" :current="currentDateTab" mode="date" @change="changeTimeRange" />
  21. <uni-datetime-picker type="datetimerange" :end="new Date().getTime()" v-model="query.create_time"
  22. returnType="timestamp" :clearIcon="false" class="uni-stat-datetime-picker"
  23. :class="{'uni-stat__actived': currentDateTab < 0 && !!query.create_time.length}"
  24. @change="useDatetimePicker" />
  25. </view>
  26. <view class="uni-stat--x">
  27. <uni-stat-tabs label="平台选择" type="boldLine" mode="platform" v-model="query.platform_id" @change="changePlatform" />
  28. <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="concat(channel_code, '---',channel_name) as value, channel_name as text" orderby="text asc" label="渠道/场景值选择" v-model="query.channel_id" @change="changeChannel"/>
  29. </view>
  30. <view class="uni-stat--x" style="display: flex;align-items: center;padding: 10px;">
  31. <text style="width: 80px;">事件ID</text>
  32. <view style="width: 300px;">
  33. <uni-easyinput v-model="formData.event_key" placeholder="事件ID" @change="changeFormData($event,'event_key')" @clear="changeFormData('','event_key')"></uni-easyinput>
  34. </view>
  35. <text style="width: 80px;margin-left: 10px;">设备标识</text>
  36. <view style="width: 300px;">
  37. <uni-easyinput v-model="formData.device_id" placeholder="设备标识" @change="changeFormData($event,'device_id')" @clear="changeFormData('','device_id')"></uni-easyinput>
  38. </view>
  39. </view>
  40. <view class="uni-stat--x p-m">
  41. <uni-table :loading="loading" border stripe :emptyText="errorMessage || $t('common.empty')">
  42. <uni-tr>
  43. <block v-for="(mapper, index) in fieldsMap" :key="index">
  44. <uni-th v-if="mapper.title" :key="index" align="center">
  45. {{mapper.title}}
  46. </uni-th>
  47. </block>
  48. </uni-tr>
  49. <uni-tr v-for="(item ,i) in tableData" :key="i">
  50. <uni-td align="center" v-for="(mapper, index) in fieldsMap" :key="index">
  51. {{item[mapper.field] !== undefined ? item[mapper.field] : '-'}}
  52. </uni-td>
  53. </uni-tr>
  54. </uni-table>
  55. <view class="uni-pagination-box">
  56. <uni-pagination show-icon show-page-size :page-size="options.pageSize"
  57. :current="options.pageCurrent" :total="options.total" @change="changePageCurrent"
  58. @pageSizeChange="changePageSize" />
  59. </view>
  60. </view>
  61. </view>
  62. <uni-popup ref="inputDialog" type="dialog" :maskClick="true">
  63. <uni-popup-dialog ref="inputClose" mode="input" title="请编辑名称" v-model="updateValue" placeholder="请输入内容"
  64. @confirm="editName"></uni-popup-dialog>
  65. </uni-popup>
  66. <!-- #ifndef H5 -->
  67. <fix-window />
  68. <!-- #endif -->
  69. </view>
  70. </template>
  71. <script>
  72. import {
  73. mapfields,
  74. stringifyQuery,
  75. stringifyField,
  76. stringifyGroupField,
  77. getTimeOfSomeDayAgo,
  78. division,
  79. format,
  80. parseDateTime,
  81. debounce
  82. } from '@/js_sdk/uni-stat/util.js'
  83. import fieldsMap from './fieldsMap.js'
  84. export default {
  85. data() {
  86. return {
  87. fieldsMap,
  88. formData:{
  89. event_key: '',
  90. device_id: ''
  91. },
  92. query: {
  93. appid: '',
  94. platform_id: '',
  95. uni_platform: '',
  96. platform: '',
  97. channel_id: '',
  98. channel: '',
  99. version_id: '',
  100. version:'',
  101. create_time: [],
  102. event_key: '',
  103. device_id: ''
  104. },
  105. options: {
  106. pageSize: 20,
  107. pageCurrent: 1, // 当前页
  108. total: 0, // 数据总量
  109. },
  110. loading: false,
  111. currentDateTab: 1,
  112. tableData: [],
  113. panelData: [],
  114. queryId: '',
  115. updateValue: '',
  116. channelData: [],
  117. errorMessage: ''
  118. }
  119. },
  120. computed: {
  121. channelQuery() {
  122. const platform_id = this.query.platform_id
  123. return stringifyQuery({
  124. platform_id
  125. })
  126. },
  127. versionQuery() {
  128. const {
  129. appid,
  130. uni_platform
  131. } = this.query
  132. const query = stringifyQuery({
  133. appid,
  134. uni_platform
  135. })
  136. return query
  137. }
  138. },
  139. created() {
  140. this.debounceGet = debounce(() => this.getAllData())
  141. this.getChannelData()
  142. },
  143. watch: {
  144. query: {
  145. deep: true,
  146. handler(val) {
  147. this.options.pageCurrent = 1 // 重置分页
  148. this.debounceGet()
  149. }
  150. }
  151. },
  152. methods: {
  153. changeFormData(val,key){
  154. this.query[key] = val;
  155. },
  156. useDatetimePicker() {
  157. this.currentDateTab = -1
  158. },
  159. changeAppid(id) {
  160. this.getChannelData(id, false)
  161. },
  162. changeTimeRange(id, index) {
  163. this.currentDateTab = index
  164. const start = getTimeOfSomeDayAgo(id),
  165. end = getTimeOfSomeDayAgo(0) - 1
  166. this.query.create_time = [start, end]
  167. },
  168. changePageCurrent(e) {
  169. this.options.pageCurrent = e.current
  170. this.getTableData()
  171. },
  172. changePageSize(pageSize) {
  173. this.options.pageSize = pageSize
  174. this.options.pageCurrent = 1 // 重置分页
  175. this.getTableData()
  176. },
  177. getAllData(query) {
  178. this.getTableData(query)
  179. },
  180. changePlatform(id, index, name, item) {
  181. this.getChannelData(null, id)
  182. this.query.version_id = 0
  183. this.query.uni_platform = item.code
  184. this.query.platform = item.code
  185. },
  186. changeVersion(version){
  187. if (!version) version = "";
  188. let versionArr = version.split("---");
  189. this.query.version = version.split("---")[0];
  190. this.query.platform = version.split("---")[1];
  191. },
  192. changeChannel(channel){
  193. if (!channel) channel = "";
  194. let channelArr = channel.split("---");
  195. this.query.channel = channel.split("---")[0];
  196. //this.query.platform = channel.split("---")[1];
  197. },
  198. getTableData(query = stringifyQuery(this.query, null, ['uni_platform','platform_id','version_id', 'channel_id'])) {
  199. if (!this.query.appid){
  200. this.errorMessage = "请先选择应用";
  201. return;
  202. }
  203. this.errorMessage = "";
  204. const {
  205. pageCurrent
  206. } = this.options
  207. this.loading = true
  208. const db = uniCloud.database()
  209. let collectionList = [
  210. db.collection('uni-stat-event-logs').where(query).getTemp(),
  211. db.collection('uni-stat-app-platforms').getTemp()
  212. ];
  213. db.collection(...collectionList)
  214. //.where(query)
  215. .orderBy('create_time', 'desc')
  216. .skip((pageCurrent - 1) * this.options.pageSize)
  217. .limit(this.options.pageSize)
  218. .get({
  219. getCount: true
  220. })
  221. .then(res => {
  222. const {
  223. count,
  224. data
  225. } = res.result
  226. this.tableData = []
  227. this.options.total = count
  228. for (const item of data) {
  229. item.create_time = parseDateTime(item.create_time, 'dateTime')
  230. item.platform = item.platform && item.platform[0].name
  231. mapfields(fieldsMap, item, item)
  232. this.tableData.push(item)
  233. }
  234. }).catch((err) => {
  235. console.error(err)
  236. // err.message 错误信息
  237. // err.code 错误码
  238. }).finally(() => {
  239. this.loading = false
  240. })
  241. },
  242. getChannelData(appid, platform_id) {
  243. if (!this.query.appid){
  244. this.errorMessage = "请先选择应用";
  245. return;
  246. }
  247. this.errorMessage = "";
  248. this.query.channel_id = ''
  249. const db = uniCloud.database()
  250. const condition = {}
  251. //对应应用
  252. appid = appid ? appid : this.query.appid
  253. if (appid) {
  254. condition.appid = appid
  255. }
  256. //对应平台
  257. platform_id = platform_id ? platform_id : this.query.platform_id
  258. if (platform_id) {
  259. condition.platform_id = platform_id
  260. }
  261. let platformTemp = db.collection('uni-stat-app-platforms')
  262. .field('_id, name')
  263. .getTemp()
  264. let channelTemp = db.collection('uni-stat-app-channels')
  265. .where(condition)
  266. .field('_id, channel_name, create_time, platform_id')
  267. .getTemp()
  268. db.collection(channelTemp, platformTemp)
  269. .orderBy('platform_id', 'asc')
  270. .get()
  271. .then(res => {
  272. let data = res.result.data
  273. let channels = []
  274. if (data.length > 0) {
  275. let channelName
  276. for (let i in data) {
  277. channelName = data[i].channel_name ? data[i].channel_name : '默认'
  278. if (data[i].platform_id.length > 0) {
  279. channelName = data[i].platform_id[0].name + '-' + channelName
  280. }
  281. channels.push({
  282. value: data[i]._id,
  283. text: channelName
  284. })
  285. }
  286. }
  287. this.channelData = channels
  288. })
  289. .catch((err) => {
  290. console.error(err)
  291. // err.message 错误信息
  292. // err.code 错误码
  293. }).finally(() => {})
  294. }
  295. }
  296. }
  297. </script>
  298. <style>
  299. .uni-stat-edit--x {
  300. display: flex;
  301. justify-content: space-between;
  302. }
  303. .uni-stat-edit--btn {
  304. cursor: pointer;
  305. }
  306. </style>