In doing custom-sharing, uses micro-channel jssdk , record it.
Disclaimer: This article demonstrates uniapp used jssdk , as an example of a custom web page to share
npm manner using mounting instructions below, the body of non npm manner.
npm install jweixin-module --save
1. Download the file to import jssdk
Download Link: https://www.lanzous.com/i8ujkvi
After downloading in the project, the specific placement can choose, such as directly on the root directory, or on components directory, the following reference is my screenshot:
2, references in the project
For ease of use, we have a single micro channel associated js file, the associated initialization operations.
wechat.js
// 自己封装的 uni.request 工具类
var request = require("./jsTools/request.js");
var jweixin = require('./components/jweixin-module/index.js');
export default {
//判断是否在微信中
isWechat: function() {
var ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/micromessenger/i) == 'micromessenger') {
// console.log('是微信客户端')
return true;
} else {
// console.log('不是微信客户端')
return false;
}
},
//初始化sdk配置
initJssdkShare: function(callback, url) {
console.log("init Url : "+url)
// 这是我这边封装的 request 请求工具,实际就是 uni.request 方法。
request.post(
'http://127.0.0.1:8080/mptask/api/getSignPackage',
{
url: url
},
"form",
function(res){
let success = res["success"];
let result = res["result"];
if(success){
jweixin.config({
debug: false,
appId: result.appId,
timestamp: result.timestamp,
nonceStr: result.nonceStr,
signature: result.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage'
]
});
//配置完成后,再执行分享等功能
if (callback) {
callback(result);
}
}
}
);
},
//在需要自定义分享的页面中调用
share: function(data, url) {
url = url ? url : window.location.href;
console.log("url:"+url)
if (!this.isWechat()) {
return;
}
//每次都需要重新初始化配置,才可以进行分享
this.initJssdkShare(function(signData) {
jweixin.ready(function() {
var shareData = {
title: data && data.title ? data.title : signData.site_name,
desc: data && data.desc ? data.desc : signData.site_description,
link: url,
imgUrl: data && data.img ? data.img : signData.site_logo,
success: function(res) {
// 分享后的一些操作,比如分享统计等等
},
cancel: function(res) {}
};
//分享给朋友接口
jweixin.onMenuShareAppMessage(shareData);
//分享到朋友圈接口
jweixin.onMenuShareTimeline(shareData);
});
}, url);
},
}
He says there's a simple method:
- isWechat : determining whether the current channel is a micro-environment
- initJssdkShare : background request interface data, initialization sdk
- Share : share custom methods used in this
main.js cited:
The upper wechat.js defined as vue global properties, easier to select.
import wechat from './wechat.js'
if(wechat.isWechat()){
Vue.prototype.$wechat =wechat;
}
3, background method (Java) "signature method"
Based on open source SDK WxJava
/**
* 获取jssdk签名
* @return
*/
@RequestMapping(value = "/getSignPackage", method = RequestMethod.POST)
public Result<Object> getSignPackage(String url) throws WxErrorException {
return new ResultUtil<Object>().setData(wxMpService.createJsapiSignature(url));
}
补充:关于 WxJava 的使用可以参考上一篇 微信h5非内部浏览器支付,该方法接收一个 url 参数,即前端用来分享的 url。
如下是详细的实现:
public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException {
long timestamp = System.currentTimeMillis() / 1000L;
String randomStr = RandomUtils.getRandomStr();
String jsapiTicket = this.getJsapiTicket(false);
String signature = SHA1.genWithAmple(new String[]{"jsapi_ticket=" + jsapiTicket, "noncestr=" + randomStr, "timestamp=" + timestamp, "url=" + url});
WxJsapiSignature jsapiSignature = new WxJsapiSignature();
jsapiSignature.setAppId(this.getWxMpConfigStorage().getAppId());
jsapiSignature.setTimestamp(timestamp);
jsapiSignature.setNonceStr(randomStr);
jsapiSignature.setUrl(url);
jsapiSignature.setSignature(signature);
return jsapiSignature;
}
4、前端使用
在需要自定义分享的界面,简单举个例子,以首页为例:
if (this.$wechat && this.$wechat.isWechat()) {
this.$wechat.share({
desc: "精选优质、有价值的好文章,转发给身边的人",
img: "http://img.sscai.club/click.jpeg"
});
}
补充:share 方法有个 url 参数,如不指定则使用当前页面url,具体参考 wechat.js
使用截图如下所示:
文章最后
博客地址:https://www.cgblog.com/niceyoo
如果觉得这篇文章有丶东西,不妨关注一下我,关注是对我最大的鼓励~
18年专科毕业后,期间一度迷茫,最近我创建了一个公众号用来记录自己的成长。