调整小程序类命名方式,新增更新绑定用户用户名(手机号)功能
This commit is contained in:
parent
4982ba38ee
commit
708ed4ccba
@ -0,0 +1,41 @@
|
||||
package ink.wgink.login.wechat.controller.app;
|
||||
|
||||
import ink.wgink.annotation.CheckRequestBodyAnnotation;
|
||||
import ink.wgink.interfaces.consts.ISystemConstant;
|
||||
import ink.wgink.login.wechat.service.sign.IMiniappSignService;
|
||||
import ink.wgink.module.wechat.pojo.vos.miniapp.MiniappUpdatePhoneVO;
|
||||
import ink.wgink.pojo.result.ErrorResult;
|
||||
import ink.wgink.pojo.result.SuccessResultData;
|
||||
import io.swagger.annotations.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @ClassName: MiniappAppController
|
||||
* @Description: 小程序APP接口
|
||||
* @Author: wanggeng
|
||||
* @Date: 2021/8/2 8:19 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "小程序")
|
||||
@RestController
|
||||
@RequestMapping(ISystemConstant.APP_PREFIX + "/miniapp")
|
||||
public class MiniappAppController {
|
||||
|
||||
@Autowired
|
||||
private IMiniappSignService miniappSignService;
|
||||
|
||||
@ApiOperation(value = "更新电话", notes = "由用户主动授权更新手机号,返回新token")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(name = "token", value = "token", paramType = "header")
|
||||
})
|
||||
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
|
||||
@PutMapping("update-phone")
|
||||
@CheckRequestBodyAnnotation
|
||||
public SuccessResultData<String> updatePhone(@RequestHeader("token") String token,
|
||||
@RequestBody MiniappUpdatePhoneVO miniappUpdatePhoneVO) throws Exception {
|
||||
String newToken = miniappSignService.updatePhone(token, miniappUpdatePhoneVO);
|
||||
return new SuccessResultData<>(newToken);
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +1,17 @@
|
||||
package ink.wgink.login.wechat.controller.api.sign;
|
||||
package ink.wgink.login.wechat.controller.wechat.sign;
|
||||
|
||||
import ink.wgink.annotation.CheckRequestBodyAnnotation;
|
||||
import ink.wgink.exceptions.PropertiesException;
|
||||
import ink.wgink.interfaces.consts.ISystemConstant;
|
||||
import ink.wgink.login.wechat.pojo.vos.sign.MiniAppLoginVO;
|
||||
import ink.wgink.login.wechat.service.sign.IMiniAppSignService;
|
||||
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
|
||||
import ink.wgink.login.wechat.service.sign.IMiniappSignService;
|
||||
import ink.wgink.pojo.result.ErrorResult;
|
||||
import ink.wgink.pojo.result.SuccessResultData;
|
||||
import ink.wgink.properties.wechat.miniapp.MiniAppProperties;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -27,16 +31,18 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Api(tags = ISystemConstant.API_TAGS_WECHAT_MINI_APP_PREFIX + "小程序登录")
|
||||
@RestController
|
||||
@RequestMapping(ISystemConstant.WECHAT_MINI_APP_PREFIX + "/sign")
|
||||
public class SignMiniAppController {
|
||||
public class SignMiniappController {
|
||||
|
||||
@Autowired
|
||||
private MiniAppProperties miniAppProperties;
|
||||
@Autowired
|
||||
private IMiniAppSignService miniAppSignService;
|
||||
private IMiniappSignService miniAppSignService;
|
||||
|
||||
@ApiOperation(value = "默认登录", notes = "通过jsCode完成用户与微信openid绑定并返回token")
|
||||
@ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
|
||||
@PostMapping("default")
|
||||
@CheckRequestBodyAnnotation
|
||||
public SuccessResultData<String> defaultSign(@RequestBody MiniAppLoginVO miniAppLoginVO) throws Exception {
|
||||
public SuccessResultData<String> defaultSign(@RequestBody MiniappLoginVO miniAppLoginVO) throws Exception {
|
||||
if (!miniAppProperties.getActive()) {
|
||||
throw new PropertiesException("小程序配置未激活");
|
||||
}
|
@ -15,7 +15,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
* @Version: 1.0
|
||||
**/
|
||||
@ApiModel
|
||||
public class MiniAppLoginVO {
|
||||
public class MiniappLoginVO {
|
||||
|
||||
@ApiModelProperty(name = "jsCode", value = "临时code")
|
||||
@CheckEmptyAnnotation(name = "jsCode")
|
@ -0,0 +1,37 @@
|
||||
package ink.wgink.login.wechat.pojo.vos.update;
|
||||
|
||||
import ink.wgink.annotation.CheckEmptyAnnotation;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
/**
|
||||
* @ClassName: MiniAppUpdatePhoneVO
|
||||
* @Description: 小程序更新手机
|
||||
* @Author: wanggeng
|
||||
* @Date: 2021/8/2 5:20 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@ApiModel
|
||||
public class UpdatePhoneVO {
|
||||
|
||||
@ApiModelProperty(name = "phone", value = "手机号")
|
||||
@CheckEmptyAnnotation(name = "手机号", regex = "phone")
|
||||
private String phone;
|
||||
|
||||
public String getPhone() {
|
||||
return phone == null ? "" : phone.trim();
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"phone\":\"")
|
||||
.append(phone).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package ink.wgink.login.wechat.service.sign;
|
||||
|
||||
import ink.wgink.login.wechat.pojo.vos.sign.MiniAppLoginVO;
|
||||
import ink.wgink.pojo.result.SuccessResultData;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: IMiniAppSignService
|
||||
* @Description: 小程序登录
|
||||
* @Author: wanggeng
|
||||
* @Date: 2021/4/8 5:14 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface IMiniAppSignService {
|
||||
|
||||
/**
|
||||
* 默认登录
|
||||
*
|
||||
* @param miniAppLoginVO
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
String defaultSign(MiniAppLoginVO miniAppLoginVO) throws Exception;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package ink.wgink.login.wechat.service.sign;
|
||||
|
||||
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
|
||||
import ink.wgink.module.wechat.pojo.vos.miniapp.MiniappUpdatePhoneVO;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: IMiniAppSignService
|
||||
* @Description: 小程序登录
|
||||
* @Author: wanggeng
|
||||
* @Date: 2021/4/8 5:14 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
public interface IMiniappSignService {
|
||||
|
||||
/**
|
||||
* 默认登录
|
||||
*
|
||||
* @param miniAppLoginVO
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
String defaultSign(MiniappLoginVO miniAppLoginVO) throws Exception;
|
||||
|
||||
/**
|
||||
* 更新电话
|
||||
*
|
||||
* @param token
|
||||
* @param miniappUpdatePhoneVO
|
||||
* @return
|
||||
*/
|
||||
String updatePhone(String token, MiniappUpdatePhoneVO miniappUpdatePhoneVO) throws UnsupportedEncodingException;
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package ink.wgink.login.wechat.service.sign.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import ink.wgink.exceptions.PropertiesException;
|
||||
import ink.wgink.exceptions.SearchException;
|
||||
import ink.wgink.exceptions.WechatAccessTokenForUserException;
|
||||
import ink.wgink.exceptions.WechatUserInfoException;
|
||||
import ink.wgink.interfaces.consts.ISystemConstant;
|
||||
import ink.wgink.login.base.service.BaseAppSignService;
|
||||
import ink.wgink.module.wechat.pojo.bos.miniapp.MiniAppUserInfoBO;
|
||||
import ink.wgink.module.wechat.service.miniapp.IMiniAppUserService;
|
||||
import ink.wgink.properties.wechat.miniapp.MiniAppProperties;
|
||||
import ink.wgink.login.wechat.pojo.vos.sign.MiniAppLoginVO;
|
||||
import ink.wgink.login.wechat.service.sign.IMiniAppSignService;
|
||||
import ink.wgink.service.user.pojo.pos.UserPO;
|
||||
import ink.wgink.service.user.service.IUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: MiniAppSignServiceImpl
|
||||
* @Description: 小程序登录
|
||||
* @Author: wanggeng
|
||||
* @Date: 2021/4/8 5:14 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Service
|
||||
public class MiniAppSignServiceImpl extends BaseAppSignService implements IMiniAppSignService {
|
||||
|
||||
@Autowired
|
||||
private MiniAppProperties miniAppProperties;
|
||||
@Autowired
|
||||
private IMiniAppUserService miniAppUserService;
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@Override
|
||||
public String defaultSign(MiniAppLoginVO miniAppLoginVO) throws Exception {
|
||||
if (miniAppLoginVO == null) {
|
||||
throw new PropertiesException("未有相关配置");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(2);
|
||||
params.put("appid", miniAppProperties.getAppKey());
|
||||
params.put("secret", miniAppProperties.getAppSecret());
|
||||
params.put("js_code", miniAppLoginVO.getJsCode());
|
||||
params.put("grant_type", miniAppProperties.getGrantType());
|
||||
|
||||
StringBuilder url = new StringBuilder(miniAppProperties.getAuthorizeUrl());
|
||||
url.append("?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}");
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
String resultJson = restTemplate.getForObject(url.toString(), String.class, params);
|
||||
MiniAppUserInfoBO miniAppUserInfoBO = JSONObject.parseObject(resultJson, MiniAppUserInfoBO.class);
|
||||
if (miniAppUserInfoBO == null) {
|
||||
throw new WechatUserInfoException("获取微信用户信息失败");
|
||||
}
|
||||
if (miniAppUserInfoBO.getErrcode() != null && miniAppUserInfoBO.getErrcode() != 0) {
|
||||
throw new WechatAccessTokenForUserException(String.format("获取用户信息失败,错误码:%s,错误信息:%s", miniAppUserInfoBO.getErrcode(), miniAppUserInfoBO.getErrmsg()));
|
||||
}
|
||||
LOG.debug("绑定用户");
|
||||
String userId = miniAppUserService.createAndReturnUserId(miniAppProperties.getAppKey(), miniAppUserInfoBO.getOpenid());
|
||||
UserPO userPO = userService.getPO(userId);
|
||||
if (userPO == null) {
|
||||
throw new SearchException("用户不存在");
|
||||
}
|
||||
String isRandomUsername = ISystemConstant.IS_FALSE_INT;
|
||||
if (userPO.getUserUsername().startsWith(IMiniAppUserService.MINIAPP_RANDOM_USER_PREFIX)) {
|
||||
isRandomUsername = ISystemConstant.IS_TRUE_INT;
|
||||
}
|
||||
return getToken(userPO) + "_" + isRandomUsername;
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
package ink.wgink.login.wechat.service.sign.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import ink.wgink.exceptions.PropertiesException;
|
||||
import ink.wgink.exceptions.SearchException;
|
||||
import ink.wgink.exceptions.WechatAccessTokenForUserException;
|
||||
import ink.wgink.exceptions.WechatUserInfoException;
|
||||
import ink.wgink.interfaces.consts.ISystemConstant;
|
||||
import ink.wgink.login.base.service.BaseAppSignService;
|
||||
import ink.wgink.login.wechat.pojo.vos.sign.MiniappLoginVO;
|
||||
import ink.wgink.login.wechat.service.sign.IMiniappSignService;
|
||||
import ink.wgink.module.wechat.pojo.bos.miniapp.MiniAppUserInfoBO;
|
||||
import ink.wgink.module.wechat.pojo.pos.miniapp.MiniappUserPO;
|
||||
import ink.wgink.module.wechat.pojo.vos.miniapp.MiniappUpdatePhoneVO;
|
||||
import ink.wgink.module.wechat.service.miniapp.IMiniappUserService;
|
||||
import ink.wgink.properties.wechat.miniapp.MiniAppProperties;
|
||||
import ink.wgink.service.user.pojo.pos.UserPO;
|
||||
import ink.wgink.service.user.pojo.vos.UpdateUsernameVO;
|
||||
import ink.wgink.service.user.service.IUserService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* When you feel like quitting. Think about why you started
|
||||
* 当你想要放弃的时候,想想当初你为何开始
|
||||
*
|
||||
* @ClassName: MiniAppSignServiceImpl
|
||||
* @Description: 小程序登录
|
||||
* @Author: wanggeng
|
||||
* @Date: 2021/4/8 5:14 下午
|
||||
* @Version: 1.0
|
||||
*/
|
||||
@Service
|
||||
public class MiniappSignServiceImpl extends BaseAppSignService implements IMiniappSignService {
|
||||
|
||||
@Autowired
|
||||
private MiniAppProperties miniappProperties;
|
||||
@Autowired
|
||||
private IMiniappUserService miniappUserService;
|
||||
@Autowired
|
||||
private IUserService userService;
|
||||
|
||||
@Override
|
||||
public String defaultSign(MiniappLoginVO miniAppLoginVO) throws Exception {
|
||||
if (miniAppLoginVO == null) {
|
||||
throw new PropertiesException("未有相关配置");
|
||||
}
|
||||
Map<String, Object> params = new HashMap<>(2);
|
||||
params.put("appid", miniappProperties.getAppKey());
|
||||
params.put("secret", miniappProperties.getAppSecret());
|
||||
params.put("js_code", miniAppLoginVO.getJsCode());
|
||||
params.put("grant_type", miniappProperties.getGrantType());
|
||||
|
||||
StringBuilder url = new StringBuilder(miniappProperties.getAuthorizeUrl());
|
||||
url.append("?appid={appid}&secret={secret}&js_code={js_code}&grant_type={grant_type}");
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
String resultJson = restTemplate.getForObject(url.toString(), String.class, params);
|
||||
MiniAppUserInfoBO miniAppUserInfoBO = JSONObject.parseObject(resultJson, MiniAppUserInfoBO.class);
|
||||
if (miniAppUserInfoBO == null) {
|
||||
throw new WechatUserInfoException("获取微信用户信息失败");
|
||||
}
|
||||
if (miniAppUserInfoBO.getErrcode() != null && miniAppUserInfoBO.getErrcode() != 0) {
|
||||
throw new WechatAccessTokenForUserException(String.format("获取用户信息失败,错误码:%s,错误信息:%s", miniAppUserInfoBO.getErrcode(), miniAppUserInfoBO.getErrmsg()));
|
||||
}
|
||||
LOG.debug("绑定用户");
|
||||
String userId = miniappUserService.createAndReturnUserId(miniappProperties.getAppKey(), miniAppUserInfoBO.getOpenid());
|
||||
UserPO userPO = userService.getPO(userId);
|
||||
if (userPO == null) {
|
||||
throw new SearchException("用户不存在");
|
||||
}
|
||||
return getMiniappToken(userPO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updatePhone(String token, MiniappUpdatePhoneVO miniappUpdatePhoneVO) throws UnsupportedEncodingException {
|
||||
String userId = getAppTokenUser(token).getId();
|
||||
UserPO userPO = userService.getPO(userId);
|
||||
if (userPO == null) {
|
||||
throw new SearchException("非系统用户,请刷新小程序");
|
||||
}
|
||||
UserPO userPOByUsername = userService.getPOByUsername(miniappUpdatePhoneVO.getPhone());
|
||||
if (userPOByUsername != null) {
|
||||
LOG.debug("手机号存在");
|
||||
// 不是当前用户,抛异常
|
||||
if (StringUtils.equals(userId, userPOByUsername.getUserId())) {
|
||||
throw new SearchException("该手机已经绑定");
|
||||
}
|
||||
// 是当前用户,重复绑定,返回token
|
||||
return getToken(userPO);
|
||||
}
|
||||
// 手机不存在,更新手机号
|
||||
MiniappUserPO miniappUserPO = miniappUserService.getPO(userId);
|
||||
if (miniappUserPO == null) {
|
||||
throw new SearchException("未绑定为系统用户,请刷新小程序");
|
||||
}
|
||||
LOG.debug("手机不存在,更新用户名");
|
||||
UpdateUsernameVO updateUsernameVO = new UpdateUsernameVO();
|
||||
updateUsernameVO.setUsername(miniappUpdatePhoneVO.getPhone());
|
||||
updateUsernameVO.setUpdateReason("微信用户手动更新");
|
||||
userService.updateUsername(userId, updateUsernameVO);
|
||||
LOG.debug("返回token");
|
||||
UserPO newUserPO = new UserPO();
|
||||
BeanUtils.copyProperties(userPO, newUserPO);
|
||||
newUserPO.setUserUsername(miniappUpdatePhoneVO.getPhone());
|
||||
miniappUserService.updateIsInitAccount(userId, 0);
|
||||
return getMiniappToken(userPO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得小程序token
|
||||
*
|
||||
* @param userPO
|
||||
* @return
|
||||
* @throws UnsupportedEncodingException
|
||||
*/
|
||||
private String getMiniappToken(UserPO userPO) throws UnsupportedEncodingException {
|
||||
String isRandomUsername = ISystemConstant.IS_FALSE_INT;
|
||||
if (userPO.getUserUsername().startsWith(IMiniappUserService.MINIAPP_RANDOM_USER_PREFIX)) {
|
||||
isRandomUsername = ISystemConstant.IS_TRUE_INT;
|
||||
}
|
||||
return getToken(userPO) + "_" + isRandomUsername;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user