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

前言

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

前提准备

1、 官方文档地址

open.weixin.qq.com/connect/oau... 注释: 注意:跳转回调redirect_uri,必须是https开头

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

具体步骤

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

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

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

html 复制代码
<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再次封装基础组件文档

相关推荐
10share18 小时前
基于大模型的 i18n 方案:把中文原文当 Key,轻松搞定多语言维护
前端·vue.js
啾咪orJIUMI1 天前
Vue 3 + 高德地图封装实战:点位、区域、Loca 流光与 Vue 信息窗体
前端·vue.js
FRing031 天前
vue2干货总结8(路由模块封装、router-link、声明式导航、路由重定向、编程式导航)
开发语言·前端·vue.js·vue2
无懈可击1 天前
Vue 可视化AI表单设计器 FcDesigner v3.6 版本发布,支持导出 SQL功能!
vue.js·开源·ai编程
徐小夕1 天前
JitArchives,一款基于JitWord协同AI文档打造的企业级电子档案系统
前端·vue.js·github
头发还在的女程序员1 天前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
FRing032 天前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
ruanno_top2 天前
点全选,已选的被清空了:Transfer 三态复选框的三个 bug
vue.js
时间的情敌2 天前
Cesium 加载 3D Tiles 倾斜摄影、压平、样式设置(Vue3 实战)
vue.js·3d·cesium
usdoc文档预览3 天前
USDOC系列之PPT文件在线编辑-VUE前端实现在线浏览器在线编辑PPT文档
前端·vue.js·编辑器·office365·ppt在线编辑·office文件在线编辑·文件在线编辑