新增h5公众号登录功能
This commit is contained in:
1 parent
8f7609786b
commit
1967585578
5 files changed
+112
-2
No files matched your search
+10
@@ -135,6 +135,16 @@ public class AppAuthController {
|
|||||||
,appWxMiniLoginVO.getOpenid()));
|
,appWxMiniLoginVO.getOpenid()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/auth-wechat-login")
|
||||||
|
@Operation(summary = "社交授权的跳转")
|
||||||
|
@Parameters({
|
||||||
|
@Parameter(name = "code", description = "code码", required = true)
|
||||||
|
})
|
||||||
|
public CommonResult<AppAuthLoginRespVO> wechatAuth(@RequestParam("code") String code) {
|
||||||
|
System.out.println("code:"+code);
|
||||||
|
return CommonResult.success(authService.wechatAuth(code));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+8
@@ -71,6 +71,14 @@ public interface MemberAuthService {
|
|||||||
*/
|
*/
|
||||||
AppAuthLoginRespVO weixinMiniAppLogin3(String encryptedData, String iv, String openid);
|
AppAuthLoginRespVO weixinMiniAppLogin3(String encryptedData, String iv, String openid);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信公众号的一键登录
|
||||||
|
*
|
||||||
|
* @param code code
|
||||||
|
* @return 登录结果
|
||||||
|
*/
|
||||||
|
AppAuthLoginRespVO wechatAuth(String code);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获得社交认证 URL
|
* 获得社交认证 URL
|
||||||
|
|||||||
+42
@@ -35,6 +35,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
|
||||||
|
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.mp.api.WxMpService;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
@@ -271,6 +273,46 @@ public class MemberAuthServiceImpl implements MemberAuthService {
|
|||||||
return appAuthLoginRespVO;
|
return appAuthLoginRespVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信公众号登录
|
||||||
|
* @param code code
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public AppAuthLoginRespVO wechatAuth(String code) {
|
||||||
|
WxOAuth2AccessToken wxMpOAuth2AccessToken = null;
|
||||||
|
MemberUserDO memberUserDO = null;
|
||||||
|
try {
|
||||||
|
wxMpOAuth2AccessToken = mpService.getOAuth2Service().getAccessToken(code);
|
||||||
|
WxOAuth2UserInfo wxMpUser = mpService.getOAuth2Service().getUserInfo(wxMpOAuth2AccessToken, null);
|
||||||
|
String openid = wxMpUser.getOpenid();
|
||||||
|
//根据openid查用户是否存在
|
||||||
|
memberUserDO = userMapper.selectOne(new LambdaQueryWrapper<MemberUserDO>()
|
||||||
|
.eq(MemberUserDO::getOpenid,openid));
|
||||||
|
if (memberUserDO == null) {
|
||||||
|
// 获得获得注册用户
|
||||||
|
memberUserDO = userService.createUserIfAbsent("", getClientIP(),
|
||||||
|
LoginTypeEnum.WECHAT.getValue());
|
||||||
|
//过滤掉表情
|
||||||
|
String nickname = wxMpUser.getNickname();
|
||||||
|
memberUserDO.setOpenid(openid);
|
||||||
|
memberUserDO.setNickname(nickname);
|
||||||
|
memberUserDO.setAvatar(wxMpUser.getHeadImgUrl());
|
||||||
|
memberUserDO.setUsername(openid);
|
||||||
|
userMapper.updateById(memberUserDO);
|
||||||
|
}
|
||||||
|
}catch (WxErrorException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
throw exception(MINI_AUTH_LOGIN_BAD);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建 Token 令牌,记录登录日志
|
||||||
|
AppAuthLoginRespVO appAuthLoginRespVO = createTokenAfterLoginSuccess(memberUserDO, memberUserDO.getNickname(),
|
||||||
|
LoginLogTypeEnum.LOGIN_SOCIAL);
|
||||||
|
appAuthLoginRespVO.setOpenId(memberUserDO.getOpenid());
|
||||||
|
appAuthLoginRespVO.setUserInfo(UserConvert.INSTANCE.convert3(memberUserDO));
|
||||||
|
return appAuthLoginRespVO;
|
||||||
|
}
|
||||||
|
|
||||||
private AppAuthLoginRespVO createTokenAfterLoginSuccess(MemberUserDO user, String mobile, LoginLogTypeEnum logType) {
|
private AppAuthLoginRespVO createTokenAfterLoginSuccess(MemberUserDO user, String mobile, LoginLogTypeEnum logType) {
|
||||||
// 插入登陆日志
|
// 插入登陆日志
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ onShow(() => {
|
|||||||
// 检查用户登录情况
|
// 检查用户登录情况
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
if(isWeixin()){
|
if(isWeixin()){
|
||||||
|
oAuth()
|
||||||
// H5编译的代码
|
// H5编译的代码
|
||||||
// 判断是否是微信浏览器
|
// 判断是否是微信浏览器
|
||||||
}
|
}
|
||||||
@@ -55,6 +55,56 @@ const wechatMiniLogin = () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getAuthUrl = (appId) => {
|
||||||
|
// #ifdef H5
|
||||||
|
// #endif
|
||||||
|
cookie.set('redirect', window.location.href)
|
||||||
|
const url = `${location.origin}/h5/#/pages/index/index`
|
||||||
|
cookie.set('index_url',url)
|
||||||
|
let redirect_uri = encodeURIComponent(url)
|
||||||
|
|
||||||
|
const state = 'STATE'
|
||||||
|
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
|
||||||
|
}
|
||||||
|
|
||||||
|
const oAuth = async() => {
|
||||||
|
const WX_AUTH = 'wx_auth'
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
|
const accessToken = uni.getStorageSync('accessToken');
|
||||||
|
if (cookie.has('wx_auth') && accessToken && main.isLogin) {
|
||||||
|
reject()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const { code } = parseQuery()
|
||||||
|
if (!code) {
|
||||||
|
//公众号appid
|
||||||
|
const appid = APP_ID;
|
||||||
|
location.href = getAuthUrl(appid)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
auth(code)
|
||||||
|
}
|
||||||
|
resolve()
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const auth = async(code) => {
|
||||||
|
console.log('获取微信授权:',code)
|
||||||
|
let data = await wechatAuth({'code':code})
|
||||||
|
cookie.set('wx_auth', code)
|
||||||
|
if (data) {
|
||||||
|
main.SET_OPENID(data.openId)
|
||||||
|
main.SET_MEMBER(data.userInfo);
|
||||||
|
main.SET_TOKEN(data.accessToken);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ export const VUE_APP_API_URL = 'http://localhost:48081/app-api'
|
|||||||
//export const VUE_APP_API_URL = 'https://apidc.yixiang.co/app-api'
|
//export const VUE_APP_API_URL = 'https://apidc.yixiang.co/app-api'
|
||||||
export const VUE_APP_RESOURCES_URL = 'https://h5.yixiang.co/static'
|
export const VUE_APP_RESOURCES_URL = 'https://h5.yixiang.co/static'
|
||||||
export const VUE_APP_UPLOAD_URL = VUE_APP_API_URL + '/infra/file/upload'
|
export const VUE_APP_UPLOAD_URL = VUE_APP_API_URL + '/infra/file/upload'
|
||||||
export const APP_ID = 'wxc061dee8806ff712'
|
export const APP_ID = 'wxdbdbc123c8c30b45'
|
||||||
|
|
||||||
const orderListStatus = {}
|
const orderListStatus = {}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user