|
|
@ -1,12 +1,16 @@ |
|
|
package org.jeecg.api.service.impl; |
|
|
package org.jeecg.api.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
|
import org.jeecg.api.Final.UserType; |
|
|
import org.jeecg.api.bean.LoginReq; |
|
|
import org.jeecg.api.bean.LoginReq; |
|
|
import org.jeecg.api.req.UserInfoReq; |
|
|
import org.jeecg.api.req.UserInfoReq; |
|
|
import org.jeecg.api.service.AppletLoginService; |
|
|
import org.jeecg.api.service.AppletLoginService; |
|
|
import org.jeecg.api.untils.HttpConf; |
|
|
import org.jeecg.api.untils.HttpConf; |
|
|
|
|
|
import org.jeecg.api.wxUtils.WxHttpClientUtil; |
|
|
|
|
|
import org.jeecg.api.wxUtils.WxHttpUtils; |
|
|
import org.jeecg.common.api.vo.Result; |
|
|
import org.jeecg.common.api.vo.Result; |
|
|
import org.jeecg.common.constant.CommonConstant; |
|
|
import org.jeecg.common.constant.CommonConstant; |
|
|
import org.jeecg.common.exception.JeecgBootException; |
|
|
import org.jeecg.common.exception.JeecgBootException; |
|
|
@ -38,6 +42,9 @@ import java.util.*; |
|
|
@Service |
|
|
@Service |
|
|
public class AppletLoginServiceImpl implements AppletLoginService { |
|
|
public class AppletLoginServiceImpl implements AppletLoginService { |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private WxHttpUtils wxHttpUtils; |
|
|
|
|
|
|
|
|
@Value("${wechat.mpAppId}") |
|
|
@Value("${wechat.mpAppId}") |
|
|
private String mpAppId; |
|
|
private String mpAppId; |
|
|
@Value("${wechat.mpAppSecret}") |
|
|
@Value("${wechat.mpAppSecret}") |
|
|
@ -74,45 +81,99 @@ public class AppletLoginServiceImpl implements AppletLoginService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 (UserType.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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 微信小程序登录接口 |
|
|
* 微信小程序登录接口 |
|
|
|
|
|
* |
|
|
* @param loginReq |
|
|
* @param loginReq |
|
|
|
|
|
* @param type |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
@Transactional(rollbackFor = {Exception.class}) |
|
|
@Transactional(rollbackFor = {Exception.class}) |
|
|
@Override |
|
|
@Override |
|
|
public Result<Object> appletLogin(LoginReq loginReq) { |
|
|
|
|
|
|
|
|
public Result<Object> appletLogin(LoginReq loginReq, String type) { |
|
|
|
|
|
|
|
|
|
|
|
JSONObject json_test = openLogin(loginReq.getCode(), type); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Result<Object> result = new Result<>(); |
|
|
Result<Object> result = new Result<>(); |
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
if (StringUtils.isBlank(loginReq.getCode())) { |
|
|
|
|
|
throw new JeecgBootException("小程序code为空"); |
|
|
|
|
|
} |
|
|
|
|
|
String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mpAppId + "&secret=" + mpAppSecret+ "&js_code=" + loginReq.getCode() + "&grant_type=authorization_code"; |
|
|
|
|
|
//使用工具类 |
|
|
|
|
|
JSONObject json_test = httpConf.getJSONObject(loginUrl); |
|
|
|
|
|
|
|
|
// if (StringUtils.isBlank(loginReq.getCode())) { |
|
|
|
|
|
// throw new JeecgBootException("小程序code为空"); |
|
|
|
|
|
// } |
|
|
|
|
|
// String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + mpAppId + "&secret=" + mpAppSecret+ "&js_code=" + loginReq.getCode() + "&grant_type=authorization_code"; |
|
|
|
|
|
// //使用工具类 |
|
|
|
|
|
// JSONObject json_test = httpConf.getJSONObject(loginUrl); |
|
|
|
|
|
|
|
|
String wxOpenid = json_test.getString("openid"); |
|
|
String wxOpenid = json_test.getString("openid"); |
|
|
String sessionKey = json_test.getString("session_key"); |
|
|
String sessionKey = json_test.getString("session_key"); |
|
|
|
|
|
String unionid = json_test.getString("unionid"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(wxOpenid)) { |
|
|
if (StringUtils.isBlank(wxOpenid)) { |
|
|
throw new JeecgBootException("未获取到openid"); |
|
|
throw new JeecgBootException("未获取到openid"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Random random = new Random(); |
|
|
Random random = new Random(); |
|
|
int randomNumber = 100000 + random.nextInt(900000); |
|
|
int randomNumber = 100000 + random.nextInt(900000); |
|
|
|
|
|
|
|
|
HanHaiMember member = memberService.lambdaQuery().eq(HanHaiMember::getAppletOpenid,wxOpenid).one(); |
|
|
|
|
|
|
|
|
HanHaiMember member = memberService.lambdaQuery() |
|
|
|
|
|
.eq(UserType.APPLET.equals(type), HanHaiMember::getAppletOpenid, wxOpenid) |
|
|
|
|
|
.eq(UserType.OFFICIAL.equals(type), HanHaiMember::getOfficialOpenid, wxOpenid) |
|
|
|
|
|
.or() |
|
|
|
|
|
.eq(StringUtils.isNoneEmpty(unionid), HanHaiMember::getWxUnionid, unionid) |
|
|
|
|
|
.one(); |
|
|
if (member == null) { |
|
|
if (member == null) { |
|
|
//如果user等于null说明该用户第一次登录,数据库没有该用户信息。 |
|
|
//如果user等于null说明该用户第一次登录,数据库没有该用户信息。 |
|
|
loginReq.setOpenid(wxOpenid); |
|
|
loginReq.setOpenid(wxOpenid); |
|
|
loginReq.setSession_key(sessionKey); |
|
|
loginReq.setSession_key(sessionKey); |
|
|
member = new HanHaiMember(); |
|
|
member = new HanHaiMember(); |
|
|
member.setAppletOpenid(wxOpenid); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (UserType.OFFICIAL.equals(type)){ |
|
|
|
|
|
member.setOfficialOpenid(wxOpenid);// |
|
|
|
|
|
}else { |
|
|
|
|
|
member.setAppletOpenid(wxOpenid); |
|
|
|
|
|
} |
|
|
member.setHeadImage(loginReq.getHeadimgurl()); |
|
|
member.setHeadImage(loginReq.getHeadimgurl()); |
|
|
|
|
|
|
|
|
// 生成token返回给小程序端 |
|
|
|
|
|
String token = JwtUtil.sign(member.getAppletOpenid(), wxOpenid); |
|
|
|
|
|
member.setIntentioCode(String.valueOf(randomNumber)); |
|
|
member.setIntentioCode(String.valueOf(randomNumber)); |
|
|
|
|
|
|
|
|
memberService.save(member); |
|
|
memberService.save(member); |
|
|
|
|
|
|
|
|
|
|
|
// 生成token返回给小程序端 |
|
|
|
|
|
String token = JwtUtil.sign(member.getId(), member.getId()); |
|
|
|
|
|
|
|
|
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); |
|
|
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); |
|
|
// 设置超时时间 |
|
|
// 设置超时时间 |
|
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100); |
|
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100); |
|
|
@ -124,7 +185,7 @@ public class AppletLoginServiceImpl implements AppletLoginService { |
|
|
return result; |
|
|
return result; |
|
|
} else { |
|
|
} else { |
|
|
// 生成token返回给小程序端 |
|
|
// 生成token返回给小程序端 |
|
|
String token = JwtUtil.sign(member.getAppletOpenid(), wxOpenid); |
|
|
|
|
|
|
|
|
String token = JwtUtil.sign(member.getId(), member.getId()); |
|
|
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); |
|
|
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token); |
|
|
// 设置超时时间 |
|
|
// 设置超时时间 |
|
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100); |
|
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 100); |
|
|
@ -334,7 +395,16 @@ public class AppletLoginServiceImpl implements AppletLoginService { |
|
|
return Result.error("发送验证码异常:" + e.getMessage()); |
|
|
return Result.error("发送验证码异常:" + e.getMessage()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public Result<Map<String, Object>> getSignPackage(String url) { |
|
|
|
|
|
try { |
|
|
|
|
|
return Result.ok(wxHttpUtils.getSignPackage(url)); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 生成6位数字验证码 |
|
|
* 生成6位数字验证码 |
|
|
*/ |
|
|
*/ |
|
|
|