app混合开发之微信分享设置

在app内混合开发中,分享页面是必不可少的,但是分享到微信时二次分享就会没有分享的设置,这时候就需要自己配置微信二次分享,使用说明请查看微信JS-SDK说明文档

//API.js

import axios from 'axios'

export function getWeixinJsSdkConfig(apis = []) {
  let location_href = encodeURIComponent(window.location.href.split('#')[0])
  let url = `${host}/share/config?url=${location_href}&${
    apis.map(api_name => `jsApiList[]=${api_name}`).join("&")
    }`

  return axios.get(url).then(res => {
    let res_data = res.data;
    if (res_data.code !== 0) throw res_data.msg;
    return res_data.data;
  })
}
//WXshare.js

//获取jssdk的接口
import { getWeixinJsSdkConfig } from "../api/API";


// TODO: 配置
export const weixinJsSDKAuth = new Promise((resolve) => {
  let timeout = setTimeout(function () {
    console.log("微信jssdk授权验证 等待 10000 ms 超时, 可能不在微信环境...");
    resolve(false);
  }, 10000);

  wx.ready(_ => {
    clearTimeout(timeout);
    resolve(true)
  });
  wx.error(function (res) {
    clearTimeout(timeout);
    console.error("wx.error 微信jssdk授权验证失败 %o", res);
    resolve(false);
  });

  // TODO: 在这里请求jssdk的配置信息
  (
    window._jssdk_config ?
    Promise.resolve(window._jssdk_config) :
    (getWeixinJsSdkConfig(['onMenuShareTimeline', 'onMenuShareAppMessage', 'chooseWXPay']))
  ).then(config => {
      console.log('微信JSSDK授权信息: ', config)
      wx.config(config || {});
    });
})

const wx_share_title = document.title; // TODO: 默认微信分享标题在这
const wx_share_detail = '嘻嘻嘻嘻嘻嘻!'; // TODO: 默认分享话术在这
const wx_share_img = require('../../../static/logo.jpg');
//微信分享时会讲#号后的截断,所以手动加上location.hash,防止地址错误
const wx_share_link = window.location.href.split('#')[0] + location.hash ;


// TODO: 微信二次分享公共设置
export const wxShareMessages = (shareTitle, shareDesc, shareLink, shareImg, shareSuccess) => {
  let options = {
    title: shareTitle || wx_share_title ,
    desc: shareDesc || wx_share_detail 
    link: shareLink || wx_share_link ,
    imgUrl: shareImg || wx_share_img ,
    success: shareSuccess || (_ => void 0)
  }

  console.log('微信转发参数: ', options)

  weixinJsSDKAuth.then(boo => {
    if (boo) {
      wx.onMenuShareTimeline(options)
      wx.onMenuShareAppMessage(options)
    }
  })
}

//挂载在window对象上,方便全局调用
window.wxShareMessages = wxShareMessages ;

调用方法

import { wxShareMessages } from "../misc/wxShare"
          // 微信分享回调
          //第一种使用
         wxShareMessages ( )

          //第二种使用
         wxShareMessages ( 
            title, '啧啧啧啧啧啧啧啧啧', null, img, () => {  分享时执行xx方法}
            })
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值