|
|
|
@@ -6,12 +6,19 @@ import java.text.SimpleDateFormat; |
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
import com.iformall.domain.po.MallUserInfo; |
|
|
|
import com.iformall.domain.po.WxAppinfo; |
|
|
|
import com.iformall.domain.po.base.TenantEntity; |
|
|
|
import com.iformall.douyin.miniapp.api.TtMaService; |
|
|
|
import com.iformall.service.WxAppinfoService; |
|
|
|
import com.iformall.shiro.UserSession; |
|
|
|
import com.iformall.utils.Constant; |
|
|
|
import com.iformall.utils.IPUtil; |
|
|
|
import com.iformall.utils.MaUtil; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.shiro.SecurityUtils; |
|
|
|
import org.apache.shiro.session.Session; |
|
|
|
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; |
|
|
|
@@ -23,6 +30,9 @@ import javax.servlet.http.HttpServletRequest; |
|
|
|
@RestController |
|
|
|
public class BaseController { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private WxAppinfoService wxAppinfoService; |
|
|
|
|
|
|
|
@InitBinder |
|
|
|
public void InitBinder(WebDataBinder dataBinder) { |
|
|
|
dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { |
|
|
|
@@ -60,6 +70,22 @@ public class BaseController { |
|
|
|
// return tenantId; |
|
|
|
// } |
|
|
|
|
|
|
|
public TtMaService getWeappServiceByAppInfo(WxAppinfo appinfo) { |
|
|
|
TtMaService ttMaService = MaUtil.getTtappService(appinfo); |
|
|
|
if (StringUtils.isBlank(appinfo.getAccessToken())) { |
|
|
|
// 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 |
|
|
|
updateAppAccessToken(appinfo, ttMaService); |
|
|
|
|
|
|
|
} else { |
|
|
|
// 检查token是否已过期, 1小时就重新获取 |
|
|
|
Date curDate = new Date(); |
|
|
|
if (curDate.getTime() > appinfo.getLastTokenTime().getTime() + Constant.H_EXPIRE) { |
|
|
|
updateAppAccessToken(appinfo, ttMaService); |
|
|
|
} |
|
|
|
} |
|
|
|
return ttMaService; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 返回租户信息 |
|
|
|
* 集团版帐号tenantId为空,parentTenantId为集团tenantId |
|
|
|
@@ -106,6 +132,32 @@ public class BaseController { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* WxAppInfo更新accessToken |
|
|
|
* |
|
|
|
* @param wxAppinfo |
|
|
|
* @param TtMaService |
|
|
|
*/ |
|
|
|
private void updateAppAccessToken(WxAppinfo wxAppinfo, TtMaService TtMaService) { |
|
|
|
try { |
|
|
|
String accessToken = TtMaService.getAccessToken(true); |
|
|
|
WxAppinfo updateApp = new WxAppinfo(); |
|
|
|
updateApp.setId(wxAppinfo.getId()); |
|
|
|
updateApp.setAccessToken(accessToken); |
|
|
|
updateApp.setLastTokenTime(new Date()); |
|
|
|
updateApp.setExpiresIn(7200); |
|
|
|
wxAppinfo.setAccessToken(updateApp.getAccessToken()); |
|
|
|
wxAppinfo.setLastTokenTime(updateApp.getLastTokenTime()); |
|
|
|
wxAppinfo.setExpiresIn(updateApp.getExpiresIn()); |
|
|
|
wxAppinfoService.saveOrUpdate(updateApp); |
|
|
|
|
|
|
|
} catch (WxErrorException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public String getIpAddr() { |
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
|
|
|
String ipaddress = IPUtil.getIpAddr(request); |
|
|
|
|