修改登录逻辑

master
382696293@qq.com 2 years ago
parent ca906052ec
commit a8d5031821

@ -17,7 +17,7 @@ public class WxUserMember extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* $column.columnComment
* id
*/
private Long id;

@ -149,10 +149,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="isAbutment != null">#{isAbutment},</if>
<if test="collagenDay != null">#{collagenDay},</if>
<if test="collagenMount != null">#{collagenMount},</if>
<if test="loginTime != null">#{loginTime},</if>
sysdate(),
<if test="status != null">#{status},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
sysdate(),
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>

@ -0,0 +1,69 @@
package com.flossom.miniProgram.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author flossom
*/
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "wx.config")
public class WxConfig {
/**
* appid
*/
private String appId;
/**
* appkey
*/
private String appKey;
/**
* jscode2session
* code openid session_key
*/
private String loginUrl;
/**
* code
*/
private String obtainPhoneUrl;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getLoginUrl() {
return loginUrl;
}
public void setLoginUrl(String loginUrl) {
this.loginUrl = loginUrl;
}
public String getObtainPhoneUrl() {
return obtainPhoneUrl;
}
public void setObtainPhoneUrl(String obtainPhoneUrl) {
this.obtainPhoneUrl = obtainPhoneUrl;
}
}

@ -17,18 +17,14 @@ public class UserMemberController extends BaseController {
/**
*
*
* @param code
* @param rawData
* @param signature 使 sha1( rawData + sessionkey )
* @param code
* @return
* @throws Exception
*/
@PostMapping("/login")
public AjaxResult login(
@RequestParam(value = "code") String code,
@RequestParam(value = "rawData") String rawData,
@RequestParam(value = "signature") String signature) throws Exception {
return AjaxResult.success(wxUserMemberService.login(code, rawData, signature));
@RequestParam(value = "code") String code) throws Exception {
return AjaxResult.success(wxUserMemberService.login(code));
}
/**
@ -54,4 +50,5 @@ public class UserMemberController extends BaseController {
wxUserMemberService.updateUser(userMemberUpdateVo);
return AjaxResult.success();
}
}

@ -1,40 +1,223 @@
package com.flossom.miniProgram.domain.vo;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.flossom.common.core.annotation.Excel;
public class LoginUserVo extends WxUserMember {
import java.math.BigDecimal;
import java.util.Date;
public class LoginUserVo {
/**
* id
*/
private Long id;
/**
*
*/
@Excel(name = "昵称")
private String nickname;
/**
*
*/
@Excel(name = "头像")
private String headimg;
/**
*
*/
@Excel(name = "姓名")
private String username;
/**
*
*/
@Excel(name = "积分")
private BigDecimal credit;
/**
*
*/
@Excel(name = "手机")
private String mobile;
/**
* id
*/
@Excel(name = "省id")
private String provinceId;
/**
*
*/
@Excel(name = "省")
private String province;
/**
*
* id
*/
private String accessToken;
@Excel(name = "市id")
private String cityId;
/**
*
*
*/
private String expiresIn;
@Excel(name = "市")
private String city;
/**
* id
*/
@Excel(name = "区id")
private String areaId;
/**
*
*/
@Excel(name = "区")
private String area;
/**
*
*/
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "生日", width = 30, dateFormat = "yyyy-MM-dd")
private Date birthday;
private String token;
public LoginUserVo() {
}
public LoginUserVo(String accessToken, String expiresIn) {
this.accessToken = accessToken;
this.expiresIn = expiresIn;
public LoginUserVo(Long id, String nickname, String headimg, String username, BigDecimal credit, String mobile, String provinceId, String province, String cityId, String city, String areaId, String area, Date birthday, String token) {
this.id = id;
this.nickname = nickname;
this.headimg = headimg;
this.username = username;
this.credit = credit;
this.mobile = mobile;
this.provinceId = provinceId;
this.province = province;
this.cityId = cityId;
this.city = city;
this.areaId = areaId;
this.area = area;
this.birthday = birthday;
this.token = token;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getHeadimg() {
return headimg;
}
public void setHeadimg(String headimg) {
this.headimg = headimg;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public BigDecimal getCredit() {
return credit;
}
public void setCredit(BigDecimal credit) {
this.credit = credit;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getProvinceId() {
return provinceId;
}
public void setProvinceId(String provinceId) {
this.provinceId = provinceId;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCityId() {
return cityId;
}
public void setCityId(String cityId) {
this.cityId = cityId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAreaId() {
return areaId;
}
public void setAreaId(String areaId) {
this.areaId = areaId;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getAccessToken() {
return accessToken;
public Date getBirthday() {
return birthday;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getExpiresIn() {
return expiresIn;
public String getToken() {
return token;
}
public void setExpiresIn(String expiresIn) {
this.expiresIn = expiresIn;
public void setToken(String token) {
this.token = token;
}
}

@ -6,9 +6,7 @@ import com.flossom.miniProgram.domain.vo.UserMemberUpdateVo;
public interface IWxUserMemberService {
LoginUserVo obtainUserMemberInfo(Long userNo);
LoginUserVo login(String code, String rawData, String signature) throws Exception;
LoginUserVo login(String code) throws Exception;
AjaxResult upgradeMember(String code) throws Exception;

@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class WxUserMemberServiceImpl implements IWxUserMemberService {
@ -49,84 +50,21 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
private RedisService redisService;
@Override
public LoginUserVo obtainUserMemberInfo(Long userNo) {
WxUserMember wxUserMember = new WxUserMember();
if (userNo != null) {
// 根据 userNo 查询用户信息
wxUserMember = wxUserMemberMapper.selectWxUserMemberById(userNo);
}
if (userNo == null || wxUserMember == null) {
// 未注册用户,就注册游客账户
// 微信用户表注册
wxUserMemberMapper.insertTouristUser(wxUserMember);
// 系统表注册
RegisterBody registerBody = new RegisterBody();
// registerBody.setUsername(UserConstants.WX_SYSTEM_USER_USERNAME + wxUserMember.getId());
registerBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
R<?> registerResult = remoteAuthService.register(registerBody);
if (R.FAIL == registerResult.getCode()) {
throw new ServiceException(registerResult.getMsg());
}
}
// 获取token
LoginBody loginBody = new LoginBody();
// loginBody.setUsername(UserConstants.WX_SYSTEM_USER_USERNAME + wxUserMember.getId());
loginBody.setPassword(UserConstants.WX_SYSTEM_USER_PASSWORD);
R<Map<String, String>> loginResult = remoteAuthService.login(loginBody);
if (loginResult.getCode() == R.FAIL) {
throw new ServiceException(loginResult.getMsg());
}
LoginUserVo loginUserVo = new LoginUserVo();
BeanUtils.copyProperties(wxUserMember, loginUserVo);
loginUserVo.setAccessToken(loginResult.getData().get("access_token"));
loginUserVo.setExpiresIn(loginResult.getData().get("expires_in"));
return loginUserVo;
}
@Override
public LoginUserVo login(String code, String rawData, String signature) throws Exception {
// 1、使用临时凭证 code 获取 appi + appsecret + code
public LoginUserVo login(String code) throws Exception {
// 1、使用临时凭证 code 获取 appi + appsecret + unionid
WxCode2SessionRet wxCode2SessionRet = JSON.parseObject(MiniProgramUtils.getSessionKeyAndOpenId(code), WxCode2SessionRet.class);
if (200 != wxCode2SessionRet.getErrcode()) {
logger.error(wxCode2SessionRet.getErrmsg());
throw new ServiceException("校验失败");
}
// 2、校验签名 小程序发送的签名signature与服务器端生成的签名signature2 = sha1(rawData + sessionKey)
String signature2 = DigestUtils.sha1Hex(rawData + wxCode2SessionRet.getSession_key());
if (!signature.equals(signature2)) {
logger.error("签名校验失败code = {}", code);
throw new ServiceException("校验失败");
}
// 3、根据 openid 查询用户信息
// 2、根据 openid 查询用户信息
WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberByOpenId(wxCode2SessionRet.getOpenid());
if (wxUserMember == null) {
// 未注册用户,就注册游客账户
// 2.1、封装微信用户信息
/**
* rawData:
* {
* "nickName": "Band",
* "avatarUrl": "http://wx.qlogo.cn/mmopen/vi_32/1vZvI39NWFQ9XM4LtQpFrQJ1xlgZxx3w7bQxKARol6503Iuswjjn6nIGBiaycAjAtpujxyzYsrztuuICqIM5ibXQ/0"
* "gender": 1,//不再返回
* "language": "zh_CN", //强制返回 “zh_CN”
* "city": "Guangzhou", //不再返回
* "province": "Guangdong", //不再返回
* "country": "CN", //不再返回
* }
*/
JSONObject rawDataJson = JSON.parseObject(rawData);
wxUserMember.setNickname(rawDataJson.getString("nickName"));
wxUserMember.setHeadimg(rawDataJson.getString("avatarUrl"));
wxUserMember.setOpenid(wxCode2SessionRet.getOpenid());
wxUserMember.setUnionid(wxCode2SessionRet.getUnionid());
/**
* TODO: session_keyredis
*/
redisService.setCacheObject(CacheConstants.wx_session_key_cache + wxUserMember.getId(), wxCode2SessionRet.getSession_key());
// 3.2、微信用户表注册
wxUserMemberMapper.insertWxUserMember(wxUserMember);
// 3.3、系统表注册
@ -139,17 +77,22 @@ public class WxUserMemberServiceImpl implements IWxUserMemberService {
}
}
// 4、获取 token
// 3、登录请求获取 token
R<Map<String, String>> loginResult = remoteAuthService.wxLogin(wxUserMember);
if (loginResult.getCode() == R.FAIL) {
throw new ServiceException(loginResult.getMsg());
}
// 5、返回登录信息
/**
* TODO: session_keyredis
*/
redisService.setCacheObject(CacheConstants.wx_session_key_cache + wxUserMember.getOpenid(),
wxCode2SessionRet.getSession_key(), CacheConstants.EXPIRATION, TimeUnit.MINUTES);
// 4、返回登录信息
LoginUserVo loginUserVo = new LoginUserVo();
BeanUtils.copyProperties(wxUserMember, loginUserVo);
loginUserVo.setAccessToken(loginResult.getData().get("access_token"));
loginUserVo.setExpiresIn(loginResult.getData().get("expires_in"));
loginUserVo.setToken(loginResult.getData().get("access_token"));
return loginUserVo;
}

@ -1,21 +1,28 @@
package com.flossom.miniProgram.utils;
import com.flossom.miniProgram.config.properties.WxConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
*
*/
@Component
public class MiniProgramUtils {
private static final String WX_APPID = "";
private static final String WX_SECRET = "";
private static WxConfig wxConfig;
private static final String loginUrl = "https://api.weixin.qq.com/sns/jscode2session";
private static final String getPhoneUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
@Autowired
public void setWxConfig(WxConfig wxConfig) {
MiniProgramUtils.wxConfig = wxConfig;
}
public static String getSessionKeyAndOpenId(String jsCode) throws Exception {
return getSessionKeyAndOpenId(WX_APPID, WX_SECRET, jsCode, "authorization_code");
return getSessionKeyAndOpenId(wxConfig.getAppId(), wxConfig.getAppKey(), jsCode, "authorization_code");
}
/**
@ -46,7 +53,7 @@ public class MiniProgramUtils {
params.put("secret", secret);
params.put("jsCode", jsCode);
params.put("grant_type", grant_type);
return HttpClientUtils.getParameters(loginUrl, params);
return HttpClientUtils.getParameters(wxConfig.getLoginUrl(), params);
}
/**
@ -55,15 +62,15 @@ public class MiniProgramUtils {
* errcode number
* errmsg string
* phone_info object
*
* phoneNumber string
* purePhoneNumber string
* countryCode string
*
* phoneNumber string
* purePhoneNumber string
* countryCode string
*/
public static String getPhone(String code) throws Exception {
Map<String, String> params = new HashMap();
params.put("code", code);
return HttpClientUtils.postParameters(getPhoneUrl, params);
return HttpClientUtils.postParameters(wxConfig.getObtainPhoneUrl(), params);
}

Loading…
Cancel
Save