uniapp实用功能代码(小程序支付,图片保存,返回刷新,分享到朋友圈)

发布时间 2023-11-07 11:52:17作者: 雪天xt

1. uniapp 小程序支付:

uni.request({
    url: "http://xxxxxx/payOrder", // 后端接口 返回调起支付需要的参数
    data: {
      userId:1, // 此接口需要的参数 一般有多个 此仅为示例
    },
    method: "POST",
    success: (res) => {
      console.log(res.data, "这是调起支付的参数");
      // 调起小程序支付api 下面参数为必传
      uni.requestPayment({
        provider: "wxpay", //支付类型(小程序)
        ...res.data, // 前面接口返回的数据
        success: (res) => {
          if (res.errMsg === "requestPayment:ok") {
            console.log("支付成功", res)
          }
        },
        fail: (err) => {
          console.log("支付失败", err)
        }
      })
    }
  })

2. uniapp保存图片到相册

saveImage(url) {
        uni.showLoading({
          title:'下载中...'
        })
        uni.downloadFile({
          url: url,
          success(res) {
            if (res) {
              console.log('下载成功', res)
              uni.hideLoading();
              uni.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success(res) {
                  console.log(res);
                },
                fail(res) {
                  console.log(res);
                },
              });
            }
          },
          fail: (err) => {
            if (err) {
              console.log('下载失败', err)
              uni.hideLoading();
            }
          }
        });
      }

3. uniapp返回刷新

 let pages = getCurrentPages(); // 当前页面
    let beforePage = pages[pages.length - 2]; //上一个页面
     uni.navigateBack({
      success: function() {
           beforePage.$vm.getlist();
      }
    }) 

4. uniapp 小程序分享

//发送给朋友
onShareAppMessage(){
  return {
    title: '',//分享标题
    path: ''//分享页面路径
    imageUrl: '',//分享图标
    desc:'',//自定义分享描述
  }
},


//分享朋友圈
onShareTimeline() {},

uniapp更多实用技巧,公众号:"雪天前端"