diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java index 2c5bbea24..69b48a5f8 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/UsersController.java @@ -1,24 +1,32 @@ package com.fangjia.open.controllers; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import com.fangjia.core.util.AnalyticsUtil; +import com.fangjia.core.util.BeanUtils; +import com.fangjia.core.util.DateUtil; +import com.fangjia.dal.QueryParam; import com.fangjia.dal.mongo.model.SendSmsLog; +import com.fangjia.dal.mongo.open.model.ApiUserAuthority; +import com.fangjia.dal.mongo.open.model.OpenResource; +import com.fangjia.dal.mongo.tool.model.OpenUriAnaly; +import com.fangjia.open.HttpStatusCodeEnum; +import com.fangjia.open.interceptors.AuthenticationBean; +import com.fangjia.open.model.*; import com.fangjia.open.model.weixin.SendCodeJsonModel; import com.fangjia.open.model.weixin.SendCodeModel; +import com.fangjia.service.open.*; import net.sf.json.JSONObject; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; +import org.sothis.web.mvc.ActionContext; +import org.sothis.web.mvc.ActionInvocation; import org.sothis.web.mvc.annotation.Param; import com.fangjia.dal.sql.model.User; import com.fangjia.open.HttpCode; import com.fangjia.open.OpenHttpCodeEnum; -import com.fangjia.open.model.RestfulModelAndView; -import com.fangjia.open.model.UsersModel; import com.fangjia.service.mongo.mobile.UserInfoService; import com.fangjia.service.sql.UserService; import com.fangjia.service.util.UsersUtils; @@ -26,100 +34,166 @@ import com.fangjia.service.util.UsersUtils; import javax.servlet.http.HttpServletRequest; public class UsersController { - private UserService userService; - private UserInfoService userInfoService; + private UserService userService; + private UserInfoService userInfoService; - /** - * - * @param model - * @return 1 无此用户 2 非法请求 3 密码错误 0 正常登陆 - * @throws Exception - */ - public Object loginAction(UsersModel model) throws Exception { - Map infomain = new HashMap(); - if (StringUtils.isBlank(model.getName()) || StringUtils.isBlank(model.getPwd())) { - infomain.put("flag", UsersUtils.LoginEnum.EINVAL.ordinal()); - } else { - User user = userService.findByUserName(model.getName()); - if (null == user) { - infomain.put("flag", UsersUtils.LoginEnum.NOUSER.ordinal()); - } else if (!user.getPassword().equals(DigestUtils.md5Hex(model.getPwd()))) { - infomain.put("flag", UsersUtils.LoginEnum.ERRPWD.ordinal()); - } else { - infomain.put("flag", UsersUtils.LoginEnum.SUCCESS.ordinal()); - infomain.put("displayname", user.getDisplayName()); - infomain.put("username", user.getUserName()); - infomain.put("id", user.getId()); - } - } - JSONObject json = JSONObject.fromObject(infomain); - return json.toString(); - } - - /** - * - * @param model - * @return 1 无此用户 2 非法请求 3 原始密码错误 0 修改成功 - * @throws Exception - */ - public Object modifyPwdAction(UsersModel model) throws Exception { - Map infomain = new HashMap(); - if (StringUtils.isBlank(model.getName()) || StringUtils.isBlank(model.getPwd())) { - infomain.put("flag", UsersUtils.LoginEnum.EINVAL.ordinal()); - } else { - User user = userService.findByUserName(model.getName()); - if (null == user) { - infomain.put("flag", UsersUtils.LoginEnum.NOUSER.ordinal()); - } else if (!user.getPassword().equals(DigestUtils.md5Hex(model.getPwd()))) { - infomain.put("flag", UsersUtils.LoginEnum.ERRPWD.ordinal()); - } else { - user.setPassword(DigestUtils.md5Hex(model.getNewPwd())); - userService.updateUser(user); - infomain.put("flag", UsersUtils.LoginEnum.SUCCESS.ordinal()); - } - } - JSONObject json = JSONObject.fromObject(infomain); - return json.toString(); - } - - public Object saveAction(@Param(name = "ime") String ime, @Param(name = "name") String name, @Param(name = "description") String description, @Param(name = "image") String image, @Param(name = "type") String type) { - - if (ime == null || "".equals(ime.trim())) { - RestfulModelAndView mav = new RestfulModelAndView(); - mav.setViewType("rest"); - mav.setModel(new HttpCode(OpenHttpCodeEnum.ARGUMENT_LOSE, "手机唯一标示缺失!")); - return mav; - } - if (name == null || "".equals(name.trim())) { - RestfulModelAndView mav = new RestfulModelAndView(); - mav.setViewType("rest"); - mav.setModel(new HttpCode(OpenHttpCodeEnum.ARGUMENT_LOSE, "用户名缺失!")); - return mav; - } - if (type == null || "".equals(type.trim())) { - type = "default"; - } - - com.fangjia.dal.mongo.mobile.model.User userInfo = userInfoService.save(ime, name, description, image, type); - - Map infomain = new HashMap(); - infomain.put("uid", userInfo.getId()); - infomain.put("ime", userInfo.getIme()); - infomain.put("name", userInfo.getName()); - infomain.put("type", userInfo.getType()); - infomain.put("description", userInfo.getDescription()); - infomain.put("image", userInfo.getImage()); - JSONObject json = JSONObject.fromObject(infomain); - return json.toString(); - } - - public void setUserService(UserService userService) { - this.userService = userService; - } - - public void setUserInfoService(UserInfoService userInfoService) { - this.userInfoService = userInfoService; - } + private ApiVisitCountService apiVisitCountService; + private ApiUserAuthorityService apiUserAuthorityService; + private OpenResourceService openResourceService; + private OpenUriAnalyService openUriAnalyService; + /** + * @param model + * @return 1 无此用户 2 非法请求 3 密码错误 0 正常登陆 + * @throws Exception + */ + public Object loginAction(UsersModel model) throws Exception { + Map infomain = new HashMap(); + if (StringUtils.isBlank(model.getName()) || StringUtils.isBlank(model.getPwd())) { + infomain.put("flag", UsersUtils.LoginEnum.EINVAL.ordinal()); + } else { + User user = userService.findByUserName(model.getName()); + if (null == user) { + infomain.put("flag", UsersUtils.LoginEnum.NOUSER.ordinal()); + } else if (!user.getPassword().equals(DigestUtils.md5Hex(model.getPwd()))) { + infomain.put("flag", UsersUtils.LoginEnum.ERRPWD.ordinal()); + } else { + infomain.put("flag", UsersUtils.LoginEnum.SUCCESS.ordinal()); + infomain.put("displayname", user.getDisplayName()); + infomain.put("username", user.getUserName()); + infomain.put("id", user.getId()); + } + } + JSONObject json = JSONObject.fromObject(infomain); + return json.toString(); + } + + /** + * @param model + * @return 1 无此用户 2 非法请求 3 原始密码错误 0 修改成功 + * @throws Exception + */ + public Object modifyPwdAction(UsersModel model) throws Exception { + Map infomain = new HashMap(); + if (StringUtils.isBlank(model.getName()) || StringUtils.isBlank(model.getPwd())) { + infomain.put("flag", UsersUtils.LoginEnum.EINVAL.ordinal()); + } else { + User user = userService.findByUserName(model.getName()); + if (null == user) { + infomain.put("flag", UsersUtils.LoginEnum.NOUSER.ordinal()); + } else if (!user.getPassword().equals(DigestUtils.md5Hex(model.getPwd()))) { + infomain.put("flag", UsersUtils.LoginEnum.ERRPWD.ordinal()); + } else { + user.setPassword(DigestUtils.md5Hex(model.getNewPwd())); + userService.updateUser(user); + infomain.put("flag", UsersUtils.LoginEnum.SUCCESS.ordinal()); + } + } + JSONObject json = JSONObject.fromObject(infomain); + return json.toString(); + } + + public Object saveAction(@Param(name = "ime") String ime, @Param(name = "name") String name, @Param(name = "description") String description, @Param(name = "image") String image, @Param(name = "type") String type) { + + if (ime == null || "".equals(ime.trim())) { + RestfulModelAndView mav = new RestfulModelAndView(); + mav.setViewType("rest"); + mav.setModel(new HttpCode(OpenHttpCodeEnum.ARGUMENT_LOSE, "手机唯一标示缺失!")); + return mav; + } + if (name == null || "".equals(name.trim())) { + RestfulModelAndView mav = new RestfulModelAndView(); + mav.setViewType("rest"); + mav.setModel(new HttpCode(OpenHttpCodeEnum.ARGUMENT_LOSE, "用户名缺失!")); + return mav; + } + if (type == null || "".equals(type.trim())) { + type = "default"; + } + + com.fangjia.dal.mongo.mobile.model.User userInfo = userInfoService.save(ime, name, description, image, type); + + Map infomain = new HashMap(); + infomain.put("uid", userInfo.getId()); + infomain.put("ime", userInfo.getIme()); + infomain.put("name", userInfo.getName()); + infomain.put("type", userInfo.getType()); + infomain.put("description", userInfo.getDescription()); + infomain.put("image", userInfo.getImage()); + JSONObject json = JSONObject.fromObject(infomain); + return json.toString(); + } + + public Object surplusCallCountAction() throws IllegalAccessException { + RestfulModelAndView mav = new RestfulModelAndView(); + ActionContext context = ActionContext.getContext(); + HttpServletRequest request = context.getRequest(); + AuthenticationBean instance = (AuthenticationBean) request.getAttribute(AuthenticationBean.ATTR_AUTHENTICATION_BEAN); + if (null != instance) { + User user = instance.user; + List openResourceList = openResourceService.findAll(); + List list = new ArrayList(); + long id = user.getId(); + ApiUserAuthority authority = apiUserAuthorityService.findByUserId(id); + if (null != authority) { + // 用户有的uri + List userResources = new ArrayList(); + for (OpenResource openResource : openResourceList) { + if (authority.getOpenResourseUris().contains(openResource.getUri())) { + userResources.add(openResource); + } + } + Map countMap = authority.getTotalCountMap(); + if (null != countMap) { + // 改成遍历用户有的uri + for (OpenResource openResource : userResources) { + String uri = openResource.getUri(); + if (null != countMap.get(uri) && countMap.get(uri) > 0) { + ApiMonitorInfo info = new ApiMonitorInfo(); + info.setApiUri(uri); + info.setApiName(openResource.getName()); + info.setUserId((int) id); + info.setUsername(user.getUserName()); + long curVistedCount = apiVisitCountService.findApiVisitCountByDay(id, uri, DateUtil.truncateDay(new Date())); + long total = apiVisitCountService.findApiVisitAllCount(id, uri, DateUtil.truncateDay(new Date())); + info.setSurplusCallCount(countMap.get(uri) - curVistedCount - total); + list.add(info); + } + + } + } + } + BasicModel basicModel = new BasicModel(200, "ok"); + basicModel.setResult(list); + mav.setModel(basicModel); + } else { + mav.setModel(new BasicModel(HttpStatusCodeEnum.BAD_REQUEST_ERROR_KEY)); + } + return mav; + } + + public void setUserService(UserService userService) { + this.userService = userService; + } + + public void setUserInfoService(UserInfoService userInfoService) { + this.userInfoService = userInfoService; + } + + public void setApiVisitCountService(ApiVisitCountService apiVisitCountService) { + this.apiVisitCountService = apiVisitCountService; + } + + public void setApiUserAuthorityService(ApiUserAuthorityService apiUserAuthorityService) { + this.apiUserAuthorityService = apiUserAuthorityService; + } + + public void setOpenResourceService(OpenResourceService openResourceService) { + this.openResourceService = openResourceService; + } + + public void setOpenUriAnalyService(OpenUriAnalyService openUriAnalyService) { + this.openUriAnalyService = openUriAnalyService; + } } diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiMonitorInfo.java b/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiMonitorInfo.java new file mode 100644 index 000000000..0fdb11e38 --- /dev/null +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/model/ApiMonitorInfo.java @@ -0,0 +1,64 @@ +package com.fangjia.open.model; + +import java.util.Date; + +public class ApiMonitorInfo { + private Integer userId; + private String username; + private Integer callCount; + private Date callTime; + private String apiUri; + private String apiName; + private Long surplusCallCount; + private Date endTime; + + public Long getSurplusCallCount() { + return surplusCallCount; + } + public void setSurplusCallCount(Long surplusCallCount) { + this.surplusCallCount = surplusCallCount; + } + public Date getEndTime() { + return endTime; + } + public void setEndTime(Date endTime) { + this.endTime = endTime; + } + public Integer getUserId() { + return userId; + } + public void setUserId(Integer userId) { + this.userId = userId; + } + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public Integer getCallCount() { + return callCount; + } + public void setCallCount(Integer callCount) { + this.callCount = callCount; + } + public Date getCallTime() { + return callTime; + } + public void setCallTime(Date callTime) { + this.callTime = callTime; + } + public String getApiUri() { + return apiUri; + } + public void setApiUri(String apiUri) { + this.apiUri = apiUri; + } + public String getApiName() { + return apiName; + } + public void setApiName(String apiName) { + this.apiName = apiName; + } + +}