adminAction.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package com.happy.action;
  2. import com.google.gson.Gson;
  3. import com.google.gson.reflect.TypeToken;
  4. import com.happy.Model.Admin;
  5. import com.happy.Model.AdminPower;
  6. import com.happy.Until.GetHttpParam;
  7. import com.happy.Until.PwdDefind;
  8. import com.happy.Until.ResUtil;
  9. import com.happy.dto.IPage;
  10. import com.happy.service.AdminService;
  11. import com.opensymphony.xwork2.ActionSupport;
  12. import net.sf.json.JSONObject;
  13. import org.apache.struts2.ServletActionContext;
  14. import org.apache.struts2.interceptor.ServletRequestAware;
  15. import javax.annotation.Resource;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.util.List;
  19. public class adminAction extends ActionSupport implements ServletRequestAware {
  20. private HttpServletRequest request;
  21. public HttpServletResponse response;
  22. @Resource
  23. public AdminService adminService;
  24. public String adminName;
  25. public String password;
  26. public Integer id;
  27. public int page; // 当前页
  28. public int rows;// 每页显示的行数rows
  29. public HttpServletRequest getRequest() {
  30. return request;
  31. }
  32. public void setRequest(HttpServletRequest request) {
  33. this.request = request;
  34. }
  35. public void setServletRequest(HttpServletRequest request) {
  36. this.request = request;
  37. }
  38. public HttpServletResponse getResponse() {
  39. return response;
  40. }
  41. public void setResponse(HttpServletResponse response) {
  42. this.response = response;
  43. }
  44. /**
  45. * 描述:登录
  46. * @return
  47. */
  48. public String loginAdmin() {
  49. JSONObject resultJson = new JSONObject();
  50. StringBuilder getOneSqlx = new StringBuilder("");
  51. getOneSqlx.append(" and admin_name = '").append(adminName).append("'")
  52. .append(" and password = '").append(password).append("'");
  53. Admin login = adminService.getOen(getOneSqlx.toString());
  54. if (login != null) {
  55. login.setPassword("********");
  56. resultJson.put("message", "登录成功");
  57. resultJson.put("code", 200);
  58. resultJson.put("data", login);
  59. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  60. return null;
  61. }
  62. resultJson.put("message", "用户名或密码错误");
  63. resultJson.put("code", 205);
  64. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  65. return null;
  66. }
  67. /**
  68. * 描述:新增管理端管理员账号
  69. * @return
  70. */
  71. public String insertAdmin() {
  72. JSONObject resultJson = new JSONObject();
  73. Gson gson = new Gson();
  74. com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
  75. if (json == null) {
  76. resultJson.put("message", "请传入参数");
  77. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  78. return null;
  79. }
  80. Admin admin = null;
  81. try {
  82. admin = gson.fromJson(json.toString(), new TypeToken<Admin>() {}.getType());
  83. if (admin == null) {
  84. resultJson.put("message", "数据为空");
  85. resultJson.put("code", 500);
  86. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  87. return null;
  88. }
  89. if (admin.getAdminName()==null || admin.getUserName()==null || admin.getLevel()==null || admin.getAdminPower()==null || admin.getAdminPower().equals("") || admin.getPhone()==null){
  90. resultJson.put("message", "各参数不能为空");
  91. resultJson.put("code", 500);
  92. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  93. return null;
  94. }
  95. if (!PwdDefind.vertify(admin.getPassword())){
  96. resultJson.put("message", "密码至少包含:大小写英文字母、数字、特殊符号,密码长度大于8位,小于20位");
  97. resultJson.put("code", 500);
  98. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  99. return null;
  100. }
  101. StringBuilder getOneSqlx = new StringBuilder("");
  102. getOneSqlx.append(" and admin_name = '").append(admin.getAdminName()).append("'");
  103. Admin listc = adminService.getOen(getOneSqlx.toString());
  104. if (listc != null) {
  105. resultJson.put("message", "该用户已存在");
  106. resultJson.put("code", 500);
  107. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  108. return null;
  109. }
  110. int m = adminService.insertAdmin(admin);
  111. if (m > 0) {
  112. resultJson.put("message", "添加成功");
  113. resultJson.put("code", 200);
  114. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  115. return null;
  116. } else {
  117. resultJson.put("message", "添加失败");
  118. resultJson.put("code", 502);
  119. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  120. return null;
  121. }
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. resultJson.put("message", "未知异常");
  126. resultJson.put("code", 205);
  127. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  128. return null;
  129. }
  130. /**
  131. * 描述:修改管理端管理员账号
  132. * @return
  133. */
  134. public String updateAdmin() {
  135. JSONObject resultJson = new JSONObject();
  136. Gson gson = new Gson();
  137. com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
  138. if (json == null) {
  139. resultJson.put("message", "请传入参数");
  140. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  141. return null;
  142. }
  143. Admin admin = null;
  144. try {
  145. admin = gson.fromJson(json.toString(), new TypeToken<Admin>() {}.getType());
  146. if (admin == null) {
  147. resultJson.put("message", "数据为空");
  148. resultJson.put("code", 500);
  149. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  150. return null;
  151. }
  152. if(admin.getId()==null){
  153. resultJson.put("message", "修改Id不能为空");
  154. resultJson.put("code", 500);
  155. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  156. return null;
  157. }
  158. if (admin.getAdminName()==null || admin.getUserName()==null || admin.getLevel()==null || admin.getAdminPower()==null || admin.getAdminPower().equals("") || admin.getPhone()==null){
  159. resultJson.put("message", "各参数不能为空");
  160. resultJson.put("code", 500);
  161. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  162. return null;
  163. }
  164. if (!PwdDefind.vertify(admin.getPassword())){
  165. resultJson.put("message", "密码至少包含:大小写英文字母、数字、特殊符号,密码长度大于8位,小于20位");
  166. resultJson.put("code", 500);
  167. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  168. return null;
  169. }
  170. StringBuilder getOneSqlx = new StringBuilder("");
  171. getOneSqlx.append(" and admin_name = '").append(admin.getAdminName()).append("'")
  172. .append(" and id != '").append(admin.getId()).append("'");
  173. Admin listc = adminService.getOen(getOneSqlx.toString());
  174. if (listc != null) {
  175. resultJson.put("message", "该用户已存在");
  176. resultJson.put("code", 500);
  177. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  178. return null;
  179. }
  180. int m = adminService.updateAdmin(admin);
  181. if (m > 0) {
  182. resultJson.put("message", "修改成功");
  183. resultJson.put("code", 200);
  184. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  185. return null;
  186. } else {
  187. resultJson.put("message", "修改失败");
  188. resultJson.put("code", 502);
  189. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  190. return null;
  191. }
  192. } catch (Exception e) {
  193. e.printStackTrace();
  194. }
  195. resultJson.put("message", "未知异常");
  196. resultJson.put("code", 205);
  197. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  198. return null;
  199. }
  200. /**
  201. * 描述:删除
  202. * @return
  203. */
  204. public String delAdmin(){
  205. JSONObject resultJson = new JSONObject();
  206. if (id == null) {
  207. resultJson.put("message", "请传入id");
  208. resultJson.put("code", 500);
  209. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  210. return null;
  211. }
  212. int m = adminService.delAdmin(id);
  213. if (m > 0) {
  214. resultJson.put("message", "删除成功");
  215. resultJson.put("code", 200);
  216. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  217. return null;
  218. }
  219. resultJson.put("message", "未知异常");
  220. resultJson.put("code", 205);
  221. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  222. return null;
  223. }
  224. /**
  225. * 描述:重置密码
  226. * @return
  227. */
  228. public String removePwd(){
  229. JSONObject resultJson = new JSONObject();
  230. if (id == null) {
  231. resultJson.put("message", "请传入id");
  232. resultJson.put("code", 500);
  233. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  234. return null;
  235. }
  236. Admin admin = adminService.getById(id);
  237. if (admin != null && !"".equals(admin)) {
  238. admin.setPassword("Jams123456");
  239. int m = adminService.updateAdmin(admin);
  240. if(m > 0){
  241. resultJson.put("message", "重置成功");
  242. resultJson.put("code", 200);
  243. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  244. return null;
  245. }else{
  246. resultJson.put("message", "重置失败");
  247. resultJson.put("code", 502);
  248. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  249. return null;
  250. }
  251. }
  252. resultJson.put("message", "未知异常");
  253. resultJson.put("code", 205);
  254. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  255. return null;
  256. }
  257. /**
  258. * 描述:分页查询
  259. * @return
  260. */
  261. public String queryPage(){
  262. JSONObject resultJson = new JSONObject();
  263. StringBuilder s1 = new StringBuilder("");
  264. if (adminName!=null){
  265. s1.append(" and admin_name like '%").append(adminName).append("'");
  266. }
  267. IPage<Admin> adminIPage = adminService.queryPage(s1.toString(),page,rows);
  268. if(adminIPage!=null && !"".equals(adminIPage)){
  269. resultJson.put("message", "查询分页成功");
  270. resultJson.put("code", 200);
  271. resultJson.put("data", adminIPage);
  272. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  273. }else{
  274. resultJson.put("message", "查询分页失败");
  275. resultJson.put("code", 500);
  276. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  277. }
  278. return null;
  279. }
  280. /**
  281. * 描述:根据Id查询详细
  282. * @return
  283. */
  284. public String getById(){
  285. JSONObject resultJson = new JSONObject();
  286. if (id == null) {
  287. resultJson.put("message", "请传入id");
  288. resultJson.put("code", 500);
  289. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  290. return null;
  291. }
  292. Admin admin = adminService.getById(id);
  293. if (admin != null) {
  294. resultJson.put("message", "查询成功");
  295. resultJson.put("code", 200);
  296. resultJson.put("data", admin);
  297. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  298. return null;
  299. }else{
  300. resultJson.put("message", "未查到用户信息");
  301. resultJson.put("code", 500);
  302. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  303. return null;
  304. }
  305. }
  306. /**
  307. * 描述:查询菜单
  308. * @return
  309. */
  310. public String getIndexList(){
  311. JSONObject resultjson = new JSONObject();
  312. StringBuilder s1 = new StringBuilder("");
  313. if (id==null){
  314. resultjson.put("message", "请传入用户Id");
  315. resultjson.put("code", 500);
  316. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  317. return null;
  318. }
  319. Admin admin = adminService.getById(id);
  320. if (admin==null){
  321. resultjson.put("message", "未查到用户信息");
  322. resultjson.put("code", 500);
  323. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  324. return null;
  325. }
  326. String ids = admin.getAdminPower();
  327. s1.append(" and id in (").append(ids).append(") ");
  328. List<AdminPower> lpower = adminService.queryTree(s1.toString());
  329. if (lpower!=null){
  330. resultjson.put("message", "返回成功");
  331. resultjson.put("code", 200);
  332. resultjson.put("data",lpower);
  333. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  334. return null;
  335. }
  336. resultjson.put("message", "数据为空");
  337. resultjson.put("code", 205);
  338. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  339. return null;
  340. }
  341. }