提交 95a6a0af 编写于 作者: xinxu.wang's avatar xinxu.wang

小程序授权登录之后,获取用户token

上级 201369a0
流水线 #48358 已失败 于阶段
......@@ -284,6 +284,12 @@
<artifactId>pica-cloud-message-client</artifactId>
<version>1.0.6-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.pica.cloud.wechat</groupId>
<artifactId>pica-cloud-applet-client</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
......
......@@ -25,8 +25,12 @@ import com.pica.cloud.foundation.service.starter.interceptor.EnabledLoginValidat
import com.pica.cloud.foundation.utils.constants.CommonConstants;
import com.pica.cloud.foundation.utils.entity.PicaUser;
import com.pica.cloud.foundation.utils.utils.CommonUtil;
import com.pica.cloud.foundation.utils.utils.StringUtil;
import com.pica.cloud.foundation.utils.utils.ValidateUtils;
import com.pica.cloud.foundation.utils.utils.json.Object2Map;
import com.pica.cloud.wechat.applet.client.AppletServiceClient;
import com.pica.cloud.wechat.applet.common.model.AppletModel;
import com.pica.cloud.wechat.applet.common.req.AppletReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
......@@ -67,6 +71,9 @@ public class AccountController extends AccountBaseController {
@Autowired
private DoctorService doctorService;
@Autowired
private AppletServiceClient appletServiceClient;
@GetMapping("/test")
public String test() {
return "{\"status\":\"UP\"}";
......@@ -98,6 +105,41 @@ public class AccountController extends AccountBaseController {
return PicaResponse.toResponse(StringUtils.EMPTY);
}
@ApiOperation("获取登录验证码")
@GetMapping("/authCode/applet")
public PicaResponse<String> getAuthCodeApplet(@ApiParam(value = "手机号", required = true) @RequestParam("mobilePhone") String mobilePhone,
@ApiParam(value = "验证码类型 0默认 1注册 2修改密码 4微信登录绑定手机 5修改手机 6重置密码 7患者招募提交问卷(效验") @RequestParam(value = "flag", defaultValue = "0") String flag,
@ApiParam(value = "小程序code", required = true) @RequestParam("code") String code,
@ApiParam(value = "小程序类型", required = true) @RequestParam("type") Integer type) {
AppletReq appletReq = new AppletReq();
appletReq.setCode(code);
appletReq.setType(type);
AppletModel appletModel = appletServiceClient.code2Session(appletReq).getData();
if (StringUtil.isNull(appletModel) || StringUtil.isNull(appletModel.getUnionId())) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(),"小程序code解密失败");
}
this.checkMobilePhone(mobilePhone);
String authCode = CommonUtil.createValidateCode(); //随机生成验证码
String message = "您的验证码是" + authCode + ",在10分钟内有效。如非本人操作,请忽略本短信!";
long senderId = 0L;
if (Integer.parseInt(flag) != 7) {
//判断账号是否已经存在
Account account = accountService.getByMobilePhone(mobilePhone);
if (StringUtil.isNull(account)) {
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(),"请先至云鹊医App注册认证后使用");
}
senderId = account == null ? 0L : account.getId();
}
//验证码保存到redis,失效时间10分钟
redisClient.set(this.getAuthCodeKey(mobilePhone, flag), authCode, 600);
//发送短信
super.sendMobileMessage(mobilePhone, message, senderId);
return PicaResponse.toResponse(StringUtils.EMPTY);
}
@ApiOperation("教培项目校验短信验证码")
@GetMapping("/check/authCode")
public PicaResponse checkAuthCode(@ApiParam(value = "手机号", required = true) @RequestParam("mobilePhone") String mobilePhone,
......
......@@ -3,6 +3,7 @@ package com.pica.cloud.account.account.server.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.account.account.common.req.LoginAppletReq;
import com.pica.cloud.account.account.server.entity.AesBean.AesAuthCodeAppletReq;
import com.pica.cloud.account.account.server.entity.AesBean.AesAuthCodeReq;
import com.pica.cloud.account.account.server.entity.*;
import com.pica.cloud.account.account.server.enums.AccountExceptionEnum;
......@@ -26,6 +27,9 @@ import com.pica.cloud.foundation.redis.ICacheClient;
import com.pica.cloud.foundation.service.starter.interceptor.EnabledLoginValidate;
import com.pica.cloud.foundation.utils.annotation.LoginPermission;
import com.pica.cloud.foundation.utils.utils.StringUtil;
import com.pica.cloud.wechat.applet.client.AppletServiceClient;
import com.pica.cloud.wechat.applet.common.model.AppletModel;
import com.pica.cloud.wechat.applet.common.req.AppletReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
......@@ -76,6 +80,10 @@ public class LoginController extends AccountBaseController {
private IntactUtil intactUtil;
@Autowired
private TokenUtils tokenUtils;
@Autowired
private AppletServiceClient appletServiceClient;
/**
* 密码登录接口(app、H5、web)
*
......@@ -553,7 +561,38 @@ public class LoginController extends AccountBaseController {
account1.setRegisterSource(4);
String newToken = tokenUtils.generateToken(account1);
int expiredSeconds = 24 * 60 * 60 * 30;
cacheClient.set("token-doctor-unionid-"+req.getUnionid(),newToken,expiredSeconds);
cacheClient.set("token-unionid-"+newToken,req.getUnionid(),expiredSeconds);
intactUtil.sendIntact(batchNo, "login/applet", com.pica.cloud.foundation.completeness.contract.constants.CommonConstants.INTACT_CONTENT_LOG_STATUS_3, "req:" + JSON.toJSONString(req));
return PicaResponse.toResponse(newToken);
}
@ApiOperation("云鹊医助手小程序授权登录")
@PostMapping("/login/applet/authCode")
public PicaResponse<LoginResult> loginAppletAuthCode(@RequestBody AesAuthCodeAppletReq authCodeReq) {
AppletReq appletReq = new AppletReq();
appletReq.setCode(authCodeReq.getCode());
appletReq.setType(authCodeReq.getType());
AppletModel appletModel = appletServiceClient.code2Session(appletReq).getData();
if (StringUtil.isNull(appletModel) || StringUtil.isNull(appletModel.getUnionId())) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(),"小程序code解密失败");
}
BaseRequest request = new BaseRequest();
request.setMobile(authCodeReq.getMobile());
request.setAuthCode(authCodeReq.getAuthCode());
logger.info("login-register:{}",JSONObject.toJSONString(request));
request.setProductType(1);
request.setSourceType(4);
request.setLoginIp(super.getIpAddr());
request.setUserTokenTourist(super.getUserTokenTourist());
LoginResult login = loginService.loginAndRegister(request);
if (StringUtil.isNotNull(login)) {
int expiredSeconds = 24 * 60 * 60 * 30;
cacheClient.set("token-doctor-unionid-"+appletModel.getUnionId(),login.getToken(),expiredSeconds);
cacheClient.set("token-unionid-"+login.getToken(),appletModel.getUnionId(),expiredSeconds);
}
return PicaResponse.toResponse(login);
}
}
// Copyright 2016-2101 Pica.
package com.pica.cloud.account.account.server.entity.AesBean;
/**
* @ClassName AesAuthCodeAppletReq
* @Description TODO
* @Author xinxu.wang
* @Date 2022/7/5 18:33
* @ModifyDate 2022/7/5 18:33
* @Version 1.0
*/
public class AesAuthCodeAppletReq {
private Integer bizType;
private String device_token;
private String mobile;
private Integer flag;
private String token;
private String authCode;
private String password;
private String code;
private Integer type;
/**
* 加密后的密文
*/
private String key;
/**
* 加密后的数据
*/
private String content;
// 是否需要校验该设备登录的手机号
private Boolean checkFlag = false;
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Boolean getCheckFlag() {
return checkFlag;
}
public void setCheckFlag(Boolean checkFlag) {
this.checkFlag = checkFlag;
}
public Integer getBizType() {
return bizType;
}
public void setBizType(Integer bizType) {
this.bizType = bizType;
}
public String getDevice_token() {
return device_token;
}
public void setDevice_token(String device_token) {
this.device_token = device_token;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getFlag() {
return flag;
}
public void setFlag(Integer flag) {
this.flag = flag;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getAuthCode() {
return authCode;
}
public void setAuthCode(String authCode) {
this.authCode = authCode;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册