jhprjv2/ruoyi-wechat/src/main/java/com/ruoyi/web/IndexController.java

119 lines
4.6 KiB
Java
Raw Normal View History

2024-04-17 02:18:03 +08:00
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");
/*urlauthorization code
codeaccess tokenopenid*/
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";
}
}