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 cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig; import com.simple.common.ErrorCode; import com.simple.domain.po.WxAppinfo; import com.simple.domain.po.WxCUser; 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.WxCUserService; import com.simple.service.WxMerchantBUserService; 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 WxMerchantBUser getUser() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Long cUserId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY); WxMerchantBUser user = wxMerchantBUserService.getById(cUserId); if (user == null) throw new MallinkException(ErrorCode.USER_IS_EMPTY); return user; } public String getTenantId() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Long cUserId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY); WxMerchantBUser user = wxMerchantBUserService.getById(cUserId); 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); WxMaInMemoryConfig config = new WxMaInMemoryConfig(); config.setAppid(appinfo.getAppId()); config.setSecret(appinfo.getSecret()); config.setToken(appinfo.getToken()); config.setAesKey(appinfo.getAesKey()); config.setMsgDataFormat(appinfo.getMsgDataFormat()); WxMaService service = new WxMaServiceImpl(); service.setWxMaConfig(config); return service; } public WxMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { WxMaInMemoryConfig config = new WxMaInMemoryConfig(); config.setAppid(appinfo.getAppId()); config.setSecret(appinfo.getSecret()); config.setToken(appinfo.getToken()); config.setAesKey(appinfo.getAesKey()); config.setMsgDataFormat(appinfo.getMsgDataFormat()); WxMaService service = new WxMaServiceImpl(); service.setWxMaConfig(config); return service; } }