list.vue 8.9 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 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. })
  159. } else {
  160. this.addAppidLoading = false
  161. }
  162. this.exportExcelData = data
  163. },
  164. changeSize(e) {
  165. this.pageSizeIndex = e.detail.value
  166. },
  167. addCurrentAppid(app) {
  168. // 使用 clientDB 提交当前 appid
  169. db.collection('opendb-app-list').add(app).then((res) => {
  170. this.loadData()
  171. setTimeout(() => {
  172. uni.showModal({
  173. content: `检测到数据库中无当前应用, 已自动添加应用: ${this.appName}`,
  174. showCancel: false
  175. })
  176. }, 500)
  177. }).catch((err) => {
  178. }).finally(() => {
  179. this.addAppidLoading = false
  180. })
  181. },
  182. getWhere() {
  183. const query = this.query.trim()
  184. if (!query) {
  185. return ''
  186. }
  187. const queryRe = new RegExp(query, 'i')
  188. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  189. },
  190. search() {
  191. const newWhere = this.getWhere()
  192. this.where = newWhere
  193. this.loadData()
  194. },
  195. loadData(clear = true) {
  196. this.$refs.udb.loadData({
  197. clear
  198. })
  199. },
  200. onPageChanged(e) {
  201. this.selectedIndexs.length = 0
  202. this.$refs.table.clearSelection()
  203. this.$refs.udb.loadData({
  204. current: e.current
  205. })
  206. },
  207. navigateTo(url, clear) {
  208. // clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
  209. uni.navigateTo({
  210. url,
  211. events: {
  212. refreshData: () => {
  213. this.loadData(clear)
  214. }
  215. }
  216. })
  217. },
  218. // 多选处理
  219. selectedItems() {
  220. let dataList = this.$refs.udb.dataList
  221. return this.selectedIndexs.map(i => dataList[i]._id)
  222. },
  223. // 批量删除
  224. delTable() {
  225. console.warn(
  226. "删除应用,只能删除应用表 opendb-app-list 中的应用数据记录,不能删除与应用关联的其他数据,例如:使用升级中心 uni-upgrade-center 等插件产生的数据(应用版本数据等)"
  227. )
  228. this.$refs.udb.remove(this.selectedItems(), {
  229. success: (res) => {
  230. this.$refs.table.clearSelection()
  231. }
  232. })
  233. },
  234. // 多选
  235. selectionChange(e) {
  236. this.selectedIndexs = e.detail.index
  237. },
  238. confirmDelete(id) {
  239. console.warn(
  240. "删除应用,只能删除应用表 opendb-app-list 中的应用数据记录,不能删除与应用关联的其他数据,例如:使用升级中心 uni-upgrade-center 等插件产生的数据(应用版本数据等)"
  241. )
  242. this.$refs.udb.remove(id, {
  243. confirmContent: '是否删除该应用',
  244. success: (res) => {
  245. this.$refs.table.clearSelection()
  246. }
  247. })
  248. },
  249. sortChange(e, name) {
  250. this.orderByFieldName = name;
  251. if (e.order) {
  252. this.orderby = name + ' ' + orderByMapping[e.order]
  253. } else {
  254. this.orderby = ''
  255. }
  256. this.$refs.table.clearSelection()
  257. this.$nextTick(() => {
  258. this.$refs.udb.loadData()
  259. })
  260. },
  261. filterChange(e, name) {
  262. this._filter[name] = {
  263. type: e.filterType,
  264. value: e.filter
  265. }
  266. let newWhere = filterToWhere(this._filter, db.command)
  267. if (Object.keys(newWhere).length) {
  268. this.where = newWhere
  269. } else {
  270. this.where = ''
  271. }
  272. this.$nextTick(() => {
  273. this.$refs.udb.loadData()
  274. })
  275. },
  276. publish(id) {
  277. uni.navigateTo({
  278. url: '/pages/system/app/uni-portal/uni-portal?id=' + id
  279. })
  280. }
  281. }
  282. }
  283. </script>
  284. <style>
  285. </style>