微信登录,分享,支付 cordova-plugin-wechat

// 分享功能  微信、朋友圈
toShare (data) {
      Wechat.isInstalled((installed) => {
        if (installed) {
          this.dialogs = false
          if (data === '微信') {
            Wechat.share({
              message: {
                title: this.title, // 标题
                description: this.description + '...',  // 描述
                thumb: this.picture_link, // 图片
                media: {
                  type: Wechat.Type.WEBPAGE,
                  webpageUrl: this.url // 分享的链接地址
                }
              },
              scene: Wechat.Scene.SESSION // 微信
            }, function () {
              console.log("发表成功");
            }, (reason) => {
              console.log("Failed: " + reason);
            });
          } else {
            Wechat.share({
              message: {
                title: this.title,
                description: "",
                thumb: this.picture_link,
                media: {
                  type: Wechat.Type.WEBPAGE,
                  webpageUrl: this.url
                }
              },
              scene: Wechat.Scene.TIMELINE // 朋友圈
            }, function () {
              console.log("分享成功");
            }, (reason) => {
              console.log("Failed: " + reason);
            });
          }
        } else {
          this.dialogTips = true
          this.dialogTitle = '操作失败'
          this.dialogText = '无法调用微信,请检查是否安装' 
        }
      }, (reason) => {
        console.log("Failed: " + reason);
      })
    }
    
// 微信登录
    wechat () {
      Wechat.isInstalled((installed) => {
        if (installed) {
          let scope = "snsapi_userinfo" 
          let state = "_" + (+new Date())
          Wechat.auth(scope, state, (response) => {
            this.axios.get("/api/v1/auth/wechatlogin?code=" + response.code).then(res => {
              if (res.data.status === "0") {
                localStorage.setItem("userInfo", JSON.stringify(res.data.data));
                localStorage.setItem("token", res.data.data.token);
                localStorage.setItem("refreshToken", res.data.data.refresh_token);
                sessionStorage.setItem('homeBottomNav', '0')
                sessionStorage.setItem('homeActive', '0')
                localStorage.setItem('category', '推荐')
                this.$router.replace({ path: "/Home" });
              } else {
                Toast({
                  message: res.data.info
                })
              }
            })
          }, (reason) => {
            Toast({
              message: reason
            })
          })
        } else {
          Toast({
            message: "无法调用微信, 请检查是否安装"
          })
        }
      }, (reason) => {
        console.log("Failed: " + reason);
      })
    }
    
// 微信支付
   if (res.data.data) {
      let parameter = {
        appid: res.data.data.appid,
        package: res.data.data.package,
        partnerid: res.data.data.partnerid,
        prepayid: res.data.data.prepayid,
        noncestr: res.data.data.noncestr,
        timestamp: res.data.data.timestamp,
        sign: res.data.data.sign
      }
      Wechat.sendPaymentRequest(parameter, function (res) {
        console.log("success");
        // 支付成功,跳转到订单页面
        self.$router.push({ path: '/Order', params: { status: '1' } })
      }, function (reason) {
        console.log("Failed: " + reason);
        self.dialogTips = true
        self.dialogTitle = '支付失败'
        self.dialogText = '请稍后再试或联系客服人员'
      })
      } else {
      self.dialogTips = true
      self.dialogText = '支付类型错误'
    }
    
// Order页面
  mounted () {
    setTimeout(() => {
      var refreshToken = localStorage.getItem('refreshToken')
      this.refresh(refreshToken) //刷新token
      if (this.$route.params.status && this.$route.params.status === '1') {
        setTimeout(() => {
          this.getToken()
        }, 1000)
      }
    }, 200)
  },
  methods:{
      getToken () {
      let token = localStorage.getItem('token')
      this.axios.get("/api/v1/appplay", {
        headers: {
          Authorization: 'Bearer ' + token
        }
      }).then(res => {
        if (res.data.status === "0") {
          if (res.data.data.token) {
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('refreshToken', res.data.data.refresh_token)
            let t2 = localStorage.getItem('token')
            console.log(t2)
          }
        }
      }).catch((err) => {
        console.log('err: ', err)
      })
    },
    refresh (data) {
      this.axios.put("/api/v1/refreshtoken", {}, {
        headers: {
          Authorization: 'Rearer ' + data
        }
      }).then(res => {
        if (res.data.status === "0") {
          localStorage.setItem('token', res.data.token)
          localStorage.setItem('refreshToken', res.data.refresh_token)
          this.token = res.data.token
          this.axios({
            url: '/api/v1/users/pay/orders',
            headers: {
              Authorization: 'Bearer ' + this.token
            },
            method: 'GET'
          }).then(res => {
            if (res.data.status && res.data.data.length > 0) {
              this.items = res.data.data
            }
          })
        }
      })
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值