feat: 登录拦截功能完善
This commit is contained in:
6
App.vue
6
App.vue
@@ -38,6 +38,12 @@ body,
|
|||||||
|
|
||||||
// 重置按钮样式
|
// 重置按钮样式
|
||||||
.reset-btn {
|
.reset-btn {
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
z-index: 1000;
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
outline: none;
|
outline: none;
|
||||||
|
|||||||
18
components/Interceptor/index.vue
Normal file
18
components/Interceptor/index.vue
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<template>
|
||||||
|
<button
|
||||||
|
v-if="!hasToken"
|
||||||
|
class="reset-btn"
|
||||||
|
open-type="getPhoneNumber"
|
||||||
|
@getphonenumber="onLogin"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { computed, defineEmits } from "vue";
|
||||||
|
import { useAppStore } from "@/store";
|
||||||
|
import { onLogin } from "@/hooks/useGoLogin";
|
||||||
|
|
||||||
|
const emits = defineEmits(["click"]);
|
||||||
|
|
||||||
|
const hasToken = computed(() => useAppStore().hasToken);
|
||||||
|
</script>
|
||||||
@@ -22,9 +22,6 @@ export const onLogin = (e) => {
|
|||||||
|
|
||||||
// 绑定手机号
|
// 绑定手机号
|
||||||
bindPhone(params);
|
bindPhone(params);
|
||||||
|
|
||||||
// 通知刷新
|
|
||||||
uni.$emit("TOKEN_CHANGE");
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,24 @@
|
|||||||
export const getWeChatAuthCode = () => {
|
export const getWeChatAuthCode = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
// 条件编译微信小程序、抖音小程序
|
||||||
|
let provider = "";
|
||||||
|
// #ifdef MP-WEIXIN
|
||||||
|
provider = "weixin";
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifdef MP-TOUTIAO
|
||||||
|
provider = "toutiao";
|
||||||
|
// #endif
|
||||||
|
|
||||||
uni.login({
|
uni.login({
|
||||||
provider: 'weixin',
|
provider,
|
||||||
onlyAuthorize: true,
|
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
|
console.log("微信登录成功", res);
|
||||||
resolve(res.code);
|
resolve(res.code);
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
checkUserPhone,
|
checkUserPhone,
|
||||||
} from "../request/api/LoginApi";
|
} from "../request/api/LoginApi";
|
||||||
import { getWeChatAuthCode } from "./AuthManager";
|
import { getWeChatAuthCode } from "./AuthManager";
|
||||||
|
import { useAppStore } from "@/store";
|
||||||
|
|
||||||
const loginAuth = () => {
|
const loginAuth = () => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
@@ -20,6 +21,9 @@ const loginAuth = () => {
|
|||||||
if (response.access_token) {
|
if (response.access_token) {
|
||||||
uni.setStorageSync("token", response.access_token);
|
uni.setStorageSync("token", response.access_token);
|
||||||
|
|
||||||
|
const appStore = useAppStore();
|
||||||
|
|
||||||
|
appStore.setHasToken(true);
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
reject(response.message || "登录失败");
|
reject(response.message || "登录失败");
|
||||||
|
|||||||
@@ -362,8 +362,16 @@ const initHandler = () => {
|
|||||||
initWebSocket();
|
initWebSocket();
|
||||||
};
|
};
|
||||||
|
|
||||||
// 监听token变化
|
// 绑定成功,监听token变化,初始化
|
||||||
uni.$on("TOKEN_CHANGE", () => initHandler());
|
|
||||||
|
watch(
|
||||||
|
() => appStore.hasToken,
|
||||||
|
(newValue) => {
|
||||||
|
if (newValue) {
|
||||||
|
initHandler();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -6,26 +6,22 @@
|
|||||||
v-for="(item, index) in itemList"
|
v-for="(item, index) in itemList"
|
||||||
:key="index"
|
:key="index"
|
||||||
>
|
>
|
||||||
<button
|
<view class="more-tips-item-title" @click="sendReply(item)">
|
||||||
class="reset-btn more-tips-item-title"
|
<Interceptor />
|
||||||
:open-type="needAuth ? 'getPhoneNumber' : ''"
|
|
||||||
@getphonenumber="handleGetPhoneNumber"
|
|
||||||
@click="handleClick(item)"
|
|
||||||
>
|
|
||||||
{{ item }}
|
{{ item }}
|
||||||
</button>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { defineProps, ref } from "vue";
|
import { defineProps } from "vue";
|
||||||
import { onLogin, checkToken } from "@/hooks/useGoLogin";
|
import { checkToken } from "@/hooks/useGoLogin";
|
||||||
|
import Interceptor from "@/components/Interceptor/index.vue";
|
||||||
|
|
||||||
const emits = defineEmits(["replySent"]);
|
const emits = defineEmits(["replySent"]);
|
||||||
const needAuth = ref(true);
|
|
||||||
let pendingText = "";
|
|
||||||
|
|
||||||
defineProps({
|
defineProps({
|
||||||
itemList: {
|
itemList: {
|
||||||
@@ -43,39 +39,8 @@ defineProps({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const sendReply = (text) => {
|
const sendReply = (text) => {
|
||||||
checkToken().then(() => {
|
// 向父组件传递数据
|
||||||
emits("replySent", text); // 向父组件传递数据
|
checkToken().then(() => emits("replySent", text));
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// 处理点击事件
|
|
||||||
const handleClick = (text) => {
|
|
||||||
// 检查本地token
|
|
||||||
const token = uni.getStorageSync("token");
|
|
||||||
|
|
||||||
if (token) {
|
|
||||||
// 情况2:token有值,直接发送回复,此时open-type为空字符串
|
|
||||||
needAuth.value = false;
|
|
||||||
sendReply(text);
|
|
||||||
} else {
|
|
||||||
// 情况1:token没有值,设置需要授权状态,触发微信授权
|
|
||||||
needAuth.value = true;
|
|
||||||
pendingText = text;
|
|
||||||
// 由于open-type已经动态绑定为'getPhoneNumber',点击会自动触发授权
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 处理微信授权回调
|
|
||||||
const handleGetPhoneNumber = async (e) => {
|
|
||||||
await onLogin(e);
|
|
||||||
|
|
||||||
// 授权成功后发送之前存储的文本
|
|
||||||
if (pendingText) {
|
|
||||||
sendReply(pendingText);
|
|
||||||
pendingText = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
needAuth.value = false;
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
|
position: relative;
|
||||||
|
|
||||||
.more-tips-item-title {
|
.more-tips-item-title {
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
|
|||||||
@@ -7,7 +7,9 @@
|
|||||||
:key="index"
|
:key="index"
|
||||||
>
|
>
|
||||||
<view class="mk-card-item" @click="sendReply(item)">
|
<view class="mk-card-item" @click="sendReply(item)">
|
||||||
<image :src="item.coverPhoto" mode="aspectFill"></image>
|
<Interceptor />
|
||||||
|
<image class="card-img" :src="item.coverPhoto" mode="aspectFill" />
|
||||||
|
|
||||||
<view class="overlay-gradient">
|
<view class="overlay-gradient">
|
||||||
<text class="overlay-text">{{ item.topic }}</text>
|
<text class="overlay-text">{{ item.topic }}</text>
|
||||||
</view>
|
</view>
|
||||||
@@ -18,9 +20,11 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import ModuleTitle from "@/components/ModuleTitle/index.vue";
|
|
||||||
import { defineProps } from "vue";
|
import { defineProps } from "vue";
|
||||||
import { RECOMMEND_POSTS_TITLE } from "@/constant/constant.js";
|
import { RECOMMEND_POSTS_TITLE } from "@/constant/constant";
|
||||||
|
import { checkToken } from "@/hooks/useGoLogin";
|
||||||
|
import Interceptor from "@/components/Interceptor/index.vue";
|
||||||
|
import ModuleTitle from "@/components/ModuleTitle/index.vue";
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
recommendTheme: {
|
recommendTheme: {
|
||||||
@@ -30,63 +34,13 @@ const props = defineProps({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const sendReply = (item) => {
|
const sendReply = (item) => {
|
||||||
|
checkToken().then(() => {
|
||||||
const topic = item.userInputContent || item.topic.replace(/^#/, "");
|
const topic = item.userInputContent || item.topic.replace(/^#/, "");
|
||||||
uni.$emit(RECOMMEND_POSTS_TITLE, topic);
|
uni.$emit(RECOMMEND_POSTS_TITLE, topic);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.container {
|
@import "./styles/RecommendPostsList.scss";
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.container-scroll {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: row;
|
|
||||||
overflow-x: auto;
|
|
||||||
margin: 4px 0 6px;
|
|
||||||
|
|
||||||
.mk-card-item {
|
|
||||||
flex-shrink: 0; /* 关键:防止 flex 布局压缩子元素宽度 */
|
|
||||||
width: 142px;
|
|
||||||
height: 126px;
|
|
||||||
border-radius: 10px;
|
|
||||||
overflow: hidden;
|
|
||||||
margin-right: 8px;
|
|
||||||
position: relative;
|
|
||||||
|
|
||||||
image {
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 渐变背景层 */
|
|
||||||
.overlay-gradient {
|
|
||||||
position: absolute;
|
|
||||||
bottom: 0;
|
|
||||||
left: 0;
|
|
||||||
width: 100%;
|
|
||||||
height: 50px; /* 渐变层高度,可调 */
|
|
||||||
background: linear-gradient(
|
|
||||||
to bottom,
|
|
||||||
rgba(0, 0, 0, 0.001) 0%,
|
|
||||||
rgba(0, 0, 0, 0.5) 100%
|
|
||||||
);
|
|
||||||
display: flex;
|
|
||||||
align-items: flex-end; /* 文字贴近底部 */
|
|
||||||
padding: 0 8px 6px; /* 内边距让文字与边缘保持距离 */
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 渐变层上的文字 */
|
|
||||||
.overlay-text {
|
|
||||||
color: #fff;
|
|
||||||
font-weight: 500;
|
|
||||||
font-size: 12px;
|
|
||||||
text-align: left;
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
53
pages/module/recommend/styles/RecommendPostsList.scss
Normal file
53
pages/module/recommend/styles/RecommendPostsList.scss
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
.container {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.container-scroll {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
overflow-x: auto;
|
||||||
|
margin: 4px 0 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mk-card-item {
|
||||||
|
flex-shrink: 0; /* 关键:防止 flex 布局压缩子元素宽度 */
|
||||||
|
width: 142px;
|
||||||
|
height: 126px;
|
||||||
|
border-radius: 10px;
|
||||||
|
overflow: hidden;
|
||||||
|
margin-right: 8px;
|
||||||
|
position: relative;
|
||||||
|
|
||||||
|
.card-img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 渐变背景层 */
|
||||||
|
.overlay-gradient {
|
||||||
|
position: absolute;
|
||||||
|
bottom: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 50px; /* 渐变层高度,可调 */
|
||||||
|
background: linear-gradient(
|
||||||
|
to bottom,
|
||||||
|
rgba(0, 0, 0, 0.001) 0%,
|
||||||
|
rgba(0, 0, 0, 0.5) 100%
|
||||||
|
);
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-end; /* 文字贴近底部 */
|
||||||
|
padding: 0 8px 6px; /* 内边距让文字与边缘保持距离 */
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 渐变层上的文字 */
|
||||||
|
.overlay-text {
|
||||||
|
color: #fff;
|
||||||
|
font-weight: 500;
|
||||||
|
font-size: 12px;
|
||||||
|
text-align: left;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ export const useAppStore = defineStore("app", {
|
|||||||
return {
|
return {
|
||||||
title: "",
|
title: "",
|
||||||
sceneId: "",
|
sceneId: "",
|
||||||
|
hasToken: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
getters: {},
|
getters: {},
|
||||||
@@ -16,6 +17,9 @@ export const useAppStore = defineStore("app", {
|
|||||||
setSceneId(data) {
|
setSceneId(data) {
|
||||||
this.sceneId = data;
|
this.sceneId = data;
|
||||||
},
|
},
|
||||||
|
setHasToken(data) {
|
||||||
|
this.hasToken = data;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
unistorage: true,
|
unistorage: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user