You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
4.1 KiB

  1. package com.simple.controller;
  2. import java.beans.PropertyEditorSupport;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import cn.binarywang.wx.miniapp.api.WxMaService;
  7. import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  8. import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
  9. import com.simple.common.ErrorCode;
  10. import com.simple.domain.po.WxAppinfo;
  11. import com.simple.domain.po.WxCUser;
  12. import com.simple.domain.po.WxMerchantBUser;
  13. import com.simple.exception.MallinkException;
  14. import com.simple.interceptor.AuthorizationInterceptor;
  15. import com.simple.service.WxAppinfoService;
  16. import com.simple.service.WxCUserService;
  17. import com.simple.service.WxMerchantBUserService;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.WebDataBinder;
  20. import org.springframework.web.bind.annotation.InitBinder;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import org.springframework.web.context.request.RequestContextHolder;
  23. import org.springframework.web.context.request.ServletRequestAttributes;
  24. import javax.servlet.http.HttpServletRequest;
  25. @RestController
  26. public class BaseController {
  27. @Autowired
  28. private WxMerchantBUserService wxMerchantBUserService;
  29. @Autowired
  30. private WxAppinfoService wxAppinfoService;
  31. @InitBinder
  32. public void InitBinder(WebDataBinder dataBinder) {
  33. dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
  34. public void setAsText(String value) {
  35. try {
  36. setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value));
  37. } catch (ParseException e) {
  38. try {
  39. setValue(new SimpleDateFormat("yyyy-MM-dd ").parse(value));
  40. } catch (ParseException e1) {
  41. setValue(null);
  42. }
  43. }
  44. }
  45. public String getAsText() {
  46. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) getValue());
  47. }
  48. });
  49. }
  50. public WxMerchantBUser getUser() {
  51. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  52. Long cUserId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
  53. WxMerchantBUser user = wxMerchantBUserService.getById(cUserId);
  54. if (user == null)
  55. throw new MallinkException(ErrorCode.USER_IS_EMPTY);
  56. return user;
  57. }
  58. public String getTenantId() {
  59. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  60. Long cUserId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
  61. WxMerchantBUser user = wxMerchantBUserService.getById(cUserId);
  62. if (user == null)
  63. throw new MallinkException(ErrorCode.USER_IS_EMPTY);
  64. return user.getTenantId();
  65. }
  66. public WxAppinfo getAppInfo(String appId) {
  67. return wxAppinfoService.getByAppId(appId);
  68. }
  69. public WxMaService getWeappService(String appId) {
  70. WxAppinfo appinfo = wxAppinfoService.getByAppId(appId);
  71. WxMaInMemoryConfig config = new WxMaInMemoryConfig();
  72. config.setAppid(appinfo.getAppId());
  73. config.setSecret(appinfo.getSecret());
  74. config.setToken(appinfo.getToken());
  75. config.setAesKey(appinfo.getAesKey());
  76. config.setMsgDataFormat(appinfo.getMsgDataFormat());
  77. WxMaService service = new WxMaServiceImpl();
  78. service.setWxMaConfig(config);
  79. return service;
  80. }
  81. public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) {
  82. WxMaInMemoryConfig config = new WxMaInMemoryConfig();
  83. config.setAppid(appinfo.getAppId());
  84. config.setSecret(appinfo.getSecret());
  85. config.setToken(appinfo.getToken());
  86. config.setAesKey(appinfo.getAesKey());
  87. config.setMsgDataFormat(appinfo.getMsgDataFormat());
  88. WxMaService service = new WxMaServiceImpl();
  89. service.setWxMaConfig(config);
  90. return service;
  91. }
  92. }