1.根據微信code獲取換取使用者登入態資訊
點選檢視程式碼
/**
* 根據code獲取小程式使用者openpid
*/
@Override
public R<Map<String, String>> getUnitCheckPersonOpenId(String code){
R<Map<String, String>> resMap = new R<>();
//獲取使用者openid
String wxAppIdUrl = WxLoginConstant.WX_LOGIN_URL + "?appid=" + WxLoginConstant.APP_ID + "&secret=" +
WxLoginConstant.SECRET + "&js_code=" + code + "&grant_type=" + WxLoginConstant.GRANT_TYPE;
R<Object> tokenRes = RequestUtil.GET(wxAppIdUrl, null);
Integer resCode = tokenRes.getCode();
if (resCode == HttpStatus.SC_OK){
Object tokenData = tokenRes.getData();
WxLoginResVo tokenResVo = JSON.parseObject(tokenData.toString(), WxLoginResVo.class);
if (Objects.nonNull(tokenResVo)){
String openid = tokenResVo.getOpenid();
if (StringUtils.isNotBlank(openid)){
//根據openid查詢db是否存在此記錄
WghHiddenSelfCheckUnitPersonVo unitCheckPerson = getUnitCheckPersonByOpenid(openid);
Map<String, String> map = Maps.newHashMap();
if (Objects.nonNull(unitCheckPerson)){
//1.存在
map.put("isExistOpenId","y");
//更新
} else {
//2.不存在
map.put("isExistOpenId","n");
}
//使用者唯一標識
map.put("openid",openid);
resMap.setCode(200);
resMap.setMsg("成功");
resMap.setData(map);
} else {
resMap.setCode(tokenResVo.getErrcode());
resMap.setMsg(tokenResVo.getErrmsg());
}
}
}else {
resMap.setCode(500);
resMap.setMsg("http呼叫微信介面獲取使用者openid失敗");
}
return resMap;
}s
點選檢視程式碼
/**
* 根據openid判斷是否為系統內部人員、公眾人員
*/
@Override
public R<Map<String,String>> getHiddenSelfCheckIsHasUnitInfo(String openid) {
R<Map<String, String>> resMap = new R<>();
if (StringUtils.isBlank(openid)) {
resMap.setCode(501);
resMap.setMsg("openid引數不能為空");
return resMap;
}
LambdaQueryWrapper<WghHiddenSelfCheckUnitPerson> lqw = Wrappers.lambdaQuery();
lqw.eq(StringUtils.isNotBlank(openid), WghHiddenSelfCheckUnitPerson::getOpenid, openid);
WghHiddenSelfCheckUnitPersonVo objVo = baseMapper.selectVoOne(lqw);
if (Objects.isNull(objVo)) {
resMap.setCode(502);
resMap.setMsg("無法根據openid查詢到使用者資訊");
return resMap;
}
Map<String, String> map = Maps.newHashMap();
Long sysUserId = objVo.getSysUserId();
if (Objects.nonNull(sysUserId)) {
//系統使用者
map.put("isSysUser", "y");
//生成token
SysUser sysUser = sysUserMapper.selectUserById(sysUserId);
if (Objects.nonNull(sysUser)) {
LoginUser loginUser = SysLoginService.buildLoginUser(sysUser);
LoginHelper.loginByDevice(loginUser, DeviceType.XCX);
String token = StpUtil.getTokenValue();
map.put("token", token);
} else {
resMap.setCode(401);
resMap.setMsg("系統中不存在此使用者資訊");
}
} else {
//公眾使用者
map.put("isSysUser", "n");
}
resMap.setCode(200);
resMap.setMsg("成功");
resMap.setData(map);
return resMap;
}
點選檢視程式碼
/**
* 根據openid完善小程式登入個人資訊
*/
@Override
public Boolean hiddenSelfCheckUnitUserInfo(WghHiddenSelfCheckUnitPersonBo bo) {
if (StringUtils.isBlank(bo.getPhone())){
return false;
}
// 1.根據手機號查詢db是否存在此記錄
Boolean flag;
LambdaQueryWrapper<WghHiddenSelfCheckUnitPerson> lqw = Wrappers.lambdaQuery();
lqw.eq(StringUtils.isNotBlank(bo.getPhone()),WghHiddenSelfCheckUnitPerson::getPhone,bo.getPhone());
WghHiddenSelfCheckUnitPersonVo vo = baseMapper.selectVoOne(lqw);
if (Objects.nonNull(vo)){
//1.1是則為系統使用者第一次登入,需繫結小程式openid
//更新db
UpdateWrapper<WghHiddenSelfCheckUnitPerson> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("phone", bo.getPhone());
updateWrapper.set("openid",bo.getOpenid());
flag = baseMapper.update(null,updateWrapper) > 0;
}else {
//1.2否,說明是自查自改單位人員,則新增(手機號、使用者名稱、openid)
//新增
WghHiddenSelfCheckUnitPerson add = BeanUtil.toBean(bo, WghHiddenSelfCheckUnitPerson.class);
flag = baseMapper.insert(add) > 0;
}
return flag;
}