代码:
前端页面
需要引入http://res.wx.qq.com/open/js/jweixin-1.4.0.js
后台
import com.ljzforum.base.enums.APIResponseCode;
import com.ljzforum.base.vo.APIResultVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
-
@author : 恒果果
-
create at: 2019-12-09 10:48
-
@description: 微信分享等
*/
@RestController
public class WxController {
@Resource
private WXIConfig wxiConfig;
@RequestMapping(“/wxShare”)
public ModelAndView wxShare(String url){
ModelAndView view = new ModelAndView();
view.addObject(“data”,wxShareN(url))
return view ;
}
}
/**
- 微信分享
*/
public LinkedHashMap<String,Object> wxShareN(String url) {
//微信分享
LinkedHashMap<String,Object> map = new LinkedHashMap<>();
try {
String ticket = wxService.getJssdkTicketN();
String shareUrl = url;
map.put(“appId”, wxiConfig.getAppId());
Map<String, String> signMap = WxJSSDKSign.sign(ticket, shareUrl);
map.put(“signMap”,signMap);
} catch (Exception e) {
log.error(“获取微信的JSSDK接口失败…”, e);
}
return map;
}
WXIConfig
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ljzforum.common.constant.RedisContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
-
微信接口的服务信息
-
@author
*/
@Service
@Slf4j
public class WxService {
@Resource
private RedisTemplate redisTemplate;
@Resource
private WXIConfig wxiConfig;
/**
- 获取JSSDK的秘钥
*/
public String getJssdkTicket(String access_token) {
String ticket = (String) redisTemplate.opsForValue().get(RedisContent.WX_OPEN_TOKEN_TICKET + access_token);
if (ticket == null) {
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put(“access_token”, access_token);
params.put(“type”, “jsapi”);
JSONObject result = JSONUtil.parseObj(HttpUtil.get(WXIConfig.WX_JS_SDK_GET_TICKET, params));
if (result.containsKey(“errcode”) && !result.getStr(“errmsg”).equals(“ok”)) {
log.error(“获取ticket出错:” + result.toString());
return null;
}
ticket = result.getStr(“ticket”);
redisTemplate.opsForValue().set(RedisContent.WX_OPEN_TOKEN_TICKET + access_token, ticket, 30, TimeUnit.MINUTES);
}
return ticket;
}
/**
- 获取JSSDK的秘钥
*/
public String getJssdkTicket() {
String access_token = getCgiBinTokenN();
if (access_token == null) {
return null;
}
String ticket;
try {
ticket = getJssdkTicket(access_token);
} catch (Exception e) {
log.error(“redisL里的的token不正确,重新从接口获取token”);
access_token = getCgiBinTokenN();
ticket = getJssdkTicket(access_token);
}
return ticket;
}
/**
- 获取Token数据
*/
public String getCgiBinTokenN() {
String access_token = (String) redisTemplate.opsForValue().get(RedisContent.WX_CGI_BIN_TOKEN + wxiConfig.getAppId());
if (access_token == null) {
Map<String, Object> params = new LinkedHashMap<String, Object>();
params.put(“grant_type”, “client_credential”);
params.put(“appid”, wxiConfig.getAppId());
params.put(“secret”, wxiConfig.getAppSecret());
JSONObject result = JSONUtil.parseObj(HttpUtil.get(WXIConfig.WX_CGI_BIN_GET_TOKEN, params));
if (result.containsKey(“errcode”) && result.getStr(“errcode”) != null) {
result = JSONUtil.parseObj(HttpUtil.get(WXIConfig.WX_CGI_BIN_GET_TOKEN, params));
}
access_token = result.getStr(“access_token”);
redisTemplate.opsForValue().set(RedisContent.WX_CGI_BIN_TOKEN + wxiConfig.getAppId(), access_token, 30, TimeUnit.MINUTES);
}
return access_token;
}
}
WXIConfig
import com.ljzforum.common.util.CustomizedPropertyConfigurer;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class WXIConfig {
//微信支付分配的商户号
@Value(“${mch_id}”)
public String mchId;
//支付的私人秘钥
@Value(“${app_pay_key}”)
public String appPayKey;
//获取APPID
@Value(“${app_id}”)
public String appId;
//获取微信APP_SECRET
@Value(“${app_secret}”)
public String appSecret;
//获取小程序APPID
@Value(“${mini_appid}”)
public String miniAppid;
//获取小程序SECRET
@Value(“${mini_secret}”)
public String miniSecret;
//临时的整型参数值
public static String QR_SCENE = “QR_SCENE”;
//临时的字符串参数值
public static String QR_STR_SCENE = “QR_STR_SCENE”;
//永久的整型参数值
public static String QR_LIMIT_SCENE = “QR_LIMIT_SCENE”;
//永久的字符串参数值
public static String QR_LIMIT_STR_SCENE = “QR_LIMIT_STR_SCENE”;
//统一下单
public static String WX_UNIFIED_ORDER = “https://api.mch.weixin.qq.com/pay/unifiedorder”;
//获取access token
public static String WX_TOKEN = “https://api.weixin.qq.com/cgi-bin/token”;
//获取微信服务器IP地址
public static String WX_TOKEN_CALLBACK_IP = “https://api.weixin.qq.com/cgi-bin/getcallbackip”;
//用户同意授权,获取code
public static String WX_GET_USERINFO_AUTHORIZE = “https://open.weixin.qq.com/connect/oauth2/authorize”;
//通过code换取网页授权access_token
public static String WX_GET_USERINFO_ACCESS_TOKEN = “https://api.weixin.qq.com/sns/oauth2/access_token”;
//刷新access_token(如果需要)
public static String WX_GET_USERINFO_REFRESH_TOKEN = “https://api.weixin.qq.com/sns/oauth2/refresh_token”;
//刷新拉取用户信息(需scope为 snsapi_userinfo)
public static String WX_GET_USERINFO = “https://api.weixin.qq.com/sns/userinfo”;
//通过access_token、openid获取用户信息
public static String WX_GET_INFO = “https://api.weixin.qq.com/cgi-bin/user/info”;
//获取微信用户素材
public static String WX_BATCH_GET_MATERIAL = “https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=”;
//获取微信用户素材总数
public static String WX_GET_MATERIAL_COUNT = “https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=”;
//获取微信永久素材
public static String WX_GET_MATERIAL = “https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=”;
//创建微信菜单
public static String WX_CREATE_MENU = “https://api.weixin.qq.com/cgi-bin/menu/create?access_token=”;
//查询微信菜单
public static String WX_GET_MENU = “https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=”;
//删除微信菜单
public static String WX_DELETE_MENU = “https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=”;
//微信退款
public static String WX_REFUND_URL = “https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=”;
//获取access_toke
public static String WX_CGI_BIN_GET_TOKEN = “https://api.weixin.qq.com/cgi-bin/token”;
//长链接转短链接接口
public static String WX_GET_SHORT_URL = “https://api.weixin.qq.com/cgi-bin/shorturl?access_token=”;
//新增其他类型永久素材
public static String WX_MATERIAL_ADD_MATERIAL = “http://api.weixin.qq.com/cgi-bin/material/add_material?access_token=%1s&type=%2s”;
/**
- 获取JSSDK调研说需要的钥匙
*/
public static String WX_JS_SDK_GET_TICKET = “https://api.weixin.qq.com/cgi-bin/ticket/getticket”;
//微信开放平台公众号消息加解密Key
public static String WX_PLATFORM_ENCODING_AES_KEY = (String) CustomizedPropertyConfigurer.getContextProperty(“wx_platform_encoding_aes_key”);
//微信开放平台公众号消息校验Token
public static String WX_PLATFORM_TOKEN = (String) CustomizedPropertyConfigurer.getContextProperty(“wx_platform_token”);
//微信开放平台APPID
public static String WX_PLATFORM_APP_ID = (String) CustomizedPropertyConfigurer.getContextProperty(“wx_platform_app_id”);
//微信开放平台AppSecret
public static String WX_PLATFORM_APP_SECRET = (String) CustomizedPropertyConfigurer.getContextProperty(“wx_platform_app_secret”);
//获取第三方平台component_access_token
public static String WX_GET_COMPONENT_ACCESS_TOKEN = “https://api.weixin.qq.com/cgi-bin/component/api_component_token”;
//获取预授权码pre_auth_code
public static String WX_GET_PRE_AUTH_CODE = “https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=”;
//获取公众号的接口调用凭据和授权信息
public static String WX_API_QUERY_AUTH = “https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=”;
public static String WX_API_REFRESH_TOKEN = “https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=”;
public static String WX_GET_AUTHORIZER_INFO = “https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=”;
/**
- 微信通知信息推送模版
*/
//模版id
public static String WX_TEMPLATE_ID_SUCCESS_SIGNUP = “w64cYpbQWD65KR6cziKipXNnBowEOSheqc7uoKUTR70”;
public static String WX_TEMPLATE_ID_ACTIVITY_MESSAGE = “jVXOi_SxSpGA9t9dHCHA7Ss50a4Y8KPLFVnCas5EaIc”;
//发送模版信息
public static String WX_SEND_TEMPLATE = “https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=”;
//微信短链生成接口
public static String WX_SHORT_URL = “https://api.weixin.qq.com/cgi-bin/shorturl?access_token=”;
public static String USER_LIST_URL = “https://api.weixin.qq.com/cgi-bin/user/get?access_token=”;
//生成带参数的二维码
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
“https://api.weixin.qq.com/cgi-bin/user/get?access_token=”;
//生成带参数的二维码
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-TQwkuH8G-1714839385495)]
[外链图片转存中…(img-xgTbdEBC-1714839385495)]
[外链图片转存中…(img-1yvZ7Jvb-1714839385495)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!