#微信开发平台

https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

#系统内置分享

share
http://www.html5plus.org/doc/zh_cn/share.html

示例

  1. plus.share.sendWithSystem({
  2. type:'text',
  3. content:'我要分享一个内容',
  4. href:'http://cdn.zsdx.cn/wei/images/hire/home/home.zsdx.png',
  5. }, resp=>{
  6. //...
  7. }, error=>{
  8. //...
  9. });

#官网封装api

https://uniapp.dcloud.io/api/plugins/share?id=share

#获取分享通道

  1. uni.getProvider({
  2. service: 'share',
  3. success: (e) => {
  4. console.log('success', e);
  5. let data = []
  6. for (let i = 0; i < e.provider.length; i++) {
  7. switch (e.provider[i]) {
  8. case 'weixin':
  9. data.push({
  10. name: '分享到微信好友',
  11. id: 'weixin',
  12. sort:0
  13. })
  14. data.push({
  15. name: '分享到微信朋友圈',
  16. id: 'weixin',
  17. type:'WXSenceTimeline',
  18. sort:1
  19. })
  20. break;
  21. case 'sinaweibo':
  22. data.push({
  23. name: '分享到新浪微博',
  24. id: 'sinaweibo',
  25. sort:2
  26. })
  27. break;
  28. case 'qq':
  29. data.push({
  30. name: '分享到QQ',
  31. id: 'qq',
  32. sort:3
  33. })
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. this.providerList = data.sort((x,y) => {
  40. return x.sort - y.sort
  41. });
  42. console.log({msg: this.providerList}, 'providerList');
  43. },
  44. fail: (e) => {
  45. console.log('获取分享通道失败', e);
  46. uni.showModal({
  47. content:'获取分享通道失败',
  48. showCancel:false
  49. })
  50. }
  51. });

#示例:获取分享通道数据

  1. {
  2. "msg": [{
  3. "name": "分享到微信好友",
  4. "id": "weixin",
  5. "sort": 0
  6. }, {
  7. "name": "分享到微信朋友圈",
  8. "id": "weixin",
  9. "type": "WXSenceTimeline",
  10. "sort": 1
  11. }, {
  12. "name": "分享到新浪微博",
  13. "id": "sinaweibo",
  14. "sort": 2
  15. }, {
  16. "name": "分享到QQ",
  17. "id": "qq",
  18. "sort": 3
  19. }]
  20. }

#分享参数

  1. const shareOPtions = {
  2. provider: '', //分享服务商,weixin|qq|sinaweibo
  3. type: '', //分享形式,0:图文,1:纯文字,2:纯图片,3:音乐,4:视频,5:小程序
  4. title: '', //分享内容标题
  5. scene: '', //分享场景,provider 为 weixin 时必选,微信聊天:WXSceneSession,朋友圈:WXSenceTimeline,微信收藏WXSceneFavorite
  6. summary: '', //type 为 1 时必选,分享内容摘要
  7. href: '', //type 为 0 时必选,跳转链接
  8. imageUrl: '', //type 为 0、2、5 时必选,图片地址。type为0时,推荐使用小于20Kb的图片
  9. mediaUrl: '', //type 为 3、4 时必选 音视频地址
  10. miniProgram: '', //type 为 5 时必选 分享小程序必要参数
  11. success: Function,
  12. fail: Function,
  13. complete: Function
  14. };

miniProgram指说明

  • id:微信小程序原始id
  • path:点击链接进入的页面
  • type:微信小程序版本类型,可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0
  • webUrl:兼容低版本的网页链接

#分享文字

  1. uni.share({
  2. provider: "weixin",
  3. scene: "WXSceneSession",
  4. type: 1,
  5. summary: "我正在使用HBuilderX开发uni-app,赶紧跟我一起来体验!",
  6. success: function (res) {
  7. console.log("success:" + JSON.stringify(res));
  8. },
  9. fail: function (err) {
  10. console.log("fail:" + JSON.stringify(err));
  11. }
  12. });

#分享图片

  1. uni.share({
  2. provider: "weixin",
  3. scene: "WXSceneSession",
  4. type: 2,
  5. imageUrl: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
  6. success: function (res) {
  7. console.log("success:" + JSON.stringify(res));
  8. },
  9. fail: function (err) {
  10. console.log("fail:" + JSON.stringify(err));
  11. }
  12. });

#分享图文

  1. uni.share({
  2. provider: "weixin",
  3. scene: "WXSceneSession",
  4. type: 0,
  5. href: "http://uniapp.dcloud.io/",
  6. title: "uni-app分享",
  7. summary: "我正在使用HBuilderX开发uni-app,赶紧跟我一起来体验!",
  8. imageUrl: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
  9. success: function (res) {
  10. console.log("success:" + JSON.stringify(res));
  11. },
  12. fail: function (err) {
  13. console.log("fail:" + JSON.stringify(err));
  14. }
  15. });

#微信朋友圈/文字

  1. uni.share({
  2. provider: "weixin",
  3. scene: "WXSenceTimeline",
  4. type: 1,
  5. summary: "我正在使用HBuilderX开发uni-app,赶紧跟我一起来体验!",
  6. success: function (res) {
  7. console.log("success:" + JSON.stringify(res));
  8. },
  9. fail: function (err) {
  10. console.log("fail:" + JSON.stringify(err));
  11. }
  12. });

#微信朋友圈/图片

  1. uni.share({
  2. provider: "weixin",
  3. scene: "WXSenceTimeline",
  4. type: 2,
  5. imageUrl: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
  6. success: function (res) {
  7. console.log("success:" + JSON.stringify(res));
  8. },
  9. fail: function (err) {
  10. console.log("fail:" + JSON.stringify(err));
  11. }
  12. });

#微信朋友圈/图文

  1. uni.share({
  2. provider: "weixin",
  3. scene: "WXSenceTimeline",
  4. type: 0,
  5. href: "http://uniapp.dcloud.io/",
  6. title: "uni-app分享",
  7. summary: "我正在使用HBuilderX开发uni-app,赶紧跟我一起来体验!",
  8. imageUrl: "https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/uni@2x.png",
  9. success: function (res) {
  10. console.log("success:" + JSON.stringify(res));
  11. },
  12. fail: function (err) {
  13. console.log("fail:" + JSON.stringify(err));
  14. }
  15. });

#分享到微信小程序

  1. uni.share({
  2. provider: 'weixin',
  3. scene: "WXSceneSession",
  4. type: 5,
  5. imageUrl: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png',
  6. title: '欢迎体验uniapp',
  7. miniProgram: {
  8. id: 'gh_abcdefg',
  9. path: 'pages/index/index',
  10. type: 0,
  11. webUrl: 'http://uniapp.dcloud.io'
  12. },
  13. success: ret => {
  14. console.log(JSON.stringify(ret));
  15. }
  16. });

#自定义分享权限配置/需后端处理

wx.config({ debug: true, // 开启调试模式 appId: ‘wxd581a07854791a7b’, // appId timestamp: ‘’, //生成签名的时间戳 nonceStr: ‘’, //生成签名的随机串 signature: ‘’,// 签名 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 jsApiList: [‘checkJsApi’, ‘onMenuShareQQ’, ‘onMenuShareTimeline’, ‘onMenuShareAppMessage’, ‘editAddress’, ‘getLocation’, ‘openLocation’ ] })

//微信好友 wx.onMenuShareAppMessage({ title: ‘’, // 分享标题 desc: ‘’, // 分享描述 link: ‘’, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: ‘’, // 分享图标 success: function() { //… }, cancel: function() { //… } });

//朋友圈 wx.onMenuShareTimeline({ title: ‘’, // 分享标题 link: ‘’, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: ‘’, // 分享图标 success: function() { //… }, cancel: function() { //… } }); ```

#分享配置