js微信二次分享

1.wx_share.js

(1)导出二次分享方法:

export const wechatShare = async (option = {}, successCB = () => {}) => {
   const shareOption = {
          link: location.href,
          title: 'aaa',
          desc:'bbb',
          imgUrl: '',
         ...option
    };
    wx_config();
    wx_share(shareOption, successCB);
};

(2)配置微信分享信息

export const wx_config= () => {
   axios.get('url').then(res => {  // 通过url可以得到相关配置信息,这个url一般后台给
      let configs = res.data.data.signMap;
      wx.config({
            debug: false,
            appId: configs.appId,
            nonceStr: configs.nonceStr,
            timestamp: configs.timestamp,
            signature: configs.signature,
            jsApiList: [
                      'hideMenuItems',
                      'previewImage',
                      'checkJsApi',
                      'chooseWXPay',
                      'onMenuShareTimeline',
                      'onMenuShareAppMessage',
                      'onMenuShareQQ',
                      'onMenuShareWeibo',
                      'onMenuShareQZone',
                      'getLocation',
                     'openLocation',
                     'translateVoice'
              ]
        });
    });
};

(3)分享

export const wx_share = (option = {}, successCB = () => {}) => {
    wx.ready(() => {
          wx.onMenuShareAppMessage({   // 分享给朋友
              title: option.title,
              desc: option.desc,
              link: option.link,
              imgUrl: option.imgUrl,
              success: function(res) {
                     successCB(res);  // 用户确认分享后执行的回调函数
               },
              cancel: function() {  // 用户取消分享后执行的回调函数
              }
     });
     wx.onMenuShareTimeline({   // 分享到朋友圈
          title: option.friendtitle || option.title, // 标题
          desc: option.desc,
          link: option.link, // 链接
          imgUrl: option.imgUrl, // 分享图标
          success: function(res) {
            successCB(res);
          },
          cancel: function() {  // 用户取消分享后执行的回调函数
          }
     });
  });
};

2.页面预加载

wechatShare(
{
    title: 'cccc',
    desc: 'ddddd',
    imgUrl: ""
},
() => {
    console.log("success...");
}
);

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于微信小程序的网络请求,可以使用微信官方提供的 wx.request 方法来实现。如果你希望封装一个类似 Axios 的网络请求库,可以按照以下步骤进行: 1. 创建一个封装的网络请求类,例如命名为 Axios。 2. 在 Axios 类中创建一个方法,例如命名为 request,用于发送网络请求。 3. 在 request 方法中,使用 wx.request 方法发送网络请求,并返回一个 Promise 对象以便进行异步处理。 4. 在 Promise 的 resolve 中返回请求成功的结果,reject 中返回请求失败的原因。 5. 在 Axios 类中可以添加其他常用的方法,例如 get、post、put、delete 等,根据不同的请求类型调用 request 方法发送请求。 6. 在微信小程序的页面中引入 Axios 类,通过调用 Axios 类的方法来发送网络请求。 下面是一个简单的示例代码: ```javascript class Axios { static request(options) { return new Promise((resolve, reject) => { wx.request({ url: options.url, method: options.method || 'GET', data: options.data || {}, header: options.header || {}, success: res => { resolve(res.data); }, fail: error => { reject(error); } }); }); } static get(url, data, header) { return this.request({ url, data, header, method: 'GET' }); } static post(url, data, header) { return this.request({ url, data, header, method: 'POST' }); } // 其他常用方法... } // 在页面中使用 Axios.get('https://api.example.com/data') .then(res => { console.log(res); }) .catch(error => { console.error(error); }); ``` 通过以上封装,你可以像使用 Axios 一样使用 Axios 类发送网络请求。自然地,你可以根据实际需求对该网络请求库进行扩展和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值