123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- package com.yihu.mm.controller;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.wlyy.entity.IdEntity;
- import org.apache.commons.lang3.StringUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import javax.servlet.http.HttpServletRequest;
- import java.util.*;
- public class BaseController {
- private static Logger logger = LoggerFactory.getLogger(BaseController.class);
- @Autowired
- protected HttpServletRequest request;
- /**
- * 获取排序字段名称
- * @return
- */
- public String getSortName() {
- return request.getParameter("sortname");
- }
- /**
- * 获取排序方式ASC,DESC
- * @return
- */
- public String getSortOrder() {
- return request.getParameter("sortorder");
- }
- /**
- * 獲取髮送請求用戶的uid
- * @return
- */
- public String getUID() {
- try {
- String userAgent = request.getHeader("userAgent");
- if (StringUtils.isEmpty(userAgent)) {
- userAgent = request.getHeader("User-Agent");
- }
- JSONObject json = new JSONObject(userAgent);
- return json.getString("uid");
- } catch (Exception e) {
- return null;
- }
- }
- public String getOpenid() {
- try {
- String userAgent = request.getHeader("userAgent");
- if (StringUtils.isEmpty(userAgent)) {
- userAgent = request.getHeader("User-Agent");
- }
- JSONObject json = new JSONObject(userAgent);
- return json.getString("openid");
- } catch (Exception e) {
- return null;
- }
- }
- /**
- * 获取用户ID
- * @return
- */
- public long getId() {
- try {
- String userAgent = request.getHeader("userAgent");
- if (StringUtils.isEmpty(userAgent)) {
- userAgent = request.getHeader("User-Agent");
- }
- JSONObject json = new JSONObject(userAgent);
- return json.getLong("id");
- } catch (Exception e) {
- return 0;
- }
- }
- public String getIMEI() {
- try {
- String userAgent = request.getHeader("userAgent");
- if (StringUtils.isEmpty(userAgent)) {
- userAgent = request.getHeader("User-Agent");
- }
- JSONObject json = new JSONObject(userAgent);
- return json.getString("imei");
- } catch (Exception e) {
- return null;
- }
- }
- public String getToken() {
- try {
- String userAgent = request.getHeader("userAgent");
- if (StringUtils.isEmpty(userAgent)) {
- userAgent = request.getHeader("User-Agent");
- }
- JSONObject json = new JSONObject(userAgent);
- return json.getString("token");
- } catch (Exception e) {
- return null;
- }
- }
- public void error(Exception e) {
- logger.error(getClass().getName() + ":", e.getMessage());
- e.printStackTrace();
- }
- public void warn(Exception e) {
- logger.warn(getClass().getName() + ":", e.getMessage());
- e.printStackTrace();
- }
- /**
- * 返回接口处理结果
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @return
- */
- public String error(int code, String msg) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return null;
- }
- }
- /**
- * 接口处理成功
- * @param msg
- * @return
- */
- public String success(String msg) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", 200);
- map.put("msg", msg);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return null;
- }
- }
- public String write(int code, String msg) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return null;
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @return
- */
- public String write(int code, String msg, String key, List<?> list) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- map.put(key, list);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @param value 结果数据
- * @return
- */
- public String write(int code, String msg, String key, JSONObject value) {
- try {
- JSONObject json = new JSONObject();
- json.put("status", code);
- json.put("msg", msg);
- json.put(key, value);
- return json.toString();
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @param value 结果数据
- * @return
- */
- public String write(int code, String msg, String key, JSONArray value) {
- try {
- JSONObject json = new JSONObject();
- json.put("status", code);
- json.put("msg", msg);
- json.put(key, value);
- return json.toString();
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @param total 总数
- * @param value 结果数据
- * @return
- */
- public String write(int code, String msg, int total, String key, JSONArray value) {
- try {
- JSONObject json = new JSONObject();
- json.put("status", code);
- json.put("msg", msg);
- json.put("total", total);
- json.put(key, value);
- return json.toString();
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @param value 结果数据
- * @return
- */
- public String write(int code, String msg, String key, Object value) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- map.put(key, value);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @return
- */
- public String write(int code, String msg, String key, Page<?> list) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- // 是否为第一页
- map.put("isFirst", list.isFirst());
- // 是否为最后一页
- map.put("isLast", list.isLast());
- // 总条数
- map.put("total", list.getTotalElements());
- // 总页数
- map.put("totalPages", list.getTotalPages());
- map.put(key, list.getContent());
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @return
- */
- public String write(int code, String msg, String key, Page<?> page, JSONArray array) {
- try {
- JSONObject json = new JSONObject();
- json.put("status", code);
- json.put("msg", msg);
- // 是否为第一页
- json.put("isFirst", page.isFirst());
- // 是否为最后一页
- json.put("isLast", page.isLast());
- // 总条数
- json.put("total", page.getTotalElements());
- // 总页数
- json.put("totalPages", page.getTotalPages());
- json.put(key, array);
- return json.toString();
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @param value 结果数据
- * @return
- */
- public String write(int code, String msg, String key, Map<?, ?> value) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- map.put(key, value);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @param value 结果数据
- * @return
- */
- public String write(int code, String msg, String key, String value) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- map.put(key, value);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @return
- */
- public String write(int code, String msg, String key, IdEntity entity) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("status", code);
- map.put("msg", msg);
- map.put(key, entity);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- /**
- * 返回接口处理结果
- *
- *
- * @param code 结果码,成功为200
- * @param msg 结果提示信息
- * @return
- */
- public String write(int code, String msg, boolean isFirst, boolean isLast, long total, int totalPages, String key, Object values) {
- try {
- JSONObject json = new JSONObject();
- json.put("status", code);
- json.put("msg", msg);
- // 是否为第一页
- json.put("isFirst", isFirst);
- // 是否为最后一页
- json.put("isLast", isLast);
- // 总条数
- json.put("total", total);
- // 总页数
- json.put("totalPages", totalPages);
- json.put(key, values);
- return json.toString();
- } catch (Exception e) {
- logger.error("BaseController:", e.getMessage());
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- public String trimEnd(String param, String trimChars) {
- if (param.endsWith(trimChars)) {
- param = param.substring(0, param.length() - trimChars.length());
- }
- return param;
- }
- /**
- * 无效用户消息返回
- * @param e
- * @param defaultCode
- * @param defaultMsg
- * @return
- */
- public String invalidUserException(Exception e, int defaultCode, String defaultMsg) {
- try {
- // if (e instanceof UndeclaredThrowableException) {
- // UndeclaredThrowableException ute = (UndeclaredThrowableException) e;
- // InvalidUserException iue = (InvalidUserException) ute.getUndeclaredThrowable();
- // if (iue != null) {
- // return error(iue.getCode(), iue.getMsg());
- // }
- // }
- return error(defaultCode, defaultMsg);
- } catch (Exception e2) {
- return null;
- }
- }
- /**
- * 返回表格列表数据
- * @param code 状态码:0成功,非0失败
- * @param errorMsg 错误消息
- * @param page 当前页码
- * @param rows 分页大小
- * @param list 查询的结果集
- * @return
- */
- public String write(int code, String errorMsg, int page, int rows, Page<?> list) {
- try {
- JSONObject object = new JSONObject();
- ObjectMapper mapper = new ObjectMapper();
- object.put("successFlg", code == 0);
- object.put("errorMsg", errorMsg);
- // 是否为第一页
- object.put("errorCode", code);
- // 是否为最后一页
- object.put("currPage", page);
- // 分页大小
- object.put("pageSize", rows);
- // 总条数
- object.put("totalCount", list.getTotalElements());
- // 总页数
- object.put("totalPage", list.getTotalPages());
- // 结果集
- object.put("detailModelList", list.getContent());
- return object.toString();
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
-
- public String write(int code, String errorMsg, int page, int rows, List<?> list) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("successFlg", code == 0);
- map.put("errorMsg", errorMsg);
- // 是否为第一页
- map.put("errorCode", code);
- // 是否为最后一页
- map.put("currPage", page);
- // 分页大小
- map.put("pageSize", rows);
- // 总条数
- map.put("totalCount", list.size());
- // 总页数
- map.put("totalPage", 1);
- // 结果集
- map.put("detailModelList", list);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
-
- public String error(int code, String errorMsg, int page, int rows) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("successFlg", code == 0);
- map.put("errorMsg", errorMsg);
- // 是否为第一页
- map.put("errorCode", code);
- // 是否为最后一页
- map.put("currPage", page);
- // 分页大小
- map.put("pageSize", rows);
- // 总条数
- map.put("totalCount", 0);
- // 总页数
- map.put("totalPage", 0);
- // 结果集
- map.put("detailModelList", null);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- //json串转集合
- public <T> Collection<T> jsonToEntities(String jsonDate, Collection<T> targets, Class<T> targetCls) {
- try {
- ObjectMapper objectMapper = new ObjectMapper();
- List modelList = objectMapper.readValue(jsonDate,List.class);
- Iterator ex = modelList.iterator();
- while(ex.hasNext()) {
- Object aModelList = ex.next();
- String objJsonData = objectMapper.writeValueAsString(aModelList);
- T model = objectMapper.readValue(objJsonData, targetCls);
- targets.add(model);
- }
- return targets;
- } catch (Exception var8) {
- var8.printStackTrace();
- return null;
- }
- }
- public String write(int code, String errorMsg, int page, int rows, long total, List<?> list) {
- try {
- Map<Object, Object> map = new HashMap<Object, Object>();
- ObjectMapper mapper = new ObjectMapper();
- map.put("successFlg", code == 0);
- map.put("errorMsg", errorMsg);
- // 是否为第一页
- map.put("errorCode", code);
- // 是否为最后一页
- map.put("currPage", page);
- // 分页大小
- map.put("pageSize", rows);
- // 总条数
- map.put("totalCount", total);
- // 总页数
- map.put("totalPage", 1);
- // 结果集
- map.put("detailModelList", list);
- return mapper.writeValueAsString(map);
- } catch (Exception e) {
- error(e);
- return error(-1, "服务器异常,请稍候再试!");
- }
- }
- }
|