微信公众号授权登录Vue H5项目并指定跳转到某一个页面进行操作(即通过微信授权登录获取url上的code,调用后台接口获取access_token)

前言

最近有一个需求是:盘点待审批状态,需要用微信公众号推送消息给审批人员,让其在微信中审批。因此就需要:通过微信的网页授权(即:微信公众号授权登录Vue H5项目并指定跳转到某一个页面进行操作)

前提准备

1、 官方文档地址

https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己公众号openid&redirect_uri=授权后重定向的回调链接地址(即需要调跳转的H5页面)&response_type=code&scope=snsapi_userinfo&state=123(url参数)#wechat_redirect
注释:
在这里插入图片描述
注意:跳转回调redirect_uri,必须是https开头

2、微信公众号需要配置以下:

在这里插入图片描述

具体步骤

逻辑思路
判断token是否存在(即判断是否已经登陆H5项目)

1、如果已登陆,就直接跳转页面。
2、如果没有登陆,就要进行授权登录并重置token。

在H5项目新建一个wxh5页面(此页面是白名单——即未登录也可以进入)

<template>
  <div class="wx-h5">
    <van-loading type="spinner" color="#1989fa" vertical>页面跳转中...</van-loading>
  </div>
</template>
<script>
import { setToken } from '@/utils/auth'
export default {
  name: 'WXh5',
  data() {
    return {
      code: '',
      id: '' // 盘点单id
    }
  },
  mounted() {
    console.log('获取url参数', this.getUrlCode())
    this.code = this.getUrlCode().code
    this.id = this.getUrlCode().state
    this.checkLogin()
  },
  methods: {
    // 审核页登录授权
    async getWxLogin() {
      const res = await this.$api.getWxLogin(this.code)
      if (res.success) {
        console.log('存储token', res.data)
        setToken(res.data)
        // 存储token
        if (res.data) {
          this.$router.push({
            path: '跳转路由地址',
            query: { addTitle: '盘点', id: this.id, type: 'wx' }
          })
        }
      }
    },
    // 判断是否登录
    async checkLogin() {
      const res = await this.$api.checkLogin()
      if (res.success) {
        console.log('是否登录', res.data)
        if (res.data) {
          console.log('跳转审批')
          this.$router.push({
            path: '跳转路由地址',
            query: { addTitle: '盘点', id: this.id, type: 'wx' }
          })
        } else {
          this.getWxLogin()
        }
      }
    },
    // 获取url中的code方法
    getUrlCode() {
      // 截取url中的code方法
      const url = location.search
      // eslint-disable-next-line no-new-object
      const theRequest = new Object()
      if (url.indexOf('?') != -1) {
        const str = url.substr(1)
        const strs = str.split('&')
        for (let i = 0; i < strs.length; i++) {
          theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
        }
      }
      return theRequest
    }
  }
}
</script>

<style lang="scss">
.wx-h5 {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.1);
}
</style>

相关文章

基于ElementUi再次封装基础组件文档


基于ant-design-vue再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wocwin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值