| @@ -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<String,String> 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<String,String> 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); | |||||
| } | |||||
| } | |||||
| } | |||||