index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <!-- -->
  2. <template>
  3. <div>
  4. <main-top :crumb="crumbData"></main-top>
  5. <div class="table-interface">
  6. <div class="top-body">
  7. <div class="info-top">
  8. <div class="info-left">
  9. <span>按用户状态查看:</span>
  10. <el-select
  11. style="width: 90px"
  12. v-model="region"
  13. placeholder="请选择"
  14. >
  15. <el-option label="全部" value=""></el-option>
  16. <el-option label="启用" :value="0"></el-option>
  17. <el-option label="停用" :value="1"></el-option>
  18. </el-select>
  19. <el-input
  20. style="width: 220px; margin: 0 20px"
  21. v-model="inputKey"
  22. placeholder="请输入关键字搜索"
  23. ></el-input>
  24. <el-button type="primary" @click="getInformation">查找</el-button>
  25. <el-button @click="inputKey = ''">重置</el-button>
  26. </div>
  27. <div class="info-right">
  28. <el-button type="primary" @click="show('', 'add')"
  29. >新增用户</el-button
  30. >
  31. </div>
  32. </div>
  33. <el-table :data="tableData" style="width: 100%">
  34. <el-table-column
  35. v-for="(item, idx) in data"
  36. :key="idx"
  37. :prop="item.prop"
  38. :label="item.label"
  39. >
  40. <template slot-scope="scope">
  41. <img
  42. style="width: 80px; height: 80px; border-radius: 50%"
  43. v-if="item.prop === 'head'"
  44. :src="scope.row[item.prop]"
  45. alt
  46. />
  47. <span v-else>{{ scope.row[item.prop] }}</span>
  48. </template>
  49. </el-table-column>
  50. <el-table-column label="操作">
  51. <template
  52. slot-scope="scope"
  53. v-if="scope.row.roleName !== '超级管理员'"
  54. >
  55. <span class="o-span" @click="show(scope.row, 'edit')">修改</span>
  56. <span class="o-span" @click="changeState(scope.row)">{{
  57. scope.row.qiyong === "启用" ? "停用" : "启用"
  58. }}</span>
  59. <span class="o-span" @click="del(scope.row)">注销</span>
  60. </template>
  61. </el-table-column>
  62. </el-table>
  63. <div class="e-pagination">
  64. <el-pagination
  65. @current-change="handleCurrentChange"
  66. :current-page.sync="currentPage"
  67. :page-size="size"
  68. layout="prev, pager, next, jumper"
  69. :total="total"
  70. >
  71. </el-pagination>
  72. </div>
  73. </div>
  74. </div>
  75. <el-dialog :title="editTitle" :visible.sync="dialogFormVisible" width="40%">
  76. <div class="add-con">
  77. <div class="add-left">
  78. <el-form :model="form" :rules="rules" ref="form">
  79. <el-form-item
  80. required
  81. prop="userName"
  82. label="用户账号:"
  83. :label-width="formLabelWidth"
  84. >
  85. <el-input
  86. :disabled="type === 'edit'"
  87. v-model="form.userName"
  88. autocomplete="off"
  89. ></el-input>
  90. </el-form-item>
  91. <el-form-item
  92. required
  93. label="用户角色:"
  94. :label-width="formLabelWidth"
  95. >
  96. <el-select v-model="form.roleId" placeholder="请选择角色">
  97. <el-option
  98. v-for="item in roleList"
  99. :label="item.roleName"
  100. :key="item.id"
  101. :value="item.id"
  102. ></el-option>
  103. </el-select>
  104. </el-form-item>
  105. <el-form-item
  106. required
  107. label="手机号码:"
  108. :label-width="formLabelWidth"
  109. prop="phone"
  110. >
  111. <el-input v-model="form.phone" autocomplete="off"></el-input>
  112. </el-form-item>
  113. <el-form-item label="是否启用:" :label-width="formLabelWidth">
  114. <el-radio-group v-model="form.state">
  115. <el-radio :label="0">是</el-radio>
  116. <el-radio :label="1">否</el-radio>
  117. </el-radio-group>
  118. </el-form-item>
  119. <div
  120. class="passTxt"
  121. v-show="type === 'add'"
  122. style="color: #f56c6c; padding: 0 0 0 40px"
  123. >
  124. 初始密码:123456
  125. </div>
  126. </el-form>
  127. </div>
  128. <!-- <div class="add-right">
  129. <el-upload
  130. class="avatar-uploader"
  131. :action='uploadUrl'
  132. :headers="{
  133. token,
  134. }"
  135. :show-file-list="false"
  136. :on-success="upload_success"
  137. >
  138. <img v-if="form.head" :src="form.head" class="avatar">
  139. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  140. </el-upload>
  141. <div class="avatar-img">用户头像</div>
  142. <div class="avatar-img">
  143. <el-button v-if="type==='edit'" @click="reset">重置密码</el-button>
  144. </div>
  145. </div> -->
  146. </div>
  147. <div slot="footer" class="dialog-footer">
  148. <el-button type="primary" @click="save">保 存</el-button>
  149. <el-button @click="dialogFormVisible = false">取 消</el-button>
  150. </div>
  151. </el-dialog>
  152. </div>
  153. </template>
  154. <script>
  155. // 这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
  156. // 例如:import 《组件名称》 from '《组件路径》';
  157. import MainTop from '@/components/main-top'
  158. const crumbData = [
  159. {
  160. name: '用户管理',
  161. id: 6
  162. }
  163. ]
  164. let juese = {
  165. 0: '高级管理员',
  166. 1: '普通管理员'
  167. }
  168. let zt = {
  169. 0: '启用',
  170. 1: '停用'
  171. }
  172. let urlType = {
  173. add: '/user/addUser',
  174. edit: '/user/updateUser'
  175. }
  176. export default {
  177. // import引入的组件需要注入到对象中才能使用
  178. components: {
  179. MainTop
  180. },
  181. data () {
  182. // 这里存放数据
  183. let data = [
  184. {
  185. prop: 'idx',
  186. label: '序号'
  187. },
  188. {
  189. prop: 'userName',
  190. label: '账号'
  191. },
  192. {
  193. prop: 'roleName',
  194. label: '角色'
  195. },
  196. {
  197. prop: 'phone',
  198. label: '手机号'
  199. },
  200. {
  201. prop: 'qiyong',
  202. label: '状态'
  203. }
  204. ]
  205. return {
  206. crumbData,
  207. data,
  208. region: '',
  209. infoName: '',
  210. tableData: [],
  211. dialogTableVisible: false,
  212. dialogFormVisible: false,
  213. form: {
  214. userName: '',
  215. roleId: 16,
  216. phone: '',
  217. head: '',
  218. password: '',
  219. repeatPassword: '',
  220. state: 0
  221. },
  222. total: 0,
  223. inputKey: '',
  224. currentPage: 1,
  225. size: 10,
  226. formLabelWidth: '120px',
  227. imageUrl: '',
  228. type: 'add',
  229. editTitle: '新增用户',
  230. uploadUrl: `${this.$serverName}/user/uploadHead`,
  231. token: window.localStorage.getItem('token'),
  232. roleList: [],
  233. rules: {
  234. userName: [
  235. { required: true, message: '用户账号不能为空' },
  236. { max: 20, message: '用户账号不能大于20个字符' }
  237. ],
  238. phone: [
  239. { required: true, message: '手机号不能为空' },
  240. { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号' }
  241. ]
  242. }
  243. }
  244. },
  245. watch: {
  246. currentPage () {
  247. this.refresh()
  248. },
  249. size () {
  250. this.refresh()
  251. },
  252. region () {
  253. this.refresh()
  254. },
  255. inputKey () {
  256. this.currentPage = 1
  257. this.refresh()
  258. },
  259. type (newVal) {
  260. this.editTitle = newVal === 'add' ? '新增用户' : '修改用户信息'
  261. }
  262. },
  263. mounted () {
  264. this.refresh()
  265. this.getRole()
  266. },
  267. methods: {
  268. reset () {
  269. let data = {
  270. id: this.form.id
  271. }
  272. this.$confirm('此操作将重置该用户密码, 是否继续?', '提示', {
  273. confirmButtonText: '确定',
  274. cancelButtonText: '取消',
  275. type: 'warning'
  276. })
  277. .then(() => {
  278. this.$http.post('/user/resetPassword', data).then((res) => {
  279. if (res.code === 0) {
  280. this.$alert('重置成功', '提示', {
  281. confirmButtonText: '确定',
  282. callback: (action) => {}
  283. })
  284. } else {
  285. this.$notify.error({
  286. title: '错误',
  287. message: res.msg,
  288. duration: 2000
  289. })
  290. }
  291. })
  292. })
  293. .catch(() => {
  294. this.$message({
  295. type: 'info',
  296. message: '已取消'
  297. })
  298. })
  299. },
  300. goto (item) {
  301. window.localStorage.setItem('editInfo', JSON.stringify(item))
  302. this.$router.push({ name: 'edit-information', params: { type: 1 } })
  303. this.$bus.$emit('editinfo', item)
  304. },
  305. upload_success (data) {
  306. this.form.head = data.data
  307. },
  308. refresh () {
  309. this.loading = true
  310. this.getInformation()
  311. this.loading = false
  312. },
  313. handleCurrentChange (val) {
  314. this.currentPage = val
  315. },
  316. del (item) {
  317. let data = {
  318. id: item.id
  319. }
  320. this.$confirm('此操作将注销并删除该用户, 是否继续?', '提示', {
  321. confirmButtonText: '确定',
  322. cancelButtonText: '取消',
  323. type: 'warning'
  324. })
  325. .then(() => {
  326. this.$http.post('/user/deleteUser', data).then((res) => {
  327. if (res.code === 0) {
  328. this.$alert('注销成功', '提示', {
  329. confirmButtonText: '确定',
  330. callback: (action) => {
  331. this.refresh()
  332. }
  333. })
  334. } else {
  335. this.$notify.error({
  336. title: '错误',
  337. message: res.msg,
  338. duration: 2000
  339. })
  340. }
  341. })
  342. })
  343. .catch(() => {
  344. this.$message({
  345. type: 'info',
  346. message: '已取消'
  347. })
  348. })
  349. },
  350. save () {
  351. this.$refs['form'].validate().then((res) => {
  352. let { userName, roleId, phone, head, state } = this.form
  353. let data = { userName, roleId, phone, head, state }
  354. this.$http
  355. .post(urlType[this.type], data)
  356. .then((res) => {
  357. if (res.code === 0) {
  358. this.$alert('操作成功', '提示', {
  359. confirmButtonText: '确定',
  360. callback: (action) => {
  361. this.dialogFormVisible = false
  362. this.refresh()
  363. }
  364. })
  365. } else {
  366. this.$notify.error({
  367. title: '错误',
  368. message: res.message,
  369. duration: 2000
  370. })
  371. }
  372. })
  373. .catch(() => {
  374. this.$notify.error({
  375. title: '错误',
  376. message: '添加失败',
  377. duration: 2000
  378. })
  379. })
  380. })
  381. },
  382. show (item = '', type) {
  383. this.dialogFormVisible = true
  384. this.type = type
  385. this.form = {
  386. userName: '',
  387. roleId: 16,
  388. phone: '',
  389. head: '',
  390. password: '',
  391. repeatPassword: '',
  392. state: 0
  393. }
  394. if (type === 'edit') {
  395. this.form = item
  396. }
  397. },
  398. changeState (item) {
  399. let state = item.state === 1 ? 0 : 1
  400. let data = {
  401. id: item.id,
  402. state
  403. }
  404. this.$confirm('此操作将导致用户状态发生改变, 是否继续?', '提示', {
  405. confirmButtonText: '确定',
  406. cancelButtonText: '取消',
  407. type: 'warning'
  408. })
  409. .then(() => {
  410. this.$http.post('/user/updateState', data).then((res) => {
  411. if (res.code === 0) {
  412. this.$alert('修改成功', '提示', {
  413. confirmButtonText: '确定',
  414. callback: (action) => {
  415. this.refresh()
  416. }
  417. })
  418. } else {
  419. this.$notify.error({
  420. title: '错误',
  421. message: res.msg,
  422. duration: 2000
  423. })
  424. }
  425. })
  426. })
  427. .catch(() => {
  428. this.$message({
  429. type: 'info',
  430. message: '已取消'
  431. })
  432. })
  433. },
  434. async getInformation () {
  435. let params = {
  436. state: this.region,
  437. searchKey: this.inputKey,
  438. pageNum: this.currentPage,
  439. pageSize: this.size
  440. }
  441. let result = await this.$http({
  442. method: 'post',
  443. data: params,
  444. url: '/user/list'
  445. })
  446. if (result.code !== 0) {
  447. return
  448. }
  449. this.tableData = result.data.list
  450. this.total = result.data.total
  451. this.tableData.forEach((item, i) => {
  452. item['date'] =
  453. this.$base.timestampToTime(item['startTime']) +
  454. '至' +
  455. this.$base.timestampToTime(item['endTime'])
  456. item['role'] = juese[item['roleId']]
  457. item['qiyong'] = zt[item['state']]
  458. item['idx'] = i + 1 + (this.currentPage - 1) * this.size
  459. })
  460. },
  461. // 获取角色列表
  462. async getRole () {
  463. let res = await this.$http({
  464. method: 'post',
  465. data: {
  466. state: 1
  467. },
  468. url: '/user/roleList'
  469. })
  470. this.roleList = res.data.filter((item) => item.roleName !== '超级管理员')
  471. },
  472. handleSearchBtnClick () {
  473. this.params.pageNum = 1
  474. // this.getLogs()
  475. }
  476. }
  477. }
  478. </script>
  479. <style scoped>
  480. /* 引入公共css类 */
  481. @import url(./style);
  482. </style>
  483. <style>
  484. .avatar-uploader .el-upload {
  485. border: 1px dashed #d9d9d9;
  486. border-radius: 6px;
  487. cursor: pointer;
  488. position: relative;
  489. overflow: hidden;
  490. }
  491. .avatar-uploader .el-upload:hover {
  492. border-color: #409eff;
  493. }
  494. .avatar-uploader-icon {
  495. font-size: 28px;
  496. color: #8c939d;
  497. width: 178px;
  498. height: 178px;
  499. line-height: 178px;
  500. text-align: center;
  501. }
  502. .avatar {
  503. width: 178px;
  504. height: 178px;
  505. display: block;
  506. }
  507. </style>