fix: vip info
This commit is contained in:
@@ -6,7 +6,11 @@
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="avatar-nickname">
|
<view class="avatar-nickname">
|
||||||
<button open-type="chooseAvatar" @chooseavatar="onChooseAvatar" class="avatar-selector custom-button">
|
<button
|
||||||
|
open-type="chooseAvatar"
|
||||||
|
@chooseavatar="onChooseAvatar"
|
||||||
|
class="avatar-selector custom-button"
|
||||||
|
>
|
||||||
<image v-if="avatarUrl" :src="avatarUrl" class="avatar-preview" />
|
<image v-if="avatarUrl" :src="avatarUrl" class="avatar-preview" />
|
||||||
<text v-else>获取头像</text>
|
<text v-else>获取头像</text>
|
||||||
</button>
|
</button>
|
||||||
@@ -18,101 +22,95 @@
|
|||||||
/>
|
/>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<button class="confirm-btn custom-button" @tap="confirmLogin">确认登录</button>
|
<button class="confirm-btn custom-button" @tap="confirmLogin">
|
||||||
|
确认登录
|
||||||
|
</button>
|
||||||
</view>
|
</view>
|
||||||
</uni-popup>
|
</uni-popup>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue'
|
import { ref } from "vue";
|
||||||
import { useUserStore } from '@/stores/user'
|
import { useUserStore } from "@/stores/user";
|
||||||
import { getPlatformProvider } from '@/utils/system'
|
import { getPlatformProvider } from "@/utils/system";
|
||||||
import { apiLogin } from '@/api/auth.js'
|
import { uploadImage } from "@/utils/common";
|
||||||
import { wxLogin } from '@/utils/login.js'
|
import { apiLogin } from "@/api/auth.js";
|
||||||
|
import { wxLogin } from "@/utils/login.js";
|
||||||
|
|
||||||
const popupRef = ref(null)
|
const popupRef = ref(null);
|
||||||
const avatarUrl = ref('')
|
const avatarUrl = ref("");
|
||||||
const nickname = ref('')
|
const nickname = ref("");
|
||||||
|
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore();
|
||||||
|
|
||||||
const emit = defineEmits(['logind'])
|
const emit = defineEmits(["logind"]);
|
||||||
|
|
||||||
const open = () => {
|
const open = () => {
|
||||||
popupRef.value.open()
|
popupRef.value.open();
|
||||||
}
|
};
|
||||||
|
|
||||||
const close = () => {
|
const close = () => {
|
||||||
popupRef.value.close()
|
popupRef.value.close();
|
||||||
}
|
};
|
||||||
|
|
||||||
const onChooseAvatar = (e) => {
|
const onChooseAvatar = (e) => {
|
||||||
avatarUrl.value = e.detail.avatarUrl
|
avatarUrl.value = e.detail.avatarUrl;
|
||||||
}
|
};
|
||||||
|
|
||||||
const confirmLogin = async () => {
|
const confirmLogin = async () => {
|
||||||
try {
|
try {
|
||||||
const platform = getPlatformProvider()
|
const platform = getPlatformProvider();
|
||||||
if (platform === 'mp-weixin') {
|
if (platform === "mp-weixin") {
|
||||||
const code = await wxLogin()
|
const code = await wxLogin();
|
||||||
console.log('准备登录:', { code, nickname: nickname.value, avatarUrl: avatarUrl.value })
|
const imageUrl = await uploadImage(avatarUrl.value);
|
||||||
// console.log('准备登录:', { code, nickname: nickname.value, avatarUrl: 'http://tmp/HXhtcEwQ5A3B58476c91ba545ab67c6bf9c67d9c2559.jpeg' })
|
|
||||||
|
|
||||||
const fileKeyRes = await uni.uploadFile({
|
const loginRes = await apiLogin({
|
||||||
url: 'https://api.ai-meng.com/api/common/upload',
|
code,
|
||||||
filePath: avatarUrl.value,
|
nickname: nickname.value,
|
||||||
name: 'file', // 和后端接收文件字段名一致
|
avatarUrl: imageUrl,
|
||||||
header: {
|
platform: "wx",
|
||||||
'x-app-id': '69665538a49b8ae3be50fe5d',
|
});
|
||||||
},
|
// 保存用户信息到store
|
||||||
})
|
userStore.setUserInfo({
|
||||||
if(fileKeyRes.statusCode < 400) {
|
nickName: loginRes?.user?.nickname || nickname.value,
|
||||||
const keyJson = JSON.parse(fileKeyRes.data)
|
avatarUrl: loginRes?.user?.avatar || imageUrl,
|
||||||
const url = `https://file.lihailezzc.com/${keyJson?.data.key}`
|
id: loginRes?.user?.id,
|
||||||
const loginRes = await apiLogin({ code, nickname: nickname.value, avatarUrl: url, platform: 'wx' })
|
isVip: loginRes?.isVip || false,
|
||||||
// 保存用户信息到store
|
vipExpireAt: loginRes?.vipExpireAt || null,
|
||||||
userStore.setUserInfo({
|
});
|
||||||
nickName: loginRes?.user?.nickname || nickname.value,
|
|
||||||
avatarUrl: loginRes?.user?.avatar || url,
|
|
||||||
id: loginRes?.user?.id
|
|
||||||
})
|
|
||||||
|
|
||||||
userStore.setToken(loginRes.token)
|
userStore.setToken(loginRes.token);
|
||||||
|
|
||||||
uni.showToast({ title: '登录成功', icon: 'success' })
|
uni.showToast({ title: "登录成功", icon: "success" });
|
||||||
emit('logind')
|
emit("logind");
|
||||||
popupRef.value.close()
|
popupRef.value.close();
|
||||||
|
|
||||||
// 重置临时变量
|
|
||||||
avatarUrl.value = ''
|
|
||||||
nickname.value = ''
|
|
||||||
} else {
|
|
||||||
throw Error('获取失败')
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// 重置临时变量
|
||||||
|
avatarUrl.value = "";
|
||||||
|
nickname.value = "";
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
uni.showToast({ title: '登录失败', icon: 'none' })
|
uni.showToast({ title: "登录失败", icon: "none" });
|
||||||
console.error(err)
|
console.error(err);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
defineExpose({ open, close })
|
defineExpose({ open, close });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang='scss'>
|
<style lang="scss">
|
||||||
.custom-button {
|
.custom-button {
|
||||||
border: none;
|
border: none;
|
||||||
outline: none;
|
outline: none;
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
line-height: normal;
|
line-height: normal;
|
||||||
font-family: inherit;
|
font-family: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
.custom-button::after {
|
.custom-button::after {
|
||||||
border: none;
|
border: none;
|
||||||
}
|
}
|
||||||
.popup-container {
|
.popup-container {
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
padding: 40rpx 30rpx 60rpx;
|
padding: 40rpx 30rpx 60rpx;
|
||||||
|
|||||||
Reference in New Issue
Block a user