修改微信登录验证是否已注册

This commit is contained in:
赵旭 2023-10-13 10:00:38 +08:00
parent 8cf706fa6e
commit db302aa2ad
6 changed files with 161 additions and 16 deletions

View File

@ -13,15 +13,9 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinelu.common.core.controller.BaseController; import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R; import com.xinelu.common.core.domain.R;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
@ -127,10 +121,16 @@ public class ResidentPatientInfoController extends BaseController {
return R.ok(residentPatientInfoService.getCurrentResident(openid, cityCode)); return R.ok(residentPatientInfoService.getCurrentResident(openid, cityCode));
} }
@ApiOperation("是否已注册") /*@ApiOperation("是否已注册")
@GetMapping(value = "/isRegistered/{code}") @GetMapping(value = "/isRegistered/{code}")
public R<PatientInfo> isRegistered(@PathVariable("code") String code) { public R<PatientInfo> isRegistered(@PathVariable("code") String code) {
return R.ok(residentPatientInfoService.isRegistered(code)); return R.ok(residentPatientInfoService.isRegistered(code));
}*/
@ApiOperation("是否已注册")
@GetMapping(value = "/login/{loginCode}/{phoneCode}")
public AjaxResult isRegistered(@PathVariable("loginCode") String loginCode, @PathVariable("phoneCode") String phoneCode) {
return AjaxResult.success(residentPatientInfoService.login(loginCode,phoneCode));
} }
/** /**

View File

@ -94,4 +94,6 @@ public interface ResidentPatientInfoMapper {
* @return com.xinelu.familydoctor.applet.pojo.entity.PatientInfo * @return com.xinelu.familydoctor.applet.pojo.entity.PatientInfo
**/ **/
PatientInfo getByCardNo(String cardNo); PatientInfo getByCardNo(String cardNo);
int selectPatientInfoByPhone(String phone);
} }

View File

@ -3,9 +3,7 @@ package com.xinelu.familydoctor.applet.service;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.xinelu.familydoctor.applet.pojo.body.PatientInfoBody; import com.xinelu.familydoctor.applet.pojo.body.PatientInfoBody;
import com.xinelu.familydoctor.applet.pojo.entity.PatientInfo; import com.xinelu.familydoctor.applet.pojo.entity.PatientInfo;
import com.xinelu.familydoctor.applet.pojo.query.PatientInfoQuery; import java.util.HashMap;
import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
/** /**
@ -122,5 +120,6 @@ public interface IResidentPatientInfoService {
* @Param [code] * @Param [code]
* @return java.lang.Object * @return java.lang.Object
**/ **/
PatientInfo isRegistered(String code); //PatientInfo isRegistered(String code);
HashMap<String,String> login(String logincode,String phoneCode);
} }

View File

@ -2,14 +2,16 @@ package com.xinelu.familydoctor.applet.service.impl;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.xinelu.common.config.AppletChatConfig; import com.xinelu.common.config.AppletChatConfig;
import com.xinelu.common.core.domain.AjaxResult; import com.xinelu.common.entity.AppletPhoneVO;
import com.xinelu.common.exception.ServiceException; import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.AppletChatUtil;
import com.xinelu.common.utils.bean.BeanUtils; import com.xinelu.common.utils.bean.BeanUtils;
import com.xinelu.common.utils.http.HttpService; import com.xinelu.common.utils.http.HttpService;
import com.xinelu.common.utils.http.HttpUtils; import com.xinelu.common.utils.http.HttpUtils;
@ -19,10 +21,10 @@ import com.xinelu.familydoctor.applet.mapper.ResidentPatientInfoMapper;
import com.xinelu.familydoctor.applet.pojo.body.PatientInfoBody; import com.xinelu.familydoctor.applet.pojo.body.PatientInfoBody;
import com.xinelu.familydoctor.applet.pojo.entity.PatientInfo; import com.xinelu.familydoctor.applet.pojo.entity.PatientInfo;
import com.xinelu.familydoctor.applet.service.IResidentPatientInfoService; import com.xinelu.familydoctor.applet.service.IResidentPatientInfoService;
import com.xinelu.familydoctor.applet.utils.AppletAccessTokenUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -43,6 +45,8 @@ public class ResidentPatientInfoServiceImpl implements IResidentPatientInfoServi
private HttpService httpService; private HttpService httpService;
@Resource @Resource
private AppletChatConfig appletChatConfig; private AppletChatConfig appletChatConfig;
@Resource
private AppletAccessTokenUtils appletAccessTokenUtils;
/** /**
* @return com.xinelu.familydoctor.applet.pojo.entity.PatientInfo * @return com.xinelu.familydoctor.applet.pojo.entity.PatientInfo
@ -88,7 +92,9 @@ public class ResidentPatientInfoServiceImpl implements IResidentPatientInfoServi
if (!json.containsKey("openid")) { if (!json.containsKey("openid")) {
throw new ServiceException(json.getString("errmsg")); throw new ServiceException(json.getString("errmsg"));
} }
return json.getString("openid"); String openid = json.getString("openid");
return openid;
//return json.getString("openid");
} catch (Exception e) { } catch (Exception e) {
throw new ServiceException(e.getMessage()); throw new ServiceException(e.getMessage());
} }
@ -322,8 +328,40 @@ public class ResidentPatientInfoServiceImpl implements IResidentPatientInfoServi
* @Param [code] * @Param [code]
* @return com.xinelu.familydoctor.applet.pojo.entity.PatientInfo * @return com.xinelu.familydoctor.applet.pojo.entity.PatientInfo
**/ **/
@Override /*@Override
public PatientInfo isRegistered(String code) { public PatientInfo isRegistered(String code) {
return getCurrentResident(getOpenId(code), null); return getCurrentResident(getOpenId(code), null);
}*/
@Override
public HashMap<String, String> login(String loginCode,String phoneCode) {
HashMap<String, String> HashMap = new HashMap<>();
try {
SslUtils.ignoreSsl();
String params = "appid=" + appletChatConfig.getAppletId() + "&secret=" + appletChatConfig.getSecret() + "&js_code=" + loginCode + "&grant_type=" + appletChatConfig.getGrantType();
log.info("获取openID请求参数{}", params);
String result = HttpUtils.sendPost("https://api.weixin.qq.com/sns/jscode2session", params);
JSONObject json = JSONObject.parseObject(result);
// {"errcode":40029,"errmsg":"invalid code, rid: 650bf9c3-31a6c960-2b437029"}
log.info("获取openID响应结果{}", json.toJSONString());
if (!json.containsKey("openid")) {
throw new ServiceException(json.getString("errmsg"));
}
String openid = json.getString("openid");
HashMap.put("openid",openid);
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
//获取微信accessToken
String accessToken;
//是否注册状态码
String code;
accessToken = appletAccessTokenUtils.getAppletAccessToken();
//获取用户手机号
AppletPhoneVO appletPhoneInfo = AppletChatUtil.getAppletPhoneInfo(phoneCode, accessToken);
HashMap.put("phone",appletPhoneInfo.getPhoneInfo().getPhoneNumber());
code = residentPatientInfoMapper.selectPatientInfoByPhone(appletPhoneInfo.getPhoneInfo().getPhoneNumber()) == 0 ? "0" : "1";
HashMap.put("code",code);
return HashMap;
} }
} }

View File

@ -0,0 +1,98 @@
package com.xinelu.familydoctor.applet.utils;
import com.xinelu.common.config.AppletChatConfig;
import com.xinelu.common.config.NurseAppletChatConfig;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.entity.AppletAccessToken;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.AppletChatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* @Description 获取小程序AccessToken公共方法
* @Author 纪寒
* @Date 2023-02-27 18:02:05
* @Version 1.0
*/
@Component
public class AppletAccessTokenUtils {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private AppletChatConfig appletChatConfig;
@Resource
private NurseAppletChatConfig nurseAppletChatConfig;
/**
* 返回成功状态码
*/
private static final int SUCCESS_CODE = 0;
/**
* 获取小程序AccessToken方法
*
* @return 小程序的AccessToken
*/
public String getAppletAccessToken() {
String accessToken;
String accessTokenKey = Constants.NURSE_STATION_APPLET_ACCESS_TOKEN + "accessToken";
//从Redis中取出accessToken
Object object = redisTemplate.opsForValue().get(accessTokenKey);
if (Objects.isNull(object)) {
//没有获取accessToken
AppletAccessToken appletAccessToken = AppletChatUtil.getAppletAccessToken(appletChatConfig.getAppletId(), appletChatConfig.getSecret());
if (Objects.isNull(appletAccessToken)) {
throw new ServiceException("获取微信小程序accessToken信息失败");
}
if (Objects.nonNull(appletAccessToken.getErrcode()) && appletAccessToken.getErrcode() != SUCCESS_CODE) {
throw new ServiceException("获取微信小程序accessToken信息失败失败信息为" + appletAccessToken.getErrmsg(), 201);
}
if (StringUtils.isBlank(appletAccessToken.getAccessToken())) {
throw new ServiceException("accessToken信息为空");
}
//存入Redis中
redisTemplate.opsForValue().set(accessTokenKey, appletAccessToken.getAccessToken(), 3600, TimeUnit.SECONDS);
accessToken = appletAccessToken.getAccessToken();
} else {
accessToken = (String) object;
}
return accessToken;
}
/**
* 护理员小程序获取小程序AccessToken方法
*
* @return 小程序的AccessToken
*/
public String getPersonAppletAccessToken() {
String accessToken;
String accessTokenKey = Constants.NURSE_STATION_PERSON_APPLET_ACCESS_TOKEN + "accessToken";
//从Redis中取出accessToken
Object object = redisTemplate.opsForValue().get(accessTokenKey);
if (Objects.isNull(object)) {
//没有获取accessToken
AppletAccessToken appletAccessToken = AppletChatUtil.getAppletAccessToken(nurseAppletChatConfig.getAppletId(), nurseAppletChatConfig.getSecret());
if (Objects.isNull(appletAccessToken)) {
throw new ServiceException("获取微信小程序accessToken信息失败");
}
if (Objects.nonNull(appletAccessToken.getErrcode()) && appletAccessToken.getErrcode() != SUCCESS_CODE) {
throw new ServiceException("获取微信小程序accessToken信息失败失败信息为" + appletAccessToken.getErrmsg(), 201);
}
if (StringUtils.isBlank(appletAccessToken.getAccessToken())) {
throw new ServiceException("accessToken信息为空");
}
//存入Redis中
redisTemplate.opsForValue().set(accessTokenKey, appletAccessToken.getAccessToken(), 3600, TimeUnit.SECONDS);
accessToken = appletAccessToken.getAccessToken();
} else {
accessToken = (String) object;
}
return accessToken;
}
}

View File

@ -451,4 +451,12 @@
update patient_info set is_checked = #{isChecked} where patient_code = #{patientCode} update patient_info set is_checked = #{isChecked} where patient_code = #{patientCode}
</update> </update>
<!-- 获取注册详细信息 -->
<select id="selectPatientInfoByPhone" parameterType="String" resultType="java.lang.Integer">
select count(1) from patient_info
where phone = #{pnone}
and login_flag = '1'
</select>
</mapper> </mapper>