vue_h5_tools
Version:
vue h5 开发小工具
82 lines (79 loc) • 2.79 kB
JavaScript
import Vue from 'vue'
import wx from 'weixin-js-sdk'
function getJssdk() {
return new Promise(async(resolve, reject) => {
try {
const res = await Vue.axios.post(Vue.$h5.api_path.wechat_jssdk, {
url: location.origin + location.pathname + location.search,
debug: location.hash.indexOf('wechat_debug') > -1 ? true : Vue.$h5.wechat.debug,
apis: Vue.$h5.wechat.apis
})
wx.config(res.jssdk)
wx.error(function(res) {
Vue.prototype.$alert('wx.config配置出错')
reject('wx.config配置出错')
})
wx.ready(function() {
wx.checkJsApi({
// jsApiList: ['chooseImage', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
jsApiList: Vue.$h5.wechat.apis, // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
resolve()
console.log(res)
// alert(JSON.stringify(res))
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
})
})
} catch (error) {
reject('jssdk接口读取错误')
}
})
}
// 微信配置
export default {
async init() {
try {
await getJssdk()
// await this.setShareOption()
} catch (e) {
console.error(e)
}
},
setShareOption(options, userRecordData = {}) {
const params = options ? JSON.parse(JSON.stringify(options)) : {}
const defalutValue = JSON.parse(JSON.stringify(Vue.$h5.wechat.shareContent.default))
const data = Object.assign(defalutValue, params)
data.link != null && data.link.indexOf('http') !== 0 ? data.link = Vue.$h5.wechat.baseurl + data.link : null
data.imgUrl && data.imgUrl.indexOf('http') !== 0 ? data.imgUrl = Vue.$h5.wechat.baseurl + data.imgUrl : null
const recordData = Object.assign({ link: data.link }, userRecordData)
data.success_share = () => {
data.success && data.success()
// 保存分享数据
Vue.axios.post(Vue.$h5.api_path.save_share, recordData)
}
data.cancel_share = () => {
data.cancel && data.cancel()
}
console.log(data, recordData)
// Vue.prototype.$alert(JSON.stringify(data))
wx.onMenuShareTimeline({
title: data.title,
link: data.link,
imgUrl: data.imgUrl,
success: data.success_share,
cancel: data.cancel_share
})
wx.onMenuShareAppMessage({
title: data.title,
desc: data.desc,
link: data.link,
imgUrl: data.imgUrl,
type: 'link',
dataUrl: '',
success: data.success_share,
cancel: data.cancel_share
})
}
}