解决mpvue小程序分享到朋友圈无效问题

手动修改一下mpvue这个包,在node_modules里面找到mpvue在index里面
搜索下onShareAppMessage找到

// 用户点击右上角分享
onShareAppMessage: rootVueVM.$options.onShareAppMessage
  ? function (options) { return callHook$1(rootVueVM, 'onShareAppMessage', options); } : null,

在这一段代码下面添加一个处理就可以了

// 分享朋友圈
onShareTimeline: rootVueVM.$options.onShareTimeline
  ? function (options) { return callHook$1(rootVueVM, 'onShareTimeline', options); } : null,

最好也在LIFECYCLE_HOOKS这个数组中把onShareTimeline这个添加进去

var LIFECYCLE_HOOKS = [
  'beforeCreate',
  'created',
  'beforeMount',
  'mounted',
  'beforeUpdate',
  'updated',
  'beforeDestroy',
  'destroyed',
  'activated',
  'deactivated', 'onLaunch',
  'onLoad',
  'onShow',
  'onReady',
  'onHide',
  'onUnload',
  'onPullDownRefresh',
  'onReachBottom',
  'onShareAppMessage',
  'onShareTimeline',
  'onPageScroll',
  'onTabItemTap',
  'attached',
  'ready',
  'moved',
  'detached'
];

然后打包,完美解决

如果项目中因为页面问题引入了例如mpvue-factory这种插件的还需要处理一下,用下面这个文件去处理吧,两个问题一起处理。

再高级一点的话,可以写一个fix命令,复制我下面的,放到build文件夹,检查下你们的相对路径是不是对的,不对的话改一下你们的文件目录指向,然后自己去package里面加命令执行这个文件,直接命令跑一下就可以

var chalk = require('chalk')
var path = require('path')
var fs = require('fs')
var data = ''
var dataFactory = ''

const hookConfig = '\'onShareAppMessage\','
const hookFn = '// 用户点击右上角分享\n' +
  '      onShareAppMessage: rootVueVM.$options.onShareAppMessage\n' +
  '        ? function (options) { return callHook$1(rootVueVM, \'onShareAppMessage\', options); } : null,'
const mpVueSrc = '../node_modules/mpvue/index.js'
const mpVueFactorySrc = '../node_modules/mpvue-page-factory/index.js'

const factoryHook = 'onShareAppMessage: App.onShareAppMessage ?\n' +
  '      function (options) {\n' +
  '        var rootVueVM = getRootVueVm(this);\n' +
  '        return callHook$1(rootVueVM, \'onShareAppMessage\', options);\n' +
  '      } : null,'
try {
  data = fs.readFileSync(path.join(__dirname, mpVueSrc), 'utf-8')
  if (data.indexOf('onShareTimeline') === -1) {
    data = replaceHook(data)
  }
  fs.writeFileSync(path.join(__dirname,  mpVueSrc), data)
} catch (e) {
  console.error(e)
}

try {
  dataFactory = fs.readFileSync(path.join(__dirname, mpVueFactorySrc), 'utf-8')
  if (dataFactory.indexOf('onShareTimeline') === -1) {
    dataFactory = replaceFactoryHook(dataFactory)
  }
  fs.writeFileSync(path.join(__dirname,  mpVueFactorySrc), dataFactory)
} catch (e) {
  console.error(e)
}

// 处理mpvue框架中没有处理onShareTimeline方法的问题
function replaceHook(str) {
  let res = str.replace(hookConfig, '\'onShareAppMessage\',\n' +
    '  \'onShareTimeline\',')
  res = res.replace(hookFn, '// 用户点击右上角分享\n' +
    '      onShareAppMessage: rootVueVM.$options.onShareAppMessage\n' +
    '        ? function (options) { return callHook$1(rootVueVM, \'onShareAppMessage\', options); } : null,\n' +
    '\n' +
    '      // 分享朋友圈\n' +
    '      onShareTimeline: rootVueVM.$options.onShareTimeline\n' +
    '        ? function (options) { return callHook$1(rootVueVM, \'onShareTimeline\', options); } : null,')

  return res
}

// 处理mpvue-factory插件中没有处理onShareTimeline方法的问题
function replaceFactoryHook(str) {
  let res = str.replace(factoryHook, 'onShareAppMessage: App.onShareAppMessage ?\n' +
    '      function (options) {\n' +
    '        var rootVueVM = getRootVueVm(this);\n' +
    '        return callHook$1(rootVueVM, \'onShareAppMessage\', options);\n' +
    '      } : null,\n' +
    '\n' +
    '    // 用户点击右上角分享\n' +
    '    onShareTimeline: App.onShareTimeline ?\n' +
    '      function (options) {\n' +
    '        var rootVueVM = getRootVueVm(this);\n' +
    '        return callHook$1(rootVueVM, \'onShareTimeline\', options);\n' +
    '      } : null,')
  return res
}
console.log(chalk.green(
  '  Tip: fix mpvue_share Success!'
))
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值