|
@@ -1,5 +1,8 @@
|
|
|
package com.izouma.awesomeadmin.service.impl;
|
|
package com.izouma.awesomeadmin.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.binarywang.wx.miniapp.api.WxMaService;
|
|
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|
|
|
|
+import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
|
|
import com.izouma.awesomeadmin.constant.AppConstant;
|
|
import com.izouma.awesomeadmin.constant.AppConstant;
|
|
|
import com.izouma.awesomeadmin.dao.*;
|
|
import com.izouma.awesomeadmin.dao.*;
|
|
|
import com.izouma.awesomeadmin.dto.Page;
|
|
import com.izouma.awesomeadmin.dto.Page;
|
|
@@ -18,11 +21,13 @@ import io.jsonwebtoken.Jwts;
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
import io.rong.RongCloud;
|
|
import io.rong.RongCloud;
|
|
|
import io.rong.models.SMSVerifyCodeResult;
|
|
import io.rong.models.SMSVerifyCodeResult;
|
|
|
|
|
+import org.apache.commons.lang.RandomStringUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.log4j.Logger;
|
|
import org.apache.log4j.Logger;
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.SecretKey;
|
|
@@ -59,6 +64,11 @@ public class UserInfoServiceImpl implements UserInfoService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private RecommenderLogMapper recommenderLogMapper;
|
|
private RecommenderLogMapper recommenderLogMapper;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ @Lazy
|
|
|
|
|
+ private WxMaService wxMaService;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public List<UserInfo> getUserInfoList(UserInfo record) {
|
|
public List<UserInfo> getUserInfoList(UserInfo record) {
|
|
|
|
|
|
|
@@ -591,5 +601,92 @@ public class UserInfoServiceImpl implements UserInfoService {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public UserInfo loginMiniApp(String code) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
|
|
|
|
|
+ String openId = result.getOpenid();
|
|
|
|
|
+ String sessionKey = result.getSessionKey();
|
|
|
|
|
+
|
|
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
|
|
+ userInfo.setOpenId(openId);
|
|
|
|
|
+ userInfo = getUserInfo(userInfo);
|
|
|
|
|
+
|
|
|
|
|
+ if (userInfo != null) {
|
|
|
|
|
+ userInfo.setSessionKey(sessionKey);
|
|
|
|
|
+ updateUserInfo(userInfo);
|
|
|
|
|
+ return userInfo;
|
|
|
|
|
+ }
|
|
|
|
|
+ userInfo = new UserInfo();
|
|
|
|
|
+ userInfo.setUsername(UUID.randomUUID().toString());
|
|
|
|
|
+ userInfo.setNickname("用户" + RandomStringUtils.randomAlphabetic(6));
|
|
|
|
|
+ userInfo.setOpenId(openId);
|
|
|
|
|
+ userInfo.setIcon("https://microball.oss-cn-hangzhou.aliyuncs.com/awesomeAdmin/user.png");
|
|
|
|
|
+ userInfo.setSessionKey(sessionKey);
|
|
|
|
|
+
|
|
|
|
|
+ createUserInfo(userInfo);
|
|
|
|
|
+ return userInfo;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Result getMiniAppUserInfo(String sessionKey, String rawData, String signature,
|
|
|
|
|
+ String encryptedData, String iv) {
|
|
|
|
|
+ // 用户信息校验
|
|
|
|
|
+ if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
|
|
|
|
|
+ return new Result(false, "获取用户信息失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 解密用户信息
|
|
|
|
|
+ WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
|
|
|
|
+
|
|
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
|
|
+ userInfo.setOpenId(wxUserInfo.getOpenId());
|
|
|
|
|
+ userInfo = getUserInfo(userInfo);
|
|
|
|
|
+
|
|
|
|
|
+ String avatarUrl = "https://microball.oss-cn-hangzhou.aliyuncs.com/awesomeAdmin/user.png";
|
|
|
|
|
+ try {
|
|
|
|
|
+ avatarUrl = saveAvatar(wxUserInfo.getAvatarUrl());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ logger.error("获取头像失败", e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (userInfo == null) {
|
|
|
|
|
+
|
|
|
|
|
+ userInfo = new UserInfo();
|
|
|
|
|
+ userInfo.setUsername(UUID.randomUUID().toString());
|
|
|
|
|
+ userInfo.setNickname(wxUserInfo.getNickName());
|
|
|
|
|
+ userInfo.setOpenId(wxUserInfo.getOpenId());
|
|
|
|
|
+ userInfo.setUnionId(wxUserInfo.getUnionId());
|
|
|
|
|
+ userInfo.setIcon(avatarUrl);
|
|
|
|
|
+ userInfo.setSex(Integer.valueOf(wxUserInfo.getGender()) == 1 ? "男" : "女");
|
|
|
|
|
+ userInfo.setCountry(wxUserInfo.getCountry());
|
|
|
|
|
+ userInfo.setProvince(wxUserInfo.getProvince());
|
|
|
|
|
+ userInfo.setCity(wxUserInfo.getCity());
|
|
|
|
|
+
|
|
|
|
|
+ createUserInfo(userInfo);
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ userInfo.setIcon(avatarUrl);
|
|
|
|
|
+ userInfo.setNickname(wxUserInfo.getNickName());
|
|
|
|
|
+ userInfo.setUnionId(wxUserInfo.getUnionId());
|
|
|
|
|
+ userInfo.setSex(Integer.valueOf(wxUserInfo.getGender()) == 1 ? "男" : "女");
|
|
|
|
|
+ userInfo.setCountry(wxUserInfo.getCountry());
|
|
|
|
|
+ userInfo.setProvince(wxUserInfo.getProvince());
|
|
|
|
|
+ userInfo.setCity(wxUserInfo.getCity());
|
|
|
|
|
+ updateUserInfo(userInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (userInfo != null) {
|
|
|
|
|
+ return new Result(true, userInfo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return new Result(false, "获取用户信息失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|