微信小程序登录代码移植
This commit is contained in:
parent
f9e3425457
commit
a1f8513f95
@ -5,14 +5,13 @@ import com.xinelu.applet.dto.appletlogin.StationItemInfoDTO;
|
|||||||
import com.xinelu.applet.service.appletlogin.AppletLoginService;
|
import com.xinelu.applet.service.appletlogin.AppletLoginService;
|
||||||
import com.xinelu.common.annotation.MobileRequestAuthorization;
|
import com.xinelu.common.annotation.MobileRequestAuthorization;
|
||||||
import com.xinelu.common.annotation.RepeatSubmit;
|
import com.xinelu.common.annotation.RepeatSubmit;
|
||||||
import com.xinelu.common.config.XinELuConfig;
|
|
||||||
import com.xinelu.common.core.controller.BaseController;
|
import com.xinelu.common.core.controller.BaseController;
|
||||||
|
|
||||||
import com.xinelu.common.core.domain.AjaxResult;
|
import com.xinelu.common.core.domain.AjaxResult;
|
||||||
import com.xinelu.common.custominterface.Insert;
|
import com.xinelu.common.custominterface.Insert;
|
||||||
import com.xinelu.common.custominterface.Query;
|
import com.xinelu.common.custominterface.Query;
|
||||||
import com.xinelu.common.exception.ServiceException;
|
import com.xinelu.common.exception.ServiceException;
|
||||||
import com.xinelu.common.utils.regex.RegexUtil;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@ -34,6 +33,25 @@ public class AppletLoginController extends BaseController {
|
|||||||
@Resource
|
@Resource
|
||||||
private AppletLoginService appletLoginService;
|
private AppletLoginService appletLoginService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据登录凭证获取用户的登录信息
|
||||||
|
*
|
||||||
|
* @param loginCode 登录凭证
|
||||||
|
* @param phoneCode 获取手机号登录凭证
|
||||||
|
* @return 微信用户登录信息
|
||||||
|
*/
|
||||||
|
@MobileRequestAuthorization
|
||||||
|
@GetMapping("/getWeChatUserInfo")
|
||||||
|
public AjaxResult appletLogin(@RequestParam("loginCode") String loginCode, @Param("phoneCode") String phoneCode) {
|
||||||
|
if (StringUtils.isBlank(loginCode)) {
|
||||||
|
return AjaxResult.error("登录凭证编码不能为空!");
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(phoneCode)) {
|
||||||
|
return AjaxResult.error("获取手机号凭证不存在");
|
||||||
|
}
|
||||||
|
return appletLoginService.appletLogin(loginCode, phoneCode);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户Id获取用户的登录信息
|
* 根据用户Id获取用户的登录信息
|
||||||
*
|
*
|
||||||
|
|||||||
@ -12,6 +12,15 @@ import com.xinelu.common.core.domain.AjaxResult;
|
|||||||
*/
|
*/
|
||||||
public interface AppletLoginService {
|
public interface AppletLoginService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据登录凭证获取用户的登录信息
|
||||||
|
*
|
||||||
|
* @param loginCode 登录凭证
|
||||||
|
* @param phoneCode 获取手机号登录凭证
|
||||||
|
* @return 微信用户登录信息
|
||||||
|
*/
|
||||||
|
AjaxResult appletLogin(String loginCode, String phoneCode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户的patientId查询用户是否注册
|
* 根据用户的patientId查询用户是否注册
|
||||||
*
|
*
|
||||||
|
|||||||
@ -6,13 +6,19 @@ import com.xinelu.applet.dto.appletlogin.AppointmentInfoDTO;
|
|||||||
import com.xinelu.applet.dto.appletlogin.StationItemInfoDTO;
|
import com.xinelu.applet.dto.appletlogin.StationItemInfoDTO;
|
||||||
import com.xinelu.applet.mapper.appletlogin.AppletLoginMapper;
|
import com.xinelu.applet.mapper.appletlogin.AppletLoginMapper;
|
||||||
import com.xinelu.applet.service.appletlogin.AppletLoginService;
|
import com.xinelu.applet.service.appletlogin.AppletLoginService;
|
||||||
|
import com.xinelu.applet.utils.AppletAccessTokenUtil;
|
||||||
import com.xinelu.applet.vo.appletlogin.NurserStationItemConsumableVO;
|
import com.xinelu.applet.vo.appletlogin.NurserStationItemConsumableVO;
|
||||||
import com.xinelu.applet.vo.appletlogin.NurserStationItemInfoVO;
|
import com.xinelu.applet.vo.appletlogin.NurserStationItemInfoVO;
|
||||||
|
import com.xinelu.applet.vo.appletlogin.PatientAndPersonFlagVO;
|
||||||
import com.xinelu.applet.vo.nearbynursingstation.PoserInfoHomeVO;
|
import com.xinelu.applet.vo.nearbynursingstation.PoserInfoHomeVO;
|
||||||
|
import com.xinelu.common.config.AppletChatConfig;
|
||||||
import com.xinelu.common.constant.Constants;
|
import com.xinelu.common.constant.Constants;
|
||||||
import com.xinelu.common.core.domain.AjaxResult;
|
import com.xinelu.common.core.domain.AjaxResult;
|
||||||
|
import com.xinelu.common.entity.AppletLoginVO;
|
||||||
|
import com.xinelu.common.entity.AppletPhoneVO;
|
||||||
import com.xinelu.common.enums.*;
|
import com.xinelu.common.enums.*;
|
||||||
import com.xinelu.common.exception.ServiceException;
|
import com.xinelu.common.exception.ServiceException;
|
||||||
|
import com.xinelu.common.utils.AppletChatUtil;
|
||||||
import com.xinelu.common.utils.DateUtils;
|
import com.xinelu.common.utils.DateUtils;
|
||||||
import com.xinelu.common.utils.StringUtils;
|
import com.xinelu.common.utils.StringUtils;
|
||||||
import com.xinelu.common.utils.bean.BeanUtils;
|
import com.xinelu.common.utils.bean.BeanUtils;
|
||||||
@ -28,7 +34,6 @@ import com.xinelu.manage.vo.patientinfo.PatientInfoVO;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.apache.commons.lang3.BooleanUtils;
|
import org.apache.commons.lang3.BooleanUtils;
|
||||||
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
|
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -55,19 +60,34 @@ import java.util.stream.Collectors;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class AppletLoginServiceImpl implements AppletLoginService {
|
public class AppletLoginServiceImpl implements AppletLoginService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
@Resource
|
||||||
|
private AppletChatConfig appletChatConfig;
|
||||||
@Resource
|
@Resource
|
||||||
private AppletLoginMapper appletLoginMapper;
|
private AppletLoginMapper appletLoginMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private PatientInfoMapper patientInfoMapper;
|
private PatientInfoMapper patientInfoMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private RedisTemplate<String, Object> redisTemplate;
|
|
||||||
@Resource
|
|
||||||
private AppointmentOrderMapper appointmentOrderMapper;
|
private AppointmentOrderMapper appointmentOrderMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private AppointmentOrderDetailsMapper appointmentOrderDetailsMapper;
|
private AppointmentOrderDetailsMapper appointmentOrderDetailsMapper;
|
||||||
@Resource
|
@Resource
|
||||||
|
private AppletAccessTokenUtil appletAccessTokenUtil;
|
||||||
|
@Resource
|
||||||
private AppointmentOrderProcessRecordMapper appointmentOrderProcessRecordMapper;
|
private AppointmentOrderProcessRecordMapper appointmentOrderProcessRecordMapper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回成功状态码
|
||||||
|
*/
|
||||||
|
private static final int SUCCESS_CODE = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回成功状态码
|
||||||
|
*/
|
||||||
|
private static final String OK = "ok";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 提前预约时间半天标识
|
* 提前预约时间半天标识
|
||||||
*/
|
*/
|
||||||
@ -78,6 +98,93 @@ public class AppletLoginServiceImpl implements AppletLoginService {
|
|||||||
*/
|
*/
|
||||||
private static final String ONE_DAY = "ONE_DAY";
|
private static final String ONE_DAY = "ONE_DAY";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取微信小程序access_token错误码
|
||||||
|
*/
|
||||||
|
private static final int ERROR_ACCESS_CODE = 40001;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据登录凭证获取用户的登录信息
|
||||||
|
*
|
||||||
|
* @param loginCode 登录凭证
|
||||||
|
* @param phoneCode 获取手机号登录凭证
|
||||||
|
* @return 微信用户登录信息
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public AjaxResult appletLogin(String loginCode, String phoneCode) {
|
||||||
|
//根据code获取用户的微信unionId以及openId等信息
|
||||||
|
AppletLoginVO appletLoginInfo = AppletChatUtil.getAppletLoginInfo(appletChatConfig.getAppletId(), appletChatConfig.getSecret(), loginCode, appletChatConfig.getGrantType());
|
||||||
|
if (Objects.isNull(appletLoginInfo)) {
|
||||||
|
return AjaxResult.error("获取微信小程序用户信息失败");
|
||||||
|
}
|
||||||
|
if (Objects.nonNull(appletLoginInfo.getErrcode()) && appletLoginInfo.getErrcode() != SUCCESS_CODE) {
|
||||||
|
return AjaxResult.error("获取微信小程序用户信息失败,失败信息为:" + appletLoginInfo.getErrmsg());
|
||||||
|
}
|
||||||
|
//获取微信accessToken
|
||||||
|
String accessToken;
|
||||||
|
String accessTokenKey = Constants.NURSE_STATION_APPLET_ACCESS_TOKEN + "accessToken";
|
||||||
|
//从Redis中取出accessToken
|
||||||
|
Object object = redisTemplate.opsForValue().get(accessTokenKey);
|
||||||
|
if (Objects.isNull(object)) {
|
||||||
|
//没有,获取accessToken
|
||||||
|
accessToken = appletAccessTokenUtil.getAppletAccessToken();
|
||||||
|
} else {
|
||||||
|
accessToken = (String) object;
|
||||||
|
}
|
||||||
|
//获取用户手机号
|
||||||
|
AppletPhoneVO appletPhoneInfo = AppletChatUtil.getAppletPhoneInfo(phoneCode, accessToken);
|
||||||
|
if (Objects.isNull(appletPhoneInfo)) {
|
||||||
|
return AjaxResult.error("获取用户手机号失败");
|
||||||
|
}
|
||||||
|
if (Objects.nonNull(appletPhoneInfo.getErrcode()) && appletPhoneInfo.getErrcode() == ERROR_ACCESS_CODE) {
|
||||||
|
//当前Redis缓存中的access_token无效直接删除
|
||||||
|
if (Objects.nonNull(object)) {
|
||||||
|
redisTemplate.delete(accessTokenKey);
|
||||||
|
//删除之后重新获取获取accessToken
|
||||||
|
accessToken = appletAccessTokenUtil.getAppletAccessToken();
|
||||||
|
appletPhoneInfo = AppletChatUtil.getAppletPhoneInfo(phoneCode, accessToken);
|
||||||
|
if (Objects.isNull(appletPhoneInfo)) {
|
||||||
|
return AjaxResult.error("获取用户手机号失败");
|
||||||
|
}
|
||||||
|
if (Objects.nonNull(appletPhoneInfo.getErrcode()) && appletPhoneInfo.getErrcode() == ERROR_ACCESS_CODE) {
|
||||||
|
return AjaxResult.error("登录失败!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(appletPhoneInfo.getErrmsg()) && !OK.equals(appletPhoneInfo.getErrmsg())) {
|
||||||
|
return AjaxResult.error("获取用户手机号失败,失败信息为:" + appletPhoneInfo.getErrmsg());
|
||||||
|
}
|
||||||
|
//根据手机号和openid判断当前用户是否存在
|
||||||
|
String phone = StringUtils.isBlank(appletPhoneInfo.getPhoneInfo().getPhoneNumber()) ? "" : appletPhoneInfo.getPhoneInfo().getPhoneNumber();
|
||||||
|
String openId = StringUtils.isBlank(appletLoginInfo.getOpenid()) ? "" : appletLoginInfo.getOpenid();
|
||||||
|
PatientInfoVO patientInfoByPhone = appletLoginMapper.getPatientInfoByPhone(phone, openId);
|
||||||
|
PatientAndPersonFlagVO appletParent = new PatientAndPersonFlagVO();
|
||||||
|
//判断首页是否显示“护理员小程序”跳转的链接,非护理员用户不显示跳转链接
|
||||||
|
if (Objects.nonNull(patientInfoByPhone) && Objects.nonNull(patientInfoByPhone.getPersonCount()) && patientInfoByPhone.getPersonCount() > 0) {
|
||||||
|
appletParent.setPersonLoginFlag(Boolean.TRUE);
|
||||||
|
} else {
|
||||||
|
appletParent.setPersonLoginFlag(Boolean.FALSE);
|
||||||
|
}
|
||||||
|
//会员信息为空新增个人信息
|
||||||
|
if (Objects.isNull(patientInfoByPhone)) {
|
||||||
|
appletParent.setOpenid(openId);
|
||||||
|
appletParent.setPhone(phone);
|
||||||
|
appletParent.setCreateTime(LocalDateTime.now());
|
||||||
|
appletParent.setAreaCode("");
|
||||||
|
patientInfoMapper.insertAppletLoginPatientInfo(appletParent);
|
||||||
|
appletParent.setId(appletParent.getId());
|
||||||
|
return AjaxResult.success(appletParent);
|
||||||
|
}
|
||||||
|
//更新用户的openid等微信标识信息
|
||||||
|
appletParent.setId(patientInfoByPhone.getId());
|
||||||
|
appletParent.setOpenid(openId);
|
||||||
|
appletParent.setPhone(StringUtils.isBlank(patientInfoByPhone.getPhone()) ? "" : patientInfoByPhone.getPhone());
|
||||||
|
appletParent.setUpdateTime(LocalDateTime.now());
|
||||||
|
patientInfoMapper.updatePatientInfo(appletParent);
|
||||||
|
return AjaxResult.success(appletParent);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据微信用户的patientId查询用户是否注册
|
* 根据微信用户的patientId查询用户是否注册
|
||||||
*
|
*
|
||||||
@ -348,5 +455,4 @@ public class AppletLoginServiceImpl implements AppletLoginService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user