|
@@ -26,6 +26,7 @@ import com.example.xiaoshiweixinback.okhttp.ResponseManager;
|
|
|
import com.example.xiaoshiweixinback.service.common.SmsService;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -35,14 +36,24 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Service
|
|
|
public class LoginService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private Environment environment;
|
|
|
+ @Value("${WeChat.appId}")
|
|
|
+ private String appId;
|
|
|
+
|
|
|
+ @Value("${WeChat.appSecret}")
|
|
|
+ private String appSecret;
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private Environment environment;
|
|
|
|
|
|
@Autowired
|
|
|
private JwtTokenUtil jwtTokenUtil;
|
|
@@ -58,6 +69,7 @@ public class LoginService {
|
|
|
|
|
|
/**
|
|
|
* 手机号/账号登录
|
|
|
+ *
|
|
|
* @param dto
|
|
|
* @return
|
|
|
*/
|
|
@@ -110,19 +122,20 @@ public class LoginService {
|
|
|
BeanUtil.copy(person, loginVO);
|
|
|
}
|
|
|
loginVO.setToken(this.getToken());
|
|
|
- redisService.set(AppCacheKeyUtil.getUserIdToken(person.getId()),loginVO.getToken());
|
|
|
+ redisService.set(AppCacheKeyUtil.getUserIdToken(person.getId()), loginVO.getToken());
|
|
|
LogHelper.log("登陆结束");
|
|
|
return loginVO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信小程序登录
|
|
|
+ *
|
|
|
+ * @param wxDTO
|
|
|
+ * @throws Exception
|
|
|
* @title: loginByWeChat
|
|
|
* @author: zero
|
|
|
* @date: 2024/04/01
|
|
|
- * @param wxDTO
|
|
|
* @return: LoginByWxVO
|
|
|
- * @throws Exception
|
|
|
*/
|
|
|
public LoginByWxVO loginByWeChat(LoginByWxDTO wxDTO) throws Exception {
|
|
|
String code = wxDTO.getCode();
|
|
@@ -131,50 +144,41 @@ public class LoginService {
|
|
|
|
|
|
//返回数据
|
|
|
LoginByWxVO wxVO = new LoginByWxVO();
|
|
|
- String appId = environment.getProperty("weChat.appId");
|
|
|
- String appSecret = environment.getProperty("weChat.appSecret");
|
|
|
- //1.根据code 获取微信小程序的openid和session_key
|
|
|
- HashMap<String, String> map = new HashMap<String, String>();
|
|
|
- map.put("appid", appId);
|
|
|
- map.put("secret", appSecret);
|
|
|
- map.put("js_code", code);
|
|
|
- map.put("grant_type", "authorization_code");
|
|
|
- String result = RequestManager.getInstance().requestPostBySynWithForm("https://api.weixin.qq.com/sns/jscode2session", map);
|
|
|
- Jscode2SessionWo jscode2SessionWo = ResponseManager.parseObject(result, Jscode2SessionWo.class);
|
|
|
+// //1.根据code 获取微信小程序的openid和session_key
|
|
|
+ JSONObject result = this.getSessionKeyAndOpenId(code);
|
|
|
+ Jscode2SessionWo jscode2SessionWo = ResponseManager.parseObject(result.toString(), Jscode2SessionWo.class);
|
|
|
+// HashMap<String, String> map = new HashMap<String, String>();
|
|
|
+// map.put("appid", appId);
|
|
|
+// map.put("secret", appSecret);
|
|
|
+// map.put("js_code", code);
|
|
|
+// map.put("grant_type", "authorization_code");
|
|
|
+// String result = RequestManager.getInstance().requestPostBySynWithForm("https://api.weixin.qq.com/sns/jscode2session", map);
|
|
|
+// Jscode2SessionWo jscode2SessionWo = ResponseManager.parseObject(result, Jscode2SessionWo.class);
|
|
|
if (ToolUtil.isNotEmpty(jscode2SessionWo)) {
|
|
|
// 2. 解密用户数据
|
|
|
String decryptedData = decrypt(encryptedData, jscode2SessionWo.getSession_key(), iv);
|
|
|
|
|
|
// 3. 获取用户手机号(需要用户授权)
|
|
|
String phoneNumber = "";
|
|
|
- String name = "";
|
|
|
- String fileGuid = "";
|
|
|
JSONObject userData = JSONObject.parseObject(decryptedData);
|
|
|
if (ToolUtil.isNotEmpty(userData) && userData.containsKey("purePhoneNumber")) {
|
|
|
phoneNumber = userData.getString("purePhoneNumber");
|
|
|
}
|
|
|
- if (ToolUtil.isNotEmpty(userData) && userData.containsKey("nickname")) {
|
|
|
- name = userData.getString("nickname");
|
|
|
- }
|
|
|
- if (ToolUtil.isNotEmpty(userData) && userData.containsKey("avatarUrl")) {
|
|
|
- fileGuid = userData.getString("avatarUrl");
|
|
|
- }
|
|
|
|
|
|
// 4.查询数据表
|
|
|
Person person = personMapper.selectOne(new LambdaQueryWrapper<Person>()
|
|
|
.eq(Person::getOpenId, jscode2SessionWo.getOpenid()));
|
|
|
if (ToolUtil.isNotEmpty(person)) {
|
|
|
- BeanUtil.copy(person,wxVO);
|
|
|
+ BeanUtil.copy(person, wxVO);
|
|
|
} else {
|
|
|
//添加用户表中
|
|
|
person = personMapper.selectOne(new LambdaQueryWrapper<Person>()
|
|
|
.eq(Person::getPhoneNum, phoneNumber));
|
|
|
if (ToolUtil.isNotEmpty(person)) {
|
|
|
+ person.setOpenId(jscode2SessionWo.getOpenid());
|
|
|
person.updateById();
|
|
|
} else {
|
|
|
person = new Person();
|
|
|
- person.setName(name);
|
|
|
- person.setFileGuid(fileGuid);
|
|
|
person.setPhoneNum(phoneNumber);
|
|
|
person.setOpenId(jscode2SessionWo.getOpenid());
|
|
|
String uid = IdUtil.simpleUUID();
|
|
@@ -184,18 +188,17 @@ public class LoginService {
|
|
|
wxVO.setIfFirst(true);
|
|
|
}
|
|
|
wxVO.setPhoneNum(phoneNumber);
|
|
|
- wxVO.setName(name);
|
|
|
- wxVO.setFileGuid(fileGuid);
|
|
|
}
|
|
|
wxVO.setToken(this.getToken());
|
|
|
wxVO.setOpenId(jscode2SessionWo.getOpenid());
|
|
|
- redisService.set(AppCacheKeyUtil.getUserIdToken(wxVO.getId()),wxVO.getToken());
|
|
|
+ redisService.set(AppCacheKeyUtil.getUserIdToken(wxVO.getId()), wxVO.getToken());
|
|
|
}
|
|
|
return wxVO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发送验证码
|
|
|
+ *
|
|
|
* @param vo
|
|
|
* @return
|
|
|
*/
|
|
@@ -208,13 +211,13 @@ public class LoginService {
|
|
|
if (ToolUtil.isEmpty(checkCode)) {
|
|
|
throw new BusinessException(ExceptionEnum.VERIFY_CODE);
|
|
|
}
|
|
|
- if (ToolUtil.isNotEmpty(checkCode.toString()) && ToolUtil.equals(checkCode.toString(),vo.getCheckCode())) {
|
|
|
+ if (ToolUtil.isNotEmpty(checkCode.toString()) && ToolUtil.equals(checkCode.toString(), vo.getCheckCode())) {
|
|
|
//删除校验码
|
|
|
redisService.delete(AppCacheKeyUtil.getCheckCode(vo.getPhoneNum()));
|
|
|
//生成验证码
|
|
|
String random = RandomUtil.getSixRandom();
|
|
|
//手机号和验证码放进缓存 设置过期时间5min
|
|
|
- redisService. set(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), random);
|
|
|
+ redisService.set(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), random);
|
|
|
redisService.expire(AppCacheKeyUtil.getLoginMessageCode(vo.getPhoneNum()), CacheTTLEnum.FIVE_MINUTE);
|
|
|
//发送短信
|
|
|
smsService.sendMessage(vo.getPhoneNum(), random);
|
|
@@ -248,6 +251,7 @@ public class LoginService {
|
|
|
|
|
|
/**
|
|
|
* 查询个人信息
|
|
|
+ *
|
|
|
* @param vo
|
|
|
* @return
|
|
|
*/
|
|
@@ -260,10 +264,11 @@ public class LoginService {
|
|
|
|
|
|
/**
|
|
|
* 修改个人信息
|
|
|
+ *
|
|
|
* @param vo
|
|
|
* @return
|
|
|
*/
|
|
|
- @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
|
|
|
+ @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
|
|
|
public boolean editPerson(EditPersonDTO vo) {
|
|
|
if (!RegexUtil.isPhoneLegal(vo.getPhoneNum())) {
|
|
|
throw new BusinessException(ExceptionEnum.PHONE_FORMAT_ERROR);
|
|
@@ -304,6 +309,44 @@ public class LoginService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 根据 code 获取 session_key 和 openId
|
|
|
+ *
|
|
|
+ * @param code 小程序登录时获取的 code
|
|
|
+ * @return session_key 和 openId
|
|
|
+ */
|
|
|
+ public JSONObject getSessionKeyAndOpenId(String code) {
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code";
|
|
|
+
|
|
|
+ JSONObject jsonObject = null;
|
|
|
+ try {
|
|
|
+ // 发送请求
|
|
|
+ URL requestUrl = new URL(url);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.connect();
|
|
|
+
|
|
|
+ // 读取响应
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
|
|
|
+ StringBuffer buffer = new StringBuffer();
|
|
|
+ String temp = null;
|
|
|
+ while ((temp = bufferedReader.readLine()) != null) {
|
|
|
+ buffer.append(temp);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析 JSON 数据
|
|
|
+ jsonObject = JSONObject.parseObject(buffer.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 解密用户数据
|
|
|
*
|
|
|
* @param encryptedData 包括敏感数据在内的完整用户信息的加密数据
|