公众号登录

This commit is contained in:
TinyAnts 2022-09-14 15:30:30 +08:00
parent 959b3d84dc
commit 535cf29350
4 changed files with 68 additions and 10 deletions

View File

@ -10,7 +10,6 @@ public class FrontConfig {
// 免登录验证
public static String[] notLoginUri = new String[]{
"/api/login",
"/api/index",
"/api/config",
"/api/policy",
@ -21,6 +20,7 @@ public class FrontConfig {
"/api/upload/image",
"/api/login/check",
"/api/login/codeUrl",
"/api/login/register",
"/api/login/forgotPassword",

View File

@ -1,9 +1,18 @@
package com.mdd.front.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.utils.ConfigUtil;
import com.mdd.common.utils.WeChatUtil;
import com.mdd.front.service.ILoginService;
import com.mdd.front.validate.RegParam;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpOAuth2ServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -14,6 +23,7 @@ import java.util.Map;
/**
* 登录管理
*/
@Slf4j
@RestController
@RequestMapping("/api/login")
public class LoginController {
@ -55,13 +65,32 @@ public class LoginController {
case "account":
map = iLoginService.accountLogin(params);
break;
case "office":
map = iLoginService.officeLogin();
break;
}
return AjaxResult.success(map);
}
@PostMapping("/oaLogin")
public Object oaLogin(@RequestBody Map<String, String> params) {
log.error("微信公众号 ===================");
log.error(JSON.toJSONString(params));
// iLoginService.officeLogin(params);
return AjaxResult.success();
}
/**
* 公众号跳转url
*
* @author fzr
* @param url 连接
* @return Object
*/
@GetMapping("/codeUrl")
public Object codeUrl(@RequestParam String url) {
Assert.notNull(url, "url参数不能为空");
String uri = iLoginService.codeUrl(url);
return AjaxResult.success(uri);
}
/**
* 忘记密码
*
@ -71,8 +100,8 @@ public class LoginController {
*/
@PostMapping("/forgotPassword")
public Object forgotPassword(@RequestBody Map<String, String> params) {
iLoginService.forgotPassword(params);
return AjaxResult.success();
iLoginService.forgotPassword(params);
return AjaxResult.success();
}
}

View File

@ -48,9 +48,19 @@ public interface ILoginService {
* 公众号登录
*
* @author fzr
* @param params 参数
* @return Map<String, Object>
*/
Map<String, Object> officeLogin();
Map<String, Object> officeLogin(Map<String, String> params);
/**
* 公众号跳转url
*
* @author fzr
* @param url 连接
* @return String
*/
String codeUrl(String url);
/**
* 忘记密码

View File

@ -15,9 +15,11 @@ import com.mdd.common.utils.*;
import com.mdd.front.config.FrontConfig;
import com.mdd.front.service.ILoginService;
import com.mdd.front.validate.RegParam;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpOAuth2ServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -258,10 +260,13 @@ public class LoginServiceImpl implements ILoginService {
* @return Map<String, Object>
*/
@Override
public Map<String, Object> officeLogin() {
WxMpService wxMpService = WeChatUtil.official();
public Map<String, Object> officeLogin(Map<String, String> params) {
Assert.notNull(params.get("code"), "code参数缺失!");
String code = params.get("code");
try {
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken("aaa");
WxMpService wxMpService = WeChatUtil.official();
WxOAuth2AccessToken wxOAuth2AccessToken = wxMpService.getOAuth2Service().getAccessToken(code);
WxMpUser wxMpUser = wxMpService.getUserService().userInfo(wxOAuth2AccessToken.getAccessToken());
System.out.println(wxMpUser);
} catch (WxErrorException e) {
@ -270,6 +275,20 @@ public class LoginServiceImpl implements ILoginService {
return null;
}
/**
* 公众号跳转url
*
* @author fzr
* @param url 连接
* @return String
*/
@Override
public String codeUrl(String url) {
WxMpService wxMpService = WeChatUtil.official();
WxMpOAuth2ServiceImpl wxMpOAuth2Service = new WxMpOAuth2ServiceImpl(wxMpService);
return wxMpOAuth2Service.buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
}
/**
* 忘记密码
*