|
|
@ -11,6 +11,7 @@ import org.jeecg.common.system.util.JwtUtil; |
|
|
import org.jeecg.common.system.vo.AppletUser; |
|
|
import org.jeecg.common.system.vo.AppletUser; |
|
|
import org.jeecg.common.api.IAppletUserService; |
|
|
import org.jeecg.common.api.IAppletUserService; |
|
|
import org.jeecg.common.util.RedisUtil; |
|
|
import org.jeecg.common.util.RedisUtil; |
|
|
|
|
|
import org.jeecg.modules.applet.Final.LoginType; |
|
|
import org.jeecg.modules.common.wxUtils.WxHttpClientUtil; |
|
|
import org.jeecg.modules.common.wxUtils.WxHttpClientUtil; |
|
|
import org.jeecg.modules.common.wxUtils.WxHttpUtils; |
|
|
import org.jeecg.modules.common.wxUtils.WxHttpUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -41,6 +42,36 @@ public class AppletApiLoginService { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private RedisUtil redisUtil; |
|
|
private RedisUtil redisUtil; |
|
|
|
|
|
|
|
|
|
|
|
private JSONObject openLogin(String code, String type){ |
|
|
|
|
|
log.info("开始小程序登录,code: {}", code); |
|
|
|
|
|
// 调用微信API获取openid和session_key |
|
|
|
|
|
String loginUrl = "https://api.weixin.qq.com/sns/jscode2session"; |
|
|
|
|
|
Map<String, String> params = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
if (LoginType.OFFICIAL.equals(type)){ |
|
|
|
|
|
loginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token"; |
|
|
|
|
|
params.put("appid", wxHttpUtils.getOfficialAppid()); |
|
|
|
|
|
params.put("secret", wxHttpUtils.getOfficialSecret()); |
|
|
|
|
|
params.put("code", code); |
|
|
|
|
|
params.put("grant_type", "authorization_code"); |
|
|
|
|
|
}else { |
|
|
|
|
|
params.put("appid", wxHttpUtils.getAppid()); |
|
|
|
|
|
params.put("secret", wxHttpUtils.getSecret()); |
|
|
|
|
|
params.put("js_code", code); |
|
|
|
|
|
params.put("grant_type", "authorization_code"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String response = WxHttpClientUtil.doGet(loginUrl, params); |
|
|
|
|
|
|
|
|
|
|
|
JSONObject jsonResponse = JSON.parseObject(response); |
|
|
|
|
|
|
|
|
|
|
|
// 检查微信API返回结果 |
|
|
|
|
|
if (jsonResponse.containsKey("errcode") && jsonResponse.getInteger("errcode") != 0) { |
|
|
|
|
|
log.error("微信登录失败: {}", response); |
|
|
|
|
|
throw new JeecgBootException("微信登录失败: " + jsonResponse.getString("errmsg")); |
|
|
|
|
|
} |
|
|
|
|
|
return jsonResponse; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 小程序登录 |
|
|
* 小程序登录 |
|
|
@ -48,27 +79,11 @@ public class AppletApiLoginService { |
|
|
* @param code 微信登录code |
|
|
* @param code 微信登录code |
|
|
* @return 登录结果 |
|
|
* @return 登录结果 |
|
|
*/ |
|
|
*/ |
|
|
public Result<Map<String, Object>> login(String code,String inviter) { |
|
|
|
|
|
|
|
|
public Result<Map<String, Object>> login(String code, String inviter, String type) { |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
log.info("开始小程序登录,code: {}", code); |
|
|
|
|
|
// 调用微信API获取openid和session_key |
|
|
|
|
|
String loginUrl = "https://api.weixin.qq.com/sns/jscode2session"; |
|
|
|
|
|
Map<String, String> params = new HashMap<>(); |
|
|
|
|
|
params.put("appid", wxHttpUtils.getAppid()); |
|
|
|
|
|
params.put("secret", wxHttpUtils.getSecret()); |
|
|
|
|
|
params.put("js_code", code); |
|
|
|
|
|
params.put("grant_type", "authorization_code"); |
|
|
|
|
|
|
|
|
|
|
|
String response = WxHttpClientUtil.doGet(loginUrl, params); |
|
|
|
|
|
JSONObject jsonResponse = JSON.parseObject(response); |
|
|
|
|
|
|
|
|
|
|
|
// 检查微信API返回结果 |
|
|
|
|
|
if (jsonResponse.containsKey("errcode") && jsonResponse.getInteger("errcode") != 0) { |
|
|
|
|
|
log.error("微信登录失败: {}", response); |
|
|
|
|
|
return Result.error("微信登录失败: " + jsonResponse.getString("errmsg")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject jsonResponse = openLogin(code, type); |
|
|
|
|
|
|
|
|
String openid = jsonResponse.getString("openid"); |
|
|
String openid = jsonResponse.getString("openid"); |
|
|
String sessionKey = jsonResponse.getString("session_key"); |
|
|
String sessionKey = jsonResponse.getString("session_key"); |
|
|
String unionid = jsonResponse.getString("unionid"); |
|
|
String unionid = jsonResponse.getString("unionid"); |
|
|
@ -78,8 +93,11 @@ public class AppletApiLoginService { |
|
|
// 查找或创建用户 |
|
|
// 查找或创建用户 |
|
|
AppletUser appletUser = findOrCreateUser(openid, unionid, inviter); |
|
|
AppletUser appletUser = findOrCreateUser(openid, unionid, inviter); |
|
|
|
|
|
|
|
|
|
|
|
if (LoginType.OFFICIAL.equals(type)){ |
|
|
|
|
|
String access_token = jsonResponse.getString("access_token"); |
|
|
|
|
|
setUserInfo(appletUser, access_token); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 生成JWT token |
|
|
// 生成JWT token |
|
|
String token = JwtUtil.sign(appletUser.getOpenid(), appletUser.getId()); |
|
|
String token = JwtUtil.sign(appletUser.getOpenid(), appletUser.getId()); |
|
|
|
|
|
|
|
|
@ -87,7 +105,6 @@ public class AppletApiLoginService { |
|
|
// 设置超时时间 |
|
|
// 设置超时时间 |
|
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000); |
|
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建返回结果 |
|
|
// 构建返回结果 |
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
result.put("token", token); |
|
|
result.put("token", token); |
|
|
@ -104,6 +121,20 @@ public class AppletApiLoginService { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void setUserInfo(AppletUser user, String access_token) { |
|
|
|
|
|
//开始获取用户信息 |
|
|
|
|
|
String memberUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+user.getOfficialId()+"&lang=zh_CN"; |
|
|
|
|
|
|
|
|
|
|
|
String json = WxHttpClientUtil.doGet(memberUrl); |
|
|
|
|
|
|
|
|
|
|
|
//使用工具类 |
|
|
|
|
|
JSONObject json_member = JSONObject.parseObject(json); |
|
|
|
|
|
String nickname = json_member.getString("nickname"); |
|
|
|
|
|
String headimgurl = json_member.getString("headimgurl"); |
|
|
|
|
|
user.setName(nickname); |
|
|
|
|
|
user.setAvatar(headimgurl); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取用户手机号 |
|
|
* 获取用户手机号 |
|
|
* |
|
|
* |
|
|
@ -181,7 +212,7 @@ public class AppletApiLoginService { |
|
|
private AppletUser findOrCreateUser(String openid, String unionid, String inviter) { |
|
|
private AppletUser findOrCreateUser(String openid, String unionid, String inviter) { |
|
|
try { |
|
|
try { |
|
|
// 根据openid查找用户 |
|
|
// 根据openid查找用户 |
|
|
AppletUser user = appletUserService.getByOpenid(openid); |
|
|
|
|
|
|
|
|
AppletUser user = appletUserService.getByUnionId(openid, unionid); |
|
|
|
|
|
|
|
|
if (user == null) { |
|
|
if (user == null) { |
|
|
// 创建新用户 |
|
|
// 创建新用户 |
|
|
@ -193,8 +224,7 @@ public class AppletApiLoginService { |
|
|
user.setInviter(inviter); |
|
|
user.setInviter(inviter); |
|
|
user.setIsPromote("N"); |
|
|
user.setIsPromote("N"); |
|
|
|
|
|
|
|
|
// TODO: 如果需要unionid字段,需要在AppletUser实体类中添加该字段 |
|
|
|
|
|
// user.setUnionid(unionid); |
|
|
|
|
|
|
|
|
user.setUnionId(unionid); |
|
|
|
|
|
|
|
|
// 保存到数据库 |
|
|
// 保存到数据库 |
|
|
appletUserService.save(user); |
|
|
appletUserService.save(user); |
|
|
@ -268,4 +298,19 @@ public class AppletApiLoginService { |
|
|
return Result.error("获取失败: " + e.getMessage()); |
|
|
return Result.error("获取失败: " + e.getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 获取公众号签名 |
|
|
|
|
|
* |
|
|
|
|
|
* @param url URL |
|
|
|
|
|
* @return 结果 |
|
|
|
|
|
*/ |
|
|
|
|
|
public Result<Map<String, Object>> getSignPackage(String url) { |
|
|
|
|
|
try { |
|
|
|
|
|
return Result.ok(wxHttpUtils.getSignPackage(url)); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
log.error("获取公众号签名异常", e); |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |