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.
 
 
 
 
 

93 lines
3.2 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 com.simple.common.ErrorCode;
  8. import com.simple.domain.po.WxAppinfo;
  9. import com.simple.domain.po.WxMerchantBUser;
  10. import com.simple.exception.MallinkException;
  11. import com.simple.interceptor.AuthorizationInterceptor;
  12. import com.simple.service.WxAppinfoService;
  13. import com.simple.service.WxMerchantBUserService;
  14. import com.simple.utils.MaUtil;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.WebDataBinder;
  17. import org.springframework.web.bind.annotation.InitBinder;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import org.springframework.web.context.request.RequestContextHolder;
  20. import org.springframework.web.context.request.ServletRequestAttributes;
  21. import javax.servlet.http.HttpServletRequest;
  22. @RestController
  23. public class BaseController {
  24. @Autowired
  25. private WxMerchantBUserService wxMerchantBUserService;
  26. @Autowired
  27. private WxAppinfoService wxAppinfoService;
  28. @InitBinder
  29. public void InitBinder(WebDataBinder dataBinder) {
  30. dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
  31. public void setAsText(String value) {
  32. try {
  33. setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value));
  34. } catch (ParseException e) {
  35. try {
  36. setValue(new SimpleDateFormat("yyyy-MM-dd ").parse(value));
  37. } catch (ParseException e1) {
  38. setValue(null);
  39. }
  40. }
  41. }
  42. public String getAsText() {
  43. return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) getValue());
  44. }
  45. });
  46. }
  47. public Long getUserId() {
  48. HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  49. Long bUserId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
  50. return bUserId;
  51. }
  52. public WxMerchantBUser getUser() {
  53. Long bUserId = getUserId();
  54. WxMerchantBUser user = wxMerchantBUserService.getById(bUserId);
  55. if (user == null)
  56. throw new MallinkException(ErrorCode.USER_IS_EMPTY);
  57. return user;
  58. }
  59. public String getTenantId() {
  60. Long bUserId = getUserId();
  61. WxMerchantBUser user = wxMerchantBUserService.getById(bUserId);
  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. WxMaService service = MaUtil.getWeappService(appinfo);
  72. return service;
  73. }
  74. public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) {
  75. WxMaService service = MaUtil.getWeappService(appinfo);
  76. return service;
  77. }
  78. }