index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template> 
  2. <div class="app-container">
  3. <el-card class="filter-container" shadow="never">
  4. <div>
  5. <i class="el-icon-search"></i>
  6. <span>筛选搜索</span>
  7. <el-button
  8. style="float:right"
  9. type="primary"
  10. @click="handleSearchList()"
  11. size="small">
  12. 查询搜索
  13. </el-button>
  14. <el-button
  15. style="float:right;margin-right: 15px"
  16. @click="handleResetSearch()"
  17. size="small">
  18. 重置
  19. </el-button>
  20. </div>
  21. <div style="margin-top: 15px">
  22. <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
  23. <el-form-item label="输入搜索:">
  24. <el-input v-model="listQuery.keyword" class="input-width" placeholder="帐号/姓名" clearable></el-input>
  25. </el-form-item>
  26. </el-form>
  27. </div>
  28. </el-card>
  29. <el-card class="operate-container" shadow="never">
  30. <i class="el-icon-tickets"></i>
  31. <span>数据列表</span>
  32. <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
  33. </el-card>
  34. <div class="table-container">
  35. <el-table ref="adminTable"
  36. :data="list"
  37. style="width: 100%;"
  38. v-loading="listLoading" border>
  39. <el-table-column label="编号" width="100" align="center">
  40. <template slot-scope="scope">{{scope.row.id}}</template>
  41. </el-table-column>
  42. <el-table-column label="帐号" align="center">
  43. <template slot-scope="scope">{{scope.row.username}}</template>
  44. </el-table-column>
  45. <el-table-column label="姓名" align="center">
  46. <template slot-scope="scope">{{scope.row.nickName}}</template>
  47. </el-table-column>
  48. <el-table-column label="邮箱" align="center">
  49. <template slot-scope="scope">{{scope.row.email}}</template>
  50. </el-table-column>
  51. <el-table-column label="添加时间" width="160" align="center">
  52. <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
  53. </el-table-column>
  54. <el-table-column label="最后登录" width="160" align="center">
  55. <template slot-scope="scope">{{scope.row.loginTime | formatDateTime}}</template>
  56. </el-table-column>
  57. <el-table-column label="是否启用" width="140" align="center">
  58. <template slot-scope="scope">
  59. <el-switch
  60. @change="handleStatusChange(scope.$index, scope.row)"
  61. :active-value="1"
  62. :inactive-value="0"
  63. v-model="scope.row.status">
  64. </el-switch>
  65. </template>
  66. </el-table-column>
  67. <el-table-column label="操作" width="180" align="center">
  68. <template slot-scope="scope">
  69. <el-button size="mini"
  70. type="text"
  71. @click="handleSelectRole(scope.$index, scope.row)">分配角色
  72. </el-button>
  73. <el-button size="mini"
  74. type="text"
  75. @click="handleUpdate(scope.$index, scope.row)">
  76. 编辑
  77. </el-button>
  78. <el-button size="mini"
  79. type="text"
  80. @click="handleDelete(scope.$index, scope.row)">删除
  81. </el-button>
  82. </template>
  83. </el-table-column>
  84. </el-table>
  85. </div>
  86. <div class="pagination-container">
  87. <el-pagination
  88. background
  89. @size-change="handleSizeChange"
  90. @current-change="handleCurrentChange"
  91. layout="total, sizes,prev, pager, next,jumper"
  92. :current-page.sync="listQuery.pageNum"
  93. :page-size="listQuery.pageSize"
  94. :page-sizes="[10,15,20]"
  95. :total="total">
  96. </el-pagination>
  97. </div>
  98. <el-dialog
  99. :title="isEdit?'编辑用户':'添加用户'"
  100. :visible.sync="dialogVisible"
  101. width="40%">
  102. <el-form :model="admin"
  103. ref="adminForm"
  104. label-width="150px" size="small">
  105. <el-form-item label="帐号:">
  106. <el-input v-model="admin.username" style="width: 250px"></el-input>
  107. </el-form-item>
  108. <el-form-item label="姓名:">
  109. <el-input v-model="admin.nickName" style="width: 250px"></el-input>
  110. </el-form-item>
  111. <el-form-item label="邮箱:">
  112. <el-input v-model="admin.email" style="width: 250px"></el-input>
  113. </el-form-item>
  114. <el-form-item label="密码:">
  115. <el-input v-model="admin.password" type="password" style="width: 250px"></el-input>
  116. </el-form-item>
  117. <el-form-item label="备注:">
  118. <el-input v-model="admin.note"
  119. type="textarea"
  120. :rows="5"
  121. style="width: 250px"></el-input>
  122. </el-form-item>
  123. <el-form-item label="是否启用:">
  124. <el-radio-group v-model="admin.status">
  125. <el-radio :label="1">是</el-radio>
  126. <el-radio :label="0">否</el-radio>
  127. </el-radio-group>
  128. </el-form-item>
  129. </el-form>
  130. <span slot="footer" class="dialog-footer">
  131. <el-button @click="dialogVisible = false" size="small">取 消</el-button>
  132. <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
  133. </span>
  134. </el-dialog>
  135. <el-dialog
  136. title="分配角色"
  137. :visible.sync="allocDialogVisible"
  138. width="30%">
  139. <el-select v-model="allocRoleIds" multiple placeholder="请选择" size="small" style="width: 80%">
  140. <el-option
  141. v-for="item in allRoleList"
  142. :key="item.id"
  143. :label="item.name"
  144. :value="item.id">
  145. </el-option>
  146. </el-select>
  147. <span slot="footer" class="dialog-footer">
  148. <el-button @click="allocDialogVisible = false" size="small">取 消</el-button>
  149. <el-button type="primary" @click="handleAllocDialogConfirm()" size="small">确 定</el-button>
  150. </span>
  151. </el-dialog>
  152. </div>
  153. </template>
  154. <script>
  155. import {fetchList,createAdmin,updateAdmin,updateStatus,deleteAdmin,getRoleByAdmin,allocRole} from '@/api/login';
  156. import {fetchAllRoleList} from '@/api/role';
  157. import {formatDate} from '@/utils/date';
  158. const defaultListQuery = {
  159. pageNum: 1,
  160. pageSize: 10,
  161. keyword: null
  162. };
  163. const defaultAdmin = {
  164. id: null,
  165. username: null,
  166. password: null,
  167. nickName: null,
  168. email: null,
  169. note: null,
  170. status: 1
  171. };
  172. export default {
  173. name: 'adminList',
  174. data() {
  175. return {
  176. listQuery: Object.assign({}, defaultListQuery),
  177. list: null,
  178. total: null,
  179. listLoading: false,
  180. dialogVisible: false,
  181. admin: Object.assign({}, defaultAdmin),
  182. isEdit: false,
  183. allocDialogVisible: false,
  184. allocRoleIds:[],
  185. allRoleList:[],
  186. allocAdminId:null
  187. }
  188. },
  189. created() {
  190. this.getList();
  191. this.getAllRoleList();
  192. },
  193. filters: {
  194. formatDateTime(time) {
  195. if (time == null || time === '') {
  196. return 'N/A';
  197. }
  198. let date = new Date(time);
  199. return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
  200. }
  201. },
  202. methods: {
  203. handleResetSearch() {
  204. this.listQuery = Object.assign({}, defaultListQuery);
  205. },
  206. handleSearchList() {
  207. this.listQuery.pageNum = 1;
  208. this.getList();
  209. },
  210. handleSizeChange(val) {
  211. this.listQuery.pageNum = 1;
  212. this.listQuery.pageSize = val;
  213. this.getList();
  214. },
  215. handleCurrentChange(val) {
  216. this.listQuery.pageNum = val;
  217. this.getList();
  218. },
  219. handleAdd() {
  220. this.dialogVisible = true;
  221. this.isEdit = false;
  222. this.admin = Object.assign({},defaultAdmin);
  223. },
  224. handleStatusChange(index, row) {
  225. this.$confirm('是否要修改该状态?', '提示', {
  226. confirmButtonText: '确定',
  227. cancelButtonText: '取消',
  228. type: 'warning'
  229. }).then(() => {
  230. updateStatus(row.id, {status: row.status}).then(response => {
  231. this.$message({
  232. type: 'success',
  233. message: '修改成功!'
  234. });
  235. });
  236. }).catch(() => {
  237. this.$message({
  238. type: 'info',
  239. message: '取消修改'
  240. });
  241. this.getList();
  242. });
  243. },
  244. handleDelete(index, row) {
  245. this.$confirm('是否要删除该用户?', '提示', {
  246. confirmButtonText: '确定',
  247. cancelButtonText: '取消',
  248. type: 'warning'
  249. }).then(() => {
  250. deleteAdmin(row.id).then(response => {
  251. this.$message({
  252. type: 'success',
  253. message: '删除成功!'
  254. });
  255. this.getList();
  256. });
  257. });
  258. },
  259. handleUpdate(index, row) {
  260. this.dialogVisible = true;
  261. this.isEdit = true;
  262. this.admin = Object.assign({},row);
  263. },
  264. handleDialogConfirm() {
  265. this.$confirm('是否要确认?', '提示', {
  266. confirmButtonText: '确定',
  267. cancelButtonText: '取消',
  268. type: 'warning'
  269. }).then(() => {
  270. if (this.isEdit) {
  271. updateAdmin(this.admin.id,this.admin).then(response => {
  272. this.$message({
  273. message: '修改成功!',
  274. type: 'success'
  275. });
  276. this.dialogVisible =false;
  277. this.getList();
  278. })
  279. } else {
  280. createAdmin(this.admin).then(response => {
  281. this.$message({
  282. message: '添加成功!',
  283. type: 'success'
  284. });
  285. this.dialogVisible =false;
  286. this.getList();
  287. })
  288. }
  289. })
  290. },
  291. handleAllocDialogConfirm(){
  292. this.$confirm('是否要确认?', '提示', {
  293. confirmButtonText: '确定',
  294. cancelButtonText: '取消',
  295. type: 'warning'
  296. }).then(() => {
  297. let params = new URLSearchParams();
  298. params.append("adminId", this.allocAdminId);
  299. params.append("roleIds", this.allocRoleIds);
  300. allocRole(params).then(response => {
  301. this.$message({
  302. message: '分配成功!',
  303. type: 'success'
  304. });
  305. this.allocDialogVisible = false;
  306. })
  307. })
  308. },
  309. handleSelectRole(index,row){
  310. this.allocAdminId = row.id;
  311. this.allocDialogVisible = true;
  312. this.getRoleListByAdmin(row.id);
  313. },
  314. getList() {
  315. this.listLoading = true;
  316. fetchList(this.listQuery).then(response => {
  317. this.listLoading = false;
  318. this.list = response.data.list;
  319. this.total = response.data.total;
  320. });
  321. },
  322. getAllRoleList() {
  323. fetchAllRoleList().then(response => {
  324. this.allRoleList = response.data;
  325. });
  326. },
  327. getRoleListByAdmin(adminId) {
  328. getRoleByAdmin(adminId).then(response => {
  329. let allocRoleList = response.data;
  330. this.allocRoleIds=[];
  331. if(allocRoleList!=null&&allocRoleList.length>0){
  332. for(let i=0;i<allocRoleList.length;i++){
  333. this.allocRoleIds.push(allocRoleList[i].id);
  334. }
  335. }
  336. });
  337. }
  338. }
  339. }
  340. </script>
  341. <style></style>