list.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <view class="fix-top-window">
  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"
  7. :placeholder="$t('common.placeholder.query')" />
  8. <button class="uni-button hide-on-phone" type="default" size="mini"
  9. @click="search">{{$t('common.button.search')}}</button>
  10. <button class="uni-button" type="primary" size="mini"
  11. @click="navigateTo('./add')">{{$t('common.button.add')}}</button>
  12. <button class="uni-button" type="warn" size="mini" :disabled="!selectedIndexs.length"
  13. @click="delTable">{{$t('common.button.batchDelete')}}</button>
  14. <!-- #ifdef H5 -->
  15. <!-- #ifndef VUE3 -->
  16. <download-excel class="hide-on-phone" :fields="exportExcel.fields" :data="exportExcelData"
  17. :type="exportExcel.type" :name="exportExcel.filename">
  18. <button class="uni-button" type="primary" size="mini">{{$t('common.button.exportExcel')}}</button>
  19. </download-excel>
  20. <!-- #endif -->
  21. <!-- #endif -->
  22. </view>
  23. </view>
  24. <view class="uni-container">
  25. <unicloud-db ref="udb" collection="opendb-app-list" field="appid,name,description,create_date"
  26. :where="where" page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
  27. :page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error,options}"
  28. :options="options" loadtime="manual" @load="onqueryload">
  29. <uni-table ref="table" :loading="loading || addAppidLoading"
  30. :emptyText="error.message || $t('common.empty')" border stripe type="selection"
  31. @selection-change="selectionChange" class="table-pc">
  32. <uni-tr>
  33. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'appid')"
  34. sortable @sort-change="sortChange($event, 'appid')">AppID</uni-th>
  35. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'name')"
  36. sortable @sort-change="sortChange($event, 'name')">应用名称</uni-th>
  37. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'description')"
  38. sortable @sort-change="sortChange($event, 'description')" :width="descriptionThWidth">应用描述
  39. </uni-th>
  40. <uni-th align="center" filter-type="timestamp"
  41. @filter-change="filterChange($event, 'create_date')" sortable
  42. @sort-change="sortChange($event, 'create_date')">创建时间</uni-th>
  43. <uni-th align="center" :width="buttonThWidth">操作</uni-th>
  44. </uni-tr>
  45. <uni-tr v-for="(item,index) in data" :key="index" :disabled="item.appid === appid">
  46. <uni-td align="center">{{item.appid}}</uni-td>
  47. <uni-td align="center">{{item.name}}</uni-td>
  48. <uni-td align="left">{{item.description}}</uni-td>
  49. <uni-td align="center">
  50. <uni-dateformat :threshold="[0, 0]" :date="item.create_date"></uni-dateformat>
  51. </uni-td>
  52. <uni-td align="center">
  53. <view v-if="item.appid === appid">
  54. -
  55. </view>
  56. <view v-else class="uni-group">
  57. <button @click="publish(item._id)" class="uni-button" size="mini"
  58. type="primary">{{$t('common.button.publish')}}</button>
  59. <button
  60. @click="navigateTo('/uni_modules/uni-upgrade-center/pages/version/list?appid='+item.appid, false)"
  61. class="uni-button" size="mini"
  62. type="primary">{{$t('common.button.version')}}</button>
  63. <button @click="navigateTo('./add?id='+item.appid, false)" class="uni-button"
  64. size="mini" type="primary">{{$t('common.button.edit')}}</button>
  65. <button @click="confirmDelete(item._id)" class="uni-button" size="mini"
  66. type="warn">{{$t('common.button.delete')}}</button>
  67. </view>
  68. </uni-td>
  69. </uni-tr>
  70. </uni-table>
  71. <view class="uni-pagination-box">
  72. <uni-pagination show-icon show-page-size :page-size="pagination.size" v-model="pagination.current"
  73. :total="pagination.count" @change="onPageChanged" @pageSizeChange="pageSizeChange" />
  74. </view>
  75. </unicloud-db>
  76. </view>
  77. <!-- #ifndef H5 -->
  78. <fix-window />
  79. <!-- #endif -->
  80. </view>
  81. </template>
  82. <script>
  83. import {
  84. enumConverter,
  85. filterToWhere
  86. } from '../../../js_sdk/validator/opendb-app-list.js';
  87. import {
  88. mapState
  89. } from 'vuex'
  90. const db = uniCloud.database()
  91. // 表查询配置
  92. const dbOrderBy = 'create_date' // 排序字段
  93. const dbSearchFields = [] // 模糊搜索字段,支持模糊搜索的字段列表
  94. // 分页配置
  95. const pageSize = 20
  96. const pageCurrent = 1
  97. const orderByMapping = {
  98. "ascending": "asc",
  99. "descending": "desc"
  100. }
  101. export default {
  102. data() {
  103. return {
  104. query: '',
  105. where: '',
  106. orderby: dbOrderBy,
  107. orderByFieldName: "",
  108. selectedIndexs: [],
  109. options: {
  110. pageSize,
  111. pageCurrent,
  112. filterData: {},
  113. ...enumConverter
  114. },
  115. imageStyles: {
  116. width: 64,
  117. height: 64
  118. },
  119. exportExcel: {
  120. "filename": "opendb-app-list.xls",
  121. "type": "xls",
  122. "fields": {
  123. "AppID": "appid",
  124. "应用名称": "name",
  125. "应用描述": "description",
  126. "创建时间": "create_date"
  127. }
  128. },
  129. exportExcelData: [],
  130. addAppidLoading: true,
  131. descriptionThWidth: 380,
  132. buttonThWidth: 400
  133. }
  134. },
  135. onLoad() {
  136. this._filter = {}
  137. },
  138. onReady() {
  139. this.$refs.udb.loadData()
  140. },
  141. computed: {
  142. ...mapState('app', ['appName', 'appid'])
  143. },
  144. methods: {
  145. pageSizeChange(pageSize) {
  146. this.options.pageSize = pageSize
  147. this.options.pageCurrent = 1
  148. this.$nextTick(() => {
  149. this.loadData()
  150. })
  151. },
  152. onqueryload(data) {
  153. if (!data.find(item => item.appid === this.appid)) {
  154. this.addCurrentAppid({
  155. appid: this.appid,
  156. name: this.appName,
  157. description: "admin 管理后台",
  158. create_date: Date.now()
  159. })
  160. } else {
  161. this.addAppidLoading = false
  162. }
  163. this.exportExcelData = data
  164. },
  165. changeSize(e) {
  166. this.pageSizeIndex = e.detail.value
  167. },
  168. addCurrentAppid(app) {
  169. // 使用 clientDB 提交当前 appid
  170. db.collection('opendb-app-list').add(app).then((res) => {
  171. this.loadData()
  172. setTimeout(() => {
  173. uni.showModal({
  174. content: `检测到数据库中无当前应用, 已自动添加应用: ${this.appName}`,
  175. showCancel: false
  176. })
  177. }, 500)
  178. }).catch((err) => {
  179. }).finally(() => {
  180. this.addAppidLoading = false
  181. })
  182. },
  183. getWhere() {
  184. const query = this.query.trim()
  185. if (!query) {
  186. return ''
  187. }
  188. const queryRe = new RegExp(query, 'i')
  189. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  190. },
  191. search() {
  192. const newWhere = this.getWhere()
  193. this.where = newWhere
  194. this.loadData()
  195. },
  196. loadData(clear = true) {
  197. this.$refs.udb.loadData({
  198. clear
  199. })
  200. },
  201. onPageChanged(e) {
  202. this.selectedIndexs.length = 0
  203. this.$refs.table.clearSelection()
  204. this.$refs.udb.loadData({
  205. current: e.current
  206. })
  207. },
  208. navigateTo(url, clear) {
  209. // clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
  210. uni.navigateTo({
  211. url,
  212. events: {
  213. refreshData: () => {
  214. this.loadData(clear)
  215. }
  216. }
  217. })
  218. },
  219. // 多选处理
  220. selectedItems() {
  221. var dataList = this.$refs.udb.dataList
  222. return this.selectedIndexs.map(i => dataList[i]._id)
  223. },
  224. // 批量删除
  225. delTable() {
  226. console.warn(
  227. "删除应用,只能删除应用表 opendb-app-list 中的应用数据记录,不能删除与应用关联的其他数据,例如:使用升级中心 uni-upgrade-center 等插件产生的数据(应用版本数据等)"
  228. )
  229. this.$refs.udb.remove(this.selectedItems(), {
  230. success: (res) => {
  231. this.$refs.table.clearSelection()
  232. }
  233. })
  234. },
  235. // 多选
  236. selectionChange(e) {
  237. this.selectedIndexs = e.detail.index
  238. },
  239. confirmDelete(id) {
  240. console.warn(
  241. "删除应用,只能删除应用表 opendb-app-list 中的应用数据记录,不能删除与应用关联的其他数据,例如:使用升级中心 uni-upgrade-center 等插件产生的数据(应用版本数据等)"
  242. )
  243. this.$refs.udb.remove(id, {
  244. confirmContent: '是否删除该应用',
  245. success: (res) => {
  246. this.$refs.table.clearSelection()
  247. }
  248. })
  249. },
  250. sortChange(e, name) {
  251. this.orderByFieldName = name;
  252. if (e.order) {
  253. this.orderby = name + ' ' + orderByMapping[e.order]
  254. } else {
  255. this.orderby = ''
  256. }
  257. this.$refs.table.clearSelection()
  258. this.$nextTick(() => {
  259. this.$refs.udb.loadData()
  260. })
  261. },
  262. filterChange(e, name) {
  263. this._filter[name] = {
  264. type: e.filterType,
  265. value: e.filter
  266. }
  267. let newWhere = filterToWhere(this._filter, db.command)
  268. if (Object.keys(newWhere).length) {
  269. this.where = newWhere
  270. } else {
  271. this.where = ''
  272. }
  273. this.$nextTick(() => {
  274. this.$refs.udb.loadData()
  275. })
  276. },
  277. publish(id) {
  278. uni.navigateTo({
  279. url: '/pages/system/app/uni-portal/uni-portal?id=' + id
  280. })
  281. }
  282. }
  283. }
  284. </script>
  285. <style>
  286. </style>