UserList.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <template>
  2. <div>
  3. <div class="box box-primary">
  4. <div class="box-header">
  5. <h4 class="text-primary text-center">用户列表</h4>
  6. </div>
  7. <div class="box-body">
  8. <form class="form-inline" autocomplete="off" spellcheck="false">
  9. <div class="form-group form-group-sm">
  10. <div class="input-group input-group-sm">
  11. <button type="button" class="btn btn-sm btn-primary" @click.prevent="$refs['userEditDlg'].show()">
  12. <i class="fa fa-plus"></i> 添加用户
  13. </button>
  14. </div>
  15. </div>
  16. <span class="hidden-xs">&nbsp;&nbsp;</span>
  17. <div class="form-group form-group-sm">
  18. <label>搜索</label>
  19. <input type="text" class="form-control" placeholder="关键字" v-model.trim="q" @keydown.enter.prevent ref="q">
  20. </div>
  21. <span class="hidden-xs">&nbsp;&nbsp;</span>
  22. <div class="form-group form-group-sm">
  23. <label>状态</label>
  24. <select class="form-control" v-model.trim="enable">
  25. <option value="">全部</option>
  26. <option value="true">启用</option>
  27. <option value="false">禁用</option>
  28. </select>
  29. </div>
  30. </form>
  31. <br>
  32. <div class="clearfix"></div>
  33. <el-table :data="users" stripe :default-sort="{prop: 'LastLoginAt', order: 'descending'}" @sort-change="sortChange" v-loading="loading" element-loading-text="加载中...">
  34. <el-table-column prop="Username" label="用户名" min-width="120" show-overflow-tooltip sortable="custom"></el-table-column>
  35. <el-table-column label="操作" min-width="300" v-if="isMobile()" class-name="opt-group">
  36. <template slot-scope="props">
  37. <div class="btn-group btn-group-xs" v-if="props.row.Role != '超级管理员'">
  38. <button type="button" class="btn btn-warning" @click.prevent="editUser(props.row)">
  39. <i class="fa fa-edit"></i> 编辑
  40. </button>
  41. <button type="button" class="btn btn-primary" @click.prevent="editChannel(props.row)">
  42. <i class="fa fa-check"></i> 关联通道
  43. </button>
  44. <button type="button" class="btn btn-info" @click.prevent="resetPassword(props.row)">
  45. <i class="fa fa-key"></i> 重置密码
  46. </button>
  47. <button type="button" class="btn btn-danger" @click.prevent="removeUser(props.row)">
  48. <i class="fa fa-remove"></i> 删除
  49. </button>
  50. </div>
  51. </template>
  52. </el-table-column>
  53. <el-table-column prop="Role" label="角色" min-width="200" show-overflow-tooltip sortable="custom"></el-table-column>
  54. <el-table-column prop="Enable" label="是否启用" min-width="100">
  55. <template slot-scope="props">
  56. <span v-if="props.row.Enable" class="text-success">启用</span>
  57. <span v-else>禁用</span>
  58. </template>
  59. </el-table-column>
  60. <el-table-column prop="LastLoginAt" label="最近登录" min-width="160" sortable="custom"></el-table-column>
  61. <el-table-column prop="UpdatedAt" label="更新时间" min-width="160" sortable="custom"></el-table-column>
  62. <el-table-column prop="CreatedAt" label="创建时间" min-width="160" sortable="custom"></el-table-column>
  63. <el-table-column label="操作" min-width="300" fixed="right" v-if="!isMobile()" class-name="opt-group">
  64. <template slot-scope="props">
  65. <div class="btn-group btn-group-xs" v-if="props.row.Role != '超级管理员'">
  66. <button type="button" class="btn btn-warning" @click.prevent="editUser(props.row)">
  67. <i class="fa fa-edit"></i> 编辑
  68. </button>
  69. <button type="button" class="btn btn-primary" @click.prevent="editChannel(props.row)">
  70. <i class="fa fa-check"></i> 关联通道
  71. </button>
  72. <button type="button" class="btn btn-info" @click.prevent="resetPassword(props.row)">
  73. <i class="fa fa-key"></i> 重置密码
  74. </button>
  75. <button type="button" class="btn btn-danger" @click.prevent="removeUser(props.row)">
  76. <i class="fa fa-remove"></i> 删除
  77. </button>
  78. </div>
  79. </template>
  80. </el-table-column>
  81. </el-table>
  82. </div>
  83. <div class="box-footer" v-if="total > 0">
  84. <el-pagination layout="total,prev,pager,next" :pager-count="isMobile() ? 3 : 5" class="pull-right" :total="total" :page-size.sync="pageSize" :current-page.sync="currentPage"></el-pagination>
  85. </div>
  86. </div>
  87. <UserEditDlg ref="userEditDlg" @submit="getUsers()"></UserEditDlg>
  88. <UserChannelListDlg ref="userChannelListDlg" size="modal-lg" :title="userChannelListDlgTitle"></UserChannelListDlg>
  89. </div>
  90. </template>
  91. <script>
  92. import _ from "lodash";
  93. import UserEditDlg from "components/UserEditDlg";
  94. import UserChannelListDlg from "components/UserChannelListDlg"
  95. import { mapState } from "vuex";
  96. export default {
  97. props: {
  98. },
  99. data() {
  100. return {
  101. q: "",
  102. enable: "",
  103. total: 0,
  104. pageSize: 10,
  105. currentPage: 1,
  106. sort: "ID",
  107. order: "asc",
  108. loading: false,
  109. users: [],
  110. userChannelListDlgTitle: "关联通道",
  111. };
  112. },
  113. computed: {
  114. ...mapState(['userInfo', 'serverInfo']),
  115. },
  116. components: {
  117. UserEditDlg, UserChannelListDlg
  118. },
  119. mounted() {
  120. // this.timer = setInterval(() => {
  121. // this.getUsers();
  122. // }, 3000);
  123. },
  124. beforeDestroy() {
  125. if (this.timer) {
  126. clearInterval(this.timer);
  127. this.timer = 0;
  128. }
  129. },
  130. methods: {
  131. ready(){
  132. this.$watch('q', function(newVal, oldVal) {
  133. this.doDelaySearch();
  134. });
  135. this.$watch('enable', function(newVal, oldVal) {
  136. this.doSearch();
  137. });
  138. this.$watch('currentPage', function(newVal, oldVal) {
  139. this.doSearch(newVal);
  140. });
  141. },
  142. doSearch(page = 1) {
  143. var query = {};
  144. if (this.q) query["q"] = this.q;
  145. if (this.enable) query["enable"] = this.enable;
  146. this.$router.replace({
  147. path: `/user/${page}`,
  148. query: query
  149. });
  150. },
  151. doDelaySearch: _.debounce(function() {
  152. this.doSearch();
  153. }, 500),
  154. getUsers() {
  155. $.get("/api/v1/user/list", {
  156. q: this.q,
  157. start: (this.currentPage -1) * this.pageSize,
  158. limit: this.pageSize,
  159. enable: this.enable,
  160. sort: this.sort,
  161. order: this.order
  162. })
  163. .then(ret => {
  164. this.total = ret.UserCount;
  165. this.users = ret.UserList;
  166. }).always(() => {});
  167. },
  168. sortChange(data) {
  169. this.sort = data.prop;
  170. this.order = data.order == "ascending" ? "asc" : "desc";
  171. this.getUsers();
  172. },
  173. formatName(row, col, cell) {
  174. if (cell) return cell;
  175. return "-";
  176. },
  177. editUser(row) {
  178. this.$refs['userEditDlg'].show(row);
  179. },
  180. editChannel(row) {
  181. this.userChannelListDlgTitle = `关联通道(${row.Username || row.ID})`;
  182. this.$refs['userChannelListDlg'].show(row.ID);
  183. },
  184. resetPassword(row) {
  185. this.$prompt(`请输入 ${row.Username || row.ID} 的新密码:`, "重置密码", {
  186. confirmButtonText: '确定',
  187. cancelButtonText: '取消',
  188. inputPattern: /\S+/,
  189. inputErrorMessage: '请输入有效密码',
  190. closeOnClickModal: false,
  191. }).then(({ value }) => {
  192. $.get("/api/v1/user/resetpassword", {
  193. id: row.ID,
  194. password: value,
  195. }).then(() => {
  196. this.$message({
  197. type: 'success',
  198. message: "密码重置成功!"
  199. })
  200. })
  201. }).catch(() => {});
  202. },
  203. removeUser(row) {
  204. this.$confirm(`确认删除 ${row.Username || row.ID}`, "提示").then(() => {
  205. $.get("/api/v1/user/remove", {
  206. id: row.ID
  207. }).always(() => {
  208. this.getUsers();
  209. });
  210. }).catch(() => {});
  211. }
  212. },
  213. beforeRouteEnter(to, from, next) {
  214. next(vm => {
  215. vm.q = to.query.q || "";
  216. vm.enable = to.query.enable || "";
  217. vm.currentPage = parseInt(to.params.page) || 1;
  218. vm.ready();
  219. });
  220. },
  221. beforeRouteUpdate(to, from, next) {
  222. next();
  223. this.$nextTick(() => {
  224. this.q = to.query.q || "";
  225. this.enable = to.query.enable || "";
  226. this.currentPage = parseInt(to.params.page) || 1;
  227. this.users = [];
  228. this.getUsers();
  229. });
  230. }
  231. };
  232. </script>
  233. <style lang="less">
  234. .opt-group .cell {
  235. overflow: visible;
  236. }
  237. </style>