ykt-wxapp/store/account.js

44 lines
1.0 KiB
JavaScript
Raw Normal View History

2026-01-19 18:52:18 +08:00
import { ref } from "vue";
import { defineStore } from "pinia";
import api from '@/utils/api';
import { toast } from '@/utils/widget';
const env = __VITE_ENV__;
export default defineStore("accountStore", () => {
const appid = env.MP_WX_APP_ID;
const account = ref(null);
const loading = ref(false)
async function login(phoneCode = '') {
if (loading.value) return;
loading.value = true;
try {
const { code } = await uni.login({
appid,
provider: "weixin",
scope: "snsapi_base",
});
console.log('logincode: ', code)
if (code) {
const res = await api('wxAppLogin', {
phoneCode,
code,
});
loading.value = false
console.log(res)
if (res.success && res.data && res.data.mobile) {
account.value = res.data;
return res.data
}
}
toast('登录失败,请重新登录');
} catch (e) {
toast('登录失败,请重新登录');
}
loading.value = false
}
return { account, login }
})