一、微信公众号平台配置网页授权域名

二、示例代码
<template>
<button open-type="login" @tap="wxLogin">允许</button>
</template>
<script setup lang="ts">
import { onLoad } from "@dcloudio/uni-app";
import loginApi from "@/api/loginApi";
import { ref } from "vue";
const isWeixin = ref(false);
onLoad(() => {
isWeixin.value = isWechat();
if (isWeixin.value) {
checkWeChatCode();
}
})
const isWechat = () => {
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
}
const getUrlCode = (name: string) => {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1]
.replace(/\+/g, '%20')) || null
}
const checkWeChatCode = () => {
let code = getUrlCode('code');
if (code) {
bindUser(code)
}
}
const wxLogin = () => {
let uri = encodeURIComponent(window.location.href);
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
}
const bindUser = (code: string) => {
}
</script>