| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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, // 用户信息
- inpage:false, //是否当前页面
- }),
- 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){
- uni.showModal({
- title:'温馨提示',
- content:res.msg,
- showCancel:false
- })
- return
- }
-
- this.showLoginModal = false;
- this.isLogin = true;
- this.token = res.data?.token;
- this.userInfo = res.data;
- uni.setStorageSync('token',this.userInfo?.token)
-
- const currentDate = new Date();
- const futureDate = new Date(currentDate);
- futureDate.setDate(currentDate.getDate() + 7);
- uni.setStorageSync('expaireTime',Date.parse(futureDate))
-
- 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'
- });
- },
- },
- });
|