index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <template>
  2. <div class="global-variable">
  3. <div class="condition">
  4. <el-form
  5. :inline="true"
  6. ref="ruleForm"
  7. :model="formInline"
  8. class="demo-form-inline"
  9. :rules="rules"
  10. >
  11. <el-form-item label="用户名称:" prop="userName">
  12. <el-input
  13. size="small"
  14. v-model="formInline.userName"
  15. placeholder="请输入用户名称"
  16. ></el-input>
  17. </el-form-item>
  18. <el-form-item label="角色名称:" prop="roleId">
  19. <el-select
  20. v-model="formInline.roleId"
  21. placeholder="请选择角色"
  22. size="small"
  23. >
  24. <el-option
  25. :label="item.roleDescription"
  26. v-for="item in roleList"
  27. :key="item.id"
  28. :value="item.id + ''"
  29. ></el-option>
  30. </el-select>
  31. </el-form-item>
  32. <el-form-item>
  33. <el-button type="primary" @click="onSubmit" size="small"
  34. >查询</el-button
  35. >
  36. <el-button @click="reset('ruleForm')" size="small">重置</el-button>
  37. </el-form-item>
  38. </el-form>
  39. </div>
  40. <div class="list-page">
  41. <div class="newly">
  42. <el-button type="primary" @click="newnuedialog" size="small"
  43. >新增</el-button
  44. >
  45. </div>
  46. <el-table
  47. v-loading="loading"
  48. class="center-align-table"
  49. :data="tableData"
  50. border
  51. :cell-style="rowStyle"
  52. >
  53. <el-table-column
  54. align="center"
  55. fixed
  56. prop="userId"
  57. label="员工ID"
  58. width="100"
  59. >
  60. </el-table-column>
  61. <el-table-column align="center" label="员工名称" prop="userName">
  62. </el-table-column>
  63. <el-table-column prop="loginName" align="center" label="登录账号">
  64. </el-table-column>
  65. <el-table-column align="center" prop="userPhone" label="手机号">
  66. </el-table-column>
  67. <el-table-column prop="roleName" align="center" label="角色">
  68. </el-table-column>
  69. <el-table-column prop="state" align="center" label="状态" width="100">
  70. <template slot-scope="scope">
  71. <span>
  72. {{
  73. scope.row.state == 0
  74. ? "未启用"
  75. : scope.row.state == 1
  76. ? "启用"
  77. : "禁用"
  78. }}</span
  79. >
  80. </template>
  81. </el-table-column>
  82. <el-table-column
  83. prop="transition"
  84. align="center"
  85. fixed="right"
  86. label="操作"
  87. width="200"
  88. >
  89. <template slot-scope="scope">
  90. <el-button @click="compile(scope.row)" type="text" size="small"
  91. >编辑</el-button
  92. >
  93. <el-button
  94. v-if="scope.row.state == 0 || scope.row.state == 2"
  95. @click="setState(1, scope.row.userId)"
  96. type="text"
  97. size="small"
  98. >启用</el-button
  99. >
  100. <el-button
  101. v-else
  102. style="color: #666"
  103. @click="setState(2, scope.row.userId)"
  104. type="text"
  105. size="small"
  106. >停用</el-button
  107. >
  108. <el-button
  109. style="color: #f90"
  110. @click="editPwd(scope.row)"
  111. type="text"
  112. size="small"
  113. >修改密码</el-button
  114. >
  115. <el-button
  116. style="color: #f00"
  117. @click="deleted(scope.row)"
  118. type="text"
  119. size="small"
  120. :disabled="scope.row.state == 1"
  121. >删除</el-button
  122. >
  123. </template>
  124. </el-table-column>
  125. </el-table>
  126. <div class="pagination-container">
  127. <el-pagination
  128. @current-change="handleCurrentChange"
  129. :current-page.sync="formInline.pageNum"
  130. layout="total, prev, pager, next"
  131. :page-size="formInline.pageSize"
  132. :total="formInline.totalSize"
  133. >
  134. </el-pagination>
  135. </div>
  136. </div>
  137. <!-- 弹出层 -->
  138. <!-- 新增 /编辑-->
  139. <el-dialog :title="title" :visible.sync="nuedialog" width="500px">
  140. <div v-loading="loadingView" class="views">
  141. <el-form
  142. :model="ruleForm"
  143. :rules="addUserRules"
  144. ref="addUserForm"
  145. label-width="100px"
  146. class="demo-ruleForm"
  147. >
  148. <el-form-item label="员工名称" prop="userName">
  149. <el-input
  150. v-model="ruleForm.userName"
  151. placeholder="请输入员工名称"
  152. ></el-input>
  153. </el-form-item>
  154. <el-form-item label="电话号码" prop="phone">
  155. <el-input
  156. v-model="ruleForm.phone"
  157. placeholder="请输入电话号码"
  158. ></el-input>
  159. </el-form-item>
  160. <el-form-item label="账号名称" prop="loginName">
  161. <el-input
  162. v-model="ruleForm.loginName"
  163. placeholder="请输入账号名称"
  164. autocomplete="off"
  165. ></el-input>
  166. </el-form-item>
  167. <el-form-item label="密码" prop="pwd" v-if="title === '新增'">
  168. <el-input
  169. v-model="ruleForm.pwd"
  170. autocomplete="new-password"
  171. show-password
  172. placeholder="请输入密码"
  173. ></el-input>
  174. </el-form-item>
  175. <el-form-item label="角色名称:" prop="roleId">
  176. <el-select
  177. v-model="ruleForm.roleId"
  178. placeholder="请选择角色"
  179. style="width: 100%"
  180. >
  181. <el-option
  182. :label="item.roleDescription"
  183. v-for="item in roleList"
  184. :key="item.id"
  185. :value="item.id + ''"
  186. ></el-option>
  187. </el-select>
  188. </el-form-item>
  189. </el-form>
  190. <span slot="footer" class="dialog-footer">
  191. <el-button @click="cancel('addUserForm')" size="small"
  192. >取 消</el-button
  193. >
  194. <el-button
  195. type="primary"
  196. @click="submitForm('addUserForm')"
  197. size="small"
  198. >确 定</el-button
  199. >
  200. </span>
  201. </div>
  202. </el-dialog>
  203. <!-- 风场详情 -->
  204. <el-dialog title="修改密码" :visible.sync="unusualdialog" width="500px">
  205. <div v-loading="loadingView" class="views">
  206. <el-form
  207. :model="editUserPassword"
  208. ref="editUserPassword"
  209. label-width="100px"
  210. class="demo-ruleForm"
  211. >
  212. <el-form-item
  213. label="旧密码"
  214. prop="oldPWD"
  215. :rules="[{ required: true, message: '旧密码不能为空' }]"
  216. >
  217. <el-input
  218. v-model.number="editUserPassword.oldPWD"
  219. autocomplete="off"
  220. show-password
  221. ></el-input>
  222. </el-form-item>
  223. <el-form-item
  224. label="新密码"
  225. prop="newPWD"
  226. :rules="[{ required: true, message: '新密码不能为空' }]"
  227. >
  228. <el-input
  229. v-model.number="editUserPassword.newPWD"
  230. autocomplete="off"
  231. show-password
  232. ></el-input>
  233. </el-form-item>
  234. </el-form>
  235. <span slot="footer" class="dialog-footer">
  236. <el-button
  237. @click="
  238. () => {
  239. this.$refs['editUserPassword'].resetFields();
  240. this.unusualdialog = false;
  241. this.loadingView = false;
  242. }
  243. "
  244. size="small"
  245. >取 消</el-button
  246. >
  247. <el-button
  248. type="primary"
  249. @click="editUserPwd('editUserPassword')"
  250. size="small"
  251. >确 定</el-button
  252. >
  253. </span>
  254. </div>
  255. </el-dialog>
  256. </div>
  257. </template>
  258. <script>
  259. import {
  260. getUserTableList,
  261. getRoleTableList,
  262. enableUser,
  263. disableUser,
  264. getUserInfoByUserId,
  265. addUser,
  266. editUser,
  267. deleteUserInfo,
  268. updatePWD,
  269. } from "@/api/system.js";
  270. export default {
  271. data() {
  272. const validateUserName = (rule, value, callback) => {
  273. const regex = /^[^\u4e00-\u9fa5\W]+$/;
  274. if (!value) {
  275. callback(new Error("账号名称不能为空"));
  276. } else if (!regex.test(value)) {
  277. callback(new Error("账号名称不能包含中文及特殊字符"));
  278. } else {
  279. callback();
  280. }
  281. };
  282. const validatePhone = (rule, value, callback) => {
  283. const phoneRegex = /^[1][3-9][0-9]{9}$/;
  284. if (!value) {
  285. return callback(new Error("手机号不能为空"));
  286. }
  287. if (!phoneRegex.test(value)) {
  288. return callback(new Error("请输入正确的手机号"));
  289. }
  290. callback();
  291. };
  292. return {
  293. loadingView: false,
  294. loading: false, //数据加载中
  295. rules: {
  296. roleId: { trigger: "change" },
  297. userName: { trigger: "blur" },
  298. },
  299. roleList: [],
  300. formInline: {
  301. userName: undefined,
  302. roleId: undefined,
  303. pageNum: 1,
  304. pageSize: 10,
  305. // sort: "desc",
  306. totalSize: 0,
  307. },
  308. tableData: [],
  309. // 新增编辑表单
  310. ruleForm: {
  311. phone: null,
  312. pwd: null,
  313. roleId: null,
  314. userName: null,
  315. loginName: null,
  316. },
  317. addUserRules: {
  318. phone: [
  319. { required: true, message: "请输入手机号", trigger: "blur" },
  320. { validator: validatePhone, trigger: "blur" },
  321. ],
  322. loginName: [
  323. { required: true, message: "请输入账号名称", trigger: "blur" },
  324. { validator: validateUserName, trigger: "blur" },
  325. ],
  326. pwd: { required: true, message: "请输入密码", trigger: "blur" },
  327. roleId: { required: true, message: "请选择角色", trigger: "change" },
  328. userName: [
  329. { required: true, message: "请输入员工姓名", trigger: "blur" },
  330. ],
  331. },
  332. //修改密码
  333. editUserPassword: {
  334. oldPWD: "",
  335. newPWD: "",
  336. userId: "",
  337. },
  338. nuedialog: false,
  339. unusualdialog: false,
  340. title: "",
  341. };
  342. },
  343. created() {
  344. this.getTableList();
  345. this.getRoleList();
  346. },
  347. methods: {
  348. //修改密码
  349. editUserPwd(formName) {
  350. this.$refs[formName].validate((valid) => {
  351. if (valid) {
  352. this.loadingView = true;
  353. updatePWD({ ...this.editUserPassword })
  354. .then((res) => {
  355. this.$message({
  356. message: "密码修改成功",
  357. type: "success",
  358. });
  359. this.getTableList();
  360. this.unusualdialog = false;
  361. this.loadingView = false;
  362. })
  363. .catch(() => {
  364. this.loadingView = false;
  365. });
  366. } else {
  367. return false;
  368. }
  369. });
  370. },
  371. //分页数据切换
  372. handleCurrentChange(val) {
  373. this.formInline.pageNum = val;
  374. this.getTableList();
  375. },
  376. //修改状态
  377. setState(state, userId) {
  378. switch (state) {
  379. case 1:
  380. //启用接口
  381. enableUser({ userId })
  382. .then((res) => {
  383. this.$message({
  384. message: res.msg,
  385. type: "success",
  386. });
  387. this.getTableList();
  388. })
  389. .catch((error) => {});
  390. break;
  391. case 2:
  392. //禁用接口
  393. disableUser({ userId })
  394. .then((res) => {
  395. this.$message({
  396. message: res.msg,
  397. type: "success",
  398. });
  399. this.getTableList();
  400. })
  401. .catch((error) => {});
  402. break;
  403. default:
  404. return;
  405. }
  406. },
  407. async getTableList() {
  408. try {
  409. this.loading = true;
  410. const result = await getUserTableList({
  411. ...this.formInline,
  412. totalSize: undefined,
  413. });
  414. this.tableData = result.data.list;
  415. this.formInline.totalSize = result.data.totalSize;
  416. this.loading = false;
  417. } catch (error) {
  418. this.$message({
  419. type: "error",
  420. message: "请检查是否连接网络",
  421. });
  422. }
  423. },
  424. async getRoleList() {
  425. try {
  426. const result = await getRoleTableList({ pageNum: 1, pageSize: 100000 });
  427. this.roleList = result.data.list;
  428. } catch (error) {
  429. this.$message({
  430. type: "error",
  431. message: "请检查是否连接网络",
  432. });
  433. }
  434. },
  435. rowStyle() {
  436. return "text-align:center";
  437. },
  438. // 查询
  439. onSubmit() {
  440. this.getTableList();
  441. },
  442. // 重置
  443. reset(formName) {
  444. this.$refs[formName].resetFields();
  445. this.getTableList();
  446. },
  447. // 新增,编辑确定
  448. submitForm(formName) {
  449. this.$refs[formName].validate((valid) => {
  450. if (valid) {
  451. this.loadingView = true;
  452. switch (this.title) {
  453. case "新增":
  454. addUser({ ...this.ruleForm, userId: undefined })
  455. .then((res) => {
  456. this.$message({
  457. type: "success",
  458. message: res.msg,
  459. });
  460. this.getTableList();
  461. this.nuedialog = false;
  462. this.loadingView = false;
  463. })
  464. .catch(() => {
  465. this.loadingView = false;
  466. });
  467. break;
  468. case "编辑":
  469. editUser({ ...this.ruleForm })
  470. .then((res) => {
  471. this.$message({
  472. type: "success",
  473. message: res.msg,
  474. });
  475. this.getTableList();
  476. this.nuedialog = false;
  477. this.loadingView = false;
  478. })
  479. .catch(() => {
  480. this.loadingView = false;
  481. });
  482. break;
  483. }
  484. // this.nuedialog = false;
  485. } else {
  486. return false;
  487. }
  488. });
  489. },
  490. //点击取消
  491. cancel(formName) {
  492. this.$refs[formName] && this.$refs[formName].resetFields();
  493. this.ruleForm = {
  494. phone: null,
  495. pwd: null,
  496. roleId: null,
  497. userName: null,
  498. userId: null,
  499. loginName: null,
  500. };
  501. this.nuedialog = false;
  502. this.loadingView = false;
  503. },
  504. //异常详情
  505. particulars(row) {
  506. this.unusualdialog = true;
  507. },
  508. // 编辑 回显数据
  509. async compile(row) {
  510. try {
  511. const result = await getUserInfoByUserId({ userId: row.userId });
  512. const userInfo = result.data;
  513. // 直接替换整个对象,以确保 Vue 的响应式系统能够检测到变化
  514. this.ruleForm = {
  515. phone: userInfo.userPhone,
  516. pwd: undefined, // 如果需要重置密码字段
  517. roleId: userInfo.roleId.toString(),
  518. userName: userInfo.userName,
  519. userId: userInfo.userId, // 如果需要用户ID
  520. loginName: userInfo.loginName,
  521. };
  522. this.nuedialog = true;
  523. this.title = "编辑";
  524. } catch (error) {
  525. this.$message.error("获取用户信息失败");
  526. }
  527. },
  528. // 删除
  529. deleted(row) {
  530. this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
  531. confirmButtonText: "确定",
  532. cancelButtonText: "取消",
  533. type: "warning",
  534. })
  535. .then(() => {
  536. deleteUserInfo({ userId: row.userId })
  537. .then((res) => {
  538. this.$message({
  539. type: "success",
  540. message: "删除成功!",
  541. });
  542. this.getTableList();
  543. })
  544. .catch(() => {});
  545. })
  546. .catch(() => {
  547. this.$message({
  548. type: "info",
  549. message: "已取消删除",
  550. });
  551. });
  552. },
  553. editPwd(row) {
  554. this.editUserPassword.userId = row.userId;
  555. this.unusualdialog = true;
  556. },
  557. // 新增
  558. newnuedialog() {
  559. this.cancel("addUserForm");
  560. this.ruleForm = {
  561. loginName: "",
  562. pwd: "",
  563. userName: "",
  564. };
  565. this.nuedialog = true;
  566. this.title = "新增";
  567. },
  568. },
  569. };
  570. </script>
  571. <style lang="scss" scoped>
  572. .general {
  573. display: flex;
  574. flex-wrap: wrap;
  575. .condition {
  576. width: 50%;
  577. display: flex;
  578. p {
  579. width: 100px;
  580. text-align: right;
  581. line-height: 40px;
  582. }
  583. span {
  584. line-height: 40px;
  585. padding-left: 20px;
  586. }
  587. .el-select {
  588. width: 100%;
  589. margin-bottom: 20px;
  590. }
  591. .el-input {
  592. margin-bottom: 20px;
  593. }
  594. }
  595. }
  596. .attachment {
  597. display: flex;
  598. padding-top: 10px;
  599. p {
  600. margin-right: 20px;
  601. color: #409eff;
  602. }
  603. }
  604. .addition {
  605. display: flex;
  606. justify-content: flex-end;
  607. margin-bottom: 10px;
  608. }
  609. .demo-ruleForm {
  610. .el-form-item {
  611. margin-bottom: 25px;
  612. }
  613. }
  614. </style>