微信環(huán)境錯(cuò)誤頁面师妙。
<template>
<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">請(qǐng)?jiān)谖⑿趴蛻舳舜蜷_鏈接</h4></div></div>
</template>
<script setup >
import { } from 'vue'
</script>
<style scoped>
@import '../assets/css/weui.css'
</style>
路由判斷
router.beforeEach((to, from, next)=>{
if(to.path == "/error") {next()}else{
if(isWeixin()){
// 微信環(huán)境
next()
}else{
next("/error")
}
}
})
/**
* 是否是微信環(huán)境
* @returns
*/
function isWeixin () {
var ua = window.navigator.userAgent.toLowerCase()
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true
} else {
return false
}
}
授權(quán)
async function getAuthorize() {
//先判斷有沒有授權(quán)(判斷地址欄code唐瀑,有就-用戶同意了授權(quán),沒有-沒授權(quán)或者拒絕授權(quán))
var code = getUrlParam("code"); //此處使用的是history路由模式椭坚,hash這么拿不到澡刹。getUrlParam()方法在下面
console.log('code',code)
if (code) {
console.log("獲取到了 code", code);
let param = {
code: code,
};
try{
Api.getUserInfo(param).then((res) => {
userInfo.value = res?.data?.data || null
});
}catch(e){
showDialog({title:'提示',message:'用戶信息獲取失敗歉秫,請(qǐng)稍后再試'})
}
getCompanyList()
} else {
var appid = "wx0008f9b5e1198d**";
var redirect_uri = "https://*******/wechat_web/index.html";
var newRedirect = encodeURIComponent(redirect_uri); // 此處需要將你的回調(diào)頁面進(jìn)行轉(zhuǎn)碼葛躏,此方法直接使用
// window.location.+newRedirect +"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
window.location. + appid + "&redirect_uri=" + newRedirect + "&response_type=code&scope=snsapi_userinfo#wechat_redirect";
}
// }
}
//獲取url中的參數(shù)
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構(gòu)造一個(gè)含有目標(biāo)參數(shù)的正則表達(dá)式對(duì)象
var r = window.location.search.substr(1).match(reg); //匹配目標(biāo)參數(shù)
if (r != null) {
return unescape(r[2]);
}
return null; //返回參數(shù)值
}