feat: 登录拦截功能完善

This commit is contained in:
duanshuwen
2025-09-13 22:01:50 +08:00
parent 11141ae436
commit 261fb16bd6
11 changed files with 139 additions and 119 deletions

View File

@@ -6,26 +6,22 @@
v-for="(item, index) in itemList"
:key="index"
>
<button
class="reset-btn more-tips-item-title"
:open-type="needAuth ? 'getPhoneNumber' : ''"
@getphonenumber="handleGetPhoneNumber"
@click="handleClick(item)"
>
<view class="more-tips-item-title" @click="sendReply(item)">
<Interceptor />
{{ item }}
</button>
</view>
</view>
</view>
</view>
</template>
<script setup>
import { defineProps, ref } from "vue";
import { onLogin, checkToken } from "@/hooks/useGoLogin";
import { defineProps } from "vue";
import { checkToken } from "@/hooks/useGoLogin";
import Interceptor from "@/components/Interceptor/index.vue";
const emits = defineEmits(["replySent"]);
const needAuth = ref(true);
let pendingText = "";
defineProps({
itemList: {
@@ -43,39 +39,8 @@ defineProps({
});
const sendReply = (text) => {
checkToken().then(() => {
emits("replySent", text); // 向父组件传递数据
});
};
// 处理点击事件
const handleClick = (text) => {
// 检查本地token
const token = uni.getStorageSync("token");
if (token) {
// 情况2token有值直接发送回复此时open-type为空字符串
needAuth.value = false;
sendReply(text);
} else {
// 情况1token没有值设置需要授权状态触发微信授权
needAuth.value = true;
pendingText = text;
// 由于open-type已经动态绑定为'getPhoneNumber',点击会自动触发授权
}
};
// 处理微信授权回调
const handleGetPhoneNumber = async (e) => {
await onLogin(e);
// 授权成功后发送之前存储的文本
if (pendingText) {
sendReply(pendingText);
pendingText = "";
}
needAuth.value = false;
// 向父组件传递数据
checkToken().then(() => emits("replySent", text));
};
</script>