web对接微信获取OpenId

/*
 * @Author: hjl
 * @Date: 2020-08-15 18:10:33
 * @LastEditTime: 2020-08-20 20:24:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \webchat_project_copy\src\utils\wechatOpenId.js
 */

import {getUserOpenIdd} from "@/api/apiz" // getUserOpenIdd为后端提供的获取openid接口
function getUrlKey(name){ //获取url参数
  return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
}
function getCodeApi(state,urll){//获取code
  let urls = urll == undefined || '' ? 'https://web.bethuguard.com/#/wxihome' : urll
  let urlNow= encodeURIComponent(urls);
  let scope='snsapi_userinfo';    //snsapi_userinfo   //静默授权 用户无感知 https://xxxxxxxxxxxx'
  let appid='wx93371565ee646f7d'; // 测试appid 根据实际情况替换
  let url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
  window.location.replace(url);
}
function getOpenIdApi(code) {
  getUserOpenIdd(code).then(result => {
    let { data: res } = result
    if(res.code == 0){
      sessionStorage.setItem('oId',res.data.openid)
    }else{
      console.log(res.msg,'获取OpenId错误')
    }
  })
}
export const getOpenId = function (next,url) {
  let code = getUrlKey("code");
  let oId = sessionStorage.getItem('oId')
  if(oId) {
    next()
  }else {
    if (code) {
      console.log('用户授权过直接换openId')
      getOpenIdApi(code)
      next()
    } else {
      console.log('跳转授权页面')
      getCodeApi("123",url);
    }
  }
}

在需要获取OpenId的页面导入方法即可:

import { getOpenId } from "@/utils/wechatOpenId" // 引入方法

beforeRouteEnter (to, from, next) {
  getOpenId(next,'http://192.168.124.6:8080/#/userLogin') // url表示重定向地址
}
/*
 * @Author: hjl
 * @Date: 2020-08-15 18:10:33
 * @LastEditTime: 2020-08-20 20:24:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \webchat_project_copy\src\utils\wechatOpenId.js
 */

import {getUserOpenIdd} from "@/api/apiz" // getUserOpenIdd为后端提供的获取openid接口
function getUrlKey(name){ //获取url参数
  return decodeURIComponent((new RegExp('[?|&]'+name+'='+'([^&;]+?)(&|#|;|$)').exec(location.href)||[,""])[1].replace(/\+/g,'%20'))||null;
}
function getCodeApi(state,urll){//获取code
  let urls = urll == undefined || '' ? 'https://web.bethuguard.com/#/wxihome' : urll
  let urlNow= encodeURIComponent(urls);
  let scope='snsapi_userinfo';    //snsapi_userinfo   //静默授权 用户无感知 https://xxxxxxxxxxxx'
  let appid='wx93371565ee646f7d'; // 测试appid 根据实际情况替换
  let url=`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
  window.location.replace(url);
}
function getOpenIdApi(code) {
  getUserOpenIdd(code).then(result => {
    let { data: res } = result
    if(res.code == 0){
      sessionStorage.setItem('oId',res.data.openid)
    }else{
      console.log(res.msg,'获取OpenId错误')
    }
  })
}
export const getOpenId = function (next,url) {
  let code = getUrlKey("code");
  let oId = sessionStorage.getItem('oId')
  if(oId) {
    next()
  }else {
    if (code) {
      console.log('用户授权过直接换openId')
      getOpenIdApi(code)
      next()
    } else {
      console.log('跳转授权页面')
      getCodeApi("123",url);
    }
  }
}

在需要获取OpenId的页面导入方法即可:

import { getOpenId } from "@/utils/wechatOpenId" // 引入方法

beforeRouteEnter (to, from, next) {
  getOpenId(next,'http://192.168.124.6:8080/#/userLogin') // url表示重定向地址
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值