|
@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -34,31 +35,31 @@ import java.util.UUID;
|
|
|
@RestController
|
|
|
@RequestMapping("/ymallWechat")
|
|
|
public class WechatController {
|
|
|
-
|
|
|
+
|
|
|
private Logger logger = LoggerFactory.getLogger(WechatController.class);
|
|
|
-
|
|
|
+
|
|
|
private String netLoginVideoAccess = "specialist,shortVideo,oneVideoPlayer";
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private UserAccountService userAccountService;
|
|
|
@Autowired
|
|
|
private UserRoleService userRoleService;
|
|
|
-
|
|
|
+
|
|
|
@Resource
|
|
|
private JwtConfig jwtConfig;
|
|
|
-
|
|
|
+
|
|
|
@Resource
|
|
|
private WxMpHandle wxMpHandle;
|
|
|
-
|
|
|
+
|
|
|
@Value("${ymall.appid}")
|
|
|
private String appid;
|
|
|
-
|
|
|
+
|
|
|
@Value("${ymall.secret}")
|
|
|
private String secret;
|
|
|
-
|
|
|
+
|
|
|
@Resource
|
|
|
private RedirectPathSource redirectPathSource;
|
|
|
-
|
|
|
+
|
|
|
@GetMapping("/authInfo")
|
|
|
public void authInfo(@RequestParam(required = false, defaultValue = "") String redirectPath,
|
|
|
HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
@@ -75,7 +76,7 @@ public class WechatController {
|
|
|
}
|
|
|
response.sendRedirect(redirectPath);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 网页授权后跳转
|
|
|
*
|
|
@@ -90,7 +91,7 @@ public class WechatController {
|
|
|
@RequestParam(name = "code", defaultValue = "", required = false) String code,
|
|
|
@RequestParam(name = "id", defaultValue = "", required = false) String id,// ID参数占位
|
|
|
@RequestParam(name = "redirectCode", defaultValue = "", required = false) String redirectCode,
|
|
|
- @RequestParam(name = "identity", defaultValue = "0",required = false) Integer identity,
|
|
|
+ @RequestParam(name = "identity", defaultValue = "0", required = false) Integer identity,
|
|
|
HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
String fillInfoPath = redirectPathSource.getFillInfo();
|
|
|
if (StringUtils.isBlank(code)) {
|
|
@@ -105,7 +106,7 @@ public class WechatController {
|
|
|
String openid = (String) jsonObj.get("openid");
|
|
|
String token = createToken(openid);
|
|
|
logger.info("token: " + token);
|
|
|
-
|
|
|
+
|
|
|
Cookie cookie = new Cookie("token", token);
|
|
|
cookie.setPath("/");
|
|
|
cookie.setMaxAge(24 * 60 * 60);
|
|
@@ -114,28 +115,28 @@ public class WechatController {
|
|
|
redirectCodeCoookie.setPath("/");
|
|
|
redirectCodeCoookie.setMaxAge(3600);
|
|
|
response.addCookie(redirectCodeCoookie);
|
|
|
-
|
|
|
- JSONObject userInfoJson = WechatHandle.getUserInfo(openid, appid, secret, code);
|
|
|
- UserAccount userAccount = userAccountService.checkAccountByUnionid(userInfoJson);
|
|
|
- Document document = MongoDBUtil.queryVLoginToken(userAccount.getId());
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- calendar.add(Calendar.DAY_OF_YEAR,30);
|
|
|
- String loginToken = UUID.randomUUID().toString();
|
|
|
- if( Objects.isNull(document)){
|
|
|
- MongoDBUtil.insertVLoginToken(calendar.getTime(),userAccount.getId(),loginToken,1);
|
|
|
- }else{
|
|
|
- MongoDBUtil.updateVLoginTOken(calendar.getTime(),userAccount.getId(),loginToken);
|
|
|
- }
|
|
|
- Cookie cookie0 = new Cookie("loginTokenAccount", userAccount.getId());
|
|
|
- cookie0.setPath("/");
|
|
|
- cookie0.setMaxAge(24 * 60 * 60);
|
|
|
- cookie0.setDomain("360lj.com");
|
|
|
- response.addCookie(cookie0);
|
|
|
- Cookie cookie1 = new Cookie("LoginToken", loginToken);
|
|
|
- cookie1.setPath("/");
|
|
|
- cookie1.setMaxAge(24 * 60 * 60);
|
|
|
- cookie1.setDomain("360lj.com");
|
|
|
- response.addCookie(cookie1);
|
|
|
+
|
|
|
+ JSONObject userInfoJson = WechatHandle.getUserInfo(openid, appid, secret, code);
|
|
|
+ UserAccount userAccount = userAccountService.checkAccountByUnionid(userInfoJson);
|
|
|
+ Document document = MongoDBUtil.queryVLoginToken(userAccount.getId());
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.add(Calendar.DAY_OF_YEAR, 30);
|
|
|
+ String loginToken = UUID.randomUUID().toString();
|
|
|
+ if (Objects.isNull(document)) {
|
|
|
+ MongoDBUtil.insertVLoginToken(calendar.getTime(), userAccount.getId(), loginToken, 1);
|
|
|
+ } else {
|
|
|
+ MongoDBUtil.updateVLoginTOken(calendar.getTime(), userAccount.getId(), loginToken);
|
|
|
+ }
|
|
|
+ Cookie cookie0 = new Cookie("loginTokenAccount", userAccount.getId());
|
|
|
+ cookie0.setPath("/");
|
|
|
+ cookie0.setMaxAge(24 * 60 * 60);
|
|
|
+ cookie0.setDomain("360lj.com");
|
|
|
+ response.addCookie(cookie0);
|
|
|
+ Cookie cookie1 = new Cookie("LoginToken", loginToken);
|
|
|
+ cookie1.setPath("/");
|
|
|
+ cookie1.setMaxAge(24 * 60 * 60);
|
|
|
+ cookie1.setDomain("360lj.com");
|
|
|
+ response.addCookie(cookie1);
|
|
|
// if (netLoginVideoAccess.contains(redirectCode)) {
|
|
|
// if (identity.equals(0)) { //用户授权
|
|
|
// if (StringUtils.isBlank(userAccount.getNickname())) {
|
|
@@ -150,34 +151,29 @@ public class WechatController {
|
|
|
// response.sendRedirect(url);
|
|
|
// return;
|
|
|
// }
|
|
|
-
|
|
|
- if (identity.equals(0)) { //用户授权
|
|
|
- if (StringUtils.isNotBlank(userAccount.getNickname())) {
|
|
|
- response.sendRedirect(redirectPathSource.getRedirectPath(redirectCode, userAccount, id));
|
|
|
- return;
|
|
|
- }
|
|
|
- response.sendRedirect(redirectPathSource.getRedirectPath(redirectCode, null, ""));
|
|
|
- return;
|
|
|
- } else if(identity.equals(1) || identity.equals(2)){ //医生合伙人授权
|
|
|
- userRoleService.checkRole(identity, userAccount, id, redirectCode, response);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 判断是否有该用户,如果有跳转个人中心,没有则跳转注册页面
|
|
|
- if (userAccount == null) {
|
|
|
- JSONObject userInfo = WechatHandle.getUserInfo(openid, appid, secret, code);
|
|
|
- userAccountService.create(userInfo);
|
|
|
- logger.info("将跳转至: " + fillInfoPath);
|
|
|
- String html = "<script type='text/javascript'>location.href='" + fillInfoPath + "';</script>";
|
|
|
- response.getWriter().print(html);
|
|
|
- return;
|
|
|
- }
|
|
|
- if (StringUtils.isBlank(userAccount.getMobile())) {
|
|
|
- logger.info("将跳转至: " + fillInfoPath);
|
|
|
- String html = "<script type='text/javascript'>location.href='" + fillInfoPath + "';</script>";
|
|
|
- response.getWriter().print(html);
|
|
|
+
|
|
|
+ if (identity.equals(0)) { //用户授权
|
|
|
+ // 判断是否有该用户,如果有跳转个人中心,没有则跳转注册页面
|
|
|
+ if (userAccount == null) {
|
|
|
+ JSONObject userInfo = WechatHandle.getUserInfo(openid, appid, secret, code);
|
|
|
+ userAccountService.create(userInfo);
|
|
|
+ logger.info("将跳转至: " + fillInfoPath);
|
|
|
+ String html = "<script type='text/javascript'>location.href='" + fillInfoPath + "';</script>";
|
|
|
+ response.getWriter().print(html);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(userAccount.getMobile())) {
|
|
|
+ logger.info("将跳转至: " + fillInfoPath);
|
|
|
+ String html = "<script type='text/javascript'>location.href='" + fillInfoPath + "';</script>";
|
|
|
+ response.getWriter().print(html);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } else if (identity.equals(1) || identity.equals(2)) { //医生合伙人授权
|
|
|
+ userRoleService.checkRole(identity, userAccount, id, redirectCode, response);
|
|
|
return;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
String otherRedirectPath = redirectPathSource.getRedirectPath(redirectCode, userAccount, id);
|
|
|
logger.info("将跳转至: " + otherRedirectPath);
|
|
|
String html = "<script type='text/javascript'>" +
|
|
@@ -185,7 +181,7 @@ public class WechatController {
|
|
|
"</script>";
|
|
|
response.getWriter().print(html);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@GetMapping("/skip")
|
|
|
public void skipPath(@RequestParam(name = "redirectPath", required = false) String redirectPath,
|
|
|
@RequestParam(name = "redirectCode", defaultValue = "", required = false) String redirectCode,
|
|
@@ -196,17 +192,17 @@ public class WechatController {
|
|
|
String token = request.getHeader(jwtConfig.getHeader());
|
|
|
token = token != null ? token : request.getParameter(jwtConfig.getHeader());
|
|
|
openid = StringUtils.isNotBlank(openid) ? openid : jwtConfig.getUsernameFromToken(token);
|
|
|
-
|
|
|
+
|
|
|
UserAccount user = userAccountService.selectOneByOpenid(openid);
|
|
|
if (id != null && id.equals("null"))
|
|
|
id = "";
|
|
|
-
|
|
|
+
|
|
|
redirectPath = redirectPath != null ? redirectPath : redirectPathSource.getRedirectPath(redirectCode, user, id);
|
|
|
logger.info("将跳转至: " + redirectPath);
|
|
|
response.sendRedirect(redirectPath);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* JS网页授权获取,jsapi_ticket以及签名
|
|
|
*
|
|
@@ -216,8 +212,8 @@ public class WechatController {
|
|
|
public JSONObject getSignature(@RequestParam("url") String url) {
|
|
|
return wxMpHandle.getSignature(url, appid);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
public String createToken(String openid) throws RuntimeException {
|
|
|
if (StringUtils.isBlank(openid)) {
|
|
|
throw new RuntimeException("微信授权openid不能为空");
|
|
@@ -225,5 +221,5 @@ public class WechatController {
|
|
|
String token = jwtConfig.createToken(openid);
|
|
|
return token;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|