| | |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.http.HttpUtils; |
| | | import com.ruoyi.system.domain.SysConfig; |
| | | import com.ruoyi.system.mapper.SysConfigMapper; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.IWechatLoginService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | |
| | |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | |
| | | @Autowired |
| | | private ISysConfigService configService; |
| | | |
| | | @Autowired |
| | | private SysConfigMapper configMapper; |
| | | |
| | | /** |
| | | * 微信API - code2Session |
| | | */ |
| | |
| | | */ |
| | | private static final String GET_PHONE_NUMBER_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"; |
| | | |
| | | private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | |
| | | /** |
| | | * 微信API - 获取access_token |
| | | */ |
| | | private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; |
| | | /** |
| | | * 根据configKey写入或更新sys_config |
| | | */ |
| | | private void upsertConfig(String key, String value) { |
| | | SysConfig exist = configMapper.checkConfigKeyUnique(key); |
| | | if (exist != null && exist.getConfigId() != null) { |
| | | exist.setConfigValue(value); |
| | | configMapper.updateConfig(exist); |
| | | } else { |
| | | SysConfig cfg = new SysConfig(); |
| | | cfg.setConfigKey(key); |
| | | cfg.setConfigName(key); |
| | | cfg.setConfigValue(value); |
| | | cfg.setConfigType("Y"); |
| | | configMapper.insertConfig(cfg); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 通过微信code获取openid和session_key |
| | |
| | | return result; |
| | | } |
| | | |
| | | // 估算过期时间(微信access_token默认7200秒有效) |
| | | java.util.Date accessTokenExpiresAt = new java.util.Date(System.currentTimeMillis() + 7200L * 1000L); |
| | | |
| | | // 构建请求URL |
| | | String url = GET_PHONE_NUMBER_URL + "?access_token=" + accessToken; |
| | | |
| | |
| | | result.put("phoneNumber", phoneInfo.getString("phoneNumber")); |
| | | result.put("purePhoneNumber", phoneInfo.getString("purePhoneNumber")); |
| | | result.put("countryCode", phoneInfo.getString("countryCode")); |
| | | // 返回当前使用的access_token及有效期,便于调用方存储 |
| | | result.put("accessToken", accessToken); |
| | | result.put("accessTokenExpiresAt", accessTokenExpiresAt); |
| | | |
| | | return result; |
| | | } |
| | |
| | | return result; |
| | | } |
| | | |
| | | // 5. 更新用户的微信信息 |
| | | // 5. 更新用户的微信信息(包括access_token与过期时间) |
| | | SysUser updateUser = new SysUser(); |
| | | updateUser.setUserId(user.getUserId()); |
| | | updateUser.setOpenId(openId); |
| | |
| | | { |
| | | updateUser.setUnionId(unionId); |
| | | } |
| | | // 保存本次调用使用的access_token及过期时间到应用级配置(便于后续复用) |
| | | Object at = phoneResult.get("accessToken"); |
| | | Object atExp = phoneResult.get("accessTokenExpiresAt"); |
| | | if (at != null) { |
| | | String appId = wechatConfig.getAppId(); |
| | | String tokenKey = "weixin.access_token." + appId; |
| | | String expireKey = "weixin.access_token_expires." + appId; |
| | | upsertConfig(tokenKey, at.toString()); |
| | | long expireTs = (atExp instanceof java.util.Date) ? ((java.util.Date) atExp).getTime() : (System.currentTimeMillis() + 7200L * 1000L); |
| | | upsertConfig(expireKey, String.valueOf(expireTs)); |
| | | } |
| | | userService.updateUser(updateUser); |
| | | |
| | | log.info("用户{}微信信息更新成功", user.getUserName()); |