list.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. <button class="uni-button" type="primary" size="mini" :disabled="!selectedIndexs.length"
  15. @click="openTagsPopup">{{$t('common.button.tagManager')}}</button>
  16. <!-- #ifdef H5 -->
  17. <button class="uni-button" type="primary" size="mini" @click="$refs.batchSms.open()">{{$t('common.button.sendSMS')}}</button>
  18. <download-excel class="hide-on-phone" :fields="exportExcel.fields" :data="exportExcelData"
  19. :type="exportExcel.type" :name="exportExcel.filename">
  20. <button class="uni-button" type="primary" size="mini">{{$t('common.button.exportExcel')}}</button>
  21. </download-excel>
  22. <!-- #endif -->
  23. </view>
  24. </view>
  25. <view class="uni-container">
  26. <unicloud-db ref="udb" :collection="collectionList" :where="where"
  27. page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
  28. :page-current="options.pageCurrent" v-slot:default="{ data, pagination, loading, error, options }"
  29. :options="options" loadtime="manual" @load="onqueryload">
  30. <uni-table ref="table" :loading="loading" :emptyText="error.message || $t('common.empty')" border stripe
  31. type="selection" @selection-change="selectionChange">
  32. <uni-tr>
  33. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'username')"
  34. sortable @sort-change="sortChange($event, 'username')">用户名</uni-th>
  35. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'nickname')"
  36. sortable @sort-change="sortChange($event, 'nickname')">用户昵称</uni-th>
  37. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'mobile')"
  38. sortable @sort-change="sortChange($event, 'mobile')">手机号码</uni-th>
  39. <uni-th align="center" filter-type="select" :filter-data="options.filterData.status_localdata"
  40. @filter-change="filterChange($event, 'status')">用户状态</uni-th>
  41. <uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'email')"
  42. sortable @sort-change="sortChange($event, 'email')">邮箱</uni-th>
  43. <uni-th align="center">角色</uni-th>
  44. <uni-th align="center" filter-type="select" :filter-data="tagsData"
  45. @filter-change="filterChange($event, 'tags')">用户标签</uni-th>
  46. <uni-th align="center">可登录应用</uni-th>
  47. <uni-th align="center" filter-type="timestamp"
  48. @filter-change="filterChange($event, 'last_login_date')" sortable
  49. @sort-change="sortChange($event, 'last_login_date')">最后登录时间</uni-th>
  50. <uni-th align="center">操作</uni-th>
  51. </uni-tr>
  52. <uni-tr v-for="(item,index) in data" :key="index">
  53. <uni-td align="center">{{item.username}}</uni-td>
  54. <uni-td align="center">{{item.nickname}}</uni-td>
  55. <uni-td align="center">{{item.mobile}}</uni-td>
  56. <uni-td align="center">{{options.status_valuetotext[item.status]}}</uni-td>
  57. <uni-td align="center">
  58. <uni-link :href="'mailto:' + item.email" :text="item.email"></uni-link>
  59. </uni-td>
  60. <uni-td align="center"> {{ item.role }}</uni-td>
  61. <uni-td align="center">
  62. <block v-for="(tag,tagIndex) in item.tags" :key="tagIndex">
  63. <uni-tag type="primary" inverted size="small" :text="tag" v-if="item.tags" style="margin: 0 5px;"></uni-tag>
  64. </block>
  65. </uni-td>
  66. <uni-td align="center">
  67. <uni-link v-if="item.dcloud_appid === undefined" :href="noAppidWhatShouldIDoLink">
  68. 未绑定可登录应用<view class="uni-icons-help"></view>
  69. </uni-link>
  70. {{ item.dcloud_appid }}
  71. </uni-td>
  72. <uni-td align="center">
  73. <uni-dateformat :threshold="[0, 0]" :date="item.last_login_date"></uni-dateformat>
  74. </uni-td>
  75. <uni-td align="center">
  76. <view class="uni-group">
  77. <button @click="navigateTo('./edit?id=' + item._id, false)" class="uni-button" size="mini"
  78. type="primary">{{ $t('common.button.edit') }}</button>
  79. <button @click="confirmDelete(item._id)" class="uni-button" size="mini"
  80. type="warn">{{ $t('common.button.delete') }}</button>
  81. </view>
  82. </uni-td>
  83. </uni-tr>
  84. </uni-table>
  85. <view class="uni-pagination-box">
  86. <uni-pagination show-iconn show-page-size :page-size="pagination.size" v-model="pagination.current"
  87. :total="pagination.count" @change="onPageChanged" @pageSizeChange="changeSize" />
  88. </view>
  89. </unicloud-db>
  90. </view>
  91. <!-- #ifndef H5 -->
  92. <fix-window />
  93. <!-- #endif -->
  94. <uni-popup ref="tagsPopup" type="center">
  95. <view class="tags-manager--x">
  96. <view class="tags-manager--header mb">管理标签</view>
  97. <uni-data-checkbox ref="checkbox" v-model="managerTags" class="mb ml" :multiple="true"
  98. collection="uni-id-tag" field="tagid as value, name as text"></uni-data-checkbox>
  99. <view class="uni-group">
  100. <button @click="managerMultiTag" class="uni-button" type="primary"
  101. style="margin-right: 75px;">保存</button>
  102. </view>
  103. </view>
  104. </uni-popup>
  105. <!-- #ifdef H5 -->
  106. <batch-sms ref="batchSms" toType="user" :receiver="smsReceiver" :condition="smsCondition"></batch-sms>
  107. <!-- #endif -->
  108. </view>
  109. </template>
  110. <script>
  111. import {
  112. enumConverter,
  113. filterToWhere
  114. } from '../../../js_sdk/validator/uni-id-users.js';
  115. import UniForms from "@/uni_modules/uni-forms/components/uni-forms/uni-forms";
  116. import UniFormsItem from "@/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item";
  117. import UniEasyinput from "@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput";
  118. const db = uniCloud.database()
  119. // 表查询配置
  120. const dbOrderBy = 'last_login_date desc' // 排序字段
  121. const dbSearchFields = ['username', 'role.role_name', 'mobile', 'email'] // 支持模糊搜索的字段列表
  122. // 分页配置
  123. const pageSize = 20
  124. const pageCurrent = 1
  125. const orderByMapping = {
  126. "ascending": "asc",
  127. "descending": "desc"
  128. }
  129. export default {
  130. data() {
  131. return {
  132. collectionList: [ db.collection('uni-id-users').field('ali_openid,apple_openid,avatar,avatar_file,comment,dcloud_appid,department_id,email,email_confirmed,gender,invite_time,inviter_uid,last_login_date,last_login_ip,mobile,mobile_confirmed,my_invite_code,nickname,role,score,status,username,wx_unionid,qq_unionid,tags').getTemp(),db.collection('uni-id-roles').field('role_id, role_name').getTemp() ],
  133. query: '',
  134. where: '',
  135. orderby: dbOrderBy,
  136. orderByFieldName: "",
  137. selectedIndexs: [],
  138. pageSizeIndex: 0,
  139. pageSizeOption: [20, 50, 100, 500],
  140. tags: {},
  141. managerTags: [],
  142. queryTagid: '',
  143. queryUserId: '',
  144. options: {
  145. pageSize,
  146. pageCurrent,
  147. filterData: {
  148. "status_localdata": [{
  149. "text": "正常",
  150. "value": 0,
  151. "checked": true
  152. },
  153. {
  154. "text": "禁用",
  155. "value": 1
  156. },
  157. {
  158. "text": "审核中",
  159. "value": 2
  160. },
  161. {
  162. "text": "审核拒绝",
  163. "value": 3
  164. }
  165. ]
  166. },
  167. ...enumConverter
  168. },
  169. imageStyles: {
  170. width: 64,
  171. height: 64
  172. },
  173. exportExcel: {
  174. "filename": "uni-id-users.xls",
  175. "type": "xls",
  176. "fields": {
  177. "用户名": "username",
  178. "手机号码": "mobile",
  179. "用户状态": "status",
  180. "邮箱": "email",
  181. "角色": "role",
  182. "last_login_date": "last_login_date"
  183. }
  184. },
  185. exportExcelData: [],
  186. noAppidWhatShouldIDoLink: 'https://uniapp.dcloud.net.cn/uniCloud/uni-id?id=makeup-dcloud-appid',
  187. smsCondition: {}
  188. }
  189. },
  190. onLoad(e) {
  191. this._filter = {}
  192. const tagid = e.tagid
  193. const userId = e.id
  194. if (tagid) {
  195. this.queryTagid = tagid
  196. const options = {
  197. filterType: "select",
  198. filter: [tagid]
  199. }
  200. this.filterChange(options, "tags")
  201. }
  202. if (userId) {
  203. this.queryUserId = userId
  204. const options = {
  205. filterType: "select",
  206. filter: [userId]
  207. }
  208. this.filterChange(options, "_id")
  209. }
  210. },
  211. onReady() {
  212. this.loadTags()
  213. if (!this.queryTagid && !this.queryUserId) {
  214. this.$refs.udb.loadData()
  215. }
  216. },
  217. computed: {
  218. tagsData() {
  219. const dynamic_data = []
  220. for (const key in this.tags) {
  221. const tag = {
  222. value: key,
  223. text: this.tags[key]
  224. }
  225. if (key === this.queryTagid) {
  226. tag.checked = true
  227. }
  228. dynamic_data.push(tag)
  229. }
  230. return dynamic_data
  231. },
  232. smsReceiver() {
  233. if (this.selectedIndexs.length) {
  234. let dataList = this.$refs.udb.dataList
  235. return this.selectedIndexs.map(i => dataList[i]._id)
  236. } else {
  237. return undefined;
  238. }
  239. }
  240. },
  241. methods: {
  242. onqueryload(data) {
  243. for (let i = 0; i < data.length; i++) {
  244. let item = data[i]
  245. const roleArr = item.role.map(item => item.role_name)
  246. item.role = roleArr.join('、')
  247. const tagsArr = item.tags && item.tags.map(item => this.tags[item])
  248. item.tags = tagsArr
  249. if (Array.isArray(item.dcloud_appid)) {
  250. item.dcloud_appid = item.dcloud_appid.join('、')
  251. }
  252. item.last_login_date = this.$formatDate(item.last_login_date)
  253. }
  254. this.exportExcelData = data
  255. },
  256. changeSize(pageSize) {
  257. this.options.pageSize = pageSize
  258. this.options.pageCurrent = 1
  259. this.$nextTick(() => {
  260. this.loadData()
  261. })
  262. },
  263. openTagsPopup() {
  264. this.$refs.tagsPopup.open()
  265. },
  266. closeTagsPopup() {
  267. this.$refs.tagsPopup.close()
  268. },
  269. getWhere() {
  270. const query = this.query.trim()
  271. if (!query) {
  272. return ''
  273. }
  274. const queryRe = new RegExp(query, 'i')
  275. console.log(
  276. JSON.stringify(
  277. db.command.or(
  278. dbSearchFields.map(name => {
  279. return {
  280. [name]: queryRe
  281. }
  282. })
  283. )
  284. )
  285. )
  286. return db.command.or(
  287. dbSearchFields.map(name => {
  288. return {
  289. [name]: queryRe
  290. }
  291. })
  292. )
  293. return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
  294. },
  295. search() {
  296. const newWhere = this.getWhere()
  297. this.where = newWhere
  298. // 下一帧拿到查询条件
  299. this.$nextTick(() => {
  300. this.loadData()
  301. })
  302. },
  303. loadData(clear = true) {
  304. this.$refs.udb.loadData({
  305. clear
  306. })
  307. },
  308. onPageChanged(e) {
  309. this.selectedIndexs.length = 0
  310. this.$refs.table.clearSelection()
  311. this.$refs.udb.loadData({
  312. current: e.current
  313. })
  314. },
  315. navigateTo(url, clear) {
  316. // clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
  317. uni.navigateTo({
  318. url,
  319. events: {
  320. refreshData: () => {
  321. this.loadTags()
  322. this.loadData(clear)
  323. }
  324. }
  325. })
  326. },
  327. // 多选处理
  328. selectedItems() {
  329. let dataList = this.$refs.udb.dataList
  330. return this.selectedIndexs.map(i => dataList[i]._id)
  331. },
  332. // 批量删除
  333. delTable() {
  334. this.$refs.udb.remove(this.selectedItems(), {
  335. success: (res) => {
  336. this.$refs.table.clearSelection()
  337. }
  338. })
  339. },
  340. // 多选
  341. selectionChange(e) {
  342. this.selectedIndexs = e.detail.index
  343. },
  344. confirmDelete(id) {
  345. this.$refs.udb.remove(id, {
  346. success: (res) => {
  347. this.$refs.table.clearSelection()
  348. }
  349. })
  350. },
  351. sortChange(e, name) {
  352. this.orderByFieldName = name;
  353. if (e.order) {
  354. this.orderby = name + ' ' + orderByMapping[e.order]
  355. } else {
  356. this.orderby = ''
  357. }
  358. this.$refs.table.clearSelection()
  359. this.$nextTick(() => {
  360. this.$refs.udb.loadData()
  361. })
  362. },
  363. filterChange(e, name) {
  364. this._filter[name] = {
  365. type: e.filterType,
  366. value: e.filter
  367. }
  368. let newWhere = filterToWhere(this._filter, db.command)
  369. if (Object.keys(newWhere).length) {
  370. this.where = newWhere
  371. } else {
  372. this.where = ''
  373. }
  374. // uni-sms-co
  375. if (Object.keys(this._filter).length) {
  376. this.smsCondition = this._filter
  377. } else {
  378. this.smsCondition = {}
  379. }
  380. this.$nextTick(() => {
  381. this.$refs.udb.loadData()
  382. })
  383. },
  384. loadTags() {
  385. db.collection('uni-id-tag').limit(500).get().then(res => {
  386. res.result.data.map(item => {
  387. this.$set(this.tags, item.tagid, item.name)
  388. })
  389. }).catch(err => {
  390. uni.showModal({
  391. title: '提示',
  392. content: err.message,
  393. showCancel: false
  394. })
  395. })
  396. },
  397. managerMultiTag() {
  398. const ids = this.selectedItems()
  399. db.collection('uni-id-users').where({
  400. _id: db.command.in(ids)
  401. }).update({
  402. tags: this.managerTags
  403. }).then(() => {
  404. uni.showToast({
  405. title: '修改标签成功',
  406. duration: 2000
  407. })
  408. this.$refs.table.clearSelection()
  409. this.managerTags = []
  410. this.loadData()
  411. this.closeTagsPopup()
  412. }).catch(err => {
  413. uni.showModal({
  414. content: err.message || '请求服务失败',
  415. showCancel: false
  416. })
  417. }).finally(err => {
  418. uni.hideLoading()
  419. })
  420. }
  421. }
  422. }
  423. </script>
  424. <style lang="scss">
  425. .tags-manager {
  426. &--x {
  427. width: 400px;
  428. padding: 40px 30px;
  429. border-radius: 5px;
  430. background-color: #fff;
  431. }
  432. &--header {
  433. font-size: 22px;
  434. color: #333;
  435. text-align: center;
  436. }
  437. }
  438. .mb {
  439. margin-bottom: 80px;
  440. }
  441. .ml {
  442. margin-left: 30px;
  443. }
  444. </style>