Authored by chaogeng

添加mvc模块

/target/
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>yohobuy-platform-cms</artifactId>
<groupId>com.yoho.dsf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.yoho.dsf.yhplatform</groupId>
<artifactId>platform-cms-mvc</artifactId>
<packaging>jar</packaging>
<name>platform-cms-mvc</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.yoho.service.platform.model</groupId>
<artifactId>platform-service-model</artifactId>
</dependency>
<dependency>
<groupId>com.yohobuy.platform</groupId>
<artifactId>platform-common</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-redis-cluster</artifactId>
</dependency>
</dependencies>
</project>
... ...
/**
*
*/
package com.yohobuy.platform.mvc.bean;
import java.util.List;
import com.yoho.tools.common.beans.BaseBean;
/**
* @author ping.huang
* 2016年9月24日
*/
public class ERPProfileRequestBO extends BaseBean {
/**
*
*/
private static final long serialVersionUID = -6299334332126635127L;
private String password;
private String account;
private String auth_site;
private String truename;
private String staff_code;
private String email;
private Integer dept_id;
private Integer role_id;
private Integer create_id;
private Integer pid;
private String phone;
private List<Integer> pIds;
private Integer status;
private Integer pageNo;
private Integer pageSize;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getAuth_site() {
return auth_site;
}
public void setAuth_site(String auth_site) {
this.auth_site = auth_site;
}
public String getTruename() {
return truename;
}
public void setTruename(String truename) {
this.truename = truename;
}
public String getStaff_code() {
return staff_code;
}
public void setStaff_code(String staff_code) {
this.staff_code = staff_code;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getDept_id() {
return dept_id;
}
public void setDept_id(Integer dept_id) {
this.dept_id = dept_id;
}
public Integer getRole_id() {
return role_id;
}
public void setRole_id(Integer role_id) {
this.role_id = role_id;
}
public Integer getCreate_id() {
return create_id;
}
public void setCreate_id(Integer create_id) {
this.create_id = create_id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public List<Integer> getpIds() {
return pIds;
}
public void setpIds(List<Integer> pIds) {
this.pIds = pIds;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
... ...
/**
*
*/
package com.yohobuy.platform.mvc.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.core.common.utils.MD5;
import com.yoho.tools.common.beans.Response;
import com.yohobuy.platform.common.enums.CodeEnum;
import com.yohobuy.platform.model.users.response.UserInfoResponseBO;
import com.yohobuy.platform.common.helper.mvc.PlatformConstant;
import com.yohobuy.platform.mvc.service.ILoginService;
import com.yohobuy.platform.common.util.SessionUtil;
/**
* 描述:platform回调cms页面时,验证用户的登录状态
*
*/
@Controller
@RequestMapping("/LoginController")
public class LoginController {
static Logger log = LoggerFactory.getLogger(LoginController.class);
@Resource
ILoginService loginService;
private static String SALT= DynamicPropertyFactory.getInstance().getStringProperty("platform.login.salt", "").get();
@RequestMapping("/loginForPid.do")
public void loginForPid(String pid, String pidMd5, String callbackUrl , HttpServletRequest request, HttpServletResponse response) throws Exception {
log.info("enter login with pid={},pidAes is {},callbackUrl is {}", pid, pidMd5, callbackUrl);
if (StringUtils.isEmpty(pid) || StringUtils.isEmpty(pidMd5)) {
log.warn("loginForPid error. with pid={}, pidMd5={}", pid, pidMd5);
return;
}
UserInfoResponseBO userInfoResponseBO = (UserInfoResponseBO) request.getSession().getAttribute(PlatformConstant.USER_SESSION);
if (null != userInfoResponseBO && pid.equals(userInfoResponseBO.getPid())) {
request.setAttribute("pid", pid);
request.getRequestDispatcher(callbackUrl).forward(request, response);
} else if (pidMd5.equals(MD5.md5(pid + SALT))) {
log.info("loginForPid with session is empty. and login start. with pid={}", pid);
Response<UserInfoResponseBO> responseBO = loginService.loginByPid(pid);
if (responseBO != null && responseBO.getCode() == CodeEnum.SUCCESS.getCode()) {
log.info("loginForPid with session is empty. and login success. with pid={}", pid);
// 登录成功,把用户信息存入redis
SessionUtil.getOpera().setUser(responseBO.getData());
request.setAttribute("pid", pid);
request.getSession().setAttribute(PlatformConstant.USER_SESSION, responseBO.getData());
request.getRequestDispatcher(callbackUrl).forward(request, response);
}
}
}
}
... ...
package com.yohobuy.platform.mvc.helper;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.tools.common.beans.Response;
import com.yohobuy.platform.common.enums.CodeEnum;
import com.yohobuy.platform.common.enums.ErpApiEnum;
import com.yohobuy.platform.model.users.response.UserInfoResponseBO;
import com.yohobuy.platform.mvc.bean.ERPProfileRequestBO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class ErpApiServiceHelper {
static Logger log = LoggerFactory.getLogger(ErpApiServiceHelper.class);
@Resource
RestTemplate restTemplate;
private static String ERP_DOMAIN = DynamicPropertyFactory.getInstance().getStringProperty("erp.domain", "").get();
private static String ERP_STORAGE = DynamicPropertyFactory.getInstance().getStringProperty("erp.storage", "").get();
/**
* /account/profile/query_by_id http://192.168.102.47:9098/erp-gateway-web
/service/account/v1/Profile/getAllBrandProfileDataAuthorityApi http://serve.yohobuy.com
/account/profile/query_page http://192.168.102.47:9098/erp-gateway-web
/account/profileDataEntitlement/dataList http://192.168.102.47:9098/erp-gateway-web
*
*/
/**
* 根据pid,查询用户
* @param pid
* @return
*/
@SuppressWarnings("unchecked")
public UserInfoResponseBO getUserByPid(String pid) {
log.info("getUserByPid with pid={}", pid);
ERPProfileRequestBO profile = new ERPProfileRequestBO();
profile.setPid(Integer.parseInt(pid));
Response<Map<String, Object>> resp = restTemplate.postForObject(ERP_DOMAIN + ErpApiEnum.GET_USER_BY_PID.getUrl(), profile, Response.class);
if (resp == null || resp.getCode() != CodeEnum.SUCCESS.getCode()) {
log.warn("getuserbypid error with pid={}", pid);
return null;
}
UserInfoResponseBO user = toSysUserResponse(resp.getData());
return user;
}
/**
* 对象转换
*
* @param map
* @return
*/
private UserInfoResponseBO toSysUserResponse(Map<String, Object> map) {
if (MapUtils.isEmpty(map)) {
return null;
}
UserInfoResponseBO bo = new UserInfoResponseBO();
bo.setCreate_time(MapUtils.getString(map, "create_time"));
bo.setEmail(MapUtils.getString(map, "email"));
bo.setAccount(MapUtils.getString(map, "account"));
bo.setPhone(MapUtils.getString(map, "phone"));
bo.setPid(MapUtils.getString(map, "pid"));
bo.setRole_id(MapUtils.getString(map, "role_id"));
bo.setStatus(MapUtils.getString(map, "status"));
bo.setTruename(MapUtils.getString(map, "truename"));
bo.setAuth_site(MapUtils.getString(map, "auth_site"));
bo.setCreate_date(MapUtils.getString(map, "create_date"));
bo.setCreate_id(MapUtils.getString(map, "create_id"));
bo.setDept_id(MapUtils.getString(map, "dept_id"));
bo.setExpires(MapUtils.getString(map, "expires"));
bo.setIdentity(MapUtils.getString(map, "identity"));
bo.setLogin_time(MapUtils.getString(map, "login_time"));
bo.setLogout_time(MapUtils.getString(map, "logout_time"));
bo.setStaff_code(MapUtils.getString(map, "staff_code"));
return bo;
}
/**
* 北京接口返回:is_all=ture 说明是全品牌,list就是空;is_all = false 说明不是全品牌 品牌id会在list
* 根据pid,获取用户有品牌权限品牌ID。
* 如果为-1, 不存在,所有品牌都不展示
* 如果为空集合,有所有品牌权限
* 如果不为空 ,有品牌权限品牌ID
*/
public List<Integer> getBrandIds(String pid) {
String url = ERP_STORAGE + "/service/account/v1/Profile/getAllBrandProfileDataAuthorityApi";
try {
Response<Map<String, Object>> response = restTemplate.postForObject(url, "["+pid+"]", Response.class);
log.info("call erp method, url:{} and response:{}.", url, response);
if (response == null || response.getCode() != 200) {
log.warn("call erp method fail, response:{}.",response);
// 所有品牌都不展示
return Lists.newArrayList(-1);
}
Map<String,Object> data= (Map<String,Object>) response.getData();
if ((boolean)data.get("is_all")) {
//有所有品牌权限
return Lists.newArrayList();
}
Object brandIdData = data.get("list");
if (brandIdData instanceof List && ((List)brandIdData).isEmpty()) {
//有所有品牌权限
return Lists.newArrayList();
}
List<Integer> brandIds=Lists.newArrayList();
List<String> brandIdStrs=(List<String>)brandIdData;
for (String brandId : brandIdStrs) {
brandIds.add(Integer.parseInt(brandId));
}
return brandIds;
} catch (Exception e) {
log.error("call erp method fail, url:{} and error message:{}.", url, e.getMessage());
// 所有品牌都不展示
return Lists.newArrayList(-1);
}
}
//根据pid获取店铺权限
public List<Integer> getShopIds(String pid) {
String url = ERP_DOMAIN + "/account/profileDataEntitlement/dataList";
List<Integer> list = new ArrayList<>();
try{
JSONObject req = new JSONObject();
req.put("pid",Integer.valueOf(pid));
req.put("type",2);
Response<Map<String, Object>> response = restTemplate.postForObject(url, req, Response.class);
log.info("getShopIds erp method, url:{} and response:{}.", url, response);
if(response.getCode() != 200){
log.warn("call erp method fail, response:{}.",response);
return Lists.newArrayList(-1);
}
Map<String,Object> shopsMap = response.getData();
boolean isAll = (boolean) shopsMap.get("isAll");
if(isAll){
//如果是全部权限,这是权限空
return list;
}
List<Map> shopList = (List) shopsMap.get("businessList");
if (CollectionUtils.isEmpty(shopList)) {
return Lists.newArrayList(-1);
}
for (Map m : shopList){
list.add(MapUtils.getInteger(m,"id"));
}
}catch (Exception e){
log.info("get shopids from erp-gateway-wab error e={}",e.getMessage());
return Lists.newArrayList(-1);
}
return list;
}
}
... ...
/**
*
*/
package com.yohobuy.platform.mvc.service;
import com.yoho.tools.common.beans.Response;
import com.yohobuy.platform.model.users.response.UserInfoResponseBO;
/**
* @author ping.huang
* 2016年8月1日
*/
public interface ILoginService {
Response<UserInfoResponseBO> loginByPid(String pid);
}
... ...
/**
*
*/
package com.yohobuy.platform.mvc.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.yoho.tools.common.beans.Response;
import com.yohobuy.platform.model.users.response.UserInfoResponseBO;
import com.yohobuy.platform.mvc.helper.ErpApiServiceHelper;
import com.yohobuy.platform.mvc.service.ILoginService;
/**
* @author ping.huang
* 2016年8月1日
*/
@Service
public class LoginServiceImpl implements ILoginService {
static Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
@Resource
ErpApiServiceHelper erpApiServiceHelper;
@Override
public Response<UserInfoResponseBO> loginByPid(String pid) {
log.info("loginByPid with pid is {}.", pid);
UserInfoResponseBO user = erpApiServiceHelper.getUserByPid(pid);
if(user == null){
return new Response<>();
}
List<Integer> brandIds = erpApiServiceHelper.getBrandIds(pid);
user.setBrandIds(brandIds);
List<Integer> shopIds = erpApiServiceHelper.getShopIds(pid);
user.setShopIds(shopIds);
log.info("loginByPid userInfoResponseBO is {}.", user);
return new Response<>(user);
}
}
... ...
package org.mvc;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
... ...
... ... @@ -28,6 +28,11 @@
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhplatform</groupId>
<artifactId>platform-cms-mvc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhplatform</groupId>
<artifactId>platform-cms-cms</artifactId>
<version>${project.version}</version>
</dependency>
... ... @@ -82,6 +87,7 @@
</dependencies>
<modules>
<module>mvc</module>
<module>cms</module>
<module>suggest</module>
<module>operations</module>
... ...
... ... @@ -25,6 +25,10 @@
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhplatform</groupId>
<artifactId>platform-cms-mvc</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhplatform</groupId>
<artifactId>platform-cms-operations</artifactId>
</dependency>
<dependency>
... ...
//todo 分解成多个接口,或接受参数的方式,实现加载可配置化,而不是笼统的暴力加载方式,太low
var contextPath = "/pfcms";
var serverContextPath = "/platform";
var h = "/platform";
document.write("<meta charset='UTF-8'>");
//首先加载样式,保证样式先显示
... ...