工具函数
app.vue中执行这个方法来获取授权,注意要在微信公众平台配置相应的域名。
// util.js
// 微信公众号授权
import {
http
} from '@/utils/request';
import helper from './helper.js'
export default {
wxAuthorize() {
let link = window.location.href;
let params = helper.getRequestParameters(link); // 地址解析
// 已经授权登录过的就不用再授权了
if (uni.getStorageSync('info') && uni.getStorageSync('token')) return;
// 如果拿到code,调用授权接口,没有拿到就跳转微信授权链接获取
if (params.code) {
console.log(params.code)
console.log('跳接口')
// 调用后台接口,授权
} else {
let appid = '';
let uri = encodeURIComponent(link);
let authURL =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
window.location.href = authURL;
}
}
}