```const qs = require('qs')
// 应用授权作用域,
//snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
//snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
const SCOPES = ['snsapi_base', 'snsapi_userinfo']class VueWechatAuthPlugin {constructor() {this.appid = nullthis.redirect_uri = nullthis.scope = SCOPES[0]this._code = nullthis._redirect_uri = null}static makeState() {return (Math.random().toString(36).substring(2, 15) +Math.random().toString(36).substring(2, 15))}setAppId(appid) {this.appid = appid}set redirect_uri(redirect_uri) {this._redirect_uri = encodeURIComponent(redirect_uri)}get redirect_uri() {return this._redirect_uri}get state() {return localStorage.getItem('wechat_auth:state')}set state(state) {localStorage.setItem('wechat_auth:state', state)}get authUrl() {if (this.appid === null) {throw new Error('appid must not be null')}if (this.redirect_uri === null) {throw new Error('redirect uri must not be null')}this.state = VueWechatAuthPlugin.makeState()// return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appid}&redirect_uri=${this.redirect_uri}&response_type=code&scope=${this.scope}&state=${this.state}#wechat_redirect`return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appid}&redirect_uri=${this.redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`}returnFromWechat(redirect_uri) {let baseWithSearch = redirect_uri.split('#')[0]let parsedUrl = ''// 本地环境if (process.env.NODE_ENV === 'development') {parsedUrl = qs.parse(redirect_uri.split('?')[1])this.state = nullthis._code = parsedUrl.code} else {parsedUrl = qs.parse(baseWithSearch.split('?')[1])if (this.state === null) {throw new Error("You did't set state")}if (parsedUrl.state === this.state) {this.state = nullthis._code = parsedUrl.code} else {this.state = nullthrow new Error(`Wrong state: ${parsedUrl.state}`)}}}get code() {if (this._code === null) {throw new Error('Not get the code from wechat server!')}const code = this._codethis._code = nullreturn code}
}
const vueWechatAuthPlugin = new VueWechatAuthPlugin()export default vueWechatAuthPlugin

微信授权工具wechatAuth.js相关推荐

  1. 【微信】微信小程序 微信开发工具 创建js文件报错 pages/module/module.js 出现脚本错误或者未正确调用 Page()...

    创建报错pages/module/module.js 出现脚本错误或者未正确调用 Page() 解决方法: 在js文件中添加 Page({}) 转载于:https://www.cnblogs.com/ ...

  2. 微信开发者工具,page里面的data在js的方法里面修改

    不知道为啥我在用微信开发者工具中js文件中的方法里面修改data里面的值修改不成,到页面里面显示的还是初始值. 一开始在方法里面的写法是: //一开始的写法,但是wxml里面还是使用默认值 this. ...

  3. 微信开发工具onPullDownRefresh不触发问题

    前提:你尝试了其他文章里的方法仍旧发现不能触发下拉刷新 我不知道会不会有人跟我一样傻,但是还是写下这篇文章来拯救一下和我一样困惑的人吧 微信开发工具的js文件里是有自己的onPullDownRefre ...

  4. 从零开始编写一个微信小程序(微信开发者工具+JS+WuxUI组件库+云开发)万字整理,建议收藏!

    微信小程序-魔镜 笔记&源码(微信开发者工具+JS+UI组件库+云开发) 效果展示 视频演示 经历了一次升级之后,我的小程序怎么样了? 文章目录 微信小程序-魔镜 笔记&源码(微信开发 ...

  5. node.js实现微信授权登录,最详细最简单小白教程。。

    使用到的中间件,如已安装请忽略 npm install express -g npm install request --save 首先想要微信授权,必须注册的有公众号,进入微信公众号平台-开发-开发 ...

  6. 使用微信开发者工具调试微信网页授权登录-react

    转:https://www.jianshu.com/p/9ced1a297c95 1.使用localhost本地调试 使用微信开发者工具, 选择微信网页授权, 微信团队为广大的开发者提供了一个测试账号 ...

  7. 【H5微信授权】简单实现H5页面微信授权功能,微信开发者工具报错 系统错误,错误码-1,undefined解决办法【详细】

    前言 最近写到了H5公众号,需要微信授权的功能. 这里记录一下授权的流程和踩了个坑 图片 授权代码执行后会跳转到授权的地方,没有授权的会有确认授权,授权过得会这样,直接自动登录,然后再跳转到中转页 授 ...

  8. 微信公共平台接入之:网页授权(微信授权,微信access_token获取,获取微信用户信息),微信开发者工具使用,微信公众平台测试号申请接入

    1.微信公众平台文档入口 微信公众平台入口地址:https://mp.weixin.qq.com/,截图: 进入之后的文档地址: https://mp.weixin.qq.com/wiki?t=res ...

  9. PHP+laravel 微信开发工具授权登录详解

    微信小程序登录流程时序 废话不多说,直接上代码: html 代码 <button open-type="getUserInfo" bindtap="login&qu ...

最新文章

  1. J2EE复习(二)XML
  2. Android 70道面试题汇总不再愁面试
  3. 安装 | CCS5.5安装包与licence以及安装教程
  4. Java并发编程—ThreadLocal底层原理
  5. julia与python对比_有人说Julia比Python好,还给出了5个理由
  6. CSS3 @media媒体查询 适配不同尺寸设备的响应式布局(清晰详解)
  7. Linux 下 I/O 多路复用技术 epoll
  8. 查看mysql所有命令
  9. 使用Requests+xpath实现简单的数据爬取
  10. cad延伸快捷键_CAD绘图大师都在用的46组快捷键,高效绘图必备
  11. 鸿海成立AI研发中心 5年投资100亿新台币
  12. 解决:ipad QQ可以联网,浏览器不能联网
  13. 基于模型的无人机开发
  14. 英文破折号、连接号、连字符、负号的区别,注意事项和输入方法
  15. Java+JSP政府机关公文文档收发管理系统(含源码+论文+答辩PPT等)
  16. centos7 手动安装mysql8.0 并实现双机热备份 2022-1-1
  17. Win11 在 Edge 浏览器中报名教师资格证
  18. js正则匹配多个全部数据
  19. 如何从wondows到Linux
  20. virtuoso根据原理图绘制版图并联接_Cadence-virtuoso的使用简介(版图绘制).pdf

热门文章

  1. 科技界的乌托邦之城:远离“房价高交通堵”
  2. 课程设计——前方交会
  3. 从月薪6k涨到15k,熬到就只剩下这份Java中高级核心笔记了
  4. windows HBA卡 查询WWN号
  5. Elliot 的 博客搬家啦!!!
  6. python练习实例——芯片测试
  7. php soapclient 异常,PHP调用wsdl接口实例化SoapClient抛出异常
  8. 找你妹+ipad+wifi,回忆那年的经典游戏
  9. 中国富人爱“L”三大奢侈品牌 --
  10. matlab信号中改变信噪比,信噪比为负