|
|
@@ -1,16 +1,23 @@
|
|
|
package com.izouma.walkchina.service;
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
|
|
import com.izouma.walkchina.domain.UserInfo;
|
|
|
import com.izouma.walkchina.exception.ServiceException;
|
|
|
import com.izouma.walkchina.repo.UserInfoRepository;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Example;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@@ -28,7 +35,7 @@ public class UserInfoService implements UserDetailsService {
|
|
|
return userInfoRepository.findOne(Example.of(userInfo)).orElse(null);
|
|
|
}
|
|
|
|
|
|
- public UserInfo registerByUserPwd(String username, String password) throws ServiceException {
|
|
|
+ public UserInfo registerByUserPwd(String username, String password) {
|
|
|
UserInfo userInfo = userInfoRepository.findOne(Example.of(UserInfo.builder().username(username).build())).orElse(null);
|
|
|
if (userInfo != null) {
|
|
|
throw new ServiceException("该用户已存在");
|
|
|
@@ -40,4 +47,47 @@ public class UserInfoService implements UserDetailsService {
|
|
|
userInfo = userInfoRepository.save(userInfo);
|
|
|
return userInfo;
|
|
|
}
|
|
|
+
|
|
|
+ public UserInfo loginMiniApp(String code) {
|
|
|
+ try {
|
|
|
+ WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
|
|
|
+ String openId = result.getOpenid();
|
|
|
+ String sessionKey = result.getSessionKey();
|
|
|
+ UserInfo userInfo = userInfoRepository.findByOpenId(openId).orElse(null);
|
|
|
+ if (userInfo != null) {
|
|
|
+ return userInfo;
|
|
|
+ }
|
|
|
+ userInfo = UserInfo.builder()
|
|
|
+ .username(UUID.randomUUID().toString())
|
|
|
+ .nickname("用户" + RandomStringUtils.randomAlphabetic(6))
|
|
|
+ .openId(openId)
|
|
|
+ .avatar("https://microball.oss-cn-hangzhou.aliyuncs.com/awesomeAdmin/user.png")
|
|
|
+ .sex(0)
|
|
|
+ .active(true)
|
|
|
+ .sessionKey(sessionKey)
|
|
|
+ .build();
|
|
|
+ return userInfoRepository.save(userInfo);
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ throw new ServiceException("登录失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserInfo getWxUserInfo(String sessionKey, String rawData, String signature,
|
|
|
+ String encryptedData, String iv) {
|
|
|
+ // 用户信息校验
|
|
|
+ if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
|
|
|
+ throw new ServiceException("获取用户信息失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解密用户信息
|
|
|
+ WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
|
|
+ UserInfo userInfo = userInfoRepository.findByOpenId(wxUserInfo.getOpenId()).orElse(null);
|
|
|
+ if (userInfo == null) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+ throw new ServiceException("获取用户信息失败");
|
|
|
+ }
|
|
|
}
|