list.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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" @confirm="search" :placeholder="$t('common.placeholder.query')" />
  7. <button class="uni-button hide-on-phone" type="default" size="mini" @click="search">{{$t('common.button.search')}}</button>
  8. </view>
  9. </view>
  10. <view class="uni-container">
  11. <unicloud-db ref="udb" :collection="collectionList" :options="options" :where="where" page-data="replace" :orderby="orderby"
  12. :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error}">
  13. <uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe >
  14. <uni-tr>
  15. <uni-th align="center">序号</uni-th>
  16. <uni-th align="center">用户名</uni-th>
  17. <uni-th align="center">昵称</uni-th>
  18. <uni-th align="center">内容</uni-th>
  19. <uni-th align="center">IP</uni-th>
  20. <uni-th align="center">时间</uni-th>
  21. </uni-tr>
  22. <uni-tr v-for="(item,index) in data" :key="index">
  23. <uni-td align="center">{{(pagination.current -1)*pagination.size + (index+1)}}</uni-td>
  24. <uni-td align="center">{{item.user_id[0] && item.user_id[0].username || '-'}}</uni-td>
  25. <uni-td align="center">{{item.user_id[0] && item.user_id[0].nickname || '-'}}</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 dbOrderBy = 'create_date desc' // 排序字段
  45. const dbSearchFields = ["user_id.username","user_id.nickname","type", "ip"] // 支持模糊搜索的字段列表
  46. // 分页配置
  47. const pageSize = 20
  48. const pageCurrent = 1
  49. export default {
  50. data() {
  51. return {
  52. collectionList: [ db.collection('uni-id-log').field('type, ip, create_date, user_id').getTemp(),db.collection('uni-id-users').field('_id, username,nickname').getTemp() ],
  53. query: '',
  54. where: '',
  55. orderby: dbOrderBy,
  56. options: {
  57. pageSize,
  58. pageCurrent
  59. }
  60. }
  61. },
  62. methods: {
  63. getWhere() {
  64. const query = this.query.trim()
  65. if (!query) {
  66. return ''
  67. }
  68. let queryRe;
  69. try {
  70. queryRe = new RegExp(query, 'i')
  71. } catch(err){
  72. uni.showToast({
  73. title:'请勿输入\等不满足正则格式的符号',
  74. icon:"none"
  75. })
  76. return;
  77. }
  78. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  79. },
  80. search() {
  81. const newWhere = this.getWhere()
  82. const isSameWhere = newWhere === this.where
  83. this.where = newWhere
  84. if (isSameWhere) { // 相同条件时,手动强制刷新
  85. this.loadData()
  86. }
  87. },
  88. loadData(clear = true) {
  89. this.$refs.udb.loadData({
  90. clear
  91. })
  92. },
  93. onPageChanged(e) {
  94. this.$refs.udb.loadData({
  95. current: e.current
  96. })
  97. },
  98. navigateTo(url) {
  99. uni.navigateTo({
  100. url,
  101. events: {
  102. refreshData: () => {
  103. this.loadData()
  104. }
  105. }
  106. })
  107. },
  108. // 多选处理
  109. selectedItems() {
  110. let dataList = this.$refs.udb.dataList
  111. return this.selectedIndexs.map(i => dataList[i]._id)
  112. },
  113. //批量删除
  114. delTable() {
  115. this.$refs.udb.remove(this.selectedItems())
  116. },
  117. // 多选
  118. selectionChange(e) {
  119. this.selectedIndexs = e.detail.index
  120. },
  121. confirmDelete(id) {
  122. this.$refs.udb.remove(id)
  123. }
  124. }
  125. }
  126. </script>
  127. <style>
  128. </style>