# 微信封装 **Repository Path**: 34351088/wechat-packaging ## Basic Information - **Project Name**: 微信封装 - **Description**: 微信对接封装,内置缓存,实现授权,签名、消息推送、ticket自动更新缓存(1.5小时) - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2022-05-27 - **Last Updated**: 2022-05-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 微信封装 #### 介绍 微信对接封装,内置缓存,实现授权,签名、消息推送、ticket自动更新缓存(1.5小时) #### 软件架构 java封装 #### 安装教程 ### 将项目打包并引入 ### 一、引入jar包 ##### 1,项目根目录下创建lib文件夹,并添加jar包 ##### 2,修改pom.xml 文件 ##### ①,引入依赖 wx.control.centre wx-control-centre-1.0 1.0 system ${pom.basedir}/lib/wx-control-centre-1.0.jar ##### ②.1打包配置(war包) org.apache.maven.plugins maven-war-plugin ${basedir}\lib WEB-INF/lib/ **/*.jar ####②.2打包配置(jar包) org.springframework.boot spring-boot-maven-plugin true #### 使用说明 1. WeChatAuthServer 微信网页授权 2. WeChatSNSAuthServer 网页授权,获取相关信息 3. WeChatCgiBinServer 基础接口调用 4. WeChatSignatureServer 签名服务 5. WeChatMsgServer 消息推送 #### 1.调用说明 xxxxServer.serverName(param); #### ①.WeChatAuthServer ###### 1.获取授权回调地址 ###### getAuthorizeUrl(String APPID, String REDIRECT_URI, String SCOPE) |参数 | 说明 | | -------- | -------- | | APPID |第三方用户唯一凭证 | | REDIRECT_URI | 授权后重定向的回调链接地址,无需使用urlEncode对链接进行处理 | | SCOPE | 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 ) | ###### 2.获取授权回调地址-静默授权(不弹出授权页面,直接跳转,只能获取用户openid) ###### getAuthorizeUrlByBase(String APPID,String REDIRECT_URI) ###### 3.获取授权回调地址-非静默授权(弹出授权页面,可通过openid拿到昵称、性别、所在地) ###### getAuthorizeUrlByUserInfo(String APPID,String REDIRECT_URI) #### ②.WeChatSNSAuthServer ###### 1.获取网页授权凭证(主要网页授权Access_Token和openId) ###### 注:授权回调返回的code值只能使用一次 ###### getOauth2TokenByCode(String appId, String appSecret,String Code) |参数 | 说明 | | -------- | -------- | | appId | 公众号的唯一标识 | | appSecret | 公众号的appsecret | | Code | 填写授权后获取的code参数(跟在回调路由后) | ####返回网页授权凭证,用WeixinOauth2Token接收 |参数 | 说明 | | -------- | -------- | | accessToken | 网页授权接口调用凭证 | | expiresIn | 凭证有效时长 | | refreshToken | 用于刷新凭证| | openId | 用户标识| | scope | 用户授权作用域| ###### 2.获取openId ###### getOpenIdByCode(String appId, String appSecret,String Code) |参数 | 说明 | | -------- | -------- | | appId | 公众号的唯一标识 | | appSecret | 公众号的appsecret | | Code | 填写授权后获取的code参数(跟在回调路由后) | ###### 3.获取微信用户信息-网页授权作用域为snsapi_userinfo ###### getSnsUserInfo(String openId,String accessToken) |参数 | 说明 | | -------- | -------- | | openId | 用户的openId | | accessToken | 第一步获取的Access_Token | ___ ##### 返回用户信息,使用 SNSUserInfo 对象接收 |参数 | 说明 | | -------- | -------- | | openId | 用户标识 | | nickname | 用户昵称 | | sex | 性别(1是男性,2是女性,0是未知) | | country | 国家 | | province | 省份 | | city | 城市 | | headImgUrl | 用户头像链接 | | privilegeList | 用户特权信息(即将废弃) | | unionid | 用户unionid,需要绑定开放平台才会获取到 | ###### 4. 获取refresh_token 用于刷新access_token(如果需要) ###### getRefreshTokenByCode(String appId, String appSecret,String Code) |参数 | 说明 | | -------- | -------- | | appId | 公众号的唯一标识 | | appSecret | 公众号的appsecret | | Code | 填写授权后获取的code参数(跟在回调路由后) | ###### 5. 刷新access_token ###### refreshToken(String appId,String refreshToken) |参数 | 说明 | | -------- | -------- | | appId | 公众号的唯一标识 | | refreshToken | 通过getRefreshTokenByCode获取到的refresh_token参数值 | ###### 6. 检验授权凭证(access_token)是否有效 ###### checkSnsAuth(String openId,String accessToken) |参数 | 说明 | | -------- | -------- | | openId | 用户标识 | | accessToken | 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同| #### ③.WeChatCgiBinServer ###### 1.获取token。已缓存 ###### getToken(String appId,String appSecret) |参数 | 说明 | | -------- | -------- | | appId | 公众号的唯一标识 | | appSecret | 公众号的appsecret | ###### 2.获取ticket。已缓存 ###### getTicket(String accessToken) |参数 | 说明 | | -------- | -------- | | accessToken | getToken()获取的值 | #### ④.WeChatSignatureServer ###### 1.获取签名,通过appId和secert ###### getSignature(String AppId,String SECRET,String url) |参数 | 说明 | | -------- | -------- | | AppId | 公众号的唯一标识 | | SECRET | 公众号的appsecret | | url | 当前网页的URL,不包含#及其后面部分 | ###### 2.获取签名,通过jsapiTicket ###### getSignatureByTicket(String AppId,String jsapiTicket,String url) |参数 | 说明 | | -------- | -------- | | AppId | 公众号的唯一标识 | | jsapiTicket | ticket(WeChatCgiBinServer.getTicket(String accessToken)) | | url | 当前网页的URL,不包含#及其后面部分 | ---- ##### 返回数据使用JSONObject接收 |参数 | 说明 | | -------- | -------- | | state | 状态值 | | appId | 公众号的唯一标识 | | timestamp | 时间戳 | | nonceStr | 随机码 | | signature | 签名 | | jsapi_ticket | ticket | ##### ⑤.WeChatMsgServer ###### 1.根据模板id推送消息-接收对象 Message ###### templateSend(Message vo, String accessToken) |参数 | 说明 | | -------- | -------- | | toUser | 接收者openid | | templateId | 模板ID | | title | 标题 | | remark | 备注 | | keyword1 | keyword1 | | keyword2 | keyword2 | | keyword3 | keyword3 | | keyword4 |keyword4| | keyword5 | keyword5 | | smUrl | 模板跳转链接(海外帐号没有跳转能力) | | smAppId | 所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系,暂不支持小游戏) 不跳转小程序可不填| | smPagePath | 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar),要求该小程序已发布,暂不支持小游戏)不跳转小程序可不填 | | remarkColor | 备注字体颜色,不填默认为黑色 | | titleColor | 标题字体颜色,不填默认为黑色 |