| | |
| | | package com.ks.app.controller.admin; |
| | | |
| | | import com.ks.app.entity.AdminUser; |
| | | import com.google.gson.*; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.ks.app.entity.admin.AdminRole; |
| | | import com.ks.app.entity.admin.AdminUser; |
| | | import com.ks.app.service.inter.AdminUserService; |
| | | import com.ks.app.service.inter.admin.AdminUserRolesService; |
| | | import com.ks.app.vo.AcceptAdminData; |
| | | import com.ks.app.vo.AdminUserVO; |
| | | import net.sf.json.JSONObject; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.validation.BindingResult; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.yeshi.utils.JsonUtil; |
| | | import org.yeshi.utils.StringUtil; |
| | | import org.yeshi.utils.TimeUtil; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpSession; |
| | | import javax.validation.constraints.NotEmpty; |
| | | import java.lang.reflect.Type; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Controller |
| | | @RequestMapping("/admin/api/user") |
| | | @RequestMapping("/admin/api/adminuser") |
| | | public class AdminUserController { |
| | | |
| | | @Resource |
| | | private AdminUserService adminUserService; |
| | | |
| | | @Resource |
| | | private AdminUserRolesService adminUserRolesService; |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("login") |
| | |
| | | } |
| | | |
| | | AdminUser adminUser = adminUserService.selectByAccount(account); |
| | | if (adminUser == null) { |
| | | if (adminUser == null || !adminUser.getAccount().equals(account)) { |
| | | return JsonUtil.loadFalseResult("账号不存在"); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("list") |
| | | public String list(AcceptAdminData acceptAdminData, int page, int limit) { |
| | | List<AdminUser> list = adminUserService.list(page, limit); |
| | | long count = adminUserService.count(); |
| | | Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() { |
| | | @Override |
| | | public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { |
| | | return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm")); |
| | | } |
| | | }).registerTypeAdapter(AdminRole.class, new JsonSerializer<AdminRole>() { |
| | | @Override |
| | | public JsonElement serialize(AdminRole role, Type type, JsonSerializationContext jsonSerializationContext) { |
| | | return role == null ? new JsonPrimitive("") : new JsonPrimitive(role.getName()); |
| | | } |
| | | }).create(); |
| | | |
| | | List<AdminUserVO> voList = new ArrayList<>(); |
| | | //查询角色 |
| | | for (AdminUser adminUser : list) { |
| | | adminUser.setPwd(null); |
| | | List<AdminRole> roleList = adminUserRolesService.listRoleByAdminId(adminUser.getAccount()); |
| | | voList.add(AdminUserVO.create(adminUser, roleList)); |
| | | } |
| | | JSONObject data = new JSONObject(); |
| | | data.put("list", gson.toJson(voList)); |
| | | data.put("count", count); |
| | | return JsonUtil.loadTrueResult(data); |
| | | } |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("getLoginAdminInfo") |
| | | public String getLoginAdminInfo(AcceptAdminData acceptAdminData) { |
| | | |
| | | if (acceptAdminData.getAdminUser() == null) { |
| | | return JsonUtil.loadFalseResult("尚未登录"); |
| | | } |
| | | |
| | | return JsonUtil.loadTrueResult(AdminUserVO.create(acceptAdminData.getAdminUser())); |
| | | } |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("get") |
| | | public String get(AcceptAdminData acceptAdminData, String id) { |
| | | AdminUser adminUser = adminUserService.selectByAccount(id); |
| | | if (adminUser == null) { |
| | | return JsonUtil.loadFalseResult("id不存在"); |
| | | } |
| | | adminUser.setPwd(null); |
| | | List<AdminRole> roleList = adminUserRolesService.listRoleByAdminId(adminUser.getAccount()); |
| | | AdminUserVO vo = AdminUserVO.create(adminUser, roleList); |
| | | Gson gson = new GsonBuilder().registerTypeAdapter(AdminRole.class, new JsonSerializer<AdminRole>() { |
| | | @Override |
| | | public JsonElement serialize(AdminRole role, Type type, JsonSerializationContext jsonSerializationContext) { |
| | | JSONObject json = new JSONObject(); |
| | | json.put("key", role.name()); |
| | | json.put("value", role.getName()); |
| | | return role == null ? new JsonPrimitive("") : new JsonPrimitive(json.toString()); |
| | | } |
| | | }).create(); |
| | | return JsonUtil.loadTrueResult(gson.toJson(vo)); |
| | | } |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("add") |
| | | public String add(@RequestBody AdminUserVO vo) { |
| | | AdminUser adminUser = vo.toEntity(); |
| | | try { |
| | | adminUserService.add(adminUser); |
| | | } catch (Exception e) { |
| | | return JsonUtil.loadFalseResult(e.getMessage()); |
| | | } |
| | | //更新权限 |
| | | adminUserRolesService.setRoles(adminUser.getAccount(), vo.getRoleList()); |
| | | return JsonUtil.loadTrueResult(""); |
| | | } |
| | | |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("update") |
| | | public String update(@RequestBody AdminUserVO vo) { |
| | | AdminUser adminUser = vo.toEntity(); |
| | | adminUserService.update(adminUser); |
| | | //更新权限 |
| | | adminUserRolesService.setRoles(adminUser.getAccount(), vo.getRoleList()); |
| | | return JsonUtil.loadTrueResult(""); |
| | | } |
| | | |
| | | |
| | | @ResponseBody |
| | | @RequestMapping("delete") |
| | | public String delete(String ids) { |
| | | Type type = new TypeToken<List<String>>() { |
| | | }.getType(); |
| | | List<String> idList = new Gson().fromJson(ids, type); |
| | | adminUserService.delete(idList); |
| | | return JsonUtil.loadTrueResult(""); |
| | | } |
| | | |
| | | |
| | | } |