| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import { defineStore } from 'pinia';
- import api from '@/common/api/index.js'
- export const useUserStore = defineStore('user', {
- state: () => ({
- isRegister: true, // 是否注册成功
- isLogin: false, // 用户是否已登录
- showLoginModal: false, // 是否显示登录弹框
- token:'',
- userInfo: null, // 用户信息
- }),
- getters: {
- // 方便在模板中直接使用
- isLoggedIn: (state) => state.isLogin,
- },
- actions: {
- // 打开登录弹框
- openLoginModal() {
- this.showLoginModal = true;
- },
- // 关闭登录弹框
- closeLoginModal() {
- this.showLoginModal = false;
- },
-
- async register(userFrom) {
- await new Promise(resolve => {
- api.post('/wx/register',userFrom).then(({data:res})=>{
- if(res.code !== 0){
- return uni.showToast({
- title: res.msg
- })
- }
-
- this.showLoginModal = false;
- this.isRegister = true;
- uni.showToast({
- title: '注册成功',
- icon: 'success'
- });
- })
- });
- },
- async login(loginDto) {
- await new Promise(resolve => {
- api.get('/wx/login',loginDto).then(({data:res})=>{
- if(res.code !== 0){
- return uni.showToast({
- title: res.msg
- })
- }
-
- this.showLoginModal = false;
- this.isLogin = true;
- this.token = res.data?.token;
- this.userInfo = res.data;
- uni.setStorageSync('token',this.userInfo?.token)
- uni.setStorageSync('userInfo',JSON.stringify(this.userInfo))
- uni.showToast({
- title: '登录成功',
- icon: 'success'
- });
- })
- });
- },
- // 登出操作
- logout() {
- this.isRegister = false;
- this.isLogin = false;
- this.userInfo = null;
- uni.showToast({
- title: '已退出登录',
- icon: 'none'
- });
- },
- },
- });
|