介绍
功能: 支持微信/支付宝网页静默授权
为什么需要线上html
因为在开发环境的时候,是本地服务http://localhost:8080
,但是微信需要JS接口安全域名.
配置信息
- 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“网页授权域名”
- 然后把 html 文件放到服务器上, 安全域名指向这个html
- 在 vue 环境配置文件(.env.development、.env.production),中配置以下参数
- VUE_APP_WX_GET_CODE_DOMAIN = ‘第二步的html文件的线上地址’
- VUE_APP_APP_ID = ‘微信/支付宝的appid’
- 使用方法:
- 直接调取 util 文件中的 getAuthorCode 方法
- 调取完后,就可以在地址栏中,看到那个 code 了 (获取code的时候,可以使用这个方法 onGetRequest().code )
代码信息
授权文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>授权code</title>
</head>
<body>
</body>
</html>
<script>
(function () {
var tools = {
getParamer: function (key) {
var map = this.getSearch();
return map[key];
},
getSearch: function () {
var search = window.location.search;
return this.getSearchByStr(search);
},
getSearchByStr: function