javaweb分享功能实现_开发(⼀)基于Wx-java的分
享功能
最近在做服务号开发,简单总结⼀下,便于⾃⼰学习积累和分享给⼤家:
环境介绍:
Spring+ Spring MVC +Mybatis
开发语⾔:
JAVA
公众平台的开发中,只公布了⼀个基于Http协议的接⼝和加解密的算法sdk,在开发的过程中可以⾃⼰根据需求来配置数据,调⽤
相关接⼝,也可以引⽤⽐较成熟的基于java语⾔的sdk。笔者引⽤的是⽬前⽐较流⾏的
weixin-java-tools
可以参考:点击打开链接
项⽬中引⼊了weixin-java-mp 就可直接调⽤相关功能接⼝和完成jssdk认证
com.github.binarywang
weixin-java-mp
3.3.0
com.github.binarywang
(不同模块参考下⽂)
3.3.0
各模块的artifactId:
⼩程序:weixin-java-miniapp
⽀付:weixin-java-pay
开放平台:weixin-java-open
(包括和服务号):weixin-java-mp
企业号/企业:weixin-java-cp
MP_js_api
相关官⽅⽂档
WxMpService提供了以下⼏个⽅法:
public String getJsapiTicket() throwsWxErrorException;public String getJsapiTicket(boolean forceRefresh) throwsWxErrorException;public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;
分享接⼝:
1.后台准备:有趣的java小程序
建⼀个公⽤控制类: WeXinJsSdkController ,通过ajax 访问完成jssdk认证:
importme.chanjar.weixinmon.bean.WxJsapiSignature;importme.WxErrorException;importme.c
@Controller
@RequestMapping("jssdk")public classWeXinJsSdkController {
@AutowiredprivateWxMpService wxMpService;
@RequestMapping(value= "/config", method =RequestMethod.GET)
@ResponseBodypublicWxJsapiSignature wxJsSdkConfig(HttpServletRequest request, String url) {try { //直接调⽤wxMpServer 接⼝
WxJsapiSignature wxJsapiSignature =ateJsapiSignature(url);returnwxJsapiSignature;
}catch(WxErrorException e) {return null;
}
}
}
packageme.chanjar.weixinmon.bean;importjava.beans.ConstructorProperties;importjava.io.Serializable;public class WxJsapiSignature implementsSerializable {private static final long serialVersionUID = -
1116808193154384804L;privateString appId;//参数名均为⼩写字符privateString nonceStr; //参数名均为⼩写字符private long timestamp; //参数名均为⼩写字符privateString url;//包括'http(s)://'部分,以及'?'后⾯的GET参数部分,但不包括'#'hash后⾯的部分。privateString signature; //参数名均为⼩写字符public staticWxJsapiSignature.WxJsapiSignatureBuilder builder() {return newWxJsapiSignature.WxJsapiSignatureBuilder();
}publicString getAppId() {return this.appId;
}publicString getNonceStr() {Str;
}public longgetTimestamp() {return this.timestamp;
}publicString getUrl() {return this.url;
}publicString getSignature() {return this.signature;
}public voidsetAppId(String appId) {this.appId =appId;
}public voidsetNonceStr(String nonceStr) {Str =nonceStr;
}public void setTimestamp(longtimestamp) {this.timestamp =timestamp;
}public voidsetUrl(String url) {this.url =url;
}public voidsetSignature(String signature) {this.signature =signature;
}public booleanequals(Object o) {if (o == this) {return true;
}else if (!(o instanceofWxJsapiSignature)) {return false;
}else{
WxJsapiSignature other=(WxJsapiSignature)o;if (!other.canEqual(this)) {return false;
}else{
label63: {
Objectthis$appId = AppId();
Object other$AppId();if (this$appId == null) {if (other$appId == null) {breaklabel63;
}
}else if (this$appId.equals(other$appId)) {breaklabel63;
}return false;
}
Objectthis$nonceStr = NonceStr();
Object other$NonceStr();if (this$nonceStr == null) {if (other$nonceStr != null) {return false;
}
}else if (!this$nonceStr.equals(other$nonceStr)) {return false;
}if (Timestamp() !=Timestamp()) {return false;
}else{
Objectthis$url = Url();
Object other$Url();if (this$url == null) {if (other$url != null) {return false;
}
}else if (!this$url.equals(other$url)) {return false;
}
Objectthis$signature = Signature();
Object other$Signature();if (this$signature == null) {if (other$signature != null) {return false;
}
}else if (!this$signature.equals(other$signature)) {return false;
}return true;
}
}
}
}protected booleancanEqual(Object other) {return other instanceofWxJsapiSignature;
}public inthashCode() {int PRIME = true;int result = 1;
Object $appId= AppId();int result = result * 59 + ($appId == null ? 43: $appId.hashCode());
Object $nonceStr= NonceStr();
result= result * 59 + ($nonceStr == null ? 43: $nonceStr.hashCode());long $timestamp = Timestamp();
result= result * 59 + (int)($timestamp >>> 32 ^$timestamp);
Object $url= Url();
result= result * 59 + ($url == null ? 43: $url.hashCode());
Object $signature= Signature();
result= result * 59 + ($signature == null ? 43: $signature.hashCode());returnresult;
}publicString toString() {return "WxJsapiSignature(appId=" + AppId() + ", nonceStr=" + NonceStr() + ", timestamp=" + Timestamp() + ", url=" + Url() + ", signature=" + Signature() + ")";
}publicWxJsapiSignature() {
}
@ConstructorProperties({"appId", "nonceStr", "timestamp", "url", "signature"})public WxJsapiSignature(String appId, String nonceStr, longtimestamp, String url, String signature) {this.appId =Str =nonceStr;this.timestamp
=timestamp;this.url =url;this.signature =signature;
}public static classWxJsapiSignatureBuilder {privateString appId;privateString nonceStr;private longtimestamp;privateString url;privateString signature;
WxJsapiSignatureBuilder() {
}publicWxJsapiSignature.WxJsapiSignatureBuilder appId(String appId) {this.appId =appId;return this;
}publicWxJsapiSignature.WxJsapiSignatureBuilder nonceStr(String nonceStr) {Str =nonceStr;return this;
}public WxJsapiSignature.WxJsapiSignatureBuilder timestamp(longtimestamp) {this.timestamp =timestamp;return this;
}publicWxJsapiSignature.WxJsapiSignatureBuilder url(String url) {this.url =url;return this;
}publicWxJsapiSignature.WxJsapiSignatureBuilder signature(String signature) {this.signature =signature;return this;
}publicWxJsapiSignature build() {return new WxJsapiSignature(this.appId, Str, this.timestamp, this.url,
this.signature);
}publicString toString() {return "WxJsapiSignature.WxJsapiSignatureBuilder(appId=" + this.appId + ", nonceStr=" +
}
}
}
2.页⾯初始化js 配置相关接⼝:
/*初始化jssdk 分享功能*/$.get("${basePath}/jssdk/config.do",{url:window.location.href},function(data,status){if(status == "success"){
debug:false, //开启调试模式,调⽤的所有api的返回值会在客户端alert出来,若要查看传⼊的参数,可以在pc端打开,参数信息会通过log 打出,仅在pc端时才会打印。
appId: data.appId, //必填,的唯⼀标识
timestamp: data.timestamp, //必填,⽣成签名的时间戳
nonceStr: Str, //必填,⽣成签名的随机串
signature: data.signature,//必填,签名,见附录1
jsApiList: ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ'] //必填,需要使⽤的JS接⼝列表,所有JS接⼝列表见附录2
});
//对于⽤户触发时才调⽤的接⼝,则可以直接调⽤,不需要放在ready函数中。
var shareData ={
title:'${ainsubject}',
desc:'${description}',//link: 'www.baidu', // 分享域名要和当前域名保持⼀致!!
imgUrl: 'fd/tektcrm/EventNoti/images/attendance.png',
success:function(res) {//alert('已分享');
},
cancel:function(res) {//alert('已取消');
},
fail:function(res) {//alert(JSON.stringify(res));
}
};wx.onMenuShareAppMessage(shareData);wx.onMenuShareTimeline(shareData);wx.onMenuShareQQ(shareData);
});
<(function(res){//config信息验证失败会执⾏error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,//也可以在返回的res参数中查看,对于SPA可以在这⾥更新签名。//layer.msg(res);
});
}
},"json");
学习在于不断地探索、思考和总结记录,欢迎喜欢的朋友们在下⽅留⾔,与君共同进步!
---------------------
作者:Java⼩爬⾍
来源:CSDN
版权声明:本⽂为博主原创⽂章,转载请附上博⽂链接!
签名算法
签名⽣成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前⽹页的URL,不包含#及其后⾯部分) 。对所有待签名参数按照字段名的ASCII 码从⼩到⼤排序(字典序)后,使⽤URL键值对的格式(即
key1=value1&key2=value2…)拼接成字符串string1。这⾥需要注意的是所有参数名均为⼩写字符。对string1作sha1加密,字段名和字段值都采⽤原始值,不进⾏URL 转义。
即signature=sha1(string1)。 ⽰例:
noncestr=Wm3WZYTPz0wzccnW
jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-
HhTdfl2fzFy1AOcHKP7qg
timestamp=1414587457
步骤1. 对所有待签名参数按照字段名的ASCII 码从⼩到⼤排序(字典序)后,使⽤URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1:
步骤2. 对string1进⾏sha1签名,得到signature:
0f9de62fce790f9a083d5c99e95740ceb90c27ed
注意事项
1.签名⽤的noncestr和timestamp必须与wx.config中的nonceStr和timestamp相同。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论