使用微信进行分享的接口
可用在多个项目中 套路一样 只需要改掉基础数据
import ajax from \'ajax\'
import { getQueryStr } from "../utils.js"
import { URL } from \'../config.js\'
let server = getQueryStr("server") || "hjwp";
let debug = getQueryStr("debug") || false;
class WeiXin {
constructor() {
let title = "分享的题目";
let desc = `可有可无`;
let link = `${URL.gzh}Redirect?url=fanpai&server=${server}&{shareId}`;
let imgUrl = `${URL.static}questionnaire/static/images/share.png`;
this.timeLine = { title, imgUrl }
this.app = { title, desc, imgUrl }
let param = {}
this.getJsConfig((data, err) => {
if (err) {
return false
};
wx.config(data);
wx.ready(() => {
wx.onMenuShareTimeline(this.timeLine)
wx.onMenuShareAppMessage(this.app)
wx.hideMenuItems({
menuList: [\'menuItem:refresh\', \'menuItem:copyUrl\', \'menuItem:originPage\', \'menuItem:openWithQQBrowser\', \'menuItem:openWithSafari\', \'menuItem:share:qq\', \'menuItem:share:QZone\']
})
wx.showMenuItems({
menuList: [\'menuItem:share:timeline\', \'menuItem:share:appMessage\']
})
this.handleFontSize();
})
})
}
getJsConfig(callback) {
let url = `${URL.gzh}wechatapi/GetJsconfig?debug=${debug}`;
ajax.get(url, \'GET\', function(data, err) {
callback(data, err)
})
}
setShareTimeLine(timeLine) {
let { title, link, imgUrl, callback } = timeLine
this.timeLine.title = title ? title : this.timeLine.title
this.timeLine.link = title ? title : this.timeLine.link
this.timeLine.imgUrl = title ? title : this.timeLine.imgUrl
if (typeof callback == "function") {
this.timeLine.success = callback
}
}
setShareApp(app) {
let { title, link, imgUrl, callback } = app
this.app.title = title ? title : this.app.title
this.app.desc = title ? title : this.app.desc
this.app.link = title ? title : this.app.link
this.app.imgUrl = title ? title : this.app.imgUrl
if (typeof callback == "function") {
this.app.success = callback
}
}
setShareId(shareId) {
this.timeLine.link = this.timeLine.link.replace(\'{shareId}\', "shareId=" + shareId)
this.app.link = this.app.link.replace(\'{shareId}\', "shareId=" + shareId) } handleFontSize() { // 设置网页字体为默认大小
if (typeof WeixinJSBridge == "object" && typeof WeixinJSBridge.invoke == "function") {
WeixinJSBridge.invoke(\'setFontSizeCallback\', { \'fontSize\': 0 }); // 重写设置网页字体大小的事件
WeixinJSBridge.on(\'menu:setfont\', function() { WeixinJSBridge.invoke(\'setFontSizeCallback\', { \'fontSize\': 0 }); }); } }}export default new WeiXin()