发布通告功能
This commit is contained in:
parent
1190d2d41f
commit
9a6b8574ac
120
ruoyi-admin/src/main/java/com/ruoyi/busi/utils/WeChatUtils.java
Normal file
120
ruoyi-admin/src/main/java/com/ruoyi/busi/utils/WeChatUtils.java
Normal file
@ -0,0 +1,120 @@
|
||||
package com.ruoyi.busi.utils;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.payConfig.WechatPayConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WeChatUtils {
|
||||
|
||||
@Resource
|
||||
private WechatPayConfig wechatPayConfig;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
// 获取code的接口地址
|
||||
public final static String CODE_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
|
||||
|
||||
// 获取网页access_token的接口地址
|
||||
public final static String WEB_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
|
||||
|
||||
// 获取用户信息地址
|
||||
public final static String USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
|
||||
|
||||
// 获取access_token的接口地址(GET) 限2000(次/天)
|
||||
public final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
|
||||
|
||||
// 发送模板消息
|
||||
public static final String SEND_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
|
||||
|
||||
/**
|
||||
* 生成获取code的url
|
||||
*
|
||||
* @param backUrl 回调URL,用户同意授权后重定向的回调链接地址,需要进行URL编码
|
||||
* @param state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
|
||||
* @return 生成的获取code的URL
|
||||
*/
|
||||
public String getCodeUrl(String backUrl, String state) {
|
||||
String requestUrl = null;
|
||||
state = state == null ? "" : state;
|
||||
try {
|
||||
requestUrl = CODE_URL.replace("APPID", wechatPayConfig.getAppId())
|
||||
.replace("REDIRECT_URI", URLEncoder.encode(backUrl, "UTF-8"))
|
||||
.replace("STATE", state);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return requestUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信网页access_token 和 openid
|
||||
*
|
||||
* @param code 用户同意授权后,通过重定向到回调链接中获取的code参数
|
||||
* @return 包含access_token和openid的JSONObject
|
||||
*/
|
||||
public JSONObject getWebAccessTokenAndOpenid(String code) {
|
||||
String requestUrl = WEB_ACCESS_TOKEN_URL.replace("APPID", wechatPayConfig.getAppId())
|
||||
.replace("APPSECRET", wechatPayConfig.getAppSecret())
|
||||
.replace("CODE", code);
|
||||
String response = HttpUtil.get(requestUrl);
|
||||
return JSONUtil.parseObj(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息
|
||||
*
|
||||
* @param webAccessToken 通过getWebAccessTokenAndOpenid方法获取的access_token
|
||||
* @param openid 通过getWebAccessTokenAndOpenid方法获取的openid
|
||||
* @return 包含用户信息的JSONObject
|
||||
*/
|
||||
public JSONObject getUserInfo(String webAccessToken, String openid) {
|
||||
String requestUrl = USERINFO_URL.replace("ACCESS_TOKEN", webAccessToken)
|
||||
.replace("OPENID", openid);
|
||||
String response = HttpUtil.get(requestUrl);
|
||||
return JSONUtil.parseObj(response);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取access_token 先从Redis获取,如果没有再调微信获取并存Redis
|
||||
*
|
||||
* @return 获取到的access_token
|
||||
*/
|
||||
public String getToken() {
|
||||
String accessToken;
|
||||
String redisKey = "wechat_access_token";
|
||||
|
||||
// 先从Redis获取
|
||||
accessToken = redisCache.getCacheObject(redisKey);
|
||||
if (accessToken != null) {
|
||||
log.debug("从Redis获取access_token: {}", accessToken);
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
// 如果Redis中没有或者强制刷新,则从微信获取
|
||||
String requestUrl = ACCESS_TOKEN_URL.replace("APPID", wechatPayConfig.getAppId())
|
||||
.replace("APPSECRET", wechatPayConfig.getAppSecret());
|
||||
String response = HttpUtil.get(requestUrl);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(response);
|
||||
accessToken = jsonObject.getStr("access_token");
|
||||
int expiresIn = jsonObject.getInt("expires_in", 7200); // 默认有效期为7200秒
|
||||
if (accessToken != null) {
|
||||
// 将access_token存入Redis,并设置有效期
|
||||
redisCache.setCacheObject(redisKey, accessToken,expiresIn - 60,TimeUnit.SECONDS);
|
||||
log.debug("从微信获取access_token并存入Redis: {}", accessToken);
|
||||
}
|
||||
return accessToken;
|
||||
}
|
||||
}
|
@ -166,10 +166,10 @@ public class MemberUserServiceImpl extends ServiceImpl<MemberUserMapper, MemberU
|
||||
String phone = jsonObject.getString("phoneNumber");
|
||||
//根据openid判断数据库中是否有该用户
|
||||
//根据openid查询用户信息
|
||||
SysUser wxUser = userMapper.selectWxUserByOpenIdOrPhone(openid,null);
|
||||
SysUser wxUser = userMapper.selectWxUserByOpenIdOrPhone(openid,null,null,null);
|
||||
if(null==wxUser){
|
||||
//根据openId没查到,再根据手机号查
|
||||
wxUser = userMapper.selectWxUserByOpenIdOrPhone(null,phone);
|
||||
wxUser = userMapper.selectWxUserByOpenIdOrPhone(null,phone,null,null);
|
||||
}
|
||||
//如果查不到,则新增,查到了,则更新
|
||||
SysUser user = new SysUser();
|
||||
|
@ -67,6 +67,7 @@ public class WechatPayConfig {
|
||||
private String privateKeyPath;
|
||||
private String publicKeyPath;
|
||||
private String publicKeyId;
|
||||
private String wxAppSecret;
|
||||
|
||||
/**
|
||||
* 商户序列号
|
||||
|
@ -6,14 +6,14 @@ import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
|
||||
/**
|
||||
* 用户表 数据层
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public interface SysUserMapper
|
||||
{
|
||||
/**
|
||||
* 根据条件分页查询用户列表
|
||||
*
|
||||
*
|
||||
* @param sysUser 用户信息
|
||||
* @return 用户信息集合信息
|
||||
*/
|
||||
@ -21,7 +21,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 根据条件分页查询已配用户角色列表
|
||||
*
|
||||
*
|
||||
* @param user 用户信息
|
||||
* @return 用户信息集合信息
|
||||
*/
|
||||
@ -29,7 +29,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 根据条件分页查询未分配用户角色列表
|
||||
*
|
||||
*
|
||||
* @param user 用户信息
|
||||
* @return 用户信息集合信息
|
||||
*/
|
||||
@ -37,7 +37,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 通过用户名查询用户
|
||||
*
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
@ -45,7 +45,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 通过用户ID查询用户
|
||||
*
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
@ -53,7 +53,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 新增用户信息
|
||||
*
|
||||
*
|
||||
* @param user 用户信息
|
||||
* @return 结果
|
||||
*/
|
||||
@ -61,7 +61,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 修改用户信息
|
||||
*
|
||||
*
|
||||
* @param user 用户信息
|
||||
* @return 结果
|
||||
*/
|
||||
@ -69,7 +69,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 修改用户头像
|
||||
*
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @param avatar 头像地址
|
||||
* @return 结果
|
||||
@ -78,7 +78,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 重置用户密码
|
||||
*
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @param password 密码
|
||||
* @return 结果
|
||||
@ -87,7 +87,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 通过用户ID删除用户
|
||||
*
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 结果
|
||||
*/
|
||||
@ -95,7 +95,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 批量删除用户信息
|
||||
*
|
||||
*
|
||||
* @param userIds 需要删除的用户ID
|
||||
* @return 结果
|
||||
*/
|
||||
@ -103,7 +103,7 @@ public interface SysUserMapper
|
||||
|
||||
/**
|
||||
* 校验用户名称是否唯一
|
||||
*
|
||||
*
|
||||
* @param userName 用户名称
|
||||
* @return 结果
|
||||
*/
|
||||
@ -130,6 +130,6 @@ public interface SysUserMapper
|
||||
* @param openId
|
||||
* @return
|
||||
*/
|
||||
public SysUser selectWxUserByOpenIdOrPhone(@Param("openId") String openId,@Param("phone") String phone);
|
||||
public SysUser selectWxUserByOpenIdOrPhone(@Param("openId") String openId,@Param("phone") String phone,@Param("wxOpenId") String wxOpenId,@Param("unionId") String unionId);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user