package com.simple.controller; import java.beans.PropertyEditorSupport; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import cn.binarywang.wx.miniapp.api.WxMaService; import com.simple.common.ErrorCode; import com.simple.domain.po.WxAppinfo; import com.simple.domain.po.WxMerchantBUser; import com.simple.exception.MallinkException; import com.simple.interceptor.AuthorizationInterceptor; import com.simple.service.WxAppinfoService; import com.simple.service.WxMerchantBUserService; import com.simple.utils.MaUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; @RestController public class BaseController { @Autowired private WxMerchantBUserService wxMerchantBUserService; @Autowired private WxAppinfoService wxAppinfoService; @InitBinder public void InitBinder(WebDataBinder dataBinder) { dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { public void setAsText(String value) { try { setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value)); } catch (ParseException e) { try { setValue(new SimpleDateFormat("yyyy-MM-dd ").parse(value)); } catch (ParseException e1) { setValue(null); } } } public String getAsText() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) getValue()); } }); } public Long getUserId() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Long bUserId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY); return bUserId; } public WxMerchantBUser getUser() { Long bUserId = getUserId(); WxMerchantBUser user = wxMerchantBUserService.getById(bUserId); if (user == null) throw new MallinkException(ErrorCode.USER_IS_EMPTY); return user; } public String getTenantId() { Long bUserId = getUserId(); WxMerchantBUser user = wxMerchantBUserService.getById(bUserId); if (user == null) throw new MallinkException(ErrorCode.USER_IS_EMPTY); return user.getTenantId(); } public WxAppinfo getAppInfo(String appId) { return wxAppinfoService.getByAppId(appId); } public WxMaService getWeappService(String appId) { WxAppinfo appinfo = wxAppinfoService.getByAppId(appId); WxMaService service = MaUtil.getWeappService(appinfo); return service; } public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { WxMaService service = MaUtil.getWeappService(appinfo); return service; } }