From 1e8319ce2b58c06aba2dcd838010693344467489 Mon Sep 17 00:00:00 2001 From: gongbiao Date: Mon, 12 Nov 2018 11:39:06 +0800 Subject: [PATCH] =?UTF-8?q?[B=E7=AB=AF=E8=B4=A6=E5=8D=95][=E4=BF=AE?= =?UTF-8?q?=E6=94=B9][=E8=B4=A6=E5=8D=95=E6=94=AF=E4=BB=98]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iformall/controller/WxInfoController.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 mallinkBApi/src/main/java/com/iformall/controller/WxInfoController.java diff --git a/mallinkBApi/src/main/java/com/iformall/controller/WxInfoController.java b/mallinkBApi/src/main/java/com/iformall/controller/WxInfoController.java new file mode 100644 index 000000000..40618d661 --- /dev/null +++ b/mallinkBApi/src/main/java/com/iformall/controller/WxInfoController.java @@ -0,0 +1,100 @@ +package com.iformall.controller; + + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import com.iformall.common.ErrorCode; +import com.iformall.common.ResultData; +import com.iformall.domain.po.WxAppinfo; +import com.iformall.service.WxAppinfoService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import me.chanjar.weixin.common.error.WxErrorException; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * @author gongbiao + */ +@RestController +@Api(description = "微信相关") +@RequestMapping("/api/wx") +public class WxInfoController extends BaseController { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + @Autowired + WxAppinfoService wxAppinfoService; + + @ApiOperation("获取OPENID") + @PostMapping("getOpenId") + public ResultData getOpenId(@RequestBody Map param) { + String appId = param.get("appId"); + String code = param.get("code"); + if (StringUtils.isBlank(appId)) { + return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "appId不能为空"); + } + if (StringUtils.isBlank(code)) { + return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "code不能为空"); + } + WxAppinfo wxAppinfo = getAppInfo(appId); + WxMaService wxMaService = getWeappServiceByAppInfo(wxAppinfo); + + if(StringUtils.isBlank(wxAppinfo.getAccessToken())) { + // 如果没有accessToken,主动获取保存到数据库中,下次访问可以拿来使用 + try { + String accessToken = wxMaService.getAccessToken(true); + wxAppinfo.setAccessToken(accessToken); + wxAppinfo.setLastTokenTime(new Date()); + wxAppinfo.setExpiresIn(7200); + wxAppinfoService.saveOrUpdate(wxAppinfo); + } catch (WxErrorException e) { + logger.error(e.getMessage()); + } catch (Exception e) { + logger.error(e.getMessage()); + } + + wxMaService = getWeappServiceByAppInfo(wxAppinfo); + + } else { + // 检查token是否已过期, 小时就重新获取 + Date curDate = new Date(); + if(curDate.getTime() > wxAppinfo.getLastTokenTime().getTime() + 3600000) { + try { + String accessToken = wxMaService.getAccessToken(true); + wxAppinfo.setAccessToken(accessToken); + wxAppinfo.setLastTokenTime(new Date()); + wxAppinfo.setExpiresIn(7200); + wxAppinfoService.saveOrUpdate(wxAppinfo); + } catch (WxErrorException e) { + logger.error(e.getMessage()); + } catch (Exception e) { + logger.error(e.getMessage()); + } + wxMaService = getWeappServiceByAppInfo(wxAppinfo); + } + } + + try { + WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(code); + String openId = session.getOpenid(); + logger.info("openId: " + openId); + Map resultMap=new HashMap<>(); + resultMap.put("openId",openId); + return new ResultData(resultMap); + } catch (WxErrorException e) { + logger.error(e.getMessage(), e); + return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR); + } + } + +}