admin.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <div class="app-container">
  3. <!-- 查询和其他操作 -->
  4. <div class="filter-container">
  5. <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;" placeholder="请输入管理员名称"/>
  6. <el-button v-permission="['GET /admin/admin/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
  7. <el-button v-permission="['POST /admin/admin/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
  8. <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
  9. </div>
  10. <!-- 查询结果 -->
  11. <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
  12. <el-table-column align="center" label="管理员ID" prop="id" sortable/>
  13. <el-table-column align="center" label="管理员名称" prop="username"/>
  14. <el-table-column align="center" label="管理员头像" prop="avatar">
  15. <template slot-scope="scope">
  16. <img v-if="scope.row.avatar" :src="scope.row.avatar" width="40">
  17. </template>
  18. </el-table-column>
  19. <el-table-column align="center" label="管理员角色" prop="roleIds">
  20. <template slot-scope="scope">
  21. <el-tag v-for="roleId in scope.row.roleIds" :key="roleId" type="primary" style="margin-right: 20px;"> {{ formatRole(roleId) }} </el-tag>
  22. </template>
  23. </el-table-column>
  24. <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
  25. <template slot-scope="scope">
  26. <el-button v-permission="['POST /admin/admin/update']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
  27. <el-button v-permission="['POST /admin/admin/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
  28. </template>
  29. </el-table-column>
  30. </el-table>
  31. <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  32. <!-- 添加或修改对话框 -->
  33. <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
  34. <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
  35. <el-form-item label="管理员名称" prop="username">
  36. <el-input v-model="dataForm.username"/>
  37. </el-form-item>
  38. <el-form-item label="管理员密码" prop="password">
  39. <el-input v-model="dataForm.password" type="password" auto-complete="off"/>
  40. </el-form-item>
  41. <el-form-item label="管理员头像" prop="avatar">
  42. <el-upload
  43. :headers="headers"
  44. :action="uploadPath"
  45. :show-file-list="false"
  46. :on-success="uploadAvatar"
  47. class="avatar-uploader"
  48. accept=".jpg,.jpeg,.png,.gif">
  49. <img v-if="dataForm.avatar" :src="dataForm.avatar" class="avatar">
  50. <i v-else class="el-icon-plus avatar-uploader-icon"/>
  51. </el-upload>
  52. </el-form-item>
  53. <el-form-item label="管理员角色" prop="roleIds">
  54. <el-select v-model="dataForm.roleIds" multiple placeholder="请选择">
  55. <el-option
  56. v-for="item in roleOptions"
  57. :key="item.value"
  58. :label="item.label"
  59. :value="item.value"/>
  60. </el-select>
  61. </el-form-item>
  62. </el-form>
  63. <div slot="footer" class="dialog-footer">
  64. <el-button @click="dialogFormVisible = false">取消</el-button>
  65. <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
  66. <el-button v-else type="primary" @click="updateData">确定</el-button>
  67. </div>
  68. </el-dialog>
  69. </div>
  70. </template>
  71. <style>
  72. .avatar-uploader .el-upload {
  73. border: 1px dashed #d9d9d9;
  74. border-radius: 6px;
  75. cursor: pointer;
  76. position: relative;
  77. overflow: hidden;
  78. }
  79. .avatar-uploader .el-upload:hover {
  80. border-color: #20a0ff;
  81. }
  82. .avatar-uploader-icon {
  83. font-size: 28px;
  84. color: #8c939d;
  85. width: 120px;
  86. height: 120px;
  87. line-height: 120px;
  88. text-align: center;
  89. }
  90. .avatar {
  91. width: 145px;
  92. height: 145px;
  93. display: block;
  94. }
  95. </style>
  96. <script>
  97. import { listAdmin, createAdmin, updateAdmin, deleteAdmin } from '@/api/admin'
  98. import { roleOptions } from '@/api/role'
  99. import { uploadPath } from '@/api/storage'
  100. import { getToken } from '@/utils/auth'
  101. import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  102. export default {
  103. name: 'Admin',
  104. components: { Pagination },
  105. data() {
  106. return {
  107. uploadPath,
  108. list: null,
  109. total: 0,
  110. roleOptions: null,
  111. listLoading: true,
  112. listQuery: {
  113. page: 1,
  114. limit: 20,
  115. username: undefined,
  116. sort: 'add_time',
  117. order: 'desc'
  118. },
  119. dataForm: {
  120. id: undefined,
  121. username: undefined,
  122. password: undefined,
  123. avatar: undefined,
  124. roleIds: []
  125. },
  126. dialogFormVisible: false,
  127. dialogStatus: '',
  128. textMap: {
  129. update: '编辑',
  130. create: '创建'
  131. },
  132. rules: {
  133. username: [
  134. { required: true, message: '管理员名称不能为空', trigger: 'blur' }
  135. ],
  136. password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
  137. },
  138. downloadLoading: false
  139. }
  140. },
  141. computed: {
  142. headers() {
  143. return {
  144. 'X-Litemall-Admin-Token': getToken()
  145. }
  146. }
  147. },
  148. created() {
  149. this.getList()
  150. roleOptions()
  151. .then(response => {
  152. this.roleOptions = response.data.data.list
  153. })
  154. },
  155. methods: {
  156. formatRole(roleId) {
  157. for (let i = 0; i < this.roleOptions.length; i++) {
  158. if (roleId === this.roleOptions[i].value) {
  159. return this.roleOptions[i].label
  160. }
  161. }
  162. return ''
  163. },
  164. getList() {
  165. this.listLoading = true
  166. listAdmin(this.listQuery)
  167. .then(response => {
  168. this.list = response.data.data.list
  169. this.total = response.data.data.total
  170. this.listLoading = false
  171. })
  172. .catch(() => {
  173. this.list = []
  174. this.total = 0
  175. this.listLoading = false
  176. })
  177. },
  178. handleFilter() {
  179. this.listQuery.page = 1
  180. this.getList()
  181. },
  182. resetForm() {
  183. this.dataForm = {
  184. id: undefined,
  185. username: undefined,
  186. password: undefined,
  187. avatar: undefined,
  188. roleIds: []
  189. }
  190. },
  191. uploadAvatar: function(response) {
  192. this.dataForm.avatar = response.data.url
  193. },
  194. handleCreate() {
  195. this.resetForm()
  196. this.dialogStatus = 'create'
  197. this.dialogFormVisible = true
  198. this.$nextTick(() => {
  199. this.$refs['dataForm'].clearValidate()
  200. })
  201. },
  202. createData() {
  203. this.$refs['dataForm'].validate(valid => {
  204. if (valid) {
  205. createAdmin(this.dataForm)
  206. .then(response => {
  207. this.list.unshift(response.data.data)
  208. this.dialogFormVisible = false
  209. this.$notify.success({
  210. title: '成功',
  211. message: '添加管理员成功'
  212. })
  213. })
  214. .catch(response => {
  215. this.$notify.error({
  216. title: '失败',
  217. message: response.data.errmsg
  218. })
  219. })
  220. }
  221. })
  222. },
  223. handleUpdate(row) {
  224. this.dataForm = Object.assign({}, row)
  225. this.dialogStatus = 'update'
  226. this.dialogFormVisible = true
  227. this.$nextTick(() => {
  228. this.$refs['dataForm'].clearValidate()
  229. })
  230. },
  231. updateData() {
  232. this.$refs['dataForm'].validate(valid => {
  233. if (valid) {
  234. updateAdmin(this.dataForm)
  235. .then(() => {
  236. for (const v of this.list) {
  237. if (v.id === this.dataForm.id) {
  238. const index = this.list.indexOf(v)
  239. this.list.splice(index, 1, this.dataForm)
  240. break
  241. }
  242. }
  243. this.dialogFormVisible = false
  244. this.$notify.success({
  245. title: '成功',
  246. message: '更新管理员成功'
  247. })
  248. })
  249. .catch(response => {
  250. this.$notify.error({
  251. title: '失败',
  252. message: response.data.errmsg
  253. })
  254. })
  255. }
  256. })
  257. },
  258. handleDelete(row) {
  259. deleteAdmin(row)
  260. .then(response => {
  261. this.$notify.success({
  262. title: '成功',
  263. message: '删除管理员成功'
  264. })
  265. const index = this.list.indexOf(row)
  266. this.list.splice(index, 1)
  267. })
  268. .catch(response => {
  269. this.$notify.error({
  270. title: '失败',
  271. message: response.data.errmsg
  272. })
  273. })
  274. },
  275. handleDownload() {
  276. this.downloadLoading = true
  277. import('@/vendor/Export2Excel').then(excel => {
  278. const tHeader = ['管理员ID', '管理员名称', '管理员头像']
  279. const filterVal = ['id', 'username', 'avatar']
  280. excel.export_json_to_excel2(
  281. tHeader,
  282. this.list,
  283. filterVal,
  284. '管理员信息'
  285. )
  286. this.downloadLoading = false
  287. })
  288. }
  289. }
  290. }
  291. </script>