PageRenderTime 568ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/penuel/mythopoet/controllers/WeixinShareController.java

https://gitlab.com/tycoon/mythopoet
Java | 99 lines | 77 code | 12 blank | 10 comment | 6 complexity | 065dd76633d16ed34f2fbaa29173a4a8 MD5 | raw file
  1. package com.penuel.mythopoet.controllers;
  2. import java.util.Date;
  3. import java.util.SortedMap;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.commons.lang3.math.NumberUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.web.bind.annotation.CookieValue;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import com.alibaba.fastjson.JSONObject;
  20. import com.penuel.mythopoet.model.WxAuthUser;
  21. import com.penuel.mythopoet.service.WxAuthService;
  22. import com.penuel.mythopoet.utils.ResponseUtil;
  23. @Controller
  24. @RequestMapping("/share")
  25. //@LoginRequired
  26. public class WeixinShareController {
  27. private static final Logger LOGGER = LoggerFactory.getLogger(WeixinShareController.class);
  28. @Autowired
  29. private WxAuthService wxAuthService;
  30. /**
  31. * 获取wx code的url回调地址
  32. *
  33. * @param model
  34. * @param code 返回需要的code,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期<br/>
  35. * 若用户禁止授权,则重定向后不会带上code参数
  36. * @param state orderId@userId 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
  37. * @return
  38. */
  39. @RequestMapping(value = "/wxcode", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
  40. public String wxcode(Model model, @RequestParam(value = "code", defaultValue = "") String code,
  41. @RequestParam(value = "state", defaultValue = "") String state,HttpServletRequest request, HttpServletResponse response) {
  42. long userId = 0;
  43. String currentUrl = "/";
  44. try {
  45. String[] stateArray = state.split("@");
  46. userId = NumberUtils.toLong(stateArray[1]);
  47. currentUrl = stateArray[0];
  48. if ( StringUtils.isBlank(code) ) {
  49. return "redirect:"+currentUrl;
  50. }
  51. LOGGER.info("wxcode:code=" + code + ",state=" + state);
  52. WxAuthUser wxAuthUser = wxAuthService.getAccessToken(code, userId);
  53. return "redirect:"+currentUrl;
  54. } catch ( Exception e ) {
  55. LOGGER.error("WeixinShareController.wxcode Error:code=" + code + ",state=" + state, e);
  56. }
  57. return "redirect:"+currentUrl;
  58. }
  59. @RequestMapping( value = "/ticket", method = RequestMethod.POST, produces = "application/json;charset=utf-8" )
  60. @ResponseBody
  61. public String jsapiTicket(Model model, @CookieValue( "userId" ) Long userId,@RequestParam("currentUrl") String currentUrl) {
  62. try {
  63. WxAuthUser wxAuthUser = wxAuthService.getByUserId(userId);
  64. if ( wxAuthUser == null ){
  65. String authUrl = wxAuthService.authorizeURL(userId, currentUrl);
  66. return ResponseUtil.result(1001, "微信授权", authUrl);
  67. }
  68. boolean refreshTicket = false;
  69. long millSecond = (new Date()).getTime() - wxAuthUser.getJsapiRefreshTime().getTime();
  70. if ( StringUtils.isBlank(wxAuthUser.getJsapiTicket()) ){
  71. refreshTicket = true;
  72. }else if( millSecond + 5*60*1000 > wxAuthUser.getJsapiExpiresIn()*1000 ){
  73. refreshTicket = true;
  74. }
  75. LOGGER.info("WeixinShareController.jsapiTicket.wxAuthUser ="+JSONObject.toJSONString(wxAuthUser)+",millSecond="+millSecond);
  76. if ( refreshTicket ){
  77. wxAuthService.refreshJsapiTicket(wxAuthUser);
  78. }
  79. SortedMap<Object, Object> map = wxAuthService.fillJsapiTicketParam(wxAuthUser.getJsapiTicket(), currentUrl);
  80. LOGGER.info("WeixinShareController.jsapiTicket result="+ JSONObject.toJSONString(map));
  81. return ResponseUtil.result(0,"OK",map);
  82. } catch ( Exception e ) {
  83. LOGGER.error("WeixinShareController.jsapiTicket Error:userId=" + userId , e);
  84. return ResponseUtil.result(1, "获取jsAPI初始化参数失败", null);
  85. }
  86. }
  87. }