index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <template>
  2. <div class="sy-content">
  3. <el-row>
  4. <el-col>
  5. <!-- 搜索信息 -->
  6. <el-form
  7. v-show="showSearch"
  8. ref="queryForm"
  9. :model="queryParams"
  10. size="small"
  11. :inline="true"
  12. label-width="68px"
  13. >
  14. <el-form-item
  15. label="用户名称"
  16. prop="userName"
  17. >
  18. <el-input
  19. v-model="queryParams.userName"
  20. placeholder="请输入用户名称"
  21. clearable
  22. style="width: 150px"
  23. @keyup.enter.native="handleQuery"
  24. />
  25. </el-form-item>
  26. <el-form-item
  27. label="用户状态"
  28. prop="state"
  29. >
  30. <el-select
  31. v-model="queryParams.state"
  32. placeholder="用户状态"
  33. clearable
  34. style="width: 150px"
  35. >
  36. <el-option
  37. v-for="status in userStatus"
  38. :key="status.value"
  39. :label="status.name"
  40. :value="status.value"
  41. />
  42. </el-select>
  43. </el-form-item>
  44. <el-form-item
  45. label="用户类型"
  46. prop="userType"
  47. >
  48. <el-select
  49. v-model="queryParams.userType"
  50. placeholder="用户类型"
  51. clearable
  52. style="width: 150px"
  53. >
  54. <el-option
  55. v-for="type in userType"
  56. :key="type.value"
  57. :label="type.name"
  58. :value="type.value"
  59. />
  60. </el-select>
  61. </el-form-item>
  62. <el-form-item>
  63. <el-button
  64. type="primary"
  65. icon="el-icon-search"
  66. size="mini"
  67. @click="handleQuery"
  68. >搜索</el-button>
  69. <el-button
  70. icon="el-icon-refresh"
  71. size="mini"
  72. @click="resetQuery"
  73. >重置</el-button>
  74. </el-form-item>
  75. </el-form>
  76. <!-- 操作按钮 -->
  77. <el-row>
  78. <el-col :span="1.5">
  79. <el-button
  80. type="primary"
  81. plain
  82. icon="el-icon-plus"
  83. size="mini"
  84. @click="handleAdd"
  85. >新增</el-button>
  86. </el-col>
  87. <el-col
  88. :span="1.5"
  89. style="margin-left: 15px;"
  90. >
  91. <el-button
  92. type="danger"
  93. plain
  94. icon="el-icon-delete"
  95. size="mini"
  96. :disabled="multiple"
  97. @click="handleDelete"
  98. >
  99. 删除</el-button>
  100. </el-col>
  101. </el-row>
  102. <!-- 表格数据信息 -->
  103. <el-table
  104. v-loading="loading"
  105. :data="userList"
  106. @selection-change="handleSelectionChange"
  107. >
  108. <el-table-column
  109. type="selection"
  110. width="50"
  111. align="center"
  112. />
  113. <el-table-column
  114. v-if="false"
  115. key="userId"
  116. label="用户编号"
  117. align="center"
  118. prop="userId"
  119. />
  120. <el-table-column
  121. key="userName"
  122. label="用户名称"
  123. align="center"
  124. prop="userName"
  125. :show-overflow-tooltip="true"
  126. />
  127. <el-table-column
  128. key="state"
  129. label="用户状态"
  130. align="center"
  131. prop="state"
  132. :show-overflow-tooltip="true"
  133. >
  134. <template slot-scope="scope">
  135. <span>{{ convertUserStatus(scope.row.state) }}</span>
  136. </template>
  137. </el-table-column>
  138. <el-table-column
  139. key="userType"
  140. label="用户类型"
  141. align="center"
  142. prop="userType"
  143. :show-overflow-tooltip="true"
  144. >
  145. <template slot-scope="scope">
  146. <span>{{ convertUserType(scope.row.userType) }}</span>
  147. </template>
  148. </el-table-column>
  149. <el-table-column
  150. key="lastLoginTime"
  151. label="最后登录时间"
  152. align="center"
  153. prop="lastLoginTime"
  154. width="160"
  155. />
  156. <el-table-column
  157. label="创建时间"
  158. align="center"
  159. prop="createTime"
  160. width="160"
  161. />
  162. <el-table-column
  163. label="操作"
  164. align="center"
  165. width="250"
  166. class-name="small-padding fixed-width"
  167. >
  168. <template
  169. v-if="scope.row.userId !== 1"
  170. slot-scope="scope"
  171. >
  172. <el-button
  173. size="mini"
  174. type="text"
  175. icon="el-icon-edit"
  176. @click="handleUpdate(scope.row)"
  177. >修改</el-button>
  178. <el-button
  179. size="mini"
  180. type="text"
  181. icon="el-icon-delete"
  182. @click="handleDelete(scope.row)"
  183. >删除</el-button>
  184. <el-button
  185. size="mini"
  186. type="text"
  187. icon="el-icon-key"
  188. @click="handleResetPwd(scope.row)"
  189. >重置密码</el-button>
  190. </template>
  191. </el-table-column>
  192. </el-table>
  193. <!-- 分页信息 -->
  194. <pagination
  195. v-show="total > 0"
  196. :total="total"
  197. :page.sync="queryParams.page"
  198. :limit.sync="queryParams.limit"
  199. align="right"
  200. @pagination="getUserList"
  201. />
  202. </el-col>
  203. </el-row>
  204. <!-- 添加或修改用户配置对话框 -->
  205. <el-dialog
  206. :title="title"
  207. :visible.sync="open"
  208. width="600px"
  209. append-to-body
  210. :close-on-click-modal="false"
  211. >
  212. <el-form
  213. ref="form"
  214. :model="form"
  215. :rules="rules"
  216. label-width="80px"
  217. >
  218. <el-row>
  219. <el-col :span="12">
  220. <el-form-item
  221. label="用户名称"
  222. prop="userName"
  223. >
  224. <el-input
  225. v-if="form.userId == undefined"
  226. v-model="form.userName"
  227. placeholder="请输入用户名称"
  228. maxlength="30"
  229. />
  230. <el-input
  231. v-else
  232. v-model="form.userName"
  233. placeholder="请输入用户名称"
  234. maxlength="30"
  235. readonly
  236. />
  237. </el-form-item>
  238. </el-col>
  239. <el-col :span="12">
  240. <el-form-item
  241. v-if="form.userId == undefined"
  242. label="用户密码"
  243. prop="password"
  244. >
  245. <el-input
  246. v-model="form.password"
  247. placeholder="请输入用户密码"
  248. type="password"
  249. maxlength="20"
  250. show-password
  251. auto-complete="new-password"
  252. />
  253. </el-form-item>
  254. </el-col>
  255. </el-row>
  256. <el-row>
  257. <el-col :span="12">
  258. <el-form-item label="用户类型">
  259. <el-select
  260. v-model="form.userType"
  261. placeholder="请选择用户类型"
  262. >
  263. <el-option
  264. v-for="dict in userType"
  265. :key="dict.value"
  266. :label="dict.name"
  267. :value="dict.value"
  268. />
  269. </el-select>
  270. </el-form-item>
  271. </el-col>
  272. <el-col :span="12">
  273. <el-form-item
  274. v-if="form.userId != undefined"
  275. label="用户状态"
  276. >
  277. <el-radio-group v-model="form.state">
  278. <el-radio
  279. v-for="dict in userStatus"
  280. :key="dict.value"
  281. :label="dict.value"
  282. >{{ dict.name }}</el-radio>
  283. </el-radio-group>
  284. </el-form-item>
  285. </el-col>
  286. </el-row>
  287. </el-form>
  288. <div
  289. slot="footer"
  290. class="dialog-footer"
  291. >
  292. <el-button
  293. type="primary"
  294. @click="submitForm"
  295. >确 定</el-button>
  296. <el-button @click="cancel">取 消</el-button>
  297. </div>
  298. </el-dialog>
  299. </div>
  300. </template>
  301. <script>
  302. import { addUserInfo, getUserList, updateUserInfo, delUserById } from '@/api/system/user'
  303. import { getPubKey } from '@/utils/auth'
  304. import { encrypt } from '@/utils/jsencrypt'
  305. export default {
  306. name: 'Index',
  307. data() {
  308. return {
  309. // 遮罩层
  310. loading: true,
  311. // 显示搜索条件
  312. showSearch: true,
  313. // 非多个禁用
  314. multiple: true,
  315. // 查询参数
  316. queryParams: {
  317. page: 1,
  318. limit: 10,
  319. userName: undefined,
  320. userType: undefined,
  321. state: undefined
  322. },
  323. // 总条数
  324. total: 0,
  325. // 用户表格数据
  326. userList: null,
  327. // 弹出层标题
  328. title: '',
  329. // 是否显示弹出层
  330. open: false,
  331. // 表单参数
  332. form: {},
  333. // 管理员默认ID
  334. defaultAdminId: '1',
  335. // 用户状态
  336. userStatus: this.$store.getters.userStatus,
  337. // 用户类型
  338. userType: this.$store.getters.userType,
  339. // 表单校验
  340. rules: {
  341. userName: [
  342. { required: true, message: '用户名称不能为空', trigger: 'blur' },
  343. { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
  344. ],
  345. password: [
  346. { required: true, message: '用户密码不能为空', trigger: 'blur' },
  347. { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
  348. ]
  349. }
  350. }
  351. },
  352. created() {
  353. this.getUserList()
  354. },
  355. methods: {
  356. /** 搜索按钮操作 */
  357. handleQuery() {
  358. this.queryParams.page = 1
  359. this.getUserList()
  360. },
  361. /** 重置按钮操作 */
  362. resetQuery() {
  363. this.$refs['queryForm'].resetFields()
  364. this.handleQuery()
  365. },
  366. /** 多选框选中数据 */
  367. handleSelectionChange(selection) {
  368. this.ids = selection.map(item => item.userId)
  369. this.multiple = !selection.length
  370. },
  371. /** 用户状态转换 */
  372. convertUserStatus(val) {
  373. let result = ''
  374. this.userStatus.forEach((e) => {
  375. if (e.value === val) {
  376. result = e.name
  377. }
  378. })
  379. return result
  380. },
  381. /** 用户类型转换 */
  382. convertUserType(val) {
  383. let result = ''
  384. // this.userType.forEach((e) => {
  385. // if (e.value === val) {
  386. // result = e.name
  387. // }
  388. // })
  389. val === 0 ? result = '客户端用户' : result = '管理用户'
  390. return result
  391. },
  392. /** 查询用户列表 */
  393. getUserList() {
  394. this.loading = true
  395. getUserList(this.queryParams).then(response => {
  396. const data = response.data
  397. this.userList = data.userList
  398. this.total = data.count
  399. this.loading = false
  400. })
  401. },
  402. /** 删除按钮操作 */
  403. handleDelete(row) {
  404. const userId = row.userId
  405. if (!userId) {
  406. const userIdList = this.ids
  407. if (!userIdList || userIdList.length === 0) {
  408. this.$message({
  409. message: '请选择您要删除的用户',
  410. type: 'warning'
  411. })
  412. return
  413. }
  414. this.$confirm('您确定要删除选中的用户吗?', '提示', {
  415. confirmButtonText: '确定',
  416. cancelButtonText: '取消',
  417. cancelButtonClass: 'btn_custom_cancel',
  418. closeOnClickModal: false,
  419. type: 'warning'
  420. }).then(() => {
  421. for (const i in userIdList) {
  422. const id = userIdList[i]
  423. if (id === this.defaultAdminId) {
  424. continue
  425. }
  426. delUserById({
  427. 'userId': id
  428. }).then(response => {
  429. this.getUserList()
  430. })
  431. }
  432. }).catch(() => {
  433. })
  434. return
  435. }
  436. if (userId === this.defaultAdminId) {
  437. this.$message({
  438. message: '系统管理员帐号无法进行删除操作',
  439. type: 'warning'
  440. })
  441. return
  442. }
  443. this.$confirm('您确定要删除用户【' + row.userName + '】吗?', '提示', {
  444. confirmButtonText: '确定',
  445. cancelButtonText: '取消',
  446. cancelButtonClass: 'btn_custom_cancel',
  447. closeOnClickModal: false,
  448. type: 'warning'
  449. }).then(() => {
  450. delUserById({
  451. userId
  452. }).then(response => {
  453. this.$message({
  454. message: '删除成功',
  455. type: 'success'
  456. })
  457. this.getUserList()
  458. })
  459. }).catch(() => {
  460. })
  461. },
  462. /** 重置密码按钮操作 */
  463. handleResetPwd(row) {
  464. if (row.userId === this.defaultAdminId) {
  465. this.$message({
  466. message: '系统管理员帐号无法进行密码重置操作',
  467. type: 'warning'
  468. })
  469. return
  470. }
  471. this.$confirm('是否重置密码?', '提示', {
  472. confirmButtonText: '确定',
  473. cancelButtonText: '取消',
  474. cancelButtonClass: 'btn_custom_cancel',
  475. closeOnClickModal: false,
  476. type: 'warning'
  477. }).then(() => {
  478. const data = this.$store.dispatch('user/ResetPassword', {
  479. userId: row.userId,
  480. newPassword: 'cy123456'
  481. })
  482. data.then(res => {
  483. if (res.code === 200) {
  484. this.$message({
  485. type: 'success',
  486. message: '修改成功'
  487. })
  488. }
  489. })
  490. }).catch(() => {
  491. this.$message({
  492. type: 'info',
  493. message: '已取消重置'
  494. })
  495. })
  496. },
  497. /** 修改按钮操作 */
  498. handleUpdate(row) {
  499. if (row.userId === this.defaultAdminId) {
  500. this.$message({
  501. message: '系统管理员帐号无法进行修改操作',
  502. type: 'warning'
  503. })
  504. return
  505. }
  506. this.reset()
  507. this.form = {
  508. userId: row.userId,
  509. userName: row.userName,
  510. password: '',
  511. userType: row.userType,
  512. state: row.state
  513. }
  514. this.open = true
  515. this.title = '修改用户'
  516. this.form.password = ''
  517. },
  518. /** 新增按钮操作 */
  519. handleAdd() {
  520. this.reset()
  521. this.form.userType = this.userType[0].value
  522. this.open = true
  523. this.title = '添加用户'
  524. },
  525. /** 表单提交 */
  526. submitForm() {
  527. this.$refs['form'].validate(valid => {
  528. if (valid) {
  529. if (this.form.userId !== undefined) {
  530. updateUserInfo(this.form).then(response => {
  531. this.$message({
  532. message: '修改成功',
  533. type: 'success'
  534. })
  535. this.open = false
  536. this.getUserList()
  537. })
  538. } else {
  539. const publicKey = getPubKey()
  540. this.form.password = encrypt(this.form.password, publicKey)
  541. addUserInfo(this.form).then(response => {
  542. this.$message({
  543. message: '新增成功',
  544. type: 'success'
  545. })
  546. this.open = false
  547. this.getUserList()
  548. })
  549. }
  550. }
  551. })
  552. },
  553. /** 表单重置 */
  554. cancel() {
  555. this.open = false
  556. this.reset()
  557. },
  558. /** 表单重置 */
  559. reset() {
  560. this.form = {
  561. userId: undefined,
  562. userName: undefined,
  563. password: undefined,
  564. userType: '0',
  565. state: '0'
  566. }
  567. if (this.$refs['form']) {
  568. this.$refs['form'].resetFields()
  569. }
  570. }
  571. }
  572. }
  573. </script>
  574. <style rel="stylesheet/scss" lang="scss">
  575. </style>