index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <section>
  3. <!--工具条-->
  4. <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
  5. <el-form :inline="true">
  6. <el-form-item style="float:right;">
  7. <el-link type="primary" :underline="false" @click="openInsertDialog">添加人员</el-link>
  8. </el-form-item>
  9. <el-form-item style="float:right;">
  10. <el-upload
  11. ref="upload"
  12. action="#"
  13. :limit="1"
  14. :http-request="importUser"
  15. :show-file-list="false"
  16. >
  17. <el-link type="primary" :underline="false">批量导入</el-link>
  18. </el-upload>
  19. </el-form-item>
  20. </el-form>
  21. </el-col>
  22. <!--列表-->
  23. <el-table
  24. :data="list"
  25. highlight-current-row
  26. v-loading="listLoading"
  27. :height="tableHeight"
  28. style="width: 100%;"
  29. >
  30. <el-table-column type="index" width="60"></el-table-column>
  31. <el-table-column prop="name" label="姓名" sortable></el-table-column>
  32. <el-table-column prop="phone" label="手机"></el-table-column>
  33. <el-table-column label="角色">
  34. <template slot-scope="scope">
  35. {{scope.row.role == 0 ? "普通员工" :
  36. scope.row.role == 1 ? "负责人" : "管理员"}}
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="操作">
  40. <template slot-scope="scope">
  41. <el-button
  42. size="small"
  43. v-if="scope.row.role == 0 && user.role == 1"
  44. @click="switchRole(scope.$index)"
  45. >切换为管理员</el-button>
  46. <el-button
  47. size="small"
  48. v-if="scope.row.role == 2 && user.role == 1"
  49. @click="switchRole(scope.$index)"
  50. >切换为员工</el-button>
  51. <el-button size="small" type="danger" @click="deleteUser(scope.$index)">删除</el-button>
  52. </template>
  53. </el-table-column>
  54. </el-table>
  55. <!--工具条-->
  56. <!-- <el-col :span="24" class="toolbar">
  57. <el-pagination
  58. @size-change="handleSizeChange"
  59. @current-change="handleCurrentChange"
  60. :page-sizes="[20 , 50 , 80 , 100]"
  61. :page-size="20"
  62. layout="total, sizes, prev, pager, next"
  63. :total="total"
  64. style="float:right;"
  65. ></el-pagination>
  66. </el-col>-->
  67. <!-- 新增单个人员的Dialog -->
  68. <el-dialog title="新增人员" :visible.sync="dialogVisible" width="400px">
  69. <el-form ref="form1" :model="insertForm" :rules="rules" label-width="60px">
  70. <el-form-item label="名字" prop="name">
  71. <el-input v-model="insertForm.name" placeholder="请输入姓名" clearable></el-input>
  72. </el-form-item>
  73. <el-form-item label="电话" prop="phone">
  74. <el-input v-model="insertForm.phone" placeholder="请输入电话" clearable></el-input>
  75. </el-form-item>
  76. <el-form-item label="角色" prop="role">
  77. <el-select v-model="insertForm.role" placeholder="请选择角色" style="width: 100%">
  78. <el-option label="普通员工" :value="0"></el-option>
  79. <el-option label="管理员" :value="2"></el-option>
  80. </el-select>
  81. </el-form-item>
  82. </el-form>
  83. <span slot="footer" class="dialog-footer">
  84. <el-button @click="dialogVisible=false">取消</el-button>
  85. <el-button type="primary" @click="submitInsert">提交</el-button>
  86. </span>
  87. </el-dialog>
  88. </section>
  89. </template>
  90. <script>
  91. export default {
  92. data() {
  93. return {
  94. user: JSON.parse(sessionStorage.getItem("user")),
  95. tableHeight: 0,
  96. listLoading: false,
  97. total: 0,
  98. page: 1,
  99. size: 20,
  100. list: [],
  101. dialogVisible: false,
  102. insertForm: {
  103. name: null,
  104. phone: null,
  105. role: null
  106. },
  107. rules: {
  108. name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  109. phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
  110. role: [{ required: true, message: "请选择角色", trigger: "blur" }]
  111. }
  112. };
  113. },
  114. methods: {
  115. //分页
  116. handleCurrentChange(val) {
  117. this.page = val;
  118. this.getUser();
  119. },
  120. handleSizeChange(val) {
  121. this.size = val;
  122. this.getUser();
  123. },
  124. //获取所有员工的列表
  125. getUser() {
  126. this.listLoading = true;
  127. this.http.post(
  128. this.port.manage.list,
  129. {},
  130. res => {
  131. this.listLoading = false;
  132. if (res.code == "ok") {
  133. this.list = res.data;
  134. } else {
  135. this.$message({
  136. message: res.msg,
  137. type: "error"
  138. });
  139. }
  140. },
  141. error => {
  142. this.listLoading = false;
  143. this.$message({
  144. message: error,
  145. type: "error"
  146. });
  147. }
  148. );
  149. },
  150. //新增员工
  151. submitInsert() {
  152. this.$refs.form1.validate(valid => {
  153. if (valid) {
  154. this.listLoading = true;
  155. this.http.post(
  156. this.port.manage.insert,
  157. {
  158. name: this.insertForm.name,
  159. phone: this.insertForm.phone,
  160. role: this.insertForm.role
  161. },
  162. res => {
  163. this.listLoading = false;
  164. if (res.code == "ok") {
  165. this.dialogVisible = false;
  166. //重新读取列表
  167. this.getUser();
  168. } else {
  169. this.$message({
  170. message: res.msg,
  171. type: "error"
  172. });
  173. }
  174. },
  175. error => {
  176. this.listLoading = false;
  177. this.$message({
  178. message: error,
  179. type: "error"
  180. });
  181. }
  182. );
  183. }
  184. });
  185. },
  186. //获取所有员工的列表
  187. importUser(item) {
  188. //首先判断文件类型
  189. let str = item.file.name.split(".");
  190. let format = str[str.length - 1];
  191. if (format != "xls" && format != "xlsx") {
  192. this.$message({
  193. message: "请选择.xls或.xlsx文件",
  194. type: "error"
  195. });
  196. } else {
  197. this.listLoading = true;
  198. let formData = new FormData();
  199. formData.append("file", item.file);
  200. this.http.uploadFile(
  201. this.port.manage.import,
  202. formData,
  203. res => {
  204. this.$refs.upload.clearFiles();
  205. this.listLoading = false;
  206. if (res.code == "ok") {
  207. //重新读取列表
  208. this.getUser();
  209. } else {
  210. this.$message({
  211. message: res.msg,
  212. type: "error"
  213. });
  214. }
  215. },
  216. error => {
  217. this.$refs.upload.clearFiles();
  218. this.listLoading = false;
  219. this.$message({
  220. message: error,
  221. type: "error"
  222. });
  223. }
  224. );
  225. }
  226. },
  227. //切换角色 0/2
  228. switchRole(index) {
  229. this.listLoading = true;
  230. this.http.post(
  231. this.port.manage.permission,
  232. { id: this.list[index].id },
  233. res => {
  234. this.listLoading = false;
  235. if (res.code == "ok") {
  236. this.$message({
  237. message: "切换角色成功",
  238. type: "success"
  239. });
  240. //重新读取列表
  241. this.getUser();
  242. } else {
  243. this.$message({
  244. message: res.msg,
  245. type: "error"
  246. });
  247. }
  248. },
  249. error => {
  250. this.listLoading = false;
  251. this.$message({
  252. message: error,
  253. type: "error"
  254. });
  255. }
  256. );
  257. },
  258. //三天之内删了你 数据库都给你清了
  259. deleteUser(index) {
  260. this.$confirm(
  261. "确定要删除用户" + this.list[index].name + "吗?",
  262. "删除用户",
  263. {
  264. confirmButtonText: "确定",
  265. cancelButtonText: "取消",
  266. type: "warning"
  267. }
  268. )
  269. .then(() => {
  270. this.listLoading = true;
  271. this.http.post(
  272. this.port.manage.delete,
  273. { userId: this.list[index].id },
  274. res => {
  275. this.listLoading = false;
  276. if (res.code == "ok") {
  277. this.$message({
  278. message: "删除成功",
  279. type: "success"
  280. });
  281. //重新读取列表
  282. this.getUser();
  283. } else {
  284. this.$message({
  285. message: res.msg,
  286. type: "error"
  287. });
  288. }
  289. },
  290. error => {
  291. this.listLoading = false;
  292. this.$message({
  293. message: error,
  294. type: "error"
  295. });
  296. }
  297. );
  298. })
  299. .catch(() => {});
  300. },
  301. //打开单独新增的Dialog
  302. openInsertDialog() {
  303. this.insertForm.name = null;
  304. this.insertForm.phone = null;
  305. this.insertForm.role = null;
  306. this.dialogVisible = true;
  307. }
  308. },
  309. created() {
  310. let height = window.innerHeight;
  311. this.tableHeight = height - 195;
  312. const that = this;
  313. window.onresize = function temp() {
  314. that.tableHeight = window.innerHeight - 195;
  315. };
  316. },
  317. mounted() {
  318. this.getUser();
  319. }
  320. };
  321. </script>
  322. <style lang="scss" scoped>
  323. .nowTime {
  324. height: 35px;
  325. line-height: 28px;
  326. font-size: 18px;
  327. color: #333;
  328. margin-left: 10px;
  329. i {
  330. margin-right: 10px;
  331. }
  332. }
  333. </style>