package com.ruoyi.web; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.constant.CacheConstants; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysUserOpenid; import com.ruoyi.system.service.ISysUserOpenidService; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpSession; @Slf4j @Controller @RequestMapping("/wechat/wxAuth") @CrossOrigin(origins = "*",maxAge = 3600) public class IndexController { @Autowired private RedisCache redisCache; @Autowired private WxMpService wxMpService; @Autowired private ISysUserOpenidService sysUserOpenidService; /**********************************************************页面跳转*************************************************/ /** * @author :JiangYuQi * @param: : * @return: 重定向到获取用户信息的类 * 微信授权登录 */ @Anonymous @GetMapping("/model") public String model(Model model,String state) throws Exception { model.addAttribute("state",state); return "/wxAuth/index"; } /** * @author :tao * @param: : * @return: 重定向到获取用户信息的类 * 微信授权登录 */ @Anonymous @GetMapping("/authorize") public String authorize(String userOpenId, String state, HttpSession HttpSession) throws Exception { HttpSession.setAttribute("userOpenId", userOpenId); log.info("【微信网页授权】进来了,参数...{}", userOpenId); //1. 配置 //2. 调用方法 String url = RuoYiConfig.getProjectUrl() + "wechat/wxAuth/userInfo"; /* * 相当于这种形式 * URLEncoder.decode(returnUrl,"UTF-8" * https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect */ String resultUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, state); log.info("【微信网页授权】获取code,result={}", resultUrl); return "redirect:" + resultUrl; } /** * @author :tao * @param: : * @return: 重定向 * 获取用户信息类,最后重定向到指定url */ @Anonymous @GetMapping("/userInfo") public String userInfo(@RequestParam("code") String code, @RequestParam("state") String state, HttpSession HttpSession, Model model) throws Exception { model.addAttribute("state", "NO"); /*当用户同意授权后,会回调所设置的url并把authorization code传过来, 然后用这个code获得access token,其中也包含用户的openid等信息*/ WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); try { //获取access token wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code); log.info("【AccessToken:】{}", wxMpOAuth2AccessToken.getAccessToken()); // 拿到openid String openId = wxMpOAuth2AccessToken.getOpenId(); String userOpenId = HttpSession.getAttribute("userOpenId").toString(); log.info("小程序用户...{}的openid...:{}", userOpenId, openId); if(StringUtils.isNotEmpty(openId) && StringUtils.isNotEmpty(userOpenId)){ SysUserOpenid sysUserOpenid = sysUserOpenidService.selectSysUserOpenidByOpenId(userOpenId); if(sysUserOpenid!=null){ sysUserOpenid.setMsgOpenId(openId); sysUserOpenidService.updateSysUserOpenid(sysUserOpenid); //删除缓存 redisCache.deleteObject(CacheConstants.WX_MPMESSAGE_OPENID +sysUserOpenid.getLoginName()); model.addAttribute("state", "OK"); } } } catch (WxErrorException e) { log.error("【微信网页授权】{}", e); } return "wxAuth/index"; } }