From 6ef1b14f735acdc3ff77a50da1bb09a5bb983dcc Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期三, 28 五月 2025 17:37:06 +0800
Subject: [PATCH] location,userinfo
---
store/user.ts | 99 ++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 97 insertions(+), 2 deletions(-)
diff --git a/store/user.ts b/store/user.ts
index c95eed2..57b723e 100644
--- a/store/user.ts
+++ b/store/user.ts
@@ -1,6 +1,101 @@
+// stores/user.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'
+import http from '@/plugins/http.js' // 请替换成你实际的 http 封装路径
+import storage from '@/plugins/storage' // 同样替换为你的封装路径
+import message from '@/plugins/message' // 你使用的消息组件封装
+import { WechatLoginData } from '@/types/index'
+
export const useUserStore = defineStore('user', () => {
-
-})
\ No newline at end of file
+ const hasLogin = ref(false)
+ const isBind = ref(false)
+ const userInfo = ref<any>(null)
+ const address = ref<any>(null)
+
+ async function getCurrentInfo() {
+ const currentInfo = await http.request('get', '/api/current/user', {})
+ if (currentInfo && currentInfo.code == 0) {
+ console.log('当前用户信息', currentInfo)
+ userInfo.value = currentInfo.data // 赋值到 store
+ } else {
+ message.showToast('获取用户信息失败')
+ }
+ }
+
+ async function loginwx(data: WechatLoginData) {
+ const {
+ code,
+ imgurl = '',
+ nickname = '',
+ inviter = '',
+ phoneNumber = '',
+ purePhoneNumber = ''
+ } = data
+
+ const resp = await http.request('post', '/api/login/wechat', {
+ data: {
+ code,
+ imgurl,
+ nickname,
+ inviter,
+ phoneNumber,
+ purePhoneNumber
+ }
+ })
+
+ if (resp && resp.code === 0) {
+
+ isBind.value = true
+ storage.setItem('token', resp.data.access_token || '')
+ hasLogin.value = true
+ await getCurrentInfo()
+ } else {
+ message.showToast('登录失败:' + (resp?.msg || '未知错误'))
+ storage.removeItem('openid')
+ storage.removeItem('tel')
+ storage.removeItem('token')
+ }
+
+ return resp
+ }
+
+
+ function initLoginState() {
+ const token = storage.getItem('token')
+ if (token) {
+ hasLogin.value = true
+
+ const savedUserInfo = storage.getItem('userInfo')
+ if (savedUserInfo) {
+ userInfo.value = savedUserInfo
+ } else {
+ getCurrentInfo()
+ }
+ } else {
+ hasLogin.value = false
+ }
+ }
+
+
+ function updateAddress(addressParam:any) {
+
+ // 合并新信息到 userInfo 中
+ console.log("addressParam",addressParam)
+ address.value = addressParam
+ // 如果你有持久化,这里也同步一下
+ // storage.setItem('userInfo', userInfo.value)
+ }
+
+
+ return {
+ hasLogin,
+ isBind,
+ userInfo,
+ address,
+ loginwx,
+ getCurrentInfo,
+ initLoginState,
+ updateAddress,
+ }
+})
--
Gitblit v1.9.3