list.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <view>
  3. <view class="uni-header">
  4. <uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
  5. <view class="uni-group">
  6. <!-- <input class="uni-search" type="text" v-model="query" placeholder="用户/内容" />
  7. <button class="uni-button" type="default" size="mini" @click="search">搜索</button> -->
  8. <input class="uni-search" type="text" v-model="query" @confirm="search" :placeholder="$t('common.placeholder.query')" />
  9. <button class="uni-button hide-on-phone" type="default" size="mini" @click="search">{{$t('common.button.search')}}</button>
  10. </view>
  11. </view>
  12. <view class="uni-container">
  13. <unicloud-db ref="udb" :collection="collectionName" orderby="create_date desc" field="type, ip, create_date, user_id{username}[0].username as username" :options="options" :where="where" page-data="replace" :orderby="orderby"
  14. :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error}">
  15. <uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe >
  16. <uni-tr>
  17. <uni-th align="center">序号</uni-th>
  18. <uni-th align="center">用户</uni-th>
  19. <uni-th align="center">内容</uni-th>
  20. <uni-th align="center">IP</uni-th>
  21. <uni-th align="center">时间</uni-th>
  22. </uni-tr>
  23. <uni-tr v-for="(item,index) in data" :key="index">
  24. <uni-td align="center">{{(pagination.current -1)*pagination.size + (index+1)}}</uni-td>
  25. <uni-td align="center">{{item.user_id[0] && item.user_id[0].username || '-'}}</uni-td>
  26. <uni-td align="center">{{item.type}}</uni-td>
  27. <uni-td align="center">{{item.ip}}</uni-td>
  28. <uni-td align="center">
  29. <uni-dateformat :date="item.create_date" :threshold="[0, 0]" />
  30. </uni-td>
  31. </uni-tr>
  32. </uni-table>
  33. <view class="uni-pagination-box">
  34. <uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current" :total="pagination.count"
  35. @change="onPageChanged" />
  36. </view>
  37. </unicloud-db>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. const db = uniCloud.database()
  43. // 表查询配置
  44. const dbCollectionName = 'uni-id-log, uni-id-users'
  45. const dbOrderBy = 'create_date' // 排序字段
  46. const dbSearchFields = ["user_id.username","type", "ip"] // 支持模糊搜索的字段列表
  47. // 分页配置
  48. const pageSize = 20
  49. const pageCurrent = 1
  50. export default {
  51. data() {
  52. return {
  53. query: '',
  54. where: '',
  55. orderby: dbOrderBy,
  56. collectionName: dbCollectionName,
  57. options: {
  58. pageSize,
  59. pageCurrent
  60. }
  61. }
  62. },
  63. methods: {
  64. getWhere() {
  65. const query = this.query.trim()
  66. if (!query) {
  67. return ''
  68. }
  69. const queryRe = new RegExp(query, 'i')
  70. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  71. },
  72. search() {
  73. const newWhere = this.getWhere()
  74. const isSameWhere = newWhere === this.where
  75. this.where = newWhere
  76. if (isSameWhere) { // 相同条件时,手动强制刷新
  77. this.loadData()
  78. }
  79. },
  80. loadData(clear = true) {
  81. this.$refs.udb.loadData({
  82. clear
  83. })
  84. },
  85. onPageChanged(e) {
  86. this.$refs.udb.loadData({
  87. current: e.current
  88. })
  89. },
  90. navigateTo(url) {
  91. uni.navigateTo({
  92. url,
  93. events: {
  94. refreshData: () => {
  95. this.loadData()
  96. }
  97. }
  98. })
  99. },
  100. // 多选处理
  101. selectedItems() {
  102. var dataList = this.$refs.udb.dataList
  103. return this.selectedIndexs.map(i => dataList[i]._id)
  104. },
  105. //批量删除
  106. delTable() {
  107. this.$refs.udb.remove(this.selectedItems())
  108. },
  109. // 多选
  110. selectionChange(e) {
  111. this.selectedIndexs = e.detail.index
  112. },
  113. confirmDelete(id) {
  114. this.$refs.udb.remove(id)
  115. }
  116. }
  117. }
  118. </script>
  119. <style>
  120. </style>